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_PID processed by the client thread.
1200 * Called with session lock held.
1202 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1210 case LTTNG_DOMAIN_KERNEL
:
1212 struct ltt_kernel_session
*ksess
;
1214 ksess
= session
->kernel_session
;
1216 ret
= kernel_track_pid(ksess
, pid
);
1217 if (ret
!= LTTNG_OK
) {
1221 kernel_wait_quiescent(kernel_tracer_fd
);
1224 case LTTNG_DOMAIN_UST
:
1226 struct ltt_ust_session
*usess
;
1228 usess
= session
->ust_session
;
1230 ret
= trace_ust_track_pid(usess
, pid
);
1231 if (ret
!= LTTNG_OK
) {
1237 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1249 * Command LTTNG_UNTRACK_PID processed by the client thread.
1251 * Called with session lock held.
1253 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1261 case LTTNG_DOMAIN_KERNEL
:
1263 struct ltt_kernel_session
*ksess
;
1265 ksess
= session
->kernel_session
;
1267 ret
= kernel_untrack_pid(ksess
, pid
);
1268 if (ret
!= LTTNG_OK
) {
1272 kernel_wait_quiescent(kernel_tracer_fd
);
1275 case LTTNG_DOMAIN_UST
:
1277 struct ltt_ust_session
*usess
;
1279 usess
= session
->ust_session
;
1281 ret
= trace_ust_untrack_pid(usess
, pid
);
1282 if (ret
!= LTTNG_OK
) {
1288 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1300 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1302 * The wpipe arguments is used as a notifier for the kernel thread.
1304 int cmd_enable_channel(struct ltt_session
*session
,
1305 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1308 struct ltt_ust_session
*usess
= session
->ust_session
;
1309 struct lttng_ht
*chan_ht
;
1316 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1318 /* Validate channel name */
1319 if (attr
->name
[0] == '.' ||
1320 memchr(attr
->name
, '/', len
) != NULL
) {
1321 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1325 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1330 * Don't try to enable a channel if the session has been started at
1331 * some point in time before. The tracer does not allow it.
1333 if (session
->has_been_started
) {
1334 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1339 * If the session is a live session, remove the switch timer, the
1340 * live timer does the same thing but sends also synchronisation
1341 * beacons for inactive streams.
1343 if (session
->live_timer
> 0) {
1344 attr
->attr
.live_timer_interval
= session
->live_timer
;
1345 attr
->attr
.switch_timer_interval
= 0;
1348 /* Check for feature support */
1349 switch (domain
->type
) {
1350 case LTTNG_DOMAIN_JUL
:
1351 case LTTNG_DOMAIN_LOG4J
:
1352 case LTTNG_DOMAIN_PYTHON
:
1353 if (!agent_tracing_is_enabled()) {
1354 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1355 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1363 switch (domain
->type
) {
1364 case LTTNG_DOMAIN_KERNEL
:
1366 struct ltt_kernel_channel
*kchan
;
1368 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1369 session
->kernel_session
);
1370 if (kchan
== NULL
) {
1371 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1372 if (attr
->name
[0] != '\0') {
1373 session
->kernel_session
->has_non_default_channel
= 1;
1376 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1379 if (ret
!= LTTNG_OK
) {
1383 kernel_wait_quiescent(kernel_tracer_fd
);
1386 case LTTNG_DOMAIN_UST
:
1387 case LTTNG_DOMAIN_JUL
:
1388 case LTTNG_DOMAIN_LOG4J
:
1389 case LTTNG_DOMAIN_PYTHON
:
1391 struct ltt_ust_channel
*uchan
;
1396 * Current agent implementation limitations force us to allow
1397 * only one channel at once in "agent" subdomains. Each
1398 * subdomain has a default channel name which must be strictly
1401 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1402 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1403 LTTNG_SYMBOL_NAME_LEN
)) {
1404 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1407 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1408 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1409 LTTNG_SYMBOL_NAME_LEN
)) {
1410 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1413 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1414 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1415 LTTNG_SYMBOL_NAME_LEN
)) {
1416 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1421 chan_ht
= usess
->domain_global
.channels
;
1423 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1424 if (uchan
== NULL
) {
1425 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1426 if (attr
->name
[0] != '\0') {
1427 usess
->has_non_default_channel
= 1;
1430 ret
= channel_ust_enable(usess
, uchan
);
1435 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1446 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1448 int cmd_disable_event(struct ltt_session
*session
,
1449 enum lttng_domain_type domain
, char *channel_name
,
1450 struct lttng_event
*event
)
1455 DBG("Disable event command for event \'%s\'", event
->name
);
1457 event_name
= event
->name
;
1458 if (validate_event_name(event_name
)) {
1459 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1463 /* Error out on unhandled search criteria */
1464 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1465 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1466 ret
= LTTNG_ERR_UNK
;
1473 case LTTNG_DOMAIN_KERNEL
:
1475 struct ltt_kernel_channel
*kchan
;
1476 struct ltt_kernel_session
*ksess
;
1478 ksess
= session
->kernel_session
;
1481 * If a non-default channel has been created in the
1482 * session, explicitely require that -c chan_name needs
1485 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1486 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1490 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1491 if (kchan
== NULL
) {
1492 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1496 switch (event
->type
) {
1497 case LTTNG_EVENT_ALL
:
1498 case LTTNG_EVENT_TRACEPOINT
:
1499 case LTTNG_EVENT_SYSCALL
:
1500 case LTTNG_EVENT_PROBE
:
1501 case LTTNG_EVENT_FUNCTION
:
1502 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1503 if (event_name
[0] == '\0') {
1504 ret
= event_kernel_disable_event(kchan
,
1507 ret
= event_kernel_disable_event(kchan
,
1508 event_name
, event
->type
);
1510 if (ret
!= LTTNG_OK
) {
1515 ret
= LTTNG_ERR_UNK
;
1519 kernel_wait_quiescent(kernel_tracer_fd
);
1522 case LTTNG_DOMAIN_UST
:
1524 struct ltt_ust_channel
*uchan
;
1525 struct ltt_ust_session
*usess
;
1527 usess
= session
->ust_session
;
1529 if (validate_ust_event_name(event_name
)) {
1530 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1535 * If a non-default channel has been created in the
1536 * session, explicitly require that -c chan_name needs
1539 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1540 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1544 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1546 if (uchan
== NULL
) {
1547 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1551 switch (event
->type
) {
1552 case LTTNG_EVENT_ALL
:
1554 * An empty event name means that everything
1555 * should be disabled.
1557 if (event
->name
[0] == '\0') {
1558 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1560 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1563 if (ret
!= LTTNG_OK
) {
1568 ret
= LTTNG_ERR_UNK
;
1572 DBG3("Disable UST event %s in channel %s completed", event_name
,
1576 case LTTNG_DOMAIN_LOG4J
:
1577 case LTTNG_DOMAIN_JUL
:
1578 case LTTNG_DOMAIN_PYTHON
:
1581 struct ltt_ust_session
*usess
= session
->ust_session
;
1585 switch (event
->type
) {
1586 case LTTNG_EVENT_ALL
:
1589 ret
= LTTNG_ERR_UNK
;
1593 agt
= trace_ust_find_agent(usess
, domain
);
1595 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1599 * An empty event name means that everything
1600 * should be disabled.
1602 if (event
->name
[0] == '\0') {
1603 ret
= event_agent_disable_all(usess
, agt
);
1605 ret
= event_agent_disable(usess
, agt
, event_name
);
1607 if (ret
!= LTTNG_OK
) {
1614 ret
= LTTNG_ERR_UND
;
1627 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1629 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1630 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1632 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1633 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1636 * Don't try to add a context if the session has been started at
1637 * some point in time before. The tracer does not allow it and would
1638 * result in a corrupted trace.
1640 if (session
->has_been_started
) {
1641 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1645 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1646 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1647 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1651 case LTTNG_DOMAIN_KERNEL
:
1652 assert(session
->kernel_session
);
1654 if (session
->kernel_session
->channel_count
== 0) {
1655 /* Create default channel */
1656 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1657 if (ret
!= LTTNG_OK
) {
1660 chan_kern_created
= 1;
1662 /* Add kernel context to kernel tracer */
1663 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1664 if (ret
!= LTTNG_OK
) {
1668 case LTTNG_DOMAIN_JUL
:
1669 case LTTNG_DOMAIN_LOG4J
:
1672 * Validate channel name.
1673 * If no channel name is given and the domain is JUL or LOG4J,
1674 * set it to the appropriate domain-specific channel name. If
1675 * a name is provided but does not match the expexted channel
1676 * name, return an error.
1678 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1679 strcmp(channel_name
,
1680 DEFAULT_JUL_CHANNEL_NAME
)) {
1681 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1683 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1684 strcmp(channel_name
,
1685 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1686 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1689 /* break is _not_ missing here. */
1691 case LTTNG_DOMAIN_UST
:
1693 struct ltt_ust_session
*usess
= session
->ust_session
;
1694 unsigned int chan_count
;
1698 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1699 if (chan_count
== 0) {
1700 struct lttng_channel
*attr
;
1701 /* Create default channel */
1702 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1704 ret
= LTTNG_ERR_FATAL
;
1708 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1709 if (ret
!= LTTNG_OK
) {
1714 chan_ust_created
= 1;
1717 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1718 free(app_ctx_provider_name
);
1720 app_ctx_name
= NULL
;
1721 app_ctx_provider_name
= NULL
;
1722 if (ret
!= LTTNG_OK
) {
1728 ret
= LTTNG_ERR_UND
;
1736 if (chan_kern_created
) {
1737 struct ltt_kernel_channel
*kchan
=
1738 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1739 session
->kernel_session
);
1740 /* Created previously, this should NOT fail. */
1742 kernel_destroy_channel(kchan
);
1745 if (chan_ust_created
) {
1746 struct ltt_ust_channel
*uchan
=
1747 trace_ust_find_channel_by_name(
1748 session
->ust_session
->domain_global
.channels
,
1749 DEFAULT_CHANNEL_NAME
);
1750 /* Created previously, this should NOT fail. */
1752 /* Remove from the channel list of the session. */
1753 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1755 trace_ust_destroy_channel(uchan
);
1758 free(app_ctx_provider_name
);
1763 static int validate_event_name(const char *name
)
1766 const char *c
= name
;
1767 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1768 bool null_terminated
= false;
1771 * Make sure that unescaped wildcards are only used as the last
1772 * character of the event name.
1774 while (c
< event_name_end
) {
1777 null_terminated
= true;
1783 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1784 /* Wildcard is not the last character */
1785 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1794 if (!ret
&& !null_terminated
) {
1795 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1800 static inline bool name_starts_with(const char *name
, const char *prefix
)
1802 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1804 return !strncmp(name
, prefix
, max_cmp_len
);
1807 /* Perform userspace-specific event name validation */
1808 static int validate_ust_event_name(const char *name
)
1818 * Check name against all internal UST event component namespaces used
1821 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1822 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1823 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1832 * Internal version of cmd_enable_event() with a supplemental
1833 * "internal_event" flag which is used to enable internal events which should
1834 * be hidden from clients. Such events are used in the agent implementation to
1835 * enable the events through which all "agent" events are funeled.
1837 static int _cmd_enable_event(struct ltt_session
*session
,
1838 struct lttng_domain
*domain
,
1839 char *channel_name
, struct lttng_event
*event
,
1840 char *filter_expression
,
1841 struct lttng_filter_bytecode
*filter
,
1842 struct lttng_event_exclusion
*exclusion
,
1843 int wpipe
, bool internal_event
)
1845 int ret
, channel_created
= 0;
1846 struct lttng_channel
*attr
= NULL
;
1850 assert(channel_name
);
1852 /* If we have a filter, we must have its filter expression */
1853 assert(!(!!filter_expression
^ !!filter
));
1855 DBG("Enable event command for event \'%s\'", event
->name
);
1859 ret
= validate_event_name(event
->name
);
1864 switch (domain
->type
) {
1865 case LTTNG_DOMAIN_KERNEL
:
1867 struct ltt_kernel_channel
*kchan
;
1870 * If a non-default channel has been created in the
1871 * session, explicitely require that -c chan_name needs
1874 if (session
->kernel_session
->has_non_default_channel
1875 && channel_name
[0] == '\0') {
1876 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1880 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1881 session
->kernel_session
);
1882 if (kchan
== NULL
) {
1883 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1884 LTTNG_BUFFER_GLOBAL
);
1886 ret
= LTTNG_ERR_FATAL
;
1889 if (lttng_strncpy(attr
->name
, channel_name
,
1890 sizeof(attr
->name
))) {
1891 ret
= LTTNG_ERR_INVALID
;
1895 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1896 if (ret
!= LTTNG_OK
) {
1899 channel_created
= 1;
1902 /* Get the newly created kernel channel pointer */
1903 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1904 session
->kernel_session
);
1905 if (kchan
== NULL
) {
1906 /* This sould not happen... */
1907 ret
= LTTNG_ERR_FATAL
;
1911 switch (event
->type
) {
1912 case LTTNG_EVENT_ALL
:
1914 char *filter_expression_a
= NULL
;
1915 struct lttng_filter_bytecode
*filter_a
= NULL
;
1918 * We need to duplicate filter_expression and filter,
1919 * because ownership is passed to first enable
1922 if (filter_expression
) {
1923 filter_expression_a
= strdup(filter_expression
);
1924 if (!filter_expression_a
) {
1925 ret
= LTTNG_ERR_FATAL
;
1930 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1932 free(filter_expression_a
);
1933 ret
= LTTNG_ERR_FATAL
;
1936 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1938 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1939 ret
= event_kernel_enable_event(kchan
, event
,
1940 filter_expression
, filter
);
1941 /* We have passed ownership */
1942 filter_expression
= NULL
;
1944 if (ret
!= LTTNG_OK
) {
1945 if (channel_created
) {
1946 /* Let's not leak a useless channel. */
1947 kernel_destroy_channel(kchan
);
1949 free(filter_expression_a
);
1953 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1954 ret
= event_kernel_enable_event(kchan
, event
,
1955 filter_expression_a
, filter_a
);
1956 /* We have passed ownership */
1957 filter_expression_a
= NULL
;
1959 if (ret
!= LTTNG_OK
) {
1964 case LTTNG_EVENT_PROBE
:
1965 case LTTNG_EVENT_FUNCTION
:
1966 case LTTNG_EVENT_FUNCTION_ENTRY
:
1967 case LTTNG_EVENT_TRACEPOINT
:
1968 ret
= event_kernel_enable_event(kchan
, event
,
1969 filter_expression
, filter
);
1970 /* We have passed ownership */
1971 filter_expression
= NULL
;
1973 if (ret
!= LTTNG_OK
) {
1974 if (channel_created
) {
1975 /* Let's not leak a useless channel. */
1976 kernel_destroy_channel(kchan
);
1981 case LTTNG_EVENT_SYSCALL
:
1982 ret
= event_kernel_enable_event(kchan
, event
,
1983 filter_expression
, filter
);
1984 /* We have passed ownership */
1985 filter_expression
= NULL
;
1987 if (ret
!= LTTNG_OK
) {
1992 ret
= LTTNG_ERR_UNK
;
1996 kernel_wait_quiescent(kernel_tracer_fd
);
1999 case LTTNG_DOMAIN_UST
:
2001 struct ltt_ust_channel
*uchan
;
2002 struct ltt_ust_session
*usess
= session
->ust_session
;
2007 * If a non-default channel has been created in the
2008 * session, explicitely require that -c chan_name needs
2011 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2012 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2016 /* Get channel from global UST domain */
2017 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2019 if (uchan
== NULL
) {
2020 /* Create default channel */
2021 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2022 usess
->buffer_type
);
2024 ret
= LTTNG_ERR_FATAL
;
2027 if (lttng_strncpy(attr
->name
, channel_name
,
2028 sizeof(attr
->name
))) {
2029 ret
= LTTNG_ERR_INVALID
;
2033 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2034 if (ret
!= LTTNG_OK
) {
2038 /* Get the newly created channel reference back */
2039 uchan
= trace_ust_find_channel_by_name(
2040 usess
->domain_global
.channels
, channel_name
);
2044 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2046 * Don't allow users to add UST events to channels which
2047 * are assigned to a userspace subdomain (JUL, Log4J,
2050 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2054 if (!internal_event
) {
2056 * Ensure the event name is not reserved for internal
2059 ret
= validate_ust_event_name(event
->name
);
2061 WARN("Userspace event name %s failed validation.",
2063 event
->name
: "NULL");
2064 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2069 /* At this point, the session and channel exist on the tracer */
2070 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2071 filter_expression
, filter
, exclusion
,
2073 /* We have passed ownership */
2074 filter_expression
= NULL
;
2077 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2078 goto already_enabled
;
2079 } else if (ret
!= LTTNG_OK
) {
2084 case LTTNG_DOMAIN_LOG4J
:
2085 case LTTNG_DOMAIN_JUL
:
2086 case LTTNG_DOMAIN_PYTHON
:
2088 const char *default_event_name
, *default_chan_name
;
2090 struct lttng_event uevent
;
2091 struct lttng_domain tmp_dom
;
2092 struct ltt_ust_session
*usess
= session
->ust_session
;
2096 if (!agent_tracing_is_enabled()) {
2097 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2098 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2102 agt
= trace_ust_find_agent(usess
, domain
->type
);
2104 agt
= agent_create(domain
->type
);
2106 ret
= LTTNG_ERR_NOMEM
;
2109 agent_add(agt
, usess
->agents
);
2112 /* Create the default tracepoint. */
2113 memset(&uevent
, 0, sizeof(uevent
));
2114 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2115 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2116 default_event_name
= event_get_default_agent_ust_name(
2118 if (!default_event_name
) {
2119 ret
= LTTNG_ERR_FATAL
;
2122 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2123 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2126 * The domain type is changed because we are about to enable the
2127 * default channel and event for the JUL domain that are hardcoded.
2128 * This happens in the UST domain.
2130 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2131 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2133 switch (domain
->type
) {
2134 case LTTNG_DOMAIN_LOG4J
:
2135 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2137 case LTTNG_DOMAIN_JUL
:
2138 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2140 case LTTNG_DOMAIN_PYTHON
:
2141 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2144 /* The switch/case we are in makes this impossible */
2149 char *filter_expression_copy
= NULL
;
2150 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2153 const size_t filter_size
= sizeof(
2154 struct lttng_filter_bytecode
)
2157 filter_copy
= zmalloc(filter_size
);
2159 ret
= LTTNG_ERR_NOMEM
;
2162 memcpy(filter_copy
, filter
, filter_size
);
2164 filter_expression_copy
=
2165 strdup(filter_expression
);
2166 if (!filter_expression
) {
2167 ret
= LTTNG_ERR_NOMEM
;
2170 if (!filter_expression_copy
|| !filter_copy
) {
2171 free(filter_expression_copy
);
2177 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2178 (char *) default_chan_name
,
2179 &uevent
, filter_expression_copy
,
2180 filter_copy
, NULL
, wpipe
);
2183 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2184 goto already_enabled
;
2185 } else if (ret
!= LTTNG_OK
) {
2189 /* The wild card * means that everything should be enabled. */
2190 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2191 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2194 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2198 filter_expression
= NULL
;
2199 if (ret
!= LTTNG_OK
) {
2206 ret
= LTTNG_ERR_UND
;
2214 free(filter_expression
);
2223 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2224 * We own filter, exclusion, and filter_expression.
2226 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2227 char *channel_name
, struct lttng_event
*event
,
2228 char *filter_expression
,
2229 struct lttng_filter_bytecode
*filter
,
2230 struct lttng_event_exclusion
*exclusion
,
2233 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2234 filter_expression
, filter
, exclusion
, wpipe
, false);
2238 * Enable an event which is internal to LTTng. An internal should
2239 * never be made visible to clients and are immune to checks such as
2242 static int cmd_enable_event_internal(struct ltt_session
*session
,
2243 struct lttng_domain
*domain
,
2244 char *channel_name
, struct lttng_event
*event
,
2245 char *filter_expression
,
2246 struct lttng_filter_bytecode
*filter
,
2247 struct lttng_event_exclusion
*exclusion
,
2250 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2251 filter_expression
, filter
, exclusion
, wpipe
, true);
2255 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2257 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2258 struct lttng_event
**events
)
2261 ssize_t nb_events
= 0;
2264 case LTTNG_DOMAIN_KERNEL
:
2265 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2266 if (nb_events
< 0) {
2267 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2271 case LTTNG_DOMAIN_UST
:
2272 nb_events
= ust_app_list_events(events
);
2273 if (nb_events
< 0) {
2274 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2278 case LTTNG_DOMAIN_LOG4J
:
2279 case LTTNG_DOMAIN_JUL
:
2280 case LTTNG_DOMAIN_PYTHON
:
2281 nb_events
= agent_list_events(events
, domain
);
2282 if (nb_events
< 0) {
2283 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2288 ret
= LTTNG_ERR_UND
;
2295 /* Return negative value to differentiate return code */
2300 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2302 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2303 struct lttng_event_field
**fields
)
2306 ssize_t nb_fields
= 0;
2309 case LTTNG_DOMAIN_UST
:
2310 nb_fields
= ust_app_list_event_fields(fields
);
2311 if (nb_fields
< 0) {
2312 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2316 case LTTNG_DOMAIN_KERNEL
:
2317 default: /* fall-through */
2318 ret
= LTTNG_ERR_UND
;
2325 /* Return negative value to differentiate return code */
2329 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2331 return syscall_table_list(events
);
2335 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2337 * Called with session lock held.
2339 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2340 enum lttng_domain_type domain
, int32_t **pids
)
2343 ssize_t nr_pids
= 0;
2346 case LTTNG_DOMAIN_KERNEL
:
2348 struct ltt_kernel_session
*ksess
;
2350 ksess
= session
->kernel_session
;
2351 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2353 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2358 case LTTNG_DOMAIN_UST
:
2360 struct ltt_ust_session
*usess
;
2362 usess
= session
->ust_session
;
2363 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2365 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2370 case LTTNG_DOMAIN_LOG4J
:
2371 case LTTNG_DOMAIN_JUL
:
2372 case LTTNG_DOMAIN_PYTHON
:
2374 ret
= LTTNG_ERR_UND
;
2381 /* Return negative value to differentiate return code */
2386 * Command LTTNG_START_TRACE processed by the client thread.
2388 * Called with session mutex held.
2390 int cmd_start_trace(struct ltt_session
*session
)
2393 unsigned long nb_chan
= 0;
2394 struct ltt_kernel_session
*ksession
;
2395 struct ltt_ust_session
*usess
;
2399 /* Ease our life a bit ;) */
2400 ksession
= session
->kernel_session
;
2401 usess
= session
->ust_session
;
2403 /* Is the session already started? */
2404 if (session
->active
) {
2405 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2410 * Starting a session without channel is useless since after that it's not
2411 * possible to enable channel thus inform the client.
2413 if (usess
&& usess
->domain_global
.channels
) {
2414 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2417 nb_chan
+= ksession
->channel_count
;
2420 ret
= LTTNG_ERR_NO_CHANNEL
;
2424 /* Kernel tracing */
2425 if (ksession
!= NULL
) {
2426 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2427 if (ret
!= LTTNG_OK
) {
2432 /* Flag session that trace should start automatically */
2435 * Even though the start trace might fail, flag this session active so
2436 * other application coming in are started by default.
2440 ret
= ust_app_start_trace_all(usess
);
2442 ret
= LTTNG_ERR_UST_START_FAIL
;
2447 /* Flag this after a successful start. */
2448 session
->has_been_started
= 1;
2449 session
->active
= 1;
2458 * Command LTTNG_STOP_TRACE processed by the client thread.
2460 int cmd_stop_trace(struct ltt_session
*session
)
2463 struct ltt_kernel_channel
*kchan
;
2464 struct ltt_kernel_session
*ksession
;
2465 struct ltt_ust_session
*usess
;
2470 ksession
= session
->kernel_session
;
2471 usess
= session
->ust_session
;
2473 /* Session is not active. Skip everythong and inform the client. */
2474 if (!session
->active
) {
2475 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2480 if (ksession
&& ksession
->active
) {
2481 DBG("Stop kernel tracing");
2483 ret
= kernel_stop_session(ksession
);
2485 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2489 kernel_wait_quiescent(kernel_tracer_fd
);
2491 /* Flush metadata after stopping (if exists) */
2492 if (ksession
->metadata_stream_fd
>= 0) {
2493 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2495 ERR("Kernel metadata flush failed");
2499 /* Flush all buffers after stopping */
2500 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2501 ret
= kernel_flush_buffer(kchan
);
2503 ERR("Kernel flush buffer error");
2507 ksession
->active
= 0;
2510 if (usess
&& usess
->active
) {
2512 * Even though the stop trace might fail, flag this session inactive so
2513 * other application coming in are not started by default.
2517 ret
= ust_app_stop_trace_all(usess
);
2519 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2524 /* Flag inactive after a successful stop. */
2525 session
->active
= 0;
2533 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2535 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2536 struct lttng_uri
*uris
)
2539 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2540 struct ltt_ust_session
*usess
= session
->ust_session
;
2546 /* Can't set consumer URI if the session is active. */
2547 if (session
->active
) {
2548 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2552 /* Set the "global" consumer URIs */
2553 for (i
= 0; i
< nb_uri
; i
++) {
2554 ret
= add_uri_to_consumer(session
->consumer
,
2555 &uris
[i
], 0, session
->name
);
2556 if (ret
!= LTTNG_OK
) {
2561 /* Set UST session URIs */
2562 if (session
->ust_session
) {
2563 for (i
= 0; i
< nb_uri
; i
++) {
2564 ret
= add_uri_to_consumer(
2565 session
->ust_session
->consumer
,
2566 &uris
[i
], LTTNG_DOMAIN_UST
,
2568 if (ret
!= LTTNG_OK
) {
2574 /* Set kernel session URIs */
2575 if (session
->kernel_session
) {
2576 for (i
= 0; i
< nb_uri
; i
++) {
2577 ret
= add_uri_to_consumer(
2578 session
->kernel_session
->consumer
,
2579 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2581 if (ret
!= LTTNG_OK
) {
2588 * Make sure to set the session in output mode after we set URI since a
2589 * session can be created without URL (thus flagged in no output mode).
2591 session
->output_traces
= 1;
2593 ksess
->output_traces
= 1;
2597 usess
->output_traces
= 1;
2608 * Command LTTNG_CREATE_SESSION processed by the client thread.
2610 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2611 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2614 struct ltt_session
*session
;
2620 * Verify if the session already exist
2622 * XXX: There is no need for the session lock list here since the caller
2623 * (process_client_msg) is holding it. We might want to change that so a
2624 * single command does not lock the entire session list.
2626 session
= session_find_by_name(name
);
2627 if (session
!= NULL
) {
2628 ret
= LTTNG_ERR_EXIST_SESS
;
2632 /* Create tracing session in the registry */
2633 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2634 LTTNG_SOCK_GET_GID_CRED(creds
));
2635 if (ret
!= LTTNG_OK
) {
2640 * Get the newly created session pointer back
2642 * XXX: There is no need for the session lock list here since the caller
2643 * (process_client_msg) is holding it. We might want to change that so a
2644 * single command does not lock the entire session list.
2646 session
= session_find_by_name(name
);
2649 session
->live_timer
= live_timer
;
2650 /* Create default consumer output for the session not yet created. */
2651 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2652 if (session
->consumer
== NULL
) {
2653 ret
= LTTNG_ERR_FATAL
;
2654 goto consumer_error
;
2658 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2659 if (ret
!= LTTNG_OK
) {
2660 goto consumer_error
;
2662 session
->output_traces
= 1;
2664 session
->output_traces
= 0;
2665 DBG2("Session %s created with no output", session
->name
);
2668 session
->consumer
->enabled
= 1;
2673 session_destroy(session
);
2680 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2682 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2683 size_t nb_uri
, lttng_sock_cred
*creds
)
2686 struct ltt_session
*session
;
2687 struct snapshot_output
*new_output
= NULL
;
2693 * Create session in no output mode with URIs set to NULL. The uris we've
2694 * received are for a default snapshot output if one.
2696 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2697 if (ret
!= LTTNG_OK
) {
2701 /* Get the newly created session pointer back. This should NEVER fail. */
2702 session
= session_find_by_name(name
);
2705 /* Flag session for snapshot mode. */
2706 session
->snapshot_mode
= 1;
2708 /* Skip snapshot output creation if no URI is given. */
2713 new_output
= snapshot_output_alloc();
2715 ret
= LTTNG_ERR_NOMEM
;
2716 goto error_snapshot_alloc
;
2719 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2720 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2722 if (ret
== -ENOMEM
) {
2723 ret
= LTTNG_ERR_NOMEM
;
2725 ret
= LTTNG_ERR_INVALID
;
2727 goto error_snapshot
;
2731 snapshot_add_output(&session
->snapshot
, new_output
);
2738 snapshot_output_destroy(new_output
);
2739 error_snapshot_alloc
:
2740 session_destroy(session
);
2746 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2748 * Called with session lock held.
2750 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2753 struct ltt_ust_session
*usess
;
2754 struct ltt_kernel_session
*ksess
;
2759 usess
= session
->ust_session
;
2760 ksess
= session
->kernel_session
;
2762 /* Clean kernel session teardown */
2763 kernel_destroy_session(ksess
);
2765 /* UST session teardown */
2767 /* Close any relayd session */
2768 consumer_output_send_destroy_relayd(usess
->consumer
);
2770 /* Destroy every UST application related to this session. */
2771 ret
= ust_app_destroy_trace_all(usess
);
2773 ERR("Error in ust_app_destroy_trace_all");
2776 /* Clean up the rest. */
2777 trace_ust_destroy_session(usess
);
2781 * Must notify the kernel thread here to update it's poll set in order to
2782 * remove the channel(s)' fd just destroyed.
2784 ret
= notify_thread_pipe(wpipe
);
2786 PERROR("write kernel poll pipe");
2789 ret
= session_destroy(session
);
2795 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2797 int cmd_register_consumer(struct ltt_session
*session
,
2798 enum lttng_domain_type domain
, const char *sock_path
,
2799 struct consumer_data
*cdata
)
2802 struct consumer_socket
*socket
= NULL
;
2809 case LTTNG_DOMAIN_KERNEL
:
2811 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2815 /* Can't register a consumer if there is already one */
2816 if (ksess
->consumer_fds_sent
!= 0) {
2817 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2821 sock
= lttcomm_connect_unix_sock(sock_path
);
2823 ret
= LTTNG_ERR_CONNECT_FAIL
;
2826 cdata
->cmd_sock
= sock
;
2828 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2829 if (socket
== NULL
) {
2832 PERROR("close register consumer");
2834 cdata
->cmd_sock
= -1;
2835 ret
= LTTNG_ERR_FATAL
;
2839 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2840 if (socket
->lock
== NULL
) {
2841 PERROR("zmalloc pthread mutex");
2842 ret
= LTTNG_ERR_FATAL
;
2845 pthread_mutex_init(socket
->lock
, NULL
);
2846 socket
->registered
= 1;
2849 consumer_add_socket(socket
, ksess
->consumer
);
2852 pthread_mutex_lock(&cdata
->pid_mutex
);
2854 pthread_mutex_unlock(&cdata
->pid_mutex
);
2859 /* TODO: Userspace tracing */
2860 ret
= LTTNG_ERR_UND
;
2868 consumer_destroy_socket(socket
);
2874 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2876 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2877 struct lttng_domain
**domains
)
2882 struct lttng_ht_iter iter
;
2884 if (session
->kernel_session
!= NULL
) {
2885 DBG3("Listing domains found kernel domain");
2889 if (session
->ust_session
!= NULL
) {
2890 DBG3("Listing domains found UST global domain");
2894 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2896 if (agt
->being_used
) {
2907 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2908 if (*domains
== NULL
) {
2909 ret
= LTTNG_ERR_FATAL
;
2913 if (session
->kernel_session
!= NULL
) {
2914 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2916 /* Kernel session buffer type is always GLOBAL */
2917 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2922 if (session
->ust_session
!= NULL
) {
2923 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2924 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2928 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2930 if (agt
->being_used
) {
2931 (*domains
)[index
].type
= agt
->domain
;
2932 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2942 /* Return negative value to differentiate return code */
2948 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2950 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2951 struct ltt_session
*session
, struct lttng_channel
**channels
)
2953 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2956 case LTTNG_DOMAIN_KERNEL
:
2957 if (session
->kernel_session
!= NULL
) {
2958 nb_chan
= session
->kernel_session
->channel_count
;
2960 DBG3("Number of kernel channels %zd", nb_chan
);
2962 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2966 case LTTNG_DOMAIN_UST
:
2967 if (session
->ust_session
!= NULL
) {
2969 nb_chan
= lttng_ht_get_count(
2970 session
->ust_session
->domain_global
.channels
);
2973 DBG3("Number of UST global channels %zd", nb_chan
);
2975 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2980 ret
= -LTTNG_ERR_UND
;
2985 const size_t channel_size
= sizeof(struct lttng_channel
) +
2986 sizeof(struct lttcomm_channel_extended
);
2987 struct lttcomm_channel_extended
*channel_exts
;
2989 payload_size
= nb_chan
* channel_size
;
2990 *channels
= zmalloc(payload_size
);
2991 if (*channels
== NULL
) {
2992 ret
= -LTTNG_ERR_FATAL
;
2996 channel_exts
= ((void *) *channels
) +
2997 (nb_chan
* sizeof(struct lttng_channel
));
2998 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2999 if (ret
!= LTTNG_OK
) {
3014 * Command LTTNG_LIST_EVENTS processed by the client thread.
3016 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3017 struct ltt_session
*session
, char *channel_name
,
3018 struct lttng_event
**events
, size_t *total_size
)
3021 ssize_t nb_event
= 0;
3024 case LTTNG_DOMAIN_KERNEL
:
3025 if (session
->kernel_session
!= NULL
) {
3026 nb_event
= list_lttng_kernel_events(channel_name
,
3027 session
->kernel_session
, events
,
3031 case LTTNG_DOMAIN_UST
:
3033 if (session
->ust_session
!= NULL
) {
3034 nb_event
= list_lttng_ust_global_events(channel_name
,
3035 &session
->ust_session
->domain_global
, events
,
3040 case LTTNG_DOMAIN_LOG4J
:
3041 case LTTNG_DOMAIN_JUL
:
3042 case LTTNG_DOMAIN_PYTHON
:
3043 if (session
->ust_session
) {
3044 struct lttng_ht_iter iter
;
3048 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3049 &iter
.iter
, agt
, node
.node
) {
3050 if (agt
->domain
== domain
) {
3051 nb_event
= list_lttng_agent_events(
3061 ret
= LTTNG_ERR_UND
;
3068 /* Return negative value to differentiate return code */
3073 * Using the session list, filled a lttng_session array to send back to the
3074 * client for session listing.
3076 * The session list lock MUST be acquired before calling this function. Use
3077 * session_lock_list() and session_unlock_list().
3079 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3084 struct ltt_session
*session
;
3085 struct ltt_session_list
*list
= session_get_list();
3087 DBG("Getting all available session for UID %d GID %d",
3090 * Iterate over session list and append data after the control struct in
3093 cds_list_for_each_entry(session
, &list
->head
, list
) {
3095 * Only list the sessions the user can control.
3097 if (!session_access_ok(session
, uid
, gid
)) {
3101 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3102 struct ltt_ust_session
*usess
= session
->ust_session
;
3104 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3105 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3106 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3107 ret
= build_network_session_path(sessions
[i
].path
,
3108 sizeof(sessions
[i
].path
), session
);
3110 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3111 session
->consumer
->dst
.trace_path
);
3114 PERROR("snprintf session path");
3118 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3119 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3120 sessions
[i
].enabled
= session
->active
;
3121 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3122 sessions
[i
].live_timer_interval
= session
->live_timer
;
3128 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3129 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3131 int cmd_data_pending(struct ltt_session
*session
)
3134 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3135 struct ltt_ust_session
*usess
= session
->ust_session
;
3139 /* Session MUST be stopped to ask for data availability. */
3140 if (session
->active
) {
3141 ret
= LTTNG_ERR_SESSION_STARTED
;
3145 * If stopped, just make sure we've started before else the above call
3146 * will always send that there is data pending.
3148 * The consumer assumes that when the data pending command is received,
3149 * the trace has been started before or else no output data is written
3150 * by the streams which is a condition for data pending. So, this is
3151 * *VERY* important that we don't ask the consumer before a start
3154 if (!session
->has_been_started
) {
3160 if (ksess
&& ksess
->consumer
) {
3161 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3163 /* Data is still being extracted for the kernel. */
3168 if (usess
&& usess
->consumer
) {
3169 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3171 /* Data is still being extracted for the kernel. */
3176 /* Data is ready to be read by a viewer */
3184 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3186 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3188 int cmd_snapshot_add_output(struct ltt_session
*session
,
3189 struct lttng_snapshot_output
*output
, uint32_t *id
)
3192 struct snapshot_output
*new_output
;
3197 DBG("Cmd snapshot add output for session %s", session
->name
);
3200 * Can't create an output if the session is not set in no-output mode.
3202 if (session
->output_traces
) {
3203 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3207 /* Only one output is allowed until we have the "tee" feature. */
3208 if (session
->snapshot
.nb_output
== 1) {
3209 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3213 new_output
= snapshot_output_alloc();
3215 ret
= LTTNG_ERR_NOMEM
;
3219 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3220 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3221 &session
->snapshot
);
3223 if (ret
== -ENOMEM
) {
3224 ret
= LTTNG_ERR_NOMEM
;
3226 ret
= LTTNG_ERR_INVALID
;
3232 snapshot_add_output(&session
->snapshot
, new_output
);
3234 *id
= new_output
->id
;
3241 snapshot_output_destroy(new_output
);
3247 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3249 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3251 int cmd_snapshot_del_output(struct ltt_session
*session
,
3252 struct lttng_snapshot_output
*output
)
3255 struct snapshot_output
*sout
= NULL
;
3263 * Permission denied to create an output if the session is not
3264 * set in no output mode.
3266 if (session
->output_traces
) {
3267 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3272 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3274 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3275 } else if (*output
->name
!= '\0') {
3276 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3278 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3281 ret
= LTTNG_ERR_INVALID
;
3285 snapshot_delete_output(&session
->snapshot
, sout
);
3286 snapshot_output_destroy(sout
);
3295 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3297 * If no output is available, outputs is untouched and 0 is returned.
3299 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3301 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3302 struct lttng_snapshot_output
**outputs
)
3305 struct lttng_snapshot_output
*list
= NULL
;
3306 struct lttng_ht_iter iter
;
3307 struct snapshot_output
*output
;
3312 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3315 * Permission denied to create an output if the session is not
3316 * set in no output mode.
3318 if (session
->output_traces
) {
3319 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3323 if (session
->snapshot
.nb_output
== 0) {
3328 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3330 ret
= -LTTNG_ERR_NOMEM
;
3334 /* Copy list from session to the new list object. */
3336 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3337 output
, node
.node
) {
3338 assert(output
->consumer
);
3339 list
[idx
].id
= output
->id
;
3340 list
[idx
].max_size
= output
->max_size
;
3341 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3342 sizeof(list
[idx
].name
))) {
3343 ret
= -LTTNG_ERR_INVALID
;
3346 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3347 if (lttng_strncpy(list
[idx
].ctrl_url
,
3348 output
->consumer
->dst
.trace_path
,
3349 sizeof(list
[idx
].ctrl_url
))) {
3350 ret
= -LTTNG_ERR_INVALID
;
3355 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3356 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3358 ret
= -LTTNG_ERR_NOMEM
;
3363 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3364 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3366 ret
= -LTTNG_ERR_NOMEM
;
3375 ret
= session
->snapshot
.nb_output
;
3384 * Check if we can regenerate the metadata for this session.
3385 * Only kernel, UST per-uid and non-live sessions are supported.
3387 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3390 int check_regenerate_metadata_support(struct ltt_session
*session
)
3396 if (session
->live_timer
!= 0) {
3397 ret
= LTTNG_ERR_LIVE_SESSION
;
3400 if (!session
->active
) {
3401 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3404 if (session
->ust_session
) {
3405 switch (session
->ust_session
->buffer_type
) {
3406 case LTTNG_BUFFER_PER_UID
:
3408 case LTTNG_BUFFER_PER_PID
:
3409 ret
= LTTNG_ERR_PER_PID_SESSION
;
3413 ret
= LTTNG_ERR_UNK
;
3417 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3418 session
->consumer
->relay_minor_version
< 8) {
3419 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3429 int clear_metadata_file(int fd
)
3434 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3435 if (lseek_ret
< 0) {
3441 ret
= ftruncate(fd
, 0);
3443 PERROR("ftruncate");
3452 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3455 struct buffer_reg_uid
*uid_reg
= NULL
;
3456 struct buffer_reg_session
*session_reg
= NULL
;
3459 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3460 struct ust_registry_session
*registry
;
3461 struct ust_registry_channel
*chan
;
3462 struct lttng_ht_iter iter_chan
;
3464 session_reg
= uid_reg
->registry
;
3465 registry
= session_reg
->reg
.ust
;
3467 pthread_mutex_lock(®istry
->lock
);
3468 registry
->metadata_len_sent
= 0;
3469 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3470 registry
->metadata_len
= 0;
3471 registry
->metadata_version
++;
3472 if (registry
->metadata_fd
> 0) {
3473 /* Clear the metadata file's content. */
3474 ret
= clear_metadata_file(registry
->metadata_fd
);
3476 pthread_mutex_unlock(®istry
->lock
);
3481 ret
= ust_metadata_session_statedump(registry
, NULL
,
3482 registry
->major
, registry
->minor
);
3484 pthread_mutex_unlock(®istry
->lock
);
3485 ERR("Failed to generate session metadata (err = %d)",
3489 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3491 struct ust_registry_event
*event
;
3492 struct lttng_ht_iter iter_event
;
3494 ret
= ust_metadata_channel_statedump(registry
, chan
);
3496 pthread_mutex_unlock(®istry
->lock
);
3497 ERR("Failed to generate channel metadata "
3501 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3503 ret
= ust_metadata_event_statedump(registry
,
3506 pthread_mutex_unlock(®istry
->lock
);
3507 ERR("Failed to generate event metadata "
3513 pthread_mutex_unlock(®istry
->lock
);
3522 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3524 * Ask the consumer to truncate the existing metadata file(s) and
3525 * then regenerate the metadata. Live and per-pid sessions are not
3526 * supported and return an error.
3528 * Return 0 on success or else a LTTNG_ERR code.
3530 int cmd_regenerate_metadata(struct ltt_session
*session
)
3536 ret
= check_regenerate_metadata_support(session
);
3541 if (session
->kernel_session
) {
3542 ret
= kernctl_session_regenerate_metadata(
3543 session
->kernel_session
->fd
);
3545 ERR("Failed to regenerate the kernel metadata");
3550 if (session
->ust_session
) {
3551 ret
= ust_regenerate_metadata(session
->ust_session
);
3553 ERR("Failed to regenerate the UST metadata");
3557 DBG("Cmd metadata regenerate for session %s", session
->name
);
3565 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3567 * Ask the tracer to regenerate a new statedump.
3569 * Return 0 on success or else a LTTNG_ERR code.
3571 int cmd_regenerate_statedump(struct ltt_session
*session
)
3577 if (!session
->active
) {
3578 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3583 if (session
->kernel_session
) {
3584 ret
= kernctl_session_regenerate_statedump(
3585 session
->kernel_session
->fd
);
3587 * Currently, the statedump in kernel can only fail if out
3591 if (ret
== -ENOMEM
) {
3592 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3594 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3596 ERR("Failed to regenerate the kernel statedump");
3601 if (session
->ust_session
) {
3602 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3604 * Currently, the statedump in UST always returns 0.
3607 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3608 ERR("Failed to regenerate the UST statedump");
3612 DBG("Cmd regenerate statedump for session %s", session
->name
);
3620 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3621 * snapshot output is *not* set with a remote destination.
3623 * Return 0 on success or a LTTNG_ERR code.
3625 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3626 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3629 struct lttng_ht_iter iter
;
3630 struct consumer_socket
*socket
;
3633 assert(snap_output
);
3636 DBG2("Set relayd object from snapshot output");
3638 /* Ignore if snapshot consumer output is not network. */
3639 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3644 * For each consumer socket, create and send the relayd object of the
3648 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3649 socket
, node
.node
) {
3650 pthread_mutex_lock(socket
->lock
);
3651 ret
= send_consumer_relayd_sockets(0, session
->id
,
3652 snap_output
->consumer
, socket
,
3653 session
->name
, session
->hostname
,
3654 session
->live_timer
);
3655 pthread_mutex_unlock(socket
->lock
);
3656 if (ret
!= LTTNG_OK
) {
3668 * Record a kernel snapshot.
3670 * Return LTTNG_OK on success or a LTTNG_ERR code.
3672 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3673 struct snapshot_output
*output
, struct ltt_session
*session
,
3674 int wait
, uint64_t nb_packets_per_stream
)
3684 * Copy kernel session sockets so we can communicate with the right
3685 * consumer for the snapshot record command.
3687 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3689 ret
= LTTNG_ERR_NOMEM
;
3693 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3694 if (ret
!= LTTNG_OK
) {
3695 goto error_snapshot
;
3698 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3699 if (ret
!= LTTNG_OK
) {
3700 goto error_snapshot
;
3707 /* Clean up copied sockets so this output can use some other later on. */
3708 consumer_destroy_output_sockets(output
->consumer
);
3715 * Record a UST snapshot.
3717 * Return 0 on success or a LTTNG_ERR error code.
3719 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3720 struct snapshot_output
*output
, struct ltt_session
*session
,
3721 int wait
, uint64_t nb_packets_per_stream
)
3730 * Copy UST session sockets so we can communicate with the right
3731 * consumer for the snapshot record command.
3733 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3735 ret
= LTTNG_ERR_NOMEM
;
3739 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3740 if (ret
!= LTTNG_OK
) {
3741 goto error_snapshot
;
3744 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3748 ret
= LTTNG_ERR_INVALID
;
3751 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3754 goto error_snapshot
;
3760 /* Clean up copied sockets so this output can use some other later on. */
3761 consumer_destroy_output_sockets(output
->consumer
);
3767 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3768 uint64_t cur_nr_packets
)
3770 uint64_t tot_size
= 0;
3772 if (session
->kernel_session
) {
3773 struct ltt_kernel_channel
*chan
;
3774 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3776 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3777 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3779 * Don't take channel into account if we
3780 * already grab all its packets.
3784 tot_size
+= chan
->channel
->attr
.subbuf_size
3785 * chan
->stream_count
;
3789 if (session
->ust_session
) {
3790 struct ltt_ust_session
*usess
= session
->ust_session
;
3792 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3800 * Calculate the number of packets we can grab from each stream that
3801 * fits within the overall snapshot max size.
3803 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3804 * the number of packets per stream.
3806 * TODO: this approach is not perfect: we consider the worse case
3807 * (packet filling the sub-buffers) as an upper bound, but we could do
3808 * better if we do this calculation while we actually grab the packet
3809 * content: we would know how much padding we don't actually store into
3812 * This algorithm is currently bounded by the number of packets per
3815 * Since we call this algorithm before actually grabbing the data, it's
3816 * an approximation: for instance, applications could appear/disappear
3817 * in between this call and actually grabbing data.
3820 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3823 uint64_t cur_nb_packets
= 0;
3826 return 0; /* Infinite */
3829 size_left
= max_size
;
3831 uint64_t one_more_packet_tot_size
;
3833 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3835 if (!one_more_packet_tot_size
) {
3836 /* We are already grabbing all packets. */
3839 size_left
-= one_more_packet_tot_size
;
3840 if (size_left
< 0) {
3845 if (!cur_nb_packets
) {
3846 /* Not enough room to grab one packet of each stream, error. */
3849 return cur_nb_packets
;
3853 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3855 * The wait parameter is ignored so this call always wait for the snapshot to
3856 * complete before returning.
3858 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3860 int cmd_snapshot_record(struct ltt_session
*session
,
3861 struct lttng_snapshot_output
*output
, int wait
)
3864 unsigned int use_tmp_output
= 0;
3865 struct snapshot_output tmp_output
;
3866 unsigned int snapshot_success
= 0;
3872 DBG("Cmd snapshot record for session %s", session
->name
);
3874 /* Get the datetime for the snapshot output directory. */
3875 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3878 ret
= LTTNG_ERR_INVALID
;
3883 * Permission denied to create an output if the session is not
3884 * set in no output mode.
3886 if (session
->output_traces
) {
3887 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3891 /* The session needs to be started at least once. */
3892 if (!session
->has_been_started
) {
3893 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3897 /* Use temporary output for the session. */
3898 if (*output
->ctrl_url
!= '\0') {
3899 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3900 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3903 if (ret
== -ENOMEM
) {
3904 ret
= LTTNG_ERR_NOMEM
;
3906 ret
= LTTNG_ERR_INVALID
;
3910 /* Use the global session count for the temporary snapshot. */
3911 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3913 /* Use the global datetime */
3914 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3918 if (use_tmp_output
) {
3919 int64_t nb_packets_per_stream
;
3921 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3922 tmp_output
.max_size
);
3923 if (nb_packets_per_stream
< 0) {
3924 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3928 if (session
->kernel_session
) {
3929 ret
= record_kernel_snapshot(session
->kernel_session
,
3930 &tmp_output
, session
,
3931 wait
, nb_packets_per_stream
);
3932 if (ret
!= LTTNG_OK
) {
3937 if (session
->ust_session
) {
3938 ret
= record_ust_snapshot(session
->ust_session
,
3939 &tmp_output
, session
,
3940 wait
, nb_packets_per_stream
);
3941 if (ret
!= LTTNG_OK
) {
3946 snapshot_success
= 1;
3948 struct snapshot_output
*sout
;
3949 struct lttng_ht_iter iter
;
3952 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3953 &iter
.iter
, sout
, node
.node
) {
3954 int64_t nb_packets_per_stream
;
3957 * Make a local copy of the output and assign the possible
3958 * temporary value given by the caller.
3960 memset(&tmp_output
, 0, sizeof(tmp_output
));
3961 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3963 if (output
->max_size
!= (uint64_t) -1ULL) {
3964 tmp_output
.max_size
= output
->max_size
;
3967 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3968 tmp_output
.max_size
);
3969 if (nb_packets_per_stream
< 0) {
3970 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3975 /* Use temporary name. */
3976 if (*output
->name
!= '\0') {
3977 if (lttng_strncpy(tmp_output
.name
, output
->name
,
3978 sizeof(tmp_output
.name
))) {
3979 ret
= LTTNG_ERR_INVALID
;
3985 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3986 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3988 if (session
->kernel_session
) {
3989 ret
= record_kernel_snapshot(session
->kernel_session
,
3990 &tmp_output
, session
,
3991 wait
, nb_packets_per_stream
);
3992 if (ret
!= LTTNG_OK
) {
3998 if (session
->ust_session
) {
3999 ret
= record_ust_snapshot(session
->ust_session
,
4000 &tmp_output
, session
,
4001 wait
, nb_packets_per_stream
);
4002 if (ret
!= LTTNG_OK
) {
4007 snapshot_success
= 1;
4012 if (snapshot_success
) {
4013 session
->snapshot
.nb_snapshot
++;
4015 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4023 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4025 int cmd_set_session_shm_path(struct ltt_session
*session
,
4026 const char *shm_path
)
4032 * Can only set shm path before session is started.
4034 if (session
->has_been_started
) {
4035 return LTTNG_ERR_SESSION_STARTED
;
4038 strncpy(session
->shm_path
, shm_path
,
4039 sizeof(session
->shm_path
));
4040 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4046 * Init command subsystem.
4051 * Set network sequence index to 1 for streams to match a relayd
4052 * socket on the consumer side.
4054 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4055 relayd_net_seq_idx
= 1;
4056 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4058 DBG("Command subsystem initialized");