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>
35 #include "health-sessiond.h"
37 #include "kernel-consumer.h"
38 #include "lttng-sessiond.h"
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
51 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 static uint64_t relayd_net_seq_idx
;
54 static int validate_event_name(const char *);
55 static int validate_ust_event_name(const char *);
56 static int cmd_enable_event_internal(struct ltt_session
*session
,
57 struct lttng_domain
*domain
,
58 char *channel_name
, struct lttng_event
*event
,
59 char *filter_expression
,
60 struct lttng_filter_bytecode
*filter
,
61 struct lttng_event_exclusion
*exclusion
,
65 * Create a session path used by list_lttng_sessions for the case that the
66 * session consumer is on the network.
68 static int build_network_session_path(char *dst
, size_t size
,
69 struct ltt_session
*session
)
71 int ret
, kdata_port
, udata_port
;
72 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
73 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
78 memset(tmp_urls
, 0, sizeof(tmp_urls
));
79 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
81 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
83 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
84 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
85 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
88 if (session
->ust_session
&& session
->ust_session
->consumer
) {
89 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
90 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
93 if (uuri
== NULL
&& kuri
== NULL
) {
94 uri
= &session
->consumer
->dst
.net
.control
;
95 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
96 } else if (kuri
&& uuri
) {
97 ret
= uri_compare(kuri
, uuri
);
101 /* Build uuri URL string */
102 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
109 } else if (kuri
&& uuri
== NULL
) {
111 } else if (uuri
&& kuri
== NULL
) {
115 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
121 * Do we have a UST url set. If yes, this means we have both kernel and UST
124 if (*tmp_uurl
!= '\0') {
125 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
126 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
129 if (kuri
|| (!kuri
&& !uuri
)) {
132 /* No kernel URI, use the UST port. */
135 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
143 * Get run-time attributes if the session has been started (discarded events,
146 static int get_kernel_runtime_stats(struct ltt_session
*session
,
147 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
148 uint64_t *lost_packets
)
152 if (!session
->has_been_started
) {
154 *discarded_events
= 0;
159 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
160 session
->kernel_session
->consumer
,
166 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
167 session
->kernel_session
->consumer
,
178 * Get run-time attributes if the session has been started (discarded events,
181 static int get_ust_runtime_stats(struct ltt_session
*session
,
182 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
183 uint64_t *lost_packets
)
186 struct ltt_ust_session
*usess
;
188 usess
= session
->ust_session
;
190 if (!usess
|| !session
->has_been_started
) {
191 *discarded_events
= 0;
197 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
198 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
199 &usess
->buffer_reg_uid_list
,
200 usess
->consumer
, uchan
->id
,
201 uchan
->attr
.overwrite
,
204 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
205 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
206 uchan
, usess
->consumer
,
207 uchan
->attr
.overwrite
,
213 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
214 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
216 ERR("Unsupported buffer type");
226 * Fill lttng_channel array of all channels.
228 static void list_lttng_channels(enum lttng_domain_type domain
,
229 struct ltt_session
*session
, struct lttng_channel
*channels
,
230 struct lttcomm_channel_extended
*chan_exts
)
233 struct ltt_kernel_channel
*kchan
;
235 DBG("Listing channels for session %s", session
->name
);
238 case LTTNG_DOMAIN_KERNEL
:
239 /* Kernel channels */
240 if (session
->kernel_session
!= NULL
) {
241 cds_list_for_each_entry(kchan
,
242 &session
->kernel_session
->channel_list
.head
, list
) {
243 uint64_t discarded_events
, lost_packets
;
245 ret
= get_kernel_runtime_stats(session
, kchan
,
246 &discarded_events
, &lost_packets
);
250 /* Copy lttng_channel struct to array */
251 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
252 channels
[i
].enabled
= kchan
->enabled
;
253 chan_exts
[i
].discarded_events
=
255 chan_exts
[i
].lost_packets
= lost_packets
;
260 case LTTNG_DOMAIN_UST
:
262 struct lttng_ht_iter iter
;
263 struct ltt_ust_channel
*uchan
;
266 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
267 &iter
.iter
, uchan
, node
.node
) {
268 uint64_t discarded_events
, lost_packets
;
270 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
271 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
272 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
273 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
274 channels
[i
].attr
.switch_timer_interval
=
275 uchan
->attr
.switch_timer_interval
;
276 channels
[i
].attr
.read_timer_interval
=
277 uchan
->attr
.read_timer_interval
;
278 channels
[i
].enabled
= uchan
->enabled
;
279 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
280 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
283 * Map enum lttng_ust_output to enum lttng_event_output.
285 switch (uchan
->attr
.output
) {
287 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
291 * LTTNG_UST_MMAP is the only supported UST
298 ret
= get_ust_runtime_stats(session
, uchan
,
299 &discarded_events
, &lost_packets
);
303 chan_exts
[i
].discarded_events
= discarded_events
;
304 chan_exts
[i
].lost_packets
= lost_packets
;
318 static void increment_extended_len(const char *filter_expression
,
319 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
321 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
323 if (filter_expression
) {
324 *extended_len
+= strlen(filter_expression
) + 1;
328 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
332 static void append_extended_info(const char *filter_expression
,
333 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
335 struct lttcomm_event_extended_header extended_header
;
336 size_t filter_len
= 0;
337 size_t nb_exclusions
= 0;
339 if (filter_expression
) {
340 filter_len
= strlen(filter_expression
) + 1;
344 nb_exclusions
= exclusion
->count
;
347 /* Set header fields */
348 extended_header
.filter_len
= filter_len
;
349 extended_header
.nb_exclusions
= nb_exclusions
;
352 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
353 *extended_at
+= sizeof(extended_header
);
355 /* Copy filter string */
356 if (filter_expression
) {
357 memcpy(*extended_at
, filter_expression
, filter_len
);
358 *extended_at
+= filter_len
;
361 /* Copy exclusion names */
363 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
365 memcpy(*extended_at
, &exclusion
->names
, len
);
371 * Create a list of agent domain events.
373 * Return number of events in list on success or else a negative value.
375 static int list_lttng_agent_events(struct agent
*agt
,
376 struct lttng_event
**events
, size_t *total_size
)
379 unsigned int nb_event
= 0;
380 struct agent_event
*event
;
381 struct lttng_event
*tmp_events
;
382 struct lttng_ht_iter iter
;
383 size_t extended_len
= 0;
389 DBG3("Listing agent events");
392 nb_event
= lttng_ht_get_count(agt
->events
);
400 /* Compute required extended infos size */
401 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
404 * This is only valid because the commands which add events are
405 * processed in the same thread as the listing.
408 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
409 increment_extended_len(event
->filter_expression
, NULL
,
414 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
415 tmp_events
= zmalloc(*total_size
);
417 PERROR("zmalloc agent events session");
418 ret
= -LTTNG_ERR_FATAL
;
422 extended_at
= ((uint8_t *) tmp_events
) +
423 nb_event
* sizeof(struct lttng_event
);
426 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
427 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
428 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
429 tmp_events
[i
].enabled
= event
->enabled
;
430 tmp_events
[i
].loglevel
= event
->loglevel_value
;
431 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
434 /* Append extended info */
435 append_extended_info(event
->filter_expression
, NULL
,
440 *events
= tmp_events
;
444 assert(nb_event
== i
);
449 * Create a list of ust global domain events.
451 static int list_lttng_ust_global_events(char *channel_name
,
452 struct ltt_ust_domain_global
*ust_global
,
453 struct lttng_event
**events
, size_t *total_size
)
456 unsigned int nb_event
= 0;
457 struct lttng_ht_iter iter
;
458 struct lttng_ht_node_str
*node
;
459 struct ltt_ust_channel
*uchan
;
460 struct ltt_ust_event
*uevent
;
461 struct lttng_event
*tmp
;
462 size_t extended_len
= 0;
465 DBG("Listing UST global events for channel %s", channel_name
);
469 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
470 node
= lttng_ht_iter_get_node_str(&iter
);
472 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
476 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
478 nb_event
= lttng_ht_get_count(uchan
->events
);
485 DBG3("Listing UST global %d events", nb_event
);
487 /* Compute required extended infos size */
488 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
489 if (uevent
->internal
) {
494 increment_extended_len(uevent
->filter_expression
,
495 uevent
->exclusion
, &extended_len
);
498 /* All events are internal, skip. */
504 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
505 tmp
= zmalloc(*total_size
);
507 ret
= -LTTNG_ERR_FATAL
;
511 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
513 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
514 if (uevent
->internal
) {
515 /* This event should remain hidden from clients */
518 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
519 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
520 tmp
[i
].enabled
= uevent
->enabled
;
522 switch (uevent
->attr
.instrumentation
) {
523 case LTTNG_UST_TRACEPOINT
:
524 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
526 case LTTNG_UST_PROBE
:
527 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
529 case LTTNG_UST_FUNCTION
:
530 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
534 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
535 switch (uevent
->attr
.loglevel_type
) {
536 case LTTNG_UST_LOGLEVEL_ALL
:
537 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
539 case LTTNG_UST_LOGLEVEL_RANGE
:
540 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
542 case LTTNG_UST_LOGLEVEL_SINGLE
:
543 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
546 if (uevent
->filter
) {
549 if (uevent
->exclusion
) {
550 tmp
[i
].exclusion
= 1;
554 /* Append extended info */
555 append_extended_info(uevent
->filter_expression
,
556 uevent
->exclusion
, &extended_at
);
567 * Fill lttng_event array of all kernel events in the channel.
569 static int list_lttng_kernel_events(char *channel_name
,
570 struct ltt_kernel_session
*kernel_session
,
571 struct lttng_event
**events
, size_t *total_size
)
574 unsigned int nb_event
;
575 struct ltt_kernel_event
*event
;
576 struct ltt_kernel_channel
*kchan
;
577 size_t extended_len
= 0;
580 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
582 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
586 nb_event
= kchan
->event_count
;
588 DBG("Listing events for channel %s", kchan
->channel
->name
);
596 /* Compute required extended infos size */
597 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
598 increment_extended_len(event
->filter_expression
, NULL
,
602 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
603 *events
= zmalloc(*total_size
);
604 if (*events
== NULL
) {
605 ret
= LTTNG_ERR_FATAL
;
609 extended_at
= ((uint8_t *) events
) +
610 nb_event
* sizeof(struct lttng_event
);
612 /* Kernel channels */
613 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
614 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
615 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
616 (*events
)[i
].enabled
= event
->enabled
;
617 (*events
)[i
].filter
=
618 (unsigned char) !!event
->filter_expression
;
620 switch (event
->event
->instrumentation
) {
621 case LTTNG_KERNEL_TRACEPOINT
:
622 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
624 case LTTNG_KERNEL_KRETPROBE
:
625 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
626 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
627 sizeof(struct lttng_kernel_kprobe
));
629 case LTTNG_KERNEL_KPROBE
:
630 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
631 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
632 sizeof(struct lttng_kernel_kprobe
));
634 case LTTNG_KERNEL_FUNCTION
:
635 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
636 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
637 sizeof(struct lttng_kernel_function
));
639 case LTTNG_KERNEL_NOOP
:
640 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
642 case LTTNG_KERNEL_SYSCALL
:
643 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
645 case LTTNG_KERNEL_ALL
:
651 /* Append extended info */
652 append_extended_info(event
->filter_expression
, NULL
,
660 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
672 /* Negate the error code to differentiate the size from an error */
677 * Add URI so the consumer output object. Set the correct path depending on the
678 * domain adding the default trace directory.
680 static int add_uri_to_consumer(struct consumer_output
*consumer
,
681 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
682 const char *session_name
)
685 const char *default_trace_dir
;
689 if (consumer
== NULL
) {
690 DBG("No consumer detected. Don't add URI. Stopping.");
691 ret
= LTTNG_ERR_NO_CONSUMER
;
696 case LTTNG_DOMAIN_KERNEL
:
697 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
699 case LTTNG_DOMAIN_UST
:
700 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
704 * This case is possible is we try to add the URI to the global tracing
705 * session consumer object which in this case there is no subdir.
707 default_trace_dir
= "";
710 switch (uri
->dtype
) {
713 DBG2("Setting network URI to consumer");
715 if (consumer
->type
== CONSUMER_DST_NET
) {
716 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
717 consumer
->dst
.net
.control_isset
) ||
718 (uri
->stype
== LTTNG_STREAM_DATA
&&
719 consumer
->dst
.net
.data_isset
)) {
720 ret
= LTTNG_ERR_URL_EXIST
;
724 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
727 consumer
->type
= CONSUMER_DST_NET
;
729 /* Set URI into consumer output object */
730 ret
= consumer_set_network_uri(consumer
, uri
);
734 } else if (ret
== 1) {
736 * URI was the same in the consumer so we do not append the subdir
737 * again so to not duplicate output dir.
743 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
744 ret
= consumer_set_subdir(consumer
, session_name
);
746 ret
= LTTNG_ERR_FATAL
;
751 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
752 /* On a new subdir, reappend the default trace dir. */
753 strncat(consumer
->subdir
, default_trace_dir
,
754 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
755 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
760 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
761 memset(consumer
->dst
.trace_path
, 0,
762 sizeof(consumer
->dst
.trace_path
));
763 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
764 sizeof(consumer
->dst
.trace_path
));
765 /* Append default trace dir */
766 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
767 sizeof(consumer
->dst
.trace_path
) -
768 strlen(consumer
->dst
.trace_path
) - 1);
769 /* Flag consumer as local. */
770 consumer
->type
= CONSUMER_DST_LOCAL
;
781 * Init tracing by creating trace directory and sending fds kernel consumer.
783 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
786 struct lttng_ht_iter iter
;
787 struct consumer_socket
*socket
;
793 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
794 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
796 pthread_mutex_lock(socket
->lock
);
797 ret
= kernel_consumer_send_session(socket
, session
);
798 pthread_mutex_unlock(socket
->lock
);
800 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
812 * Create a socket to the relayd using the URI.
814 * On success, the relayd_sock pointer is set to the created socket.
815 * Else, it's stays untouched and a lttcomm error code is returned.
817 static int create_connect_relayd(struct lttng_uri
*uri
,
818 struct lttcomm_relayd_sock
**relayd_sock
)
821 struct lttcomm_relayd_sock
*rsock
;
823 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
824 RELAYD_VERSION_COMM_MINOR
);
826 ret
= LTTNG_ERR_FATAL
;
831 * Connect to relayd so we can proceed with a session creation. This call
832 * can possibly block for an arbitrary amount of time to set the health
833 * state to be in poll execution.
836 ret
= relayd_connect(rsock
);
839 ERR("Unable to reach lttng-relayd");
840 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
844 /* Create socket for control stream. */
845 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
846 DBG3("Creating relayd stream socket from URI");
848 /* Check relayd version */
849 ret
= relayd_version_check(rsock
);
851 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
854 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
855 DBG3("Creating relayd data socket from URI");
857 /* Command is not valid */
858 ERR("Relayd invalid stream type: %d", uri
->stype
);
859 ret
= LTTNG_ERR_INVALID
;
863 *relayd_sock
= rsock
;
868 /* The returned value is not useful since we are on an error path. */
869 (void) relayd_close(rsock
);
877 * Connect to the relayd using URI and send the socket to the right consumer.
879 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
880 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
881 struct consumer_output
*consumer
,
882 struct consumer_socket
*consumer_sock
,
883 char *session_name
, char *hostname
, int session_live_timer
)
886 struct lttcomm_relayd_sock
*rsock
= NULL
;
888 /* Connect to relayd and make version check if uri is the control. */
889 ret
= create_connect_relayd(relayd_uri
, &rsock
);
890 if (ret
!= LTTNG_OK
) {
895 /* Set the network sequence index if not set. */
896 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
897 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
899 * Increment net_seq_idx because we are about to transfer the
900 * new relayd socket to the consumer.
901 * Assign unique key so the consumer can match streams.
903 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
904 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
907 /* Send relayd socket to consumer. */
908 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
909 relayd_uri
->stype
, session_id
,
910 session_name
, hostname
, session_live_timer
);
912 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
916 /* Flag that the corresponding socket was sent. */
917 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
918 consumer_sock
->control_sock_sent
= 1;
919 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
920 consumer_sock
->data_sock_sent
= 1;
926 * Close socket which was dup on the consumer side. The session daemon does
927 * NOT keep track of the relayd socket(s) once transfer to the consumer.
931 (void) relayd_close(rsock
);
935 if (ret
!= LTTNG_OK
) {
937 * The consumer output for this session should not be used anymore
938 * since the relayd connection failed thus making any tracing or/and
939 * streaming not usable.
941 consumer
->enabled
= 0;
947 * Send both relayd sockets to a specific consumer and domain. This is a
948 * helper function to facilitate sending the information to the consumer for a
951 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
952 unsigned int session_id
, struct consumer_output
*consumer
,
953 struct consumer_socket
*sock
, char *session_name
,
954 char *hostname
, int session_live_timer
)
961 /* Sending control relayd socket. */
962 if (!sock
->control_sock_sent
) {
963 ret
= send_consumer_relayd_socket(domain
, session_id
,
964 &consumer
->dst
.net
.control
, consumer
, sock
,
965 session_name
, hostname
, session_live_timer
);
966 if (ret
!= LTTNG_OK
) {
971 /* Sending data relayd socket. */
972 if (!sock
->data_sock_sent
) {
973 ret
= send_consumer_relayd_socket(domain
, session_id
,
974 &consumer
->dst
.net
.data
, consumer
, sock
,
975 session_name
, hostname
, session_live_timer
);
976 if (ret
!= LTTNG_OK
) {
986 * Setup relayd connections for a tracing session. First creates the socket to
987 * the relayd and send them to the right domain consumer. Consumer type MUST be
990 int cmd_setup_relayd(struct ltt_session
*session
)
993 struct ltt_ust_session
*usess
;
994 struct ltt_kernel_session
*ksess
;
995 struct consumer_socket
*socket
;
996 struct lttng_ht_iter iter
;
1000 usess
= session
->ust_session
;
1001 ksess
= session
->kernel_session
;
1003 DBG("Setting relayd for session %s", session
->name
);
1007 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1008 && usess
->consumer
->enabled
) {
1009 /* For each consumer socket, send relayd sockets */
1010 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1011 socket
, node
.node
) {
1012 pthread_mutex_lock(socket
->lock
);
1013 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1014 usess
->consumer
, socket
,
1015 session
->name
, session
->hostname
,
1016 session
->live_timer
);
1017 pthread_mutex_unlock(socket
->lock
);
1018 if (ret
!= LTTNG_OK
) {
1021 /* Session is now ready for network streaming. */
1022 session
->net_handle
= 1;
1026 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1027 && ksess
->consumer
->enabled
) {
1028 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1029 socket
, node
.node
) {
1030 pthread_mutex_lock(socket
->lock
);
1031 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1032 ksess
->consumer
, socket
,
1033 session
->name
, session
->hostname
,
1034 session
->live_timer
);
1035 pthread_mutex_unlock(socket
->lock
);
1036 if (ret
!= LTTNG_OK
) {
1039 /* Session is now ready for network streaming. */
1040 session
->net_handle
= 1;
1050 * Start a kernel session by opening all necessary streams.
1052 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1055 struct ltt_kernel_channel
*kchan
;
1057 /* Open kernel metadata */
1058 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1059 ret
= kernel_open_metadata(ksess
);
1061 ret
= LTTNG_ERR_KERN_META_FAIL
;
1066 /* Open kernel metadata stream */
1067 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1068 ret
= kernel_open_metadata_stream(ksess
);
1070 ERR("Kernel create metadata stream failed");
1071 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1076 /* For each channel */
1077 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1078 if (kchan
->stream_count
== 0) {
1079 ret
= kernel_open_channel_stream(kchan
);
1081 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1084 /* Update the stream global counter */
1085 ksess
->stream_count_global
+= ret
;
1089 /* Setup kernel consumer socket and send fds to it */
1090 ret
= init_kernel_tracing(ksess
);
1092 ret
= LTTNG_ERR_KERN_START_FAIL
;
1096 /* This start the kernel tracing */
1097 ret
= kernel_start_session(ksess
);
1099 ret
= LTTNG_ERR_KERN_START_FAIL
;
1103 /* Quiescent wait after starting trace */
1104 kernel_wait_quiescent(kernel_tracer_fd
);
1115 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1117 int cmd_disable_channel(struct ltt_session
*session
,
1118 enum lttng_domain_type domain
, char *channel_name
)
1121 struct ltt_ust_session
*usess
;
1123 usess
= session
->ust_session
;
1128 case LTTNG_DOMAIN_KERNEL
:
1130 ret
= channel_kernel_disable(session
->kernel_session
,
1132 if (ret
!= LTTNG_OK
) {
1136 kernel_wait_quiescent(kernel_tracer_fd
);
1139 case LTTNG_DOMAIN_UST
:
1141 struct ltt_ust_channel
*uchan
;
1142 struct lttng_ht
*chan_ht
;
1144 chan_ht
= usess
->domain_global
.channels
;
1146 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1147 if (uchan
== NULL
) {
1148 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1152 ret
= channel_ust_disable(usess
, uchan
);
1153 if (ret
!= LTTNG_OK
) {
1159 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1171 * Command LTTNG_TRACK_PID processed by the client thread.
1173 * Called with session lock held.
1175 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1183 case LTTNG_DOMAIN_KERNEL
:
1185 struct ltt_kernel_session
*ksess
;
1187 ksess
= session
->kernel_session
;
1189 ret
= kernel_track_pid(ksess
, pid
);
1190 if (ret
!= LTTNG_OK
) {
1194 kernel_wait_quiescent(kernel_tracer_fd
);
1197 case LTTNG_DOMAIN_UST
:
1199 struct ltt_ust_session
*usess
;
1201 usess
= session
->ust_session
;
1203 ret
= trace_ust_track_pid(usess
, pid
);
1204 if (ret
!= LTTNG_OK
) {
1210 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1222 * Command LTTNG_UNTRACK_PID processed by the client thread.
1224 * Called with session lock held.
1226 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1234 case LTTNG_DOMAIN_KERNEL
:
1236 struct ltt_kernel_session
*ksess
;
1238 ksess
= session
->kernel_session
;
1240 ret
= kernel_untrack_pid(ksess
, pid
);
1241 if (ret
!= LTTNG_OK
) {
1245 kernel_wait_quiescent(kernel_tracer_fd
);
1248 case LTTNG_DOMAIN_UST
:
1250 struct ltt_ust_session
*usess
;
1252 usess
= session
->ust_session
;
1254 ret
= trace_ust_untrack_pid(usess
, pid
);
1255 if (ret
!= LTTNG_OK
) {
1261 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1273 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1275 * The wpipe arguments is used as a notifier for the kernel thread.
1277 int cmd_enable_channel(struct ltt_session
*session
,
1278 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1281 struct ltt_ust_session
*usess
= session
->ust_session
;
1282 struct lttng_ht
*chan_ht
;
1289 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1291 /* Validate channel name */
1292 if (attr
->name
[0] == '.' ||
1293 memchr(attr
->name
, '/', len
) != NULL
) {
1294 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1298 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1303 * Don't try to enable a channel if the session has been started at
1304 * some point in time before. The tracer does not allow it.
1306 if (session
->has_been_started
) {
1307 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1312 * If the session is a live session, remove the switch timer, the
1313 * live timer does the same thing but sends also synchronisation
1314 * beacons for inactive streams.
1316 if (session
->live_timer
> 0) {
1317 attr
->attr
.live_timer_interval
= session
->live_timer
;
1318 attr
->attr
.switch_timer_interval
= 0;
1322 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1323 * mode and with less than 2 subbuf so block it right away and send back an
1324 * invalid attribute error.
1326 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1327 ret
= LTTNG_ERR_INVALID
;
1331 switch (domain
->type
) {
1332 case LTTNG_DOMAIN_KERNEL
:
1334 struct ltt_kernel_channel
*kchan
;
1336 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1337 session
->kernel_session
);
1338 if (kchan
== NULL
) {
1339 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1340 if (attr
->name
[0] != '\0') {
1341 session
->kernel_session
->has_non_default_channel
= 1;
1344 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1347 if (ret
!= LTTNG_OK
) {
1351 kernel_wait_quiescent(kernel_tracer_fd
);
1354 case LTTNG_DOMAIN_UST
:
1355 case LTTNG_DOMAIN_JUL
:
1356 case LTTNG_DOMAIN_LOG4J
:
1357 case LTTNG_DOMAIN_PYTHON
:
1359 struct ltt_ust_channel
*uchan
;
1364 * Current agent implementation limitations force us to allow
1365 * only one channel at once in "agent" subdomains. Each
1366 * subdomain has a default channel name which must be strictly
1369 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1370 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1371 LTTNG_SYMBOL_NAME_LEN
)) {
1372 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1375 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1376 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1377 LTTNG_SYMBOL_NAME_LEN
)) {
1378 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1381 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1382 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1383 LTTNG_SYMBOL_NAME_LEN
)) {
1384 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1389 chan_ht
= usess
->domain_global
.channels
;
1391 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1392 if (uchan
== NULL
) {
1393 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1394 if (attr
->name
[0] != '\0') {
1395 usess
->has_non_default_channel
= 1;
1398 ret
= channel_ust_enable(usess
, uchan
);
1403 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1414 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1416 int cmd_disable_event(struct ltt_session
*session
,
1417 enum lttng_domain_type domain
, char *channel_name
,
1418 struct lttng_event
*event
)
1423 DBG("Disable event command for event \'%s\'", event
->name
);
1425 event_name
= event
->name
;
1426 if (validate_event_name(event_name
)) {
1427 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1431 /* Error out on unhandled search criteria */
1432 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1433 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1434 ret
= LTTNG_ERR_UNK
;
1441 case LTTNG_DOMAIN_KERNEL
:
1443 struct ltt_kernel_channel
*kchan
;
1444 struct ltt_kernel_session
*ksess
;
1446 ksess
= session
->kernel_session
;
1449 * If a non-default channel has been created in the
1450 * session, explicitely require that -c chan_name needs
1453 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1454 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1458 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1459 if (kchan
== NULL
) {
1460 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1464 switch (event
->type
) {
1465 case LTTNG_EVENT_ALL
:
1466 case LTTNG_EVENT_TRACEPOINT
:
1467 case LTTNG_EVENT_SYSCALL
:
1468 case LTTNG_EVENT_PROBE
:
1469 case LTTNG_EVENT_FUNCTION
:
1470 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1471 if (event_name
[0] == '\0') {
1472 ret
= event_kernel_disable_event(kchan
,
1475 ret
= event_kernel_disable_event(kchan
,
1476 event_name
, event
->type
);
1478 if (ret
!= LTTNG_OK
) {
1483 ret
= LTTNG_ERR_UNK
;
1487 kernel_wait_quiescent(kernel_tracer_fd
);
1490 case LTTNG_DOMAIN_UST
:
1492 struct ltt_ust_channel
*uchan
;
1493 struct ltt_ust_session
*usess
;
1495 usess
= session
->ust_session
;
1497 if (validate_ust_event_name(event_name
)) {
1498 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1503 * If a non-default channel has been created in the
1504 * session, explicitly require that -c chan_name needs
1507 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1508 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1512 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1514 if (uchan
== NULL
) {
1515 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1519 switch (event
->type
) {
1520 case LTTNG_EVENT_ALL
:
1522 * An empty event name means that everything
1523 * should be disabled.
1525 if (event
->name
[0] == '\0') {
1526 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1528 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1531 if (ret
!= LTTNG_OK
) {
1536 ret
= LTTNG_ERR_UNK
;
1540 DBG3("Disable UST event %s in channel %s completed", event_name
,
1544 case LTTNG_DOMAIN_LOG4J
:
1545 case LTTNG_DOMAIN_JUL
:
1546 case LTTNG_DOMAIN_PYTHON
:
1549 struct ltt_ust_session
*usess
= session
->ust_session
;
1553 switch (event
->type
) {
1554 case LTTNG_EVENT_ALL
:
1557 ret
= LTTNG_ERR_UNK
;
1561 agt
= trace_ust_find_agent(usess
, domain
);
1563 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1567 * An empty event name means that everything
1568 * should be disabled.
1570 if (event
->name
[0] == '\0') {
1571 ret
= event_agent_disable_all(usess
, agt
);
1573 ret
= event_agent_disable(usess
, agt
, event_name
);
1575 if (ret
!= LTTNG_OK
) {
1582 ret
= LTTNG_ERR_UND
;
1595 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1597 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1598 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1600 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1601 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1603 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1604 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1605 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1609 case LTTNG_DOMAIN_KERNEL
:
1610 assert(session
->kernel_session
);
1612 if (session
->kernel_session
->channel_count
== 0) {
1613 /* Create default channel */
1614 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1615 if (ret
!= LTTNG_OK
) {
1618 chan_kern_created
= 1;
1620 /* Add kernel context to kernel tracer */
1621 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1622 if (ret
!= LTTNG_OK
) {
1626 case LTTNG_DOMAIN_JUL
:
1627 case LTTNG_DOMAIN_LOG4J
:
1630 * Validate channel name.
1631 * If no channel name is given and the domain is JUL or LOG4J,
1632 * set it to the appropriate domain-specific channel name. If
1633 * a name is provided but does not match the expexted channel
1634 * name, return an error.
1636 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1637 strcmp(channel_name
,
1638 DEFAULT_JUL_CHANNEL_NAME
)) {
1639 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1641 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1642 strcmp(channel_name
,
1643 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1644 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1647 /* break is _not_ missing here. */
1649 case LTTNG_DOMAIN_UST
:
1651 struct ltt_ust_session
*usess
= session
->ust_session
;
1652 unsigned int chan_count
;
1656 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1657 if (chan_count
== 0) {
1658 struct lttng_channel
*attr
;
1659 /* Create default channel */
1660 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1662 ret
= LTTNG_ERR_FATAL
;
1666 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1667 if (ret
!= LTTNG_OK
) {
1672 chan_ust_created
= 1;
1675 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1676 free(app_ctx_provider_name
);
1678 app_ctx_name
= NULL
;
1679 app_ctx_provider_name
= NULL
;
1680 if (ret
!= LTTNG_OK
) {
1686 ret
= LTTNG_ERR_UND
;
1694 if (chan_kern_created
) {
1695 struct ltt_kernel_channel
*kchan
=
1696 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1697 session
->kernel_session
);
1698 /* Created previously, this should NOT fail. */
1700 kernel_destroy_channel(kchan
);
1703 if (chan_ust_created
) {
1704 struct ltt_ust_channel
*uchan
=
1705 trace_ust_find_channel_by_name(
1706 session
->ust_session
->domain_global
.channels
,
1707 DEFAULT_CHANNEL_NAME
);
1708 /* Created previously, this should NOT fail. */
1710 /* Remove from the channel list of the session. */
1711 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1713 trace_ust_destroy_channel(uchan
);
1716 free(app_ctx_provider_name
);
1721 static int validate_event_name(const char *name
)
1724 const char *c
= name
;
1725 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1726 bool null_terminated
= false;
1729 * Make sure that unescaped wildcards are only used as the last
1730 * character of the event name.
1732 while (c
< event_name_end
) {
1735 null_terminated
= true;
1741 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1742 /* Wildcard is not the last character */
1743 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1752 if (!ret
&& !null_terminated
) {
1753 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1758 static inline bool name_starts_with(const char *name
, const char *prefix
)
1760 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1762 return !strncmp(name
, prefix
, max_cmp_len
);
1765 /* Perform userspace-specific event name validation */
1766 static int validate_ust_event_name(const char *name
)
1776 * Check name against all internal UST event component namespaces used
1779 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1780 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1781 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1790 * Internal version of cmd_enable_event() with a supplemental
1791 * "internal_event" flag which is used to enable internal events which should
1792 * be hidden from clients. Such events are used in the agent implementation to
1793 * enable the events through which all "agent" events are funeled.
1795 static int _cmd_enable_event(struct ltt_session
*session
,
1796 struct lttng_domain
*domain
,
1797 char *channel_name
, struct lttng_event
*event
,
1798 char *filter_expression
,
1799 struct lttng_filter_bytecode
*filter
,
1800 struct lttng_event_exclusion
*exclusion
,
1801 int wpipe
, bool internal_event
)
1803 int ret
, channel_created
= 0;
1804 struct lttng_channel
*attr
;
1808 assert(channel_name
);
1810 /* If we have a filter, we must have its filter expression */
1811 assert(!(!!filter_expression
^ !!filter
));
1813 DBG("Enable event command for event \'%s\'", event
->name
);
1817 ret
= validate_event_name(event
->name
);
1822 switch (domain
->type
) {
1823 case LTTNG_DOMAIN_KERNEL
:
1825 struct ltt_kernel_channel
*kchan
;
1828 * If a non-default channel has been created in the
1829 * session, explicitely require that -c chan_name needs
1832 if (session
->kernel_session
->has_non_default_channel
1833 && channel_name
[0] == '\0') {
1834 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1838 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1839 session
->kernel_session
);
1840 if (kchan
== NULL
) {
1841 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1842 LTTNG_BUFFER_GLOBAL
);
1844 ret
= LTTNG_ERR_FATAL
;
1847 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1849 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1850 if (ret
!= LTTNG_OK
) {
1856 channel_created
= 1;
1859 /* Get the newly created kernel channel pointer */
1860 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1861 session
->kernel_session
);
1862 if (kchan
== NULL
) {
1863 /* This sould not happen... */
1864 ret
= LTTNG_ERR_FATAL
;
1868 switch (event
->type
) {
1869 case LTTNG_EVENT_ALL
:
1871 char *filter_expression_a
= NULL
;
1872 struct lttng_filter_bytecode
*filter_a
= NULL
;
1875 * We need to duplicate filter_expression and filter,
1876 * because ownership is passed to first enable
1879 if (filter_expression
) {
1880 filter_expression_a
= strdup(filter_expression
);
1881 if (!filter_expression_a
) {
1882 ret
= LTTNG_ERR_FATAL
;
1887 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1889 free(filter_expression_a
);
1890 ret
= LTTNG_ERR_FATAL
;
1893 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1895 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1896 ret
= event_kernel_enable_event(kchan
, event
,
1897 filter_expression
, filter
);
1898 /* We have passed ownership */
1899 filter_expression
= NULL
;
1901 if (ret
!= LTTNG_OK
) {
1902 if (channel_created
) {
1903 /* Let's not leak a useless channel. */
1904 kernel_destroy_channel(kchan
);
1906 free(filter_expression_a
);
1910 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1911 ret
= event_kernel_enable_event(kchan
, event
,
1912 filter_expression_a
, filter_a
);
1913 /* We have passed ownership */
1914 filter_expression_a
= NULL
;
1916 if (ret
!= LTTNG_OK
) {
1921 case LTTNG_EVENT_PROBE
:
1922 case LTTNG_EVENT_FUNCTION
:
1923 case LTTNG_EVENT_FUNCTION_ENTRY
:
1924 case LTTNG_EVENT_TRACEPOINT
:
1925 ret
= event_kernel_enable_event(kchan
, event
,
1926 filter_expression
, filter
);
1927 /* We have passed ownership */
1928 filter_expression
= NULL
;
1930 if (ret
!= LTTNG_OK
) {
1931 if (channel_created
) {
1932 /* Let's not leak a useless channel. */
1933 kernel_destroy_channel(kchan
);
1938 case LTTNG_EVENT_SYSCALL
:
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
) {
1949 ret
= LTTNG_ERR_UNK
;
1953 kernel_wait_quiescent(kernel_tracer_fd
);
1956 case LTTNG_DOMAIN_UST
:
1958 struct ltt_ust_channel
*uchan
;
1959 struct ltt_ust_session
*usess
= session
->ust_session
;
1964 * If a non-default channel has been created in the
1965 * session, explicitely require that -c chan_name needs
1968 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1969 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1973 /* Get channel from global UST domain */
1974 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1976 if (uchan
== NULL
) {
1977 /* Create default channel */
1978 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1979 usess
->buffer_type
);
1981 ret
= LTTNG_ERR_FATAL
;
1984 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1986 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1987 if (ret
!= LTTNG_OK
) {
1993 /* Get the newly created channel reference back */
1994 uchan
= trace_ust_find_channel_by_name(
1995 usess
->domain_global
.channels
, channel_name
);
1999 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2001 * Don't allow users to add UST events to channels which
2002 * are assigned to a userspace subdomain (JUL, Log4J,
2005 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2009 if (!internal_event
) {
2011 * Ensure the event name is not reserved for internal
2014 ret
= validate_ust_event_name(event
->name
);
2016 WARN("Userspace event name %s failed validation.",
2018 event
->name
: "NULL");
2019 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2024 /* At this point, the session and channel exist on the tracer */
2025 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2026 filter_expression
, filter
, exclusion
,
2028 /* We have passed ownership */
2029 filter_expression
= NULL
;
2032 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2033 goto already_enabled
;
2034 } else if (ret
!= LTTNG_OK
) {
2039 case LTTNG_DOMAIN_LOG4J
:
2040 case LTTNG_DOMAIN_JUL
:
2041 case LTTNG_DOMAIN_PYTHON
:
2043 const char *default_event_name
, *default_chan_name
;
2045 struct lttng_event uevent
;
2046 struct lttng_domain tmp_dom
;
2047 struct ltt_ust_session
*usess
= session
->ust_session
;
2051 agt
= trace_ust_find_agent(usess
, domain
->type
);
2053 agt
= agent_create(domain
->type
);
2055 ret
= LTTNG_ERR_NOMEM
;
2058 agent_add(agt
, usess
->agents
);
2061 /* Create the default tracepoint. */
2062 memset(&uevent
, 0, sizeof(uevent
));
2063 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2064 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2065 default_event_name
= event_get_default_agent_ust_name(
2067 if (!default_event_name
) {
2068 ret
= LTTNG_ERR_FATAL
;
2071 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2072 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2075 * The domain type is changed because we are about to enable the
2076 * default channel and event for the JUL domain that are hardcoded.
2077 * This happens in the UST domain.
2079 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2080 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2082 switch (domain
->type
) {
2083 case LTTNG_DOMAIN_LOG4J
:
2084 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2086 case LTTNG_DOMAIN_JUL
:
2087 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2089 case LTTNG_DOMAIN_PYTHON
:
2090 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2093 /* The switch/case we are in makes this impossible */
2098 char *filter_expression_copy
= NULL
;
2099 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2102 const size_t filter_size
= sizeof(
2103 struct lttng_filter_bytecode
)
2106 filter_copy
= zmalloc(filter_size
);
2108 ret
= LTTNG_ERR_NOMEM
;
2111 memcpy(filter_copy
, filter
, filter_size
);
2113 filter_expression_copy
=
2114 strdup(filter_expression
);
2115 if (!filter_expression
) {
2116 ret
= LTTNG_ERR_NOMEM
;
2119 if (!filter_expression_copy
|| !filter_copy
) {
2120 free(filter_expression_copy
);
2126 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2127 (char *) default_chan_name
,
2128 &uevent
, filter_expression_copy
,
2129 filter_copy
, NULL
, wpipe
);
2132 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2133 goto already_enabled
;
2134 } else if (ret
!= LTTNG_OK
) {
2138 /* The wild card * means that everything should be enabled. */
2139 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2140 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2143 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2147 filter_expression
= NULL
;
2148 if (ret
!= LTTNG_OK
) {
2155 ret
= LTTNG_ERR_UND
;
2163 free(filter_expression
);
2171 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2172 * We own filter, exclusion, and filter_expression.
2174 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2175 char *channel_name
, struct lttng_event
*event
,
2176 char *filter_expression
,
2177 struct lttng_filter_bytecode
*filter
,
2178 struct lttng_event_exclusion
*exclusion
,
2181 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2182 filter_expression
, filter
, exclusion
, wpipe
, false);
2186 * Enable an event which is internal to LTTng. An internal should
2187 * never be made visible to clients and are immune to checks such as
2190 static int cmd_enable_event_internal(struct ltt_session
*session
,
2191 struct lttng_domain
*domain
,
2192 char *channel_name
, struct lttng_event
*event
,
2193 char *filter_expression
,
2194 struct lttng_filter_bytecode
*filter
,
2195 struct lttng_event_exclusion
*exclusion
,
2198 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2199 filter_expression
, filter
, exclusion
, wpipe
, true);
2203 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2205 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2206 struct lttng_event
**events
)
2209 ssize_t nb_events
= 0;
2212 case LTTNG_DOMAIN_KERNEL
:
2213 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2214 if (nb_events
< 0) {
2215 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2219 case LTTNG_DOMAIN_UST
:
2220 nb_events
= ust_app_list_events(events
);
2221 if (nb_events
< 0) {
2222 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2226 case LTTNG_DOMAIN_LOG4J
:
2227 case LTTNG_DOMAIN_JUL
:
2228 case LTTNG_DOMAIN_PYTHON
:
2229 nb_events
= agent_list_events(events
, domain
);
2230 if (nb_events
< 0) {
2231 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2236 ret
= LTTNG_ERR_UND
;
2243 /* Return negative value to differentiate return code */
2248 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2250 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2251 struct lttng_event_field
**fields
)
2254 ssize_t nb_fields
= 0;
2257 case LTTNG_DOMAIN_UST
:
2258 nb_fields
= ust_app_list_event_fields(fields
);
2259 if (nb_fields
< 0) {
2260 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2264 case LTTNG_DOMAIN_KERNEL
:
2265 default: /* fall-through */
2266 ret
= LTTNG_ERR_UND
;
2273 /* Return negative value to differentiate return code */
2277 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2279 return syscall_table_list(events
);
2283 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2285 * Called with session lock held.
2287 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2288 enum lttng_domain_type domain
, int32_t **pids
)
2291 ssize_t nr_pids
= 0;
2294 case LTTNG_DOMAIN_KERNEL
:
2296 struct ltt_kernel_session
*ksess
;
2298 ksess
= session
->kernel_session
;
2299 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2301 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2306 case LTTNG_DOMAIN_UST
:
2308 struct ltt_ust_session
*usess
;
2310 usess
= session
->ust_session
;
2311 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2313 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2318 case LTTNG_DOMAIN_LOG4J
:
2319 case LTTNG_DOMAIN_JUL
:
2320 case LTTNG_DOMAIN_PYTHON
:
2322 ret
= LTTNG_ERR_UND
;
2329 /* Return negative value to differentiate return code */
2334 * Command LTTNG_START_TRACE processed by the client thread.
2336 * Called with session mutex held.
2338 int cmd_start_trace(struct ltt_session
*session
)
2341 unsigned long nb_chan
= 0;
2342 struct ltt_kernel_session
*ksession
;
2343 struct ltt_ust_session
*usess
;
2347 /* Ease our life a bit ;) */
2348 ksession
= session
->kernel_session
;
2349 usess
= session
->ust_session
;
2351 /* Is the session already started? */
2352 if (session
->active
) {
2353 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2358 * Starting a session without channel is useless since after that it's not
2359 * possible to enable channel thus inform the client.
2361 if (usess
&& usess
->domain_global
.channels
) {
2362 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2365 nb_chan
+= ksession
->channel_count
;
2368 ret
= LTTNG_ERR_NO_CHANNEL
;
2372 /* Kernel tracing */
2373 if (ksession
!= NULL
) {
2374 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2375 if (ret
!= LTTNG_OK
) {
2380 /* Flag session that trace should start automatically */
2383 * Even though the start trace might fail, flag this session active so
2384 * other application coming in are started by default.
2388 ret
= ust_app_start_trace_all(usess
);
2390 ret
= LTTNG_ERR_UST_START_FAIL
;
2395 /* Flag this after a successful start. */
2396 session
->has_been_started
= 1;
2397 session
->active
= 1;
2406 * Command LTTNG_STOP_TRACE processed by the client thread.
2408 int cmd_stop_trace(struct ltt_session
*session
)
2411 struct ltt_kernel_channel
*kchan
;
2412 struct ltt_kernel_session
*ksession
;
2413 struct ltt_ust_session
*usess
;
2418 ksession
= session
->kernel_session
;
2419 usess
= session
->ust_session
;
2421 /* Session is not active. Skip everythong and inform the client. */
2422 if (!session
->active
) {
2423 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2428 if (ksession
&& ksession
->active
) {
2429 DBG("Stop kernel tracing");
2431 /* Flush metadata if exist */
2432 if (ksession
->metadata_stream_fd
>= 0) {
2433 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2435 ERR("Kernel metadata flush failed");
2439 /* Flush all buffers before stopping */
2440 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2441 ret
= kernel_flush_buffer(kchan
);
2443 ERR("Kernel flush buffer error");
2447 ret
= kernel_stop_session(ksession
);
2449 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2453 kernel_wait_quiescent(kernel_tracer_fd
);
2455 ksession
->active
= 0;
2458 if (usess
&& usess
->active
) {
2460 * Even though the stop trace might fail, flag this session inactive so
2461 * other application coming in are not started by default.
2465 ret
= ust_app_stop_trace_all(usess
);
2467 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2472 /* Flag inactive after a successful stop. */
2473 session
->active
= 0;
2481 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2483 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2484 struct lttng_uri
*uris
)
2487 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2488 struct ltt_ust_session
*usess
= session
->ust_session
;
2494 /* Can't set consumer URI if the session is active. */
2495 if (session
->active
) {
2496 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2500 /* Set the "global" consumer URIs */
2501 for (i
= 0; i
< nb_uri
; i
++) {
2502 ret
= add_uri_to_consumer(session
->consumer
,
2503 &uris
[i
], 0, session
->name
);
2504 if (ret
!= LTTNG_OK
) {
2509 /* Set UST session URIs */
2510 if (session
->ust_session
) {
2511 for (i
= 0; i
< nb_uri
; i
++) {
2512 ret
= add_uri_to_consumer(
2513 session
->ust_session
->consumer
,
2514 &uris
[i
], LTTNG_DOMAIN_UST
,
2516 if (ret
!= LTTNG_OK
) {
2522 /* Set kernel session URIs */
2523 if (session
->kernel_session
) {
2524 for (i
= 0; i
< nb_uri
; i
++) {
2525 ret
= add_uri_to_consumer(
2526 session
->kernel_session
->consumer
,
2527 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2529 if (ret
!= LTTNG_OK
) {
2536 * Make sure to set the session in output mode after we set URI since a
2537 * session can be created without URL (thus flagged in no output mode).
2539 session
->output_traces
= 1;
2541 ksess
->output_traces
= 1;
2545 usess
->output_traces
= 1;
2556 * Command LTTNG_CREATE_SESSION processed by the client thread.
2558 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2559 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2562 struct ltt_session
*session
;
2568 * Verify if the session already exist
2570 * XXX: There is no need for the session lock list here since the caller
2571 * (process_client_msg) is holding it. We might want to change that so a
2572 * single command does not lock the entire session list.
2574 session
= session_find_by_name(name
);
2575 if (session
!= NULL
) {
2576 ret
= LTTNG_ERR_EXIST_SESS
;
2580 /* Create tracing session in the registry */
2581 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2582 LTTNG_SOCK_GET_GID_CRED(creds
));
2583 if (ret
!= LTTNG_OK
) {
2588 * Get the newly created session pointer back
2590 * XXX: There is no need for the session lock list here since the caller
2591 * (process_client_msg) is holding it. We might want to change that so a
2592 * single command does not lock the entire session list.
2594 session
= session_find_by_name(name
);
2597 session
->live_timer
= live_timer
;
2598 /* Create default consumer output for the session not yet created. */
2599 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2600 if (session
->consumer
== NULL
) {
2601 ret
= LTTNG_ERR_FATAL
;
2602 goto consumer_error
;
2606 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2607 if (ret
!= LTTNG_OK
) {
2608 goto consumer_error
;
2610 session
->output_traces
= 1;
2612 session
->output_traces
= 0;
2613 DBG2("Session %s created with no output", session
->name
);
2616 session
->consumer
->enabled
= 1;
2621 session_destroy(session
);
2628 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2630 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2631 size_t nb_uri
, lttng_sock_cred
*creds
)
2634 struct ltt_session
*session
;
2635 struct snapshot_output
*new_output
= NULL
;
2641 * Create session in no output mode with URIs set to NULL. The uris we've
2642 * received are for a default snapshot output if one.
2644 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2645 if (ret
!= LTTNG_OK
) {
2649 /* Get the newly created session pointer back. This should NEVER fail. */
2650 session
= session_find_by_name(name
);
2653 /* Flag session for snapshot mode. */
2654 session
->snapshot_mode
= 1;
2656 /* Skip snapshot output creation if no URI is given. */
2661 new_output
= snapshot_output_alloc();
2663 ret
= LTTNG_ERR_NOMEM
;
2664 goto error_snapshot_alloc
;
2667 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2668 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2670 if (ret
== -ENOMEM
) {
2671 ret
= LTTNG_ERR_NOMEM
;
2673 ret
= LTTNG_ERR_INVALID
;
2675 goto error_snapshot
;
2679 snapshot_add_output(&session
->snapshot
, new_output
);
2686 snapshot_output_destroy(new_output
);
2687 error_snapshot_alloc
:
2688 session_destroy(session
);
2694 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2696 * Called with session lock held.
2698 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2701 struct ltt_ust_session
*usess
;
2702 struct ltt_kernel_session
*ksess
;
2707 usess
= session
->ust_session
;
2708 ksess
= session
->kernel_session
;
2710 /* Clean kernel session teardown */
2711 kernel_destroy_session(ksess
);
2713 /* UST session teardown */
2715 /* Close any relayd session */
2716 consumer_output_send_destroy_relayd(usess
->consumer
);
2718 /* Destroy every UST application related to this session. */
2719 ret
= ust_app_destroy_trace_all(usess
);
2721 ERR("Error in ust_app_destroy_trace_all");
2724 /* Clean up the rest. */
2725 trace_ust_destroy_session(usess
);
2729 * Must notify the kernel thread here to update it's poll set in order to
2730 * remove the channel(s)' fd just destroyed.
2732 ret
= notify_thread_pipe(wpipe
);
2734 PERROR("write kernel poll pipe");
2737 ret
= session_destroy(session
);
2743 * Command LTTNG_CALIBRATE processed by the client thread.
2745 int cmd_calibrate(enum lttng_domain_type domain
,
2746 struct lttng_calibrate
*calibrate
)
2751 case LTTNG_DOMAIN_KERNEL
:
2753 struct lttng_kernel_calibrate kcalibrate
;
2755 switch (calibrate
->type
) {
2756 case LTTNG_CALIBRATE_FUNCTION
:
2758 /* Default and only possible calibrate option. */
2759 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2763 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2765 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2770 case LTTNG_DOMAIN_UST
:
2772 struct lttng_ust_calibrate ucalibrate
;
2774 switch (calibrate
->type
) {
2775 case LTTNG_CALIBRATE_FUNCTION
:
2777 /* Default and only possible calibrate option. */
2778 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2782 ret
= ust_app_calibrate_glb(&ucalibrate
);
2784 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2790 ret
= LTTNG_ERR_UND
;
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 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3015 * Command LTTNG_LIST_EVENTS processed by the client thread.
3017 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3018 struct ltt_session
*session
, char *channel_name
,
3019 struct lttng_event
**events
, size_t *total_size
)
3022 ssize_t nb_event
= 0;
3025 case LTTNG_DOMAIN_KERNEL
:
3026 if (session
->kernel_session
!= NULL
) {
3027 nb_event
= list_lttng_kernel_events(channel_name
,
3028 session
->kernel_session
, events
,
3032 case LTTNG_DOMAIN_UST
:
3034 if (session
->ust_session
!= NULL
) {
3035 nb_event
= list_lttng_ust_global_events(channel_name
,
3036 &session
->ust_session
->domain_global
, events
,
3041 case LTTNG_DOMAIN_LOG4J
:
3042 case LTTNG_DOMAIN_JUL
:
3043 case LTTNG_DOMAIN_PYTHON
:
3044 if (session
->ust_session
) {
3045 struct lttng_ht_iter iter
;
3049 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3050 &iter
.iter
, agt
, node
.node
) {
3051 if (agt
->domain
== domain
) {
3052 nb_event
= list_lttng_agent_events(
3062 ret
= LTTNG_ERR_UND
;
3069 /* Return negative value to differentiate return code */
3074 * Using the session list, filled a lttng_session array to send back to the
3075 * client for session listing.
3077 * The session list lock MUST be acquired before calling this function. Use
3078 * session_lock_list() and session_unlock_list().
3080 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3085 struct ltt_session
*session
;
3086 struct ltt_session_list
*list
= session_get_list();
3088 DBG("Getting all available session for UID %d GID %d",
3091 * Iterate over session list and append data after the control struct in
3094 cds_list_for_each_entry(session
, &list
->head
, list
) {
3096 * Only list the sessions the user can control.
3098 if (!session_access_ok(session
, uid
, gid
)) {
3102 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3103 struct ltt_ust_session
*usess
= session
->ust_session
;
3105 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3106 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3107 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3108 ret
= build_network_session_path(sessions
[i
].path
,
3109 sizeof(sessions
[i
].path
), session
);
3111 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3112 session
->consumer
->dst
.trace_path
);
3115 PERROR("snprintf session path");
3119 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3120 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3121 sessions
[i
].enabled
= session
->active
;
3122 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3123 sessions
[i
].live_timer_interval
= session
->live_timer
;
3129 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3130 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3132 int cmd_data_pending(struct ltt_session
*session
)
3135 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3136 struct ltt_ust_session
*usess
= session
->ust_session
;
3140 /* Session MUST be stopped to ask for data availability. */
3141 if (session
->active
) {
3142 ret
= LTTNG_ERR_SESSION_STARTED
;
3146 * If stopped, just make sure we've started before else the above call
3147 * will always send that there is data pending.
3149 * The consumer assumes that when the data pending command is received,
3150 * the trace has been started before or else no output data is written
3151 * by the streams which is a condition for data pending. So, this is
3152 * *VERY* important that we don't ask the consumer before a start
3155 if (!session
->has_been_started
) {
3161 if (ksess
&& ksess
->consumer
) {
3162 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3164 /* Data is still being extracted for the kernel. */
3169 if (usess
&& usess
->consumer
) {
3170 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3172 /* Data is still being extracted for the kernel. */
3177 /* Data is ready to be read by a viewer */
3185 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3187 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3189 int cmd_snapshot_add_output(struct ltt_session
*session
,
3190 struct lttng_snapshot_output
*output
, uint32_t *id
)
3193 struct snapshot_output
*new_output
;
3198 DBG("Cmd snapshot add output for session %s", session
->name
);
3201 * Permission denied to create an output if the session is not
3202 * set in no output mode.
3204 if (session
->output_traces
) {
3205 ret
= LTTNG_ERR_EPERM
;
3209 /* Only one output is allowed until we have the "tee" feature. */
3210 if (session
->snapshot
.nb_output
== 1) {
3211 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3215 new_output
= snapshot_output_alloc();
3217 ret
= LTTNG_ERR_NOMEM
;
3221 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3222 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3223 &session
->snapshot
);
3225 if (ret
== -ENOMEM
) {
3226 ret
= LTTNG_ERR_NOMEM
;
3228 ret
= LTTNG_ERR_INVALID
;
3234 snapshot_add_output(&session
->snapshot
, new_output
);
3236 *id
= new_output
->id
;
3243 snapshot_output_destroy(new_output
);
3249 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3251 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3253 int cmd_snapshot_del_output(struct ltt_session
*session
,
3254 struct lttng_snapshot_output
*output
)
3257 struct snapshot_output
*sout
= NULL
;
3265 * Permission denied to create an output if the session is not
3266 * set in no output mode.
3268 if (session
->output_traces
) {
3269 ret
= LTTNG_ERR_EPERM
;
3274 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3276 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3277 } else if (*output
->name
!= '\0') {
3278 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3280 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3283 ret
= LTTNG_ERR_INVALID
;
3287 snapshot_delete_output(&session
->snapshot
, sout
);
3288 snapshot_output_destroy(sout
);
3297 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3299 * If no output is available, outputs is untouched and 0 is returned.
3301 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3303 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3304 struct lttng_snapshot_output
**outputs
)
3307 struct lttng_snapshot_output
*list
= NULL
;
3308 struct lttng_ht_iter iter
;
3309 struct snapshot_output
*output
;
3314 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3317 * Permission denied to create an output if the session is not
3318 * set in no output mode.
3320 if (session
->output_traces
) {
3321 ret
= -LTTNG_ERR_EPERM
;
3325 if (session
->snapshot
.nb_output
== 0) {
3330 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3332 ret
= -LTTNG_ERR_NOMEM
;
3336 /* Copy list from session to the new list object. */
3338 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3339 output
, node
.node
) {
3340 assert(output
->consumer
);
3341 list
[idx
].id
= output
->id
;
3342 list
[idx
].max_size
= output
->max_size
;
3343 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3344 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3345 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3346 sizeof(list
[idx
].ctrl_url
));
3349 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3350 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3352 ret
= -LTTNG_ERR_NOMEM
;
3357 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3358 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3360 ret
= -LTTNG_ERR_NOMEM
;
3369 ret
= session
->snapshot
.nb_output
;
3377 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3378 * snapshot output is *not* set with a remote destination.
3380 * Return 0 on success or a LTTNG_ERR code.
3382 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3383 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3386 struct lttng_ht_iter iter
;
3387 struct consumer_socket
*socket
;
3390 assert(snap_output
);
3393 DBG2("Set relayd object from snapshot output");
3395 /* Ignore if snapshot consumer output is not network. */
3396 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3401 * For each consumer socket, create and send the relayd object of the
3405 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3406 socket
, node
.node
) {
3407 ret
= send_consumer_relayd_sockets(0, session
->id
,
3408 snap_output
->consumer
, socket
,
3409 session
->name
, session
->hostname
,
3410 session
->live_timer
);
3411 if (ret
!= LTTNG_OK
) {
3423 * Record a kernel snapshot.
3425 * Return LTTNG_OK on success or a LTTNG_ERR code.
3427 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3428 struct snapshot_output
*output
, struct ltt_session
*session
,
3429 int wait
, uint64_t nb_packets_per_stream
)
3437 /* Get the datetime for the snapshot output directory. */
3438 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3439 sizeof(output
->datetime
));
3441 ret
= LTTNG_ERR_INVALID
;
3446 * Copy kernel session sockets so we can communicate with the right
3447 * consumer for the snapshot record command.
3449 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3451 ret
= LTTNG_ERR_NOMEM
;
3455 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3456 if (ret
!= LTTNG_OK
) {
3457 goto error_snapshot
;
3460 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3461 if (ret
!= LTTNG_OK
) {
3462 goto error_snapshot
;
3469 /* Clean up copied sockets so this output can use some other later on. */
3470 consumer_destroy_output_sockets(output
->consumer
);
3477 * Record a UST snapshot.
3479 * Return 0 on success or a LTTNG_ERR error code.
3481 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3482 struct snapshot_output
*output
, struct ltt_session
*session
,
3483 int wait
, uint64_t nb_packets_per_stream
)
3491 /* Get the datetime for the snapshot output directory. */
3492 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3493 sizeof(output
->datetime
));
3495 ret
= LTTNG_ERR_INVALID
;
3500 * Copy UST session sockets so we can communicate with the right
3501 * consumer for the snapshot record command.
3503 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3505 ret
= LTTNG_ERR_NOMEM
;
3509 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3510 if (ret
!= LTTNG_OK
) {
3511 goto error_snapshot
;
3514 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3518 ret
= LTTNG_ERR_INVALID
;
3521 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3524 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3527 goto error_snapshot
;
3533 /* Clean up copied sockets so this output can use some other later on. */
3534 consumer_destroy_output_sockets(output
->consumer
);
3540 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3541 uint64_t cur_nr_packets
)
3543 uint64_t tot_size
= 0;
3545 if (session
->kernel_session
) {
3546 struct ltt_kernel_channel
*chan
;
3547 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3549 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3550 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3552 * Don't take channel into account if we
3553 * already grab all its packets.
3557 tot_size
+= chan
->channel
->attr
.subbuf_size
3558 * chan
->stream_count
;
3562 if (session
->ust_session
) {
3563 struct ltt_ust_session
*usess
= session
->ust_session
;
3565 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3573 * Calculate the number of packets we can grab from each stream that
3574 * fits within the overall snapshot max size.
3576 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3577 * the number of packets per stream.
3579 * TODO: this approach is not perfect: we consider the worse case
3580 * (packet filling the sub-buffers) as an upper bound, but we could do
3581 * better if we do this calculation while we actually grab the packet
3582 * content: we would know how much padding we don't actually store into
3585 * This algorithm is currently bounded by the number of packets per
3588 * Since we call this algorithm before actually grabbing the data, it's
3589 * an approximation: for instance, applications could appear/disappear
3590 * in between this call and actually grabbing data.
3593 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3596 uint64_t cur_nb_packets
= 0;
3599 return 0; /* Infinite */
3602 size_left
= max_size
;
3604 uint64_t one_more_packet_tot_size
;
3606 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3608 if (!one_more_packet_tot_size
) {
3609 /* We are already grabbing all packets. */
3612 size_left
-= one_more_packet_tot_size
;
3613 if (size_left
< 0) {
3618 if (!cur_nb_packets
) {
3619 /* Not enough room to grab one packet of each stream, error. */
3622 return cur_nb_packets
;
3626 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3628 * The wait parameter is ignored so this call always wait for the snapshot to
3629 * complete before returning.
3631 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3633 int cmd_snapshot_record(struct ltt_session
*session
,
3634 struct lttng_snapshot_output
*output
, int wait
)
3637 unsigned int use_tmp_output
= 0;
3638 struct snapshot_output tmp_output
;
3639 unsigned int snapshot_success
= 0;
3644 DBG("Cmd snapshot record for session %s", session
->name
);
3647 * Permission denied to create an output if the session is not
3648 * set in no output mode.
3650 if (session
->output_traces
) {
3651 ret
= LTTNG_ERR_EPERM
;
3655 /* The session needs to be started at least once. */
3656 if (!session
->has_been_started
) {
3657 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3661 /* Use temporary output for the session. */
3662 if (*output
->ctrl_url
!= '\0') {
3663 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3664 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3667 if (ret
== -ENOMEM
) {
3668 ret
= LTTNG_ERR_NOMEM
;
3670 ret
= LTTNG_ERR_INVALID
;
3674 /* Use the global session count for the temporary snapshot. */
3675 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3679 if (session
->kernel_session
) {
3680 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3682 if (use_tmp_output
) {
3683 int64_t nb_packets_per_stream
;
3685 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3686 tmp_output
.max_size
);
3687 if (nb_packets_per_stream
< 0) {
3688 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3691 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3692 wait
, nb_packets_per_stream
);
3693 if (ret
!= LTTNG_OK
) {
3696 snapshot_success
= 1;
3698 struct snapshot_output
*sout
;
3699 struct lttng_ht_iter iter
;
3702 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3703 &iter
.iter
, sout
, node
.node
) {
3704 int64_t nb_packets_per_stream
;
3707 * Make a local copy of the output and assign the possible
3708 * temporary value given by the caller.
3710 memset(&tmp_output
, 0, sizeof(tmp_output
));
3711 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3713 if (output
->max_size
!= (uint64_t) -1ULL) {
3714 tmp_output
.max_size
= output
->max_size
;
3717 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3718 tmp_output
.max_size
);
3719 if (nb_packets_per_stream
< 0) {
3720 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3724 /* Use temporary name. */
3725 if (*output
->name
!= '\0') {
3726 strncpy(tmp_output
.name
, output
->name
,
3727 sizeof(tmp_output
.name
));
3730 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3732 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3733 session
, wait
, nb_packets_per_stream
);
3734 if (ret
!= LTTNG_OK
) {
3738 snapshot_success
= 1;
3744 if (session
->ust_session
) {
3745 struct ltt_ust_session
*usess
= session
->ust_session
;
3747 if (use_tmp_output
) {
3748 int64_t nb_packets_per_stream
;
3750 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3751 tmp_output
.max_size
);
3752 if (nb_packets_per_stream
< 0) {
3753 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3756 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3757 wait
, nb_packets_per_stream
);
3758 if (ret
!= LTTNG_OK
) {
3761 snapshot_success
= 1;
3763 struct snapshot_output
*sout
;
3764 struct lttng_ht_iter iter
;
3767 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3768 &iter
.iter
, sout
, node
.node
) {
3769 int64_t nb_packets_per_stream
;
3772 * Make a local copy of the output and assign the possible
3773 * temporary value given by the caller.
3775 memset(&tmp_output
, 0, sizeof(tmp_output
));
3776 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3778 if (output
->max_size
!= (uint64_t) -1ULL) {
3779 tmp_output
.max_size
= output
->max_size
;
3782 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3783 tmp_output
.max_size
);
3784 if (nb_packets_per_stream
< 0) {
3785 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3790 /* Use temporary name. */
3791 if (*output
->name
!= '\0') {
3792 strncpy(tmp_output
.name
, output
->name
,
3793 sizeof(tmp_output
.name
));
3796 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3798 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3799 wait
, nb_packets_per_stream
);
3800 if (ret
!= LTTNG_OK
) {
3804 snapshot_success
= 1;
3810 if (snapshot_success
) {
3811 session
->snapshot
.nb_snapshot
++;
3813 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3821 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3823 int cmd_set_session_shm_path(struct ltt_session
*session
,
3824 const char *shm_path
)
3830 * Can only set shm path before session is started.
3832 if (session
->has_been_started
) {
3833 return LTTNG_ERR_SESSION_STARTED
;
3836 strncpy(session
->shm_path
, shm_path
,
3837 sizeof(session
->shm_path
));
3838 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3844 * Init command subsystem.
3849 * Set network sequence index to 1 for streams to match a relayd
3850 * socket on the consumer side.
3852 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3853 relayd_net_seq_idx
= 1;
3854 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3856 DBG("Command subsystem initialized");