2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
30 #include <common/compat/string.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/dynamic-buffer.h>
33 #include <common/buffer-view.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/action/action.h>
37 #include <lttng/channel-internal.h>
38 #include <common/string-utils/string-utils.h>
43 #include "health-sessiond.h"
45 #include "kernel-consumer.h"
46 #include "lttng-sessiond.h"
50 #include "buffer-registry.h"
51 #include "notification-thread.h"
52 #include "notification-thread-commands.h"
57 * Used to keep a unique index for each relayd socket created where this value
58 * is associated with streams on the consumer so it can match the right relayd
59 * to send to. It must be accessed with the relayd_net_seq_idx_lock
62 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
63 static uint64_t relayd_net_seq_idx
;
65 static int validate_ust_event_name(const char *);
66 static int cmd_enable_event_internal(struct ltt_session
*session
,
67 struct lttng_domain
*domain
,
68 char *channel_name
, struct lttng_event
*event
,
69 char *filter_expression
,
70 struct lttng_filter_bytecode
*filter
,
71 struct lttng_event_exclusion
*exclusion
,
75 * Create a session path used by list_lttng_sessions for the case that the
76 * session consumer is on the network.
78 static int build_network_session_path(char *dst
, size_t size
,
79 struct ltt_session
*session
)
81 int ret
, kdata_port
, udata_port
;
82 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
83 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
88 memset(tmp_urls
, 0, sizeof(tmp_urls
));
89 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
91 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
93 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
94 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
95 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
98 if (session
->ust_session
&& session
->ust_session
->consumer
) {
99 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
100 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
103 if (uuri
== NULL
&& kuri
== NULL
) {
104 uri
= &session
->consumer
->dst
.net
.control
;
105 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
106 } else if (kuri
&& uuri
) {
107 ret
= uri_compare(kuri
, uuri
);
111 /* Build uuri URL string */
112 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
119 } else if (kuri
&& uuri
== NULL
) {
121 } else if (uuri
&& kuri
== NULL
) {
125 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
131 * Do we have a UST url set. If yes, this means we have both kernel and UST
134 if (*tmp_uurl
!= '\0') {
135 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
136 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
139 if (kuri
|| (!kuri
&& !uuri
)) {
142 /* No kernel URI, use the UST port. */
145 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
153 * Get run-time attributes if the session has been started (discarded events,
156 static int get_kernel_runtime_stats(struct ltt_session
*session
,
157 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
158 uint64_t *lost_packets
)
162 if (!session
->has_been_started
) {
164 *discarded_events
= 0;
169 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
170 session
->kernel_session
->consumer
,
176 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
177 session
->kernel_session
->consumer
,
188 * Get run-time attributes if the session has been started (discarded events,
191 static int get_ust_runtime_stats(struct ltt_session
*session
,
192 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
193 uint64_t *lost_packets
)
196 struct ltt_ust_session
*usess
;
198 if (!discarded_events
|| !lost_packets
) {
203 usess
= session
->ust_session
;
204 assert(discarded_events
);
205 assert(lost_packets
);
207 if (!usess
|| !session
->has_been_started
) {
208 *discarded_events
= 0;
214 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
215 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
216 &usess
->buffer_reg_uid_list
,
217 usess
->consumer
, uchan
->id
,
218 uchan
->attr
.overwrite
,
221 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
222 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
223 uchan
, usess
->consumer
,
224 uchan
->attr
.overwrite
,
230 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
231 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
233 ERR("Unsupported buffer type");
244 * Fill lttng_channel array of all channels.
246 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
247 struct ltt_session
*session
, struct lttng_channel
*channels
,
248 struct lttng_channel_extended
*chan_exts
)
251 struct ltt_kernel_channel
*kchan
;
253 DBG("Listing channels for session %s", session
->name
);
256 case LTTNG_DOMAIN_KERNEL
:
257 /* Kernel channels */
258 if (session
->kernel_session
!= NULL
) {
259 cds_list_for_each_entry(kchan
,
260 &session
->kernel_session
->channel_list
.head
, list
) {
261 uint64_t discarded_events
, lost_packets
;
262 struct lttng_channel_extended
*extended
;
264 extended
= (struct lttng_channel_extended
*)
265 kchan
->channel
->attr
.extended
.ptr
;
267 ret
= get_kernel_runtime_stats(session
, kchan
,
268 &discarded_events
, &lost_packets
);
272 /* Copy lttng_channel struct to array */
273 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
274 channels
[i
].enabled
= kchan
->enabled
;
275 chan_exts
[i
].discarded_events
=
277 chan_exts
[i
].lost_packets
= lost_packets
;
278 chan_exts
[i
].monitor_timer_interval
=
279 extended
->monitor_timer_interval
;
284 case LTTNG_DOMAIN_UST
:
286 struct lttng_ht_iter iter
;
287 struct ltt_ust_channel
*uchan
;
290 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
291 &iter
.iter
, uchan
, node
.node
) {
292 uint64_t discarded_events
= 0, lost_packets
= 0;
294 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
295 LTTNG_SYMBOL_NAME_LEN
)) {
298 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
299 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
300 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
301 channels
[i
].attr
.switch_timer_interval
=
302 uchan
->attr
.switch_timer_interval
;
303 channels
[i
].attr
.read_timer_interval
=
304 uchan
->attr
.read_timer_interval
;
305 channels
[i
].enabled
= uchan
->enabled
;
306 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
307 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
310 * Map enum lttng_ust_output to enum lttng_event_output.
312 switch (uchan
->attr
.output
) {
314 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
318 * LTTNG_UST_MMAP is the only supported UST
325 chan_exts
[i
].monitor_timer_interval
=
326 uchan
->monitor_timer_interval
;
328 ret
= get_ust_runtime_stats(session
, uchan
,
329 &discarded_events
, &lost_packets
);
333 chan_exts
[i
].discarded_events
= discarded_events
;
334 chan_exts
[i
].lost_packets
= lost_packets
;
346 return -LTTNG_ERR_FATAL
;
352 static void increment_extended_len(const char *filter_expression
,
353 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
355 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
357 if (filter_expression
) {
358 *extended_len
+= strlen(filter_expression
) + 1;
362 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
366 static void append_extended_info(const char *filter_expression
,
367 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
369 struct lttcomm_event_extended_header extended_header
;
370 size_t filter_len
= 0;
371 size_t nb_exclusions
= 0;
373 if (filter_expression
) {
374 filter_len
= strlen(filter_expression
) + 1;
378 nb_exclusions
= exclusion
->count
;
381 /* Set header fields */
382 extended_header
.filter_len
= filter_len
;
383 extended_header
.nb_exclusions
= nb_exclusions
;
386 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
387 *extended_at
+= sizeof(extended_header
);
389 /* Copy filter string */
390 if (filter_expression
) {
391 memcpy(*extended_at
, filter_expression
, filter_len
);
392 *extended_at
+= filter_len
;
395 /* Copy exclusion names */
397 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
399 memcpy(*extended_at
, &exclusion
->names
, len
);
405 * Create a list of agent domain events.
407 * Return number of events in list on success or else a negative value.
409 static int list_lttng_agent_events(struct agent
*agt
,
410 struct lttng_event
**events
, size_t *total_size
)
413 unsigned int nb_event
= 0;
414 struct agent_event
*event
;
415 struct lttng_event
*tmp_events
;
416 struct lttng_ht_iter iter
;
417 size_t extended_len
= 0;
423 DBG3("Listing agent events");
426 nb_event
= lttng_ht_get_count(agt
->events
);
434 /* Compute required extended infos size */
435 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
438 * This is only valid because the commands which add events are
439 * processed in the same thread as the listing.
442 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
443 increment_extended_len(event
->filter_expression
, NULL
,
448 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
449 tmp_events
= zmalloc(*total_size
);
451 PERROR("zmalloc agent events session");
452 ret
= -LTTNG_ERR_FATAL
;
456 extended_at
= ((uint8_t *) tmp_events
) +
457 nb_event
* sizeof(struct lttng_event
);
460 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
461 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
462 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
463 tmp_events
[i
].enabled
= event
->enabled
;
464 tmp_events
[i
].loglevel
= event
->loglevel_value
;
465 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
468 /* Append extended info */
469 append_extended_info(event
->filter_expression
, NULL
,
474 *events
= tmp_events
;
478 assert(nb_event
== i
);
483 * Create a list of ust global domain events.
485 static int list_lttng_ust_global_events(char *channel_name
,
486 struct ltt_ust_domain_global
*ust_global
,
487 struct lttng_event
**events
, size_t *total_size
)
490 unsigned int nb_event
= 0;
491 struct lttng_ht_iter iter
;
492 struct lttng_ht_node_str
*node
;
493 struct ltt_ust_channel
*uchan
;
494 struct ltt_ust_event
*uevent
;
495 struct lttng_event
*tmp
;
496 size_t extended_len
= 0;
499 DBG("Listing UST global events for channel %s", channel_name
);
503 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
504 node
= lttng_ht_iter_get_node_str(&iter
);
506 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
510 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
512 nb_event
= lttng_ht_get_count(uchan
->events
);
519 DBG3("Listing UST global %d events", nb_event
);
521 /* Compute required extended infos size */
522 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
523 if (uevent
->internal
) {
528 increment_extended_len(uevent
->filter_expression
,
529 uevent
->exclusion
, &extended_len
);
532 /* All events are internal, skip. */
538 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
539 tmp
= zmalloc(*total_size
);
541 ret
= -LTTNG_ERR_FATAL
;
545 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
547 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
548 if (uevent
->internal
) {
549 /* This event should remain hidden from clients */
552 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
553 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
554 tmp
[i
].enabled
= uevent
->enabled
;
556 switch (uevent
->attr
.instrumentation
) {
557 case LTTNG_UST_TRACEPOINT
:
558 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
560 case LTTNG_UST_PROBE
:
561 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
563 case LTTNG_UST_FUNCTION
:
564 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
568 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
569 switch (uevent
->attr
.loglevel_type
) {
570 case LTTNG_UST_LOGLEVEL_ALL
:
571 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
573 case LTTNG_UST_LOGLEVEL_RANGE
:
574 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
576 case LTTNG_UST_LOGLEVEL_SINGLE
:
577 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
580 if (uevent
->filter
) {
583 if (uevent
->exclusion
) {
584 tmp
[i
].exclusion
= 1;
588 /* Append extended info */
589 append_extended_info(uevent
->filter_expression
,
590 uevent
->exclusion
, &extended_at
);
601 * Fill lttng_event array of all kernel events in the channel.
603 static int list_lttng_kernel_events(char *channel_name
,
604 struct ltt_kernel_session
*kernel_session
,
605 struct lttng_event
**events
, size_t *total_size
)
608 unsigned int nb_event
;
609 struct ltt_kernel_event
*event
;
610 struct ltt_kernel_channel
*kchan
;
611 size_t extended_len
= 0;
614 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
616 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
620 nb_event
= kchan
->event_count
;
622 DBG("Listing events for channel %s", kchan
->channel
->name
);
630 /* Compute required extended infos size */
631 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
632 increment_extended_len(event
->filter_expression
, NULL
,
636 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
637 *events
= zmalloc(*total_size
);
638 if (*events
== NULL
) {
639 ret
= LTTNG_ERR_FATAL
;
643 extended_at
= ((void *) *events
) +
644 nb_event
* sizeof(struct lttng_event
);
646 /* Kernel channels */
647 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
648 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
649 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
650 (*events
)[i
].enabled
= event
->enabled
;
651 (*events
)[i
].filter
=
652 (unsigned char) !!event
->filter_expression
;
654 switch (event
->event
->instrumentation
) {
655 case LTTNG_KERNEL_TRACEPOINT
:
656 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
658 case LTTNG_KERNEL_KRETPROBE
:
659 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
660 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
661 sizeof(struct lttng_kernel_kprobe
));
663 case LTTNG_KERNEL_KPROBE
:
664 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
665 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
666 sizeof(struct lttng_kernel_kprobe
));
668 case LTTNG_KERNEL_FUNCTION
:
669 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
670 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
671 sizeof(struct lttng_kernel_function
));
673 case LTTNG_KERNEL_NOOP
:
674 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
676 case LTTNG_KERNEL_SYSCALL
:
677 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
679 case LTTNG_KERNEL_ALL
:
685 /* Append extended info */
686 append_extended_info(event
->filter_expression
, NULL
,
694 /* Negate the error code to differentiate the size from an error */
699 * Add URI so the consumer output object. Set the correct path depending on the
700 * domain adding the default trace directory.
702 static int add_uri_to_consumer(struct consumer_output
*consumer
,
703 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
704 const char *session_name
)
707 const char *default_trace_dir
;
711 if (consumer
== NULL
) {
712 DBG("No consumer detected. Don't add URI. Stopping.");
713 ret
= LTTNG_ERR_NO_CONSUMER
;
718 case LTTNG_DOMAIN_KERNEL
:
719 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
721 case LTTNG_DOMAIN_UST
:
722 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
726 * This case is possible is we try to add the URI to the global tracing
727 * session consumer object which in this case there is no subdir.
729 default_trace_dir
= "";
732 switch (uri
->dtype
) {
735 DBG2("Setting network URI to consumer");
737 if (consumer
->type
== CONSUMER_DST_NET
) {
738 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
739 consumer
->dst
.net
.control_isset
) ||
740 (uri
->stype
== LTTNG_STREAM_DATA
&&
741 consumer
->dst
.net
.data_isset
)) {
742 ret
= LTTNG_ERR_URL_EXIST
;
746 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
749 consumer
->type
= CONSUMER_DST_NET
;
751 /* Set URI into consumer output object */
752 ret
= consumer_set_network_uri(consumer
, uri
);
756 } else if (ret
== 1) {
758 * URI was the same in the consumer so we do not append the subdir
759 * again so to not duplicate output dir.
765 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
766 ret
= consumer_set_subdir(consumer
, session_name
);
768 ret
= LTTNG_ERR_FATAL
;
773 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
774 /* On a new subdir, reappend the default trace dir. */
775 strncat(consumer
->subdir
, default_trace_dir
,
776 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
777 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
782 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
783 memset(consumer
->dst
.trace_path
, 0,
784 sizeof(consumer
->dst
.trace_path
));
785 /* Explicit length checks for strcpy and strcat. */
786 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
787 >= sizeof(consumer
->dst
.trace_path
)) {
788 ret
= LTTNG_ERR_FATAL
;
791 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
792 /* Append default trace dir */
793 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
794 /* Flag consumer as local. */
795 consumer
->type
= CONSUMER_DST_LOCAL
;
806 * Init tracing by creating trace directory and sending fds kernel consumer.
808 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
811 struct lttng_ht_iter iter
;
812 struct consumer_socket
*socket
;
818 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
819 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
821 pthread_mutex_lock(socket
->lock
);
822 ret
= kernel_consumer_send_session(socket
, session
);
823 pthread_mutex_unlock(socket
->lock
);
825 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
837 * Create a socket to the relayd using the URI.
839 * On success, the relayd_sock pointer is set to the created socket.
840 * Else, it's stays untouched and a lttcomm error code is returned.
842 static int create_connect_relayd(struct lttng_uri
*uri
,
843 struct lttcomm_relayd_sock
**relayd_sock
,
844 struct consumer_output
*consumer
)
847 struct lttcomm_relayd_sock
*rsock
;
849 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
850 RELAYD_VERSION_COMM_MINOR
);
852 ret
= LTTNG_ERR_FATAL
;
857 * Connect to relayd so we can proceed with a session creation. This call
858 * can possibly block for an arbitrary amount of time to set the health
859 * state to be in poll execution.
862 ret
= relayd_connect(rsock
);
865 ERR("Unable to reach lttng-relayd");
866 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
870 /* Create socket for control stream. */
871 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
872 DBG3("Creating relayd stream socket from URI");
874 /* Check relayd version */
875 ret
= relayd_version_check(rsock
);
877 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
880 consumer
->relay_major_version
= rsock
->major
;
881 consumer
->relay_minor_version
= rsock
->minor
;
882 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
883 DBG3("Creating relayd data socket from URI");
885 /* Command is not valid */
886 ERR("Relayd invalid stream type: %d", uri
->stype
);
887 ret
= LTTNG_ERR_INVALID
;
891 *relayd_sock
= rsock
;
896 /* The returned value is not useful since we are on an error path. */
897 (void) relayd_close(rsock
);
905 * Connect to the relayd using URI and send the socket to the right consumer.
907 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
908 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
909 struct consumer_output
*consumer
,
910 struct consumer_socket
*consumer_sock
,
911 char *session_name
, char *hostname
, int session_live_timer
)
914 struct lttcomm_relayd_sock
*rsock
= NULL
;
916 /* Connect to relayd and make version check if uri is the control. */
917 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
918 if (ret
!= LTTNG_OK
) {
923 /* Set the network sequence index if not set. */
924 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
925 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
927 * Increment net_seq_idx because we are about to transfer the
928 * new relayd socket to the consumer.
929 * Assign unique key so the consumer can match streams.
931 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
932 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
935 /* Send relayd socket to consumer. */
936 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
937 relayd_uri
->stype
, session_id
,
938 session_name
, hostname
, session_live_timer
);
940 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
944 /* Flag that the corresponding socket was sent. */
945 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
946 consumer_sock
->control_sock_sent
= 1;
947 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
948 consumer_sock
->data_sock_sent
= 1;
954 * Close socket which was dup on the consumer side. The session daemon does
955 * NOT keep track of the relayd socket(s) once transfer to the consumer.
959 (void) relayd_close(rsock
);
963 if (ret
!= LTTNG_OK
) {
965 * The consumer output for this session should not be used anymore
966 * since the relayd connection failed thus making any tracing or/and
967 * streaming not usable.
969 consumer
->enabled
= 0;
975 * Send both relayd sockets to a specific consumer and domain. This is a
976 * helper function to facilitate sending the information to the consumer for a
979 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
980 unsigned int session_id
, struct consumer_output
*consumer
,
981 struct consumer_socket
*sock
, char *session_name
,
982 char *hostname
, int session_live_timer
)
989 /* Sending control relayd socket. */
990 if (!sock
->control_sock_sent
) {
991 ret
= send_consumer_relayd_socket(domain
, session_id
,
992 &consumer
->dst
.net
.control
, consumer
, sock
,
993 session_name
, hostname
, session_live_timer
);
994 if (ret
!= LTTNG_OK
) {
999 /* Sending data relayd socket. */
1000 if (!sock
->data_sock_sent
) {
1001 ret
= send_consumer_relayd_socket(domain
, session_id
,
1002 &consumer
->dst
.net
.data
, consumer
, sock
,
1003 session_name
, hostname
, session_live_timer
);
1004 if (ret
!= LTTNG_OK
) {
1014 * Setup relayd connections for a tracing session. First creates the socket to
1015 * the relayd and send them to the right domain consumer. Consumer type MUST be
1018 int cmd_setup_relayd(struct ltt_session
*session
)
1021 struct ltt_ust_session
*usess
;
1022 struct ltt_kernel_session
*ksess
;
1023 struct consumer_socket
*socket
;
1024 struct lttng_ht_iter iter
;
1028 usess
= session
->ust_session
;
1029 ksess
= session
->kernel_session
;
1031 DBG("Setting relayd for session %s", session
->name
);
1035 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1036 && usess
->consumer
->enabled
) {
1037 /* For each consumer socket, send relayd sockets */
1038 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1039 socket
, node
.node
) {
1040 pthread_mutex_lock(socket
->lock
);
1041 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1042 usess
->consumer
, socket
,
1043 session
->name
, session
->hostname
,
1044 session
->live_timer
);
1045 pthread_mutex_unlock(socket
->lock
);
1046 if (ret
!= LTTNG_OK
) {
1049 /* Session is now ready for network streaming. */
1050 session
->net_handle
= 1;
1052 session
->consumer
->relay_major_version
=
1053 usess
->consumer
->relay_major_version
;
1054 session
->consumer
->relay_minor_version
=
1055 usess
->consumer
->relay_minor_version
;
1058 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1059 && ksess
->consumer
->enabled
) {
1060 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1061 socket
, node
.node
) {
1062 pthread_mutex_lock(socket
->lock
);
1063 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1064 ksess
->consumer
, socket
,
1065 session
->name
, session
->hostname
,
1066 session
->live_timer
);
1067 pthread_mutex_unlock(socket
->lock
);
1068 if (ret
!= LTTNG_OK
) {
1071 /* Session is now ready for network streaming. */
1072 session
->net_handle
= 1;
1074 session
->consumer
->relay_major_version
=
1075 ksess
->consumer
->relay_major_version
;
1076 session
->consumer
->relay_minor_version
=
1077 ksess
->consumer
->relay_minor_version
;
1086 * Start a kernel session by opening all necessary streams.
1088 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1091 struct ltt_kernel_channel
*kchan
;
1093 /* Open kernel metadata */
1094 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1095 ret
= kernel_open_metadata(ksess
);
1097 ret
= LTTNG_ERR_KERN_META_FAIL
;
1102 /* Open kernel metadata stream */
1103 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1104 ret
= kernel_open_metadata_stream(ksess
);
1106 ERR("Kernel create metadata stream failed");
1107 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1112 /* For each channel */
1113 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1114 if (kchan
->stream_count
== 0) {
1115 ret
= kernel_open_channel_stream(kchan
);
1117 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1120 /* Update the stream global counter */
1121 ksess
->stream_count_global
+= ret
;
1125 /* Setup kernel consumer socket and send fds to it */
1126 ret
= init_kernel_tracing(ksess
);
1128 ret
= LTTNG_ERR_KERN_START_FAIL
;
1132 /* This start the kernel tracing */
1133 ret
= kernel_start_session(ksess
);
1135 ret
= LTTNG_ERR_KERN_START_FAIL
;
1139 /* Quiescent wait after starting trace */
1140 kernel_wait_quiescent(kernel_tracer_fd
);
1151 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1153 int cmd_disable_channel(struct ltt_session
*session
,
1154 enum lttng_domain_type domain
, char *channel_name
)
1157 struct ltt_ust_session
*usess
;
1159 usess
= session
->ust_session
;
1164 case LTTNG_DOMAIN_KERNEL
:
1166 ret
= channel_kernel_disable(session
->kernel_session
,
1168 if (ret
!= LTTNG_OK
) {
1172 kernel_wait_quiescent(kernel_tracer_fd
);
1175 case LTTNG_DOMAIN_UST
:
1177 struct ltt_ust_channel
*uchan
;
1178 struct lttng_ht
*chan_ht
;
1180 chan_ht
= usess
->domain_global
.channels
;
1182 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1183 if (uchan
== NULL
) {
1184 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1188 ret
= channel_ust_disable(usess
, uchan
);
1189 if (ret
!= LTTNG_OK
) {
1195 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1207 * Command LTTNG_TRACK_PID processed by the client thread.
1209 * Called with session lock held.
1211 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1219 case LTTNG_DOMAIN_KERNEL
:
1221 struct ltt_kernel_session
*ksess
;
1223 ksess
= session
->kernel_session
;
1225 ret
= kernel_track_pid(ksess
, pid
);
1226 if (ret
!= LTTNG_OK
) {
1230 kernel_wait_quiescent(kernel_tracer_fd
);
1233 case LTTNG_DOMAIN_UST
:
1235 struct ltt_ust_session
*usess
;
1237 usess
= session
->ust_session
;
1239 ret
= trace_ust_track_pid(usess
, pid
);
1240 if (ret
!= LTTNG_OK
) {
1246 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1258 * Command LTTNG_UNTRACK_PID processed by the client thread.
1260 * Called with session lock held.
1262 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1270 case LTTNG_DOMAIN_KERNEL
:
1272 struct ltt_kernel_session
*ksess
;
1274 ksess
= session
->kernel_session
;
1276 ret
= kernel_untrack_pid(ksess
, pid
);
1277 if (ret
!= LTTNG_OK
) {
1281 kernel_wait_quiescent(kernel_tracer_fd
);
1284 case LTTNG_DOMAIN_UST
:
1286 struct ltt_ust_session
*usess
;
1288 usess
= session
->ust_session
;
1290 ret
= trace_ust_untrack_pid(usess
, pid
);
1291 if (ret
!= LTTNG_OK
) {
1297 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1309 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1311 * The wpipe arguments is used as a notifier for the kernel thread.
1313 int cmd_enable_channel(struct ltt_session
*session
,
1314 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1317 struct ltt_ust_session
*usess
= session
->ust_session
;
1318 struct lttng_ht
*chan_ht
;
1325 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1327 /* Validate channel name */
1328 if (attr
->name
[0] == '.' ||
1329 memchr(attr
->name
, '/', len
) != NULL
) {
1330 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1334 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1339 * Don't try to enable a channel if the session has been started at
1340 * some point in time before. The tracer does not allow it.
1342 if (session
->has_been_started
) {
1343 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1348 * If the session is a live session, remove the switch timer, the
1349 * live timer does the same thing but sends also synchronisation
1350 * beacons for inactive streams.
1352 if (session
->live_timer
> 0) {
1353 attr
->attr
.live_timer_interval
= session
->live_timer
;
1354 attr
->attr
.switch_timer_interval
= 0;
1357 switch (domain
->type
) {
1358 case LTTNG_DOMAIN_KERNEL
:
1360 struct ltt_kernel_channel
*kchan
;
1362 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1363 session
->kernel_session
);
1364 if (kchan
== NULL
) {
1365 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1366 if (attr
->name
[0] != '\0') {
1367 session
->kernel_session
->has_non_default_channel
= 1;
1370 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1373 if (ret
!= LTTNG_OK
) {
1377 kernel_wait_quiescent(kernel_tracer_fd
);
1380 case LTTNG_DOMAIN_UST
:
1381 case LTTNG_DOMAIN_JUL
:
1382 case LTTNG_DOMAIN_LOG4J
:
1383 case LTTNG_DOMAIN_PYTHON
:
1385 struct ltt_ust_channel
*uchan
;
1390 * Current agent implementation limitations force us to allow
1391 * only one channel at once in "agent" subdomains. Each
1392 * subdomain has a default channel name which must be strictly
1395 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1396 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1397 LTTNG_SYMBOL_NAME_LEN
)) {
1398 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1401 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1402 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1403 LTTNG_SYMBOL_NAME_LEN
)) {
1404 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1407 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1408 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1409 LTTNG_SYMBOL_NAME_LEN
)) {
1410 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1415 chan_ht
= usess
->domain_global
.channels
;
1417 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1418 if (uchan
== NULL
) {
1419 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1420 if (attr
->name
[0] != '\0') {
1421 usess
->has_non_default_channel
= 1;
1424 ret
= channel_ust_enable(usess
, uchan
);
1429 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1440 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1442 int cmd_disable_event(struct ltt_session
*session
,
1443 enum lttng_domain_type domain
, char *channel_name
,
1444 struct lttng_event
*event
)
1449 DBG("Disable event command for event \'%s\'", event
->name
);
1451 event_name
= event
->name
;
1453 /* Error out on unhandled search criteria */
1454 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1455 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1456 ret
= LTTNG_ERR_UNK
;
1463 case LTTNG_DOMAIN_KERNEL
:
1465 struct ltt_kernel_channel
*kchan
;
1466 struct ltt_kernel_session
*ksess
;
1468 ksess
= session
->kernel_session
;
1471 * If a non-default channel has been created in the
1472 * session, explicitely require that -c chan_name needs
1475 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1476 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1480 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1481 if (kchan
== NULL
) {
1482 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1486 switch (event
->type
) {
1487 case LTTNG_EVENT_ALL
:
1488 case LTTNG_EVENT_TRACEPOINT
:
1489 case LTTNG_EVENT_SYSCALL
:
1490 case LTTNG_EVENT_PROBE
:
1491 case LTTNG_EVENT_FUNCTION
:
1492 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1493 if (event_name
[0] == '\0') {
1494 ret
= event_kernel_disable_event(kchan
,
1497 ret
= event_kernel_disable_event(kchan
,
1498 event_name
, event
->type
);
1500 if (ret
!= LTTNG_OK
) {
1505 ret
= LTTNG_ERR_UNK
;
1509 kernel_wait_quiescent(kernel_tracer_fd
);
1512 case LTTNG_DOMAIN_UST
:
1514 struct ltt_ust_channel
*uchan
;
1515 struct ltt_ust_session
*usess
;
1517 usess
= session
->ust_session
;
1519 if (validate_ust_event_name(event_name
)) {
1520 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1525 * If a non-default channel has been created in the
1526 * session, explicitly require that -c chan_name needs
1529 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1530 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1534 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1536 if (uchan
== NULL
) {
1537 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1541 switch (event
->type
) {
1542 case LTTNG_EVENT_ALL
:
1544 * An empty event name means that everything
1545 * should be disabled.
1547 if (event
->name
[0] == '\0') {
1548 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1550 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1553 if (ret
!= LTTNG_OK
) {
1558 ret
= LTTNG_ERR_UNK
;
1562 DBG3("Disable UST event %s in channel %s completed", event_name
,
1566 case LTTNG_DOMAIN_LOG4J
:
1567 case LTTNG_DOMAIN_JUL
:
1568 case LTTNG_DOMAIN_PYTHON
:
1571 struct ltt_ust_session
*usess
= session
->ust_session
;
1575 switch (event
->type
) {
1576 case LTTNG_EVENT_ALL
:
1579 ret
= LTTNG_ERR_UNK
;
1583 agt
= trace_ust_find_agent(usess
, domain
);
1585 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1589 * An empty event name means that everything
1590 * should be disabled.
1592 if (event
->name
[0] == '\0') {
1593 ret
= event_agent_disable_all(usess
, agt
);
1595 ret
= event_agent_disable(usess
, agt
, event_name
);
1597 if (ret
!= LTTNG_OK
) {
1604 ret
= LTTNG_ERR_UND
;
1617 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1619 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1620 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1622 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1623 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1625 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1626 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1627 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1631 case LTTNG_DOMAIN_KERNEL
:
1632 assert(session
->kernel_session
);
1634 if (session
->kernel_session
->channel_count
== 0) {
1635 /* Create default channel */
1636 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1637 if (ret
!= LTTNG_OK
) {
1640 chan_kern_created
= 1;
1642 /* Add kernel context to kernel tracer */
1643 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1644 if (ret
!= LTTNG_OK
) {
1648 case LTTNG_DOMAIN_JUL
:
1649 case LTTNG_DOMAIN_LOG4J
:
1652 * Validate channel name.
1653 * If no channel name is given and the domain is JUL or LOG4J,
1654 * set it to the appropriate domain-specific channel name. If
1655 * a name is provided but does not match the expexted channel
1656 * name, return an error.
1658 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1659 strcmp(channel_name
,
1660 DEFAULT_JUL_CHANNEL_NAME
)) {
1661 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1663 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1664 strcmp(channel_name
,
1665 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1666 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1669 /* break is _not_ missing here. */
1671 case LTTNG_DOMAIN_UST
:
1673 struct ltt_ust_session
*usess
= session
->ust_session
;
1674 unsigned int chan_count
;
1678 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1679 if (chan_count
== 0) {
1680 struct lttng_channel
*attr
;
1681 /* Create default channel */
1682 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1684 ret
= LTTNG_ERR_FATAL
;
1688 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1689 if (ret
!= LTTNG_OK
) {
1693 channel_attr_destroy(attr
);
1694 chan_ust_created
= 1;
1697 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1698 free(app_ctx_provider_name
);
1700 app_ctx_name
= NULL
;
1701 app_ctx_provider_name
= NULL
;
1702 if (ret
!= LTTNG_OK
) {
1708 ret
= LTTNG_ERR_UND
;
1716 if (chan_kern_created
) {
1717 struct ltt_kernel_channel
*kchan
=
1718 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1719 session
->kernel_session
);
1720 /* Created previously, this should NOT fail. */
1722 kernel_destroy_channel(kchan
);
1725 if (chan_ust_created
) {
1726 struct ltt_ust_channel
*uchan
=
1727 trace_ust_find_channel_by_name(
1728 session
->ust_session
->domain_global
.channels
,
1729 DEFAULT_CHANNEL_NAME
);
1730 /* Created previously, this should NOT fail. */
1732 /* Remove from the channel list of the session. */
1733 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1735 trace_ust_destroy_channel(uchan
);
1738 free(app_ctx_provider_name
);
1743 static inline bool name_starts_with(const char *name
, const char *prefix
)
1745 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1747 return !strncmp(name
, prefix
, max_cmp_len
);
1750 /* Perform userspace-specific event name validation */
1751 static int validate_ust_event_name(const char *name
)
1761 * Check name against all internal UST event component namespaces used
1764 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1765 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1766 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1775 * Internal version of cmd_enable_event() with a supplemental
1776 * "internal_event" flag which is used to enable internal events which should
1777 * be hidden from clients. Such events are used in the agent implementation to
1778 * enable the events through which all "agent" events are funeled.
1780 static int _cmd_enable_event(struct ltt_session
*session
,
1781 struct lttng_domain
*domain
,
1782 char *channel_name
, struct lttng_event
*event
,
1783 char *filter_expression
,
1784 struct lttng_filter_bytecode
*filter
,
1785 struct lttng_event_exclusion
*exclusion
,
1786 int wpipe
, bool internal_event
)
1788 int ret
= 0, channel_created
= 0;
1789 struct lttng_channel
*attr
= NULL
;
1793 assert(channel_name
);
1795 /* If we have a filter, we must have its filter expression */
1796 assert(!(!!filter_expression
^ !!filter
));
1798 /* Normalize event name as a globbing pattern */
1799 strutils_normalize_star_glob_pattern(event
->name
);
1801 /* Normalize exclusion names as globbing patterns */
1805 for (i
= 0; i
< exclusion
->count
; i
++) {
1806 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1808 strutils_normalize_star_glob_pattern(name
);
1812 DBG("Enable event command for event \'%s\'", event
->name
);
1816 switch (domain
->type
) {
1817 case LTTNG_DOMAIN_KERNEL
:
1819 struct ltt_kernel_channel
*kchan
;
1822 * If a non-default channel has been created in the
1823 * session, explicitely require that -c chan_name needs
1826 if (session
->kernel_session
->has_non_default_channel
1827 && channel_name
[0] == '\0') {
1828 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1832 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1833 session
->kernel_session
);
1834 if (kchan
== NULL
) {
1835 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1836 LTTNG_BUFFER_GLOBAL
);
1838 ret
= LTTNG_ERR_FATAL
;
1841 if (lttng_strncpy(attr
->name
, channel_name
,
1842 sizeof(attr
->name
))) {
1843 ret
= LTTNG_ERR_INVALID
;
1847 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1848 if (ret
!= LTTNG_OK
) {
1851 channel_created
= 1;
1854 /* Get the newly created kernel channel pointer */
1855 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1856 session
->kernel_session
);
1857 if (kchan
== NULL
) {
1858 /* This sould not happen... */
1859 ret
= LTTNG_ERR_FATAL
;
1863 switch (event
->type
) {
1864 case LTTNG_EVENT_ALL
:
1866 char *filter_expression_a
= NULL
;
1867 struct lttng_filter_bytecode
*filter_a
= NULL
;
1870 * We need to duplicate filter_expression and filter,
1871 * because ownership is passed to first enable
1874 if (filter_expression
) {
1875 filter_expression_a
= strdup(filter_expression
);
1876 if (!filter_expression_a
) {
1877 ret
= LTTNG_ERR_FATAL
;
1882 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1884 free(filter_expression_a
);
1885 ret
= LTTNG_ERR_FATAL
;
1888 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1890 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1891 ret
= event_kernel_enable_event(kchan
, event
,
1892 filter_expression
, filter
);
1893 /* We have passed ownership */
1894 filter_expression
= NULL
;
1896 if (ret
!= LTTNG_OK
) {
1897 if (channel_created
) {
1898 /* Let's not leak a useless channel. */
1899 kernel_destroy_channel(kchan
);
1901 free(filter_expression_a
);
1905 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1906 ret
= event_kernel_enable_event(kchan
, event
,
1907 filter_expression_a
, filter_a
);
1908 /* We have passed ownership */
1909 filter_expression_a
= NULL
;
1911 if (ret
!= LTTNG_OK
) {
1916 case LTTNG_EVENT_PROBE
:
1917 case LTTNG_EVENT_FUNCTION
:
1918 case LTTNG_EVENT_FUNCTION_ENTRY
:
1919 case LTTNG_EVENT_TRACEPOINT
:
1920 ret
= event_kernel_enable_event(kchan
, event
,
1921 filter_expression
, filter
);
1922 /* We have passed ownership */
1923 filter_expression
= NULL
;
1925 if (ret
!= LTTNG_OK
) {
1926 if (channel_created
) {
1927 /* Let's not leak a useless channel. */
1928 kernel_destroy_channel(kchan
);
1933 case LTTNG_EVENT_SYSCALL
:
1934 ret
= event_kernel_enable_event(kchan
, event
,
1935 filter_expression
, filter
);
1936 /* We have passed ownership */
1937 filter_expression
= NULL
;
1939 if (ret
!= LTTNG_OK
) {
1944 ret
= LTTNG_ERR_UNK
;
1948 kernel_wait_quiescent(kernel_tracer_fd
);
1951 case LTTNG_DOMAIN_UST
:
1953 struct ltt_ust_channel
*uchan
;
1954 struct ltt_ust_session
*usess
= session
->ust_session
;
1959 * If a non-default channel has been created in the
1960 * session, explicitely require that -c chan_name needs
1963 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1964 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1968 /* Get channel from global UST domain */
1969 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1971 if (uchan
== NULL
) {
1972 /* Create default channel */
1973 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1974 usess
->buffer_type
);
1976 ret
= LTTNG_ERR_FATAL
;
1979 if (lttng_strncpy(attr
->name
, channel_name
,
1980 sizeof(attr
->name
))) {
1981 ret
= LTTNG_ERR_INVALID
;
1985 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1986 if (ret
!= LTTNG_OK
) {
1990 /* Get the newly created channel reference back */
1991 uchan
= trace_ust_find_channel_by_name(
1992 usess
->domain_global
.channels
, channel_name
);
1996 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1998 * Don't allow users to add UST events to channels which
1999 * are assigned to a userspace subdomain (JUL, Log4J,
2002 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2006 if (!internal_event
) {
2008 * Ensure the event name is not reserved for internal
2011 ret
= validate_ust_event_name(event
->name
);
2013 WARN("Userspace event name %s failed validation.",
2015 event
->name
: "NULL");
2016 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2021 /* At this point, the session and channel exist on the tracer */
2022 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2023 filter_expression
, filter
, exclusion
,
2025 /* We have passed ownership */
2026 filter_expression
= NULL
;
2029 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2030 goto already_enabled
;
2031 } else if (ret
!= LTTNG_OK
) {
2036 case LTTNG_DOMAIN_LOG4J
:
2037 case LTTNG_DOMAIN_JUL
:
2038 case LTTNG_DOMAIN_PYTHON
:
2040 const char *default_event_name
, *default_chan_name
;
2042 struct lttng_event uevent
;
2043 struct lttng_domain tmp_dom
;
2044 struct ltt_ust_session
*usess
= session
->ust_session
;
2048 agt
= trace_ust_find_agent(usess
, domain
->type
);
2050 agt
= agent_create(domain
->type
);
2052 ret
= LTTNG_ERR_NOMEM
;
2055 agent_add(agt
, usess
->agents
);
2058 /* Create the default tracepoint. */
2059 memset(&uevent
, 0, sizeof(uevent
));
2060 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2061 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2062 default_event_name
= event_get_default_agent_ust_name(
2064 if (!default_event_name
) {
2065 ret
= LTTNG_ERR_FATAL
;
2068 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2069 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2072 * The domain type is changed because we are about to enable the
2073 * default channel and event for the JUL domain that are hardcoded.
2074 * This happens in the UST domain.
2076 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2077 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2079 switch (domain
->type
) {
2080 case LTTNG_DOMAIN_LOG4J
:
2081 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2083 case LTTNG_DOMAIN_JUL
:
2084 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2086 case LTTNG_DOMAIN_PYTHON
:
2087 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2090 /* The switch/case we are in makes this impossible */
2095 char *filter_expression_copy
= NULL
;
2096 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2099 const size_t filter_size
= sizeof(
2100 struct lttng_filter_bytecode
)
2103 filter_copy
= zmalloc(filter_size
);
2105 ret
= LTTNG_ERR_NOMEM
;
2108 memcpy(filter_copy
, filter
, filter_size
);
2110 filter_expression_copy
=
2111 strdup(filter_expression
);
2112 if (!filter_expression
) {
2113 ret
= LTTNG_ERR_NOMEM
;
2116 if (!filter_expression_copy
|| !filter_copy
) {
2117 free(filter_expression_copy
);
2123 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2124 (char *) default_chan_name
,
2125 &uevent
, filter_expression_copy
,
2126 filter_copy
, NULL
, wpipe
);
2129 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2130 goto already_enabled
;
2131 } else if (ret
!= LTTNG_OK
) {
2135 /* The wild card * means that everything should be enabled. */
2136 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2137 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2140 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2144 filter_expression
= NULL
;
2145 if (ret
!= LTTNG_OK
) {
2152 ret
= LTTNG_ERR_UND
;
2160 free(filter_expression
);
2163 channel_attr_destroy(attr
);
2169 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2170 * We own filter, exclusion, and filter_expression.
2172 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2173 char *channel_name
, struct lttng_event
*event
,
2174 char *filter_expression
,
2175 struct lttng_filter_bytecode
*filter
,
2176 struct lttng_event_exclusion
*exclusion
,
2179 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2180 filter_expression
, filter
, exclusion
, wpipe
, false);
2184 * Enable an event which is internal to LTTng. An internal should
2185 * never be made visible to clients and are immune to checks such as
2188 static int cmd_enable_event_internal(struct ltt_session
*session
,
2189 struct lttng_domain
*domain
,
2190 char *channel_name
, struct lttng_event
*event
,
2191 char *filter_expression
,
2192 struct lttng_filter_bytecode
*filter
,
2193 struct lttng_event_exclusion
*exclusion
,
2196 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2197 filter_expression
, filter
, exclusion
, wpipe
, true);
2201 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2203 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2204 struct lttng_event
**events
)
2207 ssize_t nb_events
= 0;
2210 case LTTNG_DOMAIN_KERNEL
:
2211 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2212 if (nb_events
< 0) {
2213 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2217 case LTTNG_DOMAIN_UST
:
2218 nb_events
= ust_app_list_events(events
);
2219 if (nb_events
< 0) {
2220 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2224 case LTTNG_DOMAIN_LOG4J
:
2225 case LTTNG_DOMAIN_JUL
:
2226 case LTTNG_DOMAIN_PYTHON
:
2227 nb_events
= agent_list_events(events
, domain
);
2228 if (nb_events
< 0) {
2229 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2234 ret
= LTTNG_ERR_UND
;
2241 /* Return negative value to differentiate return code */
2246 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2248 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2249 struct lttng_event_field
**fields
)
2252 ssize_t nb_fields
= 0;
2255 case LTTNG_DOMAIN_UST
:
2256 nb_fields
= ust_app_list_event_fields(fields
);
2257 if (nb_fields
< 0) {
2258 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2262 case LTTNG_DOMAIN_KERNEL
:
2263 default: /* fall-through */
2264 ret
= LTTNG_ERR_UND
;
2271 /* Return negative value to differentiate return code */
2275 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2277 return syscall_table_list(events
);
2281 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2283 * Called with session lock held.
2285 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2286 enum lttng_domain_type domain
, int32_t **pids
)
2289 ssize_t nr_pids
= 0;
2292 case LTTNG_DOMAIN_KERNEL
:
2294 struct ltt_kernel_session
*ksess
;
2296 ksess
= session
->kernel_session
;
2297 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2299 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2304 case LTTNG_DOMAIN_UST
:
2306 struct ltt_ust_session
*usess
;
2308 usess
= session
->ust_session
;
2309 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2311 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2316 case LTTNG_DOMAIN_LOG4J
:
2317 case LTTNG_DOMAIN_JUL
:
2318 case LTTNG_DOMAIN_PYTHON
:
2320 ret
= LTTNG_ERR_UND
;
2327 /* Return negative value to differentiate return code */
2332 * Command LTTNG_START_TRACE processed by the client thread.
2334 * Called with session mutex held.
2336 int cmd_start_trace(struct ltt_session
*session
)
2339 unsigned long nb_chan
= 0;
2340 struct ltt_kernel_session
*ksession
;
2341 struct ltt_ust_session
*usess
;
2345 /* Ease our life a bit ;) */
2346 ksession
= session
->kernel_session
;
2347 usess
= session
->ust_session
;
2349 /* Is the session already started? */
2350 if (session
->active
) {
2351 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2356 * Starting a session without channel is useless since after that it's not
2357 * possible to enable channel thus inform the client.
2359 if (usess
&& usess
->domain_global
.channels
) {
2360 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2363 nb_chan
+= ksession
->channel_count
;
2366 ret
= LTTNG_ERR_NO_CHANNEL
;
2370 /* Kernel tracing */
2371 if (ksession
!= NULL
) {
2372 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2373 if (ret
!= LTTNG_OK
) {
2378 /* Flag session that trace should start automatically */
2381 * Even though the start trace might fail, flag this session active so
2382 * other application coming in are started by default.
2386 ret
= ust_app_start_trace_all(usess
);
2388 ret
= LTTNG_ERR_UST_START_FAIL
;
2393 /* Flag this after a successful start. */
2394 session
->has_been_started
= 1;
2395 session
->active
= 1;
2404 * Command LTTNG_STOP_TRACE processed by the client thread.
2406 int cmd_stop_trace(struct ltt_session
*session
)
2409 struct ltt_kernel_channel
*kchan
;
2410 struct ltt_kernel_session
*ksession
;
2411 struct ltt_ust_session
*usess
;
2416 ksession
= session
->kernel_session
;
2417 usess
= session
->ust_session
;
2419 /* Session is not active. Skip everythong and inform the client. */
2420 if (!session
->active
) {
2421 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2426 if (ksession
&& ksession
->active
) {
2427 DBG("Stop kernel tracing");
2429 ret
= kernel_stop_session(ksession
);
2431 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2435 kernel_wait_quiescent(kernel_tracer_fd
);
2437 /* Flush metadata after stopping (if exists) */
2438 if (ksession
->metadata_stream_fd
>= 0) {
2439 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2441 ERR("Kernel metadata flush failed");
2445 /* Flush all buffers after stopping */
2446 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2447 ret
= kernel_flush_buffer(kchan
);
2449 ERR("Kernel flush buffer error");
2453 ksession
->active
= 0;
2456 if (usess
&& usess
->active
) {
2458 * Even though the stop trace might fail, flag this session inactive so
2459 * other application coming in are not started by default.
2463 ret
= ust_app_stop_trace_all(usess
);
2465 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2470 /* Flag inactive after a successful stop. */
2471 session
->active
= 0;
2479 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2481 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2482 struct lttng_uri
*uris
)
2485 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2486 struct ltt_ust_session
*usess
= session
->ust_session
;
2492 /* Can't set consumer URI if the session is active. */
2493 if (session
->active
) {
2494 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2498 /* Set the "global" consumer URIs */
2499 for (i
= 0; i
< nb_uri
; i
++) {
2500 ret
= add_uri_to_consumer(session
->consumer
,
2501 &uris
[i
], 0, session
->name
);
2502 if (ret
!= LTTNG_OK
) {
2507 /* Set UST session URIs */
2508 if (session
->ust_session
) {
2509 for (i
= 0; i
< nb_uri
; i
++) {
2510 ret
= add_uri_to_consumer(
2511 session
->ust_session
->consumer
,
2512 &uris
[i
], LTTNG_DOMAIN_UST
,
2514 if (ret
!= LTTNG_OK
) {
2520 /* Set kernel session URIs */
2521 if (session
->kernel_session
) {
2522 for (i
= 0; i
< nb_uri
; i
++) {
2523 ret
= add_uri_to_consumer(
2524 session
->kernel_session
->consumer
,
2525 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2527 if (ret
!= LTTNG_OK
) {
2534 * Make sure to set the session in output mode after we set URI since a
2535 * session can be created without URL (thus flagged in no output mode).
2537 session
->output_traces
= 1;
2539 ksess
->output_traces
= 1;
2543 usess
->output_traces
= 1;
2554 * Command LTTNG_CREATE_SESSION processed by the client thread.
2556 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2557 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2560 struct ltt_session
*session
;
2566 * Verify if the session already exist
2568 * XXX: There is no need for the session lock list here since the caller
2569 * (process_client_msg) is holding it. We might want to change that so a
2570 * single command does not lock the entire session list.
2572 session
= session_find_by_name(name
);
2573 if (session
!= NULL
) {
2574 ret
= LTTNG_ERR_EXIST_SESS
;
2578 /* Create tracing session in the registry */
2579 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2580 LTTNG_SOCK_GET_GID_CRED(creds
));
2581 if (ret
!= LTTNG_OK
) {
2586 * Get the newly created session pointer back
2588 * XXX: There is no need for the session lock list here since the caller
2589 * (process_client_msg) is holding it. We might want to change that so a
2590 * single command does not lock the entire session list.
2592 session
= session_find_by_name(name
);
2595 session
->live_timer
= live_timer
;
2596 /* Create default consumer output for the session not yet created. */
2597 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2598 if (session
->consumer
== NULL
) {
2599 ret
= LTTNG_ERR_FATAL
;
2600 goto consumer_error
;
2604 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2605 if (ret
!= LTTNG_OK
) {
2606 goto consumer_error
;
2608 session
->output_traces
= 1;
2610 session
->output_traces
= 0;
2611 DBG2("Session %s created with no output", session
->name
);
2614 session
->consumer
->enabled
= 1;
2619 session_destroy(session
);
2626 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2628 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2629 size_t nb_uri
, lttng_sock_cred
*creds
)
2632 struct ltt_session
*session
;
2633 struct snapshot_output
*new_output
= NULL
;
2639 * Create session in no output mode with URIs set to NULL. The uris we've
2640 * received are for a default snapshot output if one.
2642 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2643 if (ret
!= LTTNG_OK
) {
2647 /* Get the newly created session pointer back. This should NEVER fail. */
2648 session
= session_find_by_name(name
);
2651 /* Flag session for snapshot mode. */
2652 session
->snapshot_mode
= 1;
2654 /* Skip snapshot output creation if no URI is given. */
2659 new_output
= snapshot_output_alloc();
2661 ret
= LTTNG_ERR_NOMEM
;
2662 goto error_snapshot_alloc
;
2665 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2666 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2668 if (ret
== -ENOMEM
) {
2669 ret
= LTTNG_ERR_NOMEM
;
2671 ret
= LTTNG_ERR_INVALID
;
2673 goto error_snapshot
;
2677 snapshot_add_output(&session
->snapshot
, new_output
);
2684 snapshot_output_destroy(new_output
);
2685 error_snapshot_alloc
:
2686 session_destroy(session
);
2692 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2694 * Called with session lock held.
2696 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2699 struct ltt_ust_session
*usess
;
2700 struct ltt_kernel_session
*ksess
;
2705 usess
= session
->ust_session
;
2706 ksess
= session
->kernel_session
;
2708 /* Clean kernel session teardown */
2709 kernel_destroy_session(ksess
);
2711 /* UST session teardown */
2713 /* Close any relayd session */
2714 consumer_output_send_destroy_relayd(usess
->consumer
);
2716 /* Destroy every UST application related to this session. */
2717 ret
= ust_app_destroy_trace_all(usess
);
2719 ERR("Error in ust_app_destroy_trace_all");
2722 /* Clean up the rest. */
2723 trace_ust_destroy_session(usess
);
2727 * Must notify the kernel thread here to update it's poll set in order to
2728 * remove the channel(s)' fd just destroyed.
2730 ret
= notify_thread_pipe(wpipe
);
2732 PERROR("write kernel poll pipe");
2735 ret
= session_destroy(session
);
2741 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2743 int cmd_register_consumer(struct ltt_session
*session
,
2744 enum lttng_domain_type domain
, const char *sock_path
,
2745 struct consumer_data
*cdata
)
2748 struct consumer_socket
*socket
= NULL
;
2755 case LTTNG_DOMAIN_KERNEL
:
2757 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2761 /* Can't register a consumer if there is already one */
2762 if (ksess
->consumer_fds_sent
!= 0) {
2763 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2767 sock
= lttcomm_connect_unix_sock(sock_path
);
2769 ret
= LTTNG_ERR_CONNECT_FAIL
;
2772 cdata
->cmd_sock
= sock
;
2774 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2775 if (socket
== NULL
) {
2778 PERROR("close register consumer");
2780 cdata
->cmd_sock
= -1;
2781 ret
= LTTNG_ERR_FATAL
;
2785 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2786 if (socket
->lock
== NULL
) {
2787 PERROR("zmalloc pthread mutex");
2788 ret
= LTTNG_ERR_FATAL
;
2791 pthread_mutex_init(socket
->lock
, NULL
);
2792 socket
->registered
= 1;
2795 consumer_add_socket(socket
, ksess
->consumer
);
2798 pthread_mutex_lock(&cdata
->pid_mutex
);
2800 pthread_mutex_unlock(&cdata
->pid_mutex
);
2805 /* TODO: Userspace tracing */
2806 ret
= LTTNG_ERR_UND
;
2814 consumer_destroy_socket(socket
);
2820 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2822 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2823 struct lttng_domain
**domains
)
2828 struct lttng_ht_iter iter
;
2830 if (session
->kernel_session
!= NULL
) {
2831 DBG3("Listing domains found kernel domain");
2835 if (session
->ust_session
!= NULL
) {
2836 DBG3("Listing domains found UST global domain");
2840 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2842 if (agt
->being_used
) {
2853 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2854 if (*domains
== NULL
) {
2855 ret
= LTTNG_ERR_FATAL
;
2859 if (session
->kernel_session
!= NULL
) {
2860 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2862 /* Kernel session buffer type is always GLOBAL */
2863 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2868 if (session
->ust_session
!= NULL
) {
2869 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2870 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2874 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2876 if (agt
->being_used
) {
2877 (*domains
)[index
].type
= agt
->domain
;
2878 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2888 /* Return negative value to differentiate return code */
2894 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2896 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2897 struct ltt_session
*session
, struct lttng_channel
**channels
)
2899 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2902 case LTTNG_DOMAIN_KERNEL
:
2903 if (session
->kernel_session
!= NULL
) {
2904 nb_chan
= session
->kernel_session
->channel_count
;
2906 DBG3("Number of kernel channels %zd", nb_chan
);
2908 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2912 case LTTNG_DOMAIN_UST
:
2913 if (session
->ust_session
!= NULL
) {
2915 nb_chan
= lttng_ht_get_count(
2916 session
->ust_session
->domain_global
.channels
);
2919 DBG3("Number of UST global channels %zd", nb_chan
);
2921 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2926 ret
= -LTTNG_ERR_UND
;
2931 const size_t channel_size
= sizeof(struct lttng_channel
) +
2932 sizeof(struct lttng_channel_extended
);
2933 struct lttng_channel_extended
*channel_exts
;
2935 payload_size
= nb_chan
* channel_size
;
2936 *channels
= zmalloc(payload_size
);
2937 if (*channels
== NULL
) {
2938 ret
= -LTTNG_ERR_FATAL
;
2942 channel_exts
= ((void *) *channels
) +
2943 (nb_chan
* sizeof(struct lttng_channel
));
2944 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2945 if (ret
!= LTTNG_OK
) {
2960 * Command LTTNG_LIST_EVENTS processed by the client thread.
2962 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2963 struct ltt_session
*session
, char *channel_name
,
2964 struct lttng_event
**events
, size_t *total_size
)
2967 ssize_t nb_event
= 0;
2970 case LTTNG_DOMAIN_KERNEL
:
2971 if (session
->kernel_session
!= NULL
) {
2972 nb_event
= list_lttng_kernel_events(channel_name
,
2973 session
->kernel_session
, events
,
2977 case LTTNG_DOMAIN_UST
:
2979 if (session
->ust_session
!= NULL
) {
2980 nb_event
= list_lttng_ust_global_events(channel_name
,
2981 &session
->ust_session
->domain_global
, events
,
2986 case LTTNG_DOMAIN_LOG4J
:
2987 case LTTNG_DOMAIN_JUL
:
2988 case LTTNG_DOMAIN_PYTHON
:
2989 if (session
->ust_session
) {
2990 struct lttng_ht_iter iter
;
2994 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2995 &iter
.iter
, agt
, node
.node
) {
2996 if (agt
->domain
== domain
) {
2997 nb_event
= list_lttng_agent_events(
3007 ret
= LTTNG_ERR_UND
;
3014 /* Return negative value to differentiate return code */
3019 * Using the session list, filled a lttng_session array to send back to the
3020 * client for session listing.
3022 * The session list lock MUST be acquired before calling this function. Use
3023 * session_lock_list() and session_unlock_list().
3025 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3030 struct ltt_session
*session
;
3031 struct ltt_session_list
*list
= session_get_list();
3033 DBG("Getting all available session for UID %d GID %d",
3036 * Iterate over session list and append data after the control struct in
3039 cds_list_for_each_entry(session
, &list
->head
, list
) {
3041 * Only list the sessions the user can control.
3043 if (!session_access_ok(session
, uid
, gid
)) {
3047 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3048 struct ltt_ust_session
*usess
= session
->ust_session
;
3050 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3051 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3052 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3053 ret
= build_network_session_path(sessions
[i
].path
,
3054 sizeof(sessions
[i
].path
), session
);
3056 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3057 session
->consumer
->dst
.trace_path
);
3060 PERROR("snprintf session path");
3064 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3065 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3066 sessions
[i
].enabled
= session
->active
;
3067 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3068 sessions
[i
].live_timer_interval
= session
->live_timer
;
3074 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3075 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3077 int cmd_data_pending(struct ltt_session
*session
)
3080 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3081 struct ltt_ust_session
*usess
= session
->ust_session
;
3085 /* Session MUST be stopped to ask for data availability. */
3086 if (session
->active
) {
3087 ret
= LTTNG_ERR_SESSION_STARTED
;
3091 * If stopped, just make sure we've started before else the above call
3092 * will always send that there is data pending.
3094 * The consumer assumes that when the data pending command is received,
3095 * the trace has been started before or else no output data is written
3096 * by the streams which is a condition for data pending. So, this is
3097 * *VERY* important that we don't ask the consumer before a start
3100 if (!session
->has_been_started
) {
3106 if (ksess
&& ksess
->consumer
) {
3107 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3109 /* Data is still being extracted for the kernel. */
3114 if (usess
&& usess
->consumer
) {
3115 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3117 /* Data is still being extracted for the kernel. */
3122 /* Data is ready to be read by a viewer */
3130 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3132 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3134 int cmd_snapshot_add_output(struct ltt_session
*session
,
3135 struct lttng_snapshot_output
*output
, uint32_t *id
)
3138 struct snapshot_output
*new_output
;
3143 DBG("Cmd snapshot add output for session %s", session
->name
);
3146 * Can't create an output if the session is not set in no-output mode.
3148 if (session
->output_traces
) {
3149 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3153 /* Only one output is allowed until we have the "tee" feature. */
3154 if (session
->snapshot
.nb_output
== 1) {
3155 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3159 new_output
= snapshot_output_alloc();
3161 ret
= LTTNG_ERR_NOMEM
;
3165 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3166 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3167 &session
->snapshot
);
3169 if (ret
== -ENOMEM
) {
3170 ret
= LTTNG_ERR_NOMEM
;
3172 ret
= LTTNG_ERR_INVALID
;
3178 snapshot_add_output(&session
->snapshot
, new_output
);
3180 *id
= new_output
->id
;
3187 snapshot_output_destroy(new_output
);
3193 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3195 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3197 int cmd_snapshot_del_output(struct ltt_session
*session
,
3198 struct lttng_snapshot_output
*output
)
3201 struct snapshot_output
*sout
= NULL
;
3209 * Permission denied to create an output if the session is not
3210 * set in no output mode.
3212 if (session
->output_traces
) {
3213 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3218 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3220 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3221 } else if (*output
->name
!= '\0') {
3222 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3224 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3227 ret
= LTTNG_ERR_INVALID
;
3231 snapshot_delete_output(&session
->snapshot
, sout
);
3232 snapshot_output_destroy(sout
);
3241 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3243 * If no output is available, outputs is untouched and 0 is returned.
3245 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3247 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3248 struct lttng_snapshot_output
**outputs
)
3251 struct lttng_snapshot_output
*list
= NULL
;
3252 struct lttng_ht_iter iter
;
3253 struct snapshot_output
*output
;
3258 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3261 * Permission denied to create an output if the session is not
3262 * set in no output mode.
3264 if (session
->output_traces
) {
3265 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3269 if (session
->snapshot
.nb_output
== 0) {
3274 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3276 ret
= -LTTNG_ERR_NOMEM
;
3280 /* Copy list from session to the new list object. */
3282 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3283 output
, node
.node
) {
3284 assert(output
->consumer
);
3285 list
[idx
].id
= output
->id
;
3286 list
[idx
].max_size
= output
->max_size
;
3287 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3288 sizeof(list
[idx
].name
))) {
3289 ret
= -LTTNG_ERR_INVALID
;
3292 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3293 if (lttng_strncpy(list
[idx
].ctrl_url
,
3294 output
->consumer
->dst
.trace_path
,
3295 sizeof(list
[idx
].ctrl_url
))) {
3296 ret
= -LTTNG_ERR_INVALID
;
3301 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3302 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3304 ret
= -LTTNG_ERR_NOMEM
;
3309 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3310 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3312 ret
= -LTTNG_ERR_NOMEM
;
3321 ret
= session
->snapshot
.nb_output
;
3330 * Check if we can regenerate the metadata for this session.
3331 * Only kernel, UST per-uid and non-live sessions are supported.
3333 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3336 int check_regenerate_metadata_support(struct ltt_session
*session
)
3342 if (session
->live_timer
!= 0) {
3343 ret
= LTTNG_ERR_LIVE_SESSION
;
3346 if (!session
->active
) {
3347 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3350 if (session
->ust_session
) {
3351 switch (session
->ust_session
->buffer_type
) {
3352 case LTTNG_BUFFER_PER_UID
:
3354 case LTTNG_BUFFER_PER_PID
:
3355 ret
= LTTNG_ERR_PER_PID_SESSION
;
3359 ret
= LTTNG_ERR_UNK
;
3363 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3364 session
->consumer
->relay_minor_version
< 8) {
3365 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3375 int clear_metadata_file(int fd
)
3379 ret
= lseek(fd
, 0, SEEK_SET
);
3385 ret
= ftruncate(fd
, 0);
3387 PERROR("ftruncate");
3396 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3399 struct buffer_reg_uid
*uid_reg
= NULL
;
3400 struct buffer_reg_session
*session_reg
= NULL
;
3403 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3404 struct ust_registry_session
*registry
;
3405 struct ust_registry_channel
*chan
;
3406 struct lttng_ht_iter iter_chan
;
3408 session_reg
= uid_reg
->registry
;
3409 registry
= session_reg
->reg
.ust
;
3411 pthread_mutex_lock(®istry
->lock
);
3412 registry
->metadata_len_sent
= 0;
3413 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3414 registry
->metadata_len
= 0;
3415 registry
->metadata_version
++;
3416 if (registry
->metadata_fd
> 0) {
3417 /* Clear the metadata file's content. */
3418 ret
= clear_metadata_file(registry
->metadata_fd
);
3420 pthread_mutex_unlock(®istry
->lock
);
3425 ret
= ust_metadata_session_statedump(registry
, NULL
,
3426 registry
->major
, registry
->minor
);
3428 pthread_mutex_unlock(®istry
->lock
);
3429 ERR("Failed to generate session metadata (err = %d)",
3433 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3435 struct ust_registry_event
*event
;
3436 struct lttng_ht_iter iter_event
;
3438 ret
= ust_metadata_channel_statedump(registry
, chan
);
3440 pthread_mutex_unlock(®istry
->lock
);
3441 ERR("Failed to generate channel metadata "
3445 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3447 ret
= ust_metadata_event_statedump(registry
,
3450 pthread_mutex_unlock(®istry
->lock
);
3451 ERR("Failed to generate event metadata "
3457 pthread_mutex_unlock(®istry
->lock
);
3466 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3468 * Ask the consumer to truncate the existing metadata file(s) and
3469 * then regenerate the metadata. Live and per-pid sessions are not
3470 * supported and return an error.
3472 * Return 0 on success or else a LTTNG_ERR code.
3474 int cmd_regenerate_metadata(struct ltt_session
*session
)
3480 ret
= check_regenerate_metadata_support(session
);
3485 if (session
->kernel_session
) {
3486 ret
= kernctl_session_regenerate_metadata(
3487 session
->kernel_session
->fd
);
3489 ERR("Failed to regenerate the kernel metadata");
3494 if (session
->ust_session
) {
3495 ret
= ust_regenerate_metadata(session
->ust_session
);
3497 ERR("Failed to regenerate the UST metadata");
3501 DBG("Cmd metadata regenerate for session %s", session
->name
);
3509 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3511 * Ask the tracer to regenerate a new statedump.
3513 * Return 0 on success or else a LTTNG_ERR code.
3515 int cmd_regenerate_statedump(struct ltt_session
*session
)
3521 if (!session
->active
) {
3522 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3527 if (session
->kernel_session
) {
3528 ret
= kernctl_session_regenerate_statedump(
3529 session
->kernel_session
->fd
);
3531 * Currently, the statedump in kernel can only fail if out
3535 if (ret
== -ENOMEM
) {
3536 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3538 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3540 ERR("Failed to regenerate the kernel statedump");
3545 if (session
->ust_session
) {
3546 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3548 * Currently, the statedump in UST always returns 0.
3551 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3552 ERR("Failed to regenerate the UST statedump");
3556 DBG("Cmd regenerate statedump for session %s", session
->name
);
3563 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3564 struct notification_thread_handle
*notification_thread
)
3568 ssize_t sock_recv_len
;
3569 struct lttng_trigger
*trigger
= NULL
;
3570 struct lttng_buffer_view view
;
3571 struct lttng_dynamic_buffer trigger_buffer
;
3573 lttng_dynamic_buffer_init(&trigger_buffer
);
3574 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3575 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3577 ret
= LTTNG_ERR_NOMEM
;
3581 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3583 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3584 ERR("Failed to receive \"register trigger\" command payload");
3585 /* TODO: should this be a new error enum ? */
3586 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3590 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3591 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3593 ERR("Invalid trigger payload received in \"register trigger\" command");
3594 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3598 ret
= notification_thread_command_register_trigger(notification_thread
,
3600 /* Ownership of trigger was transferred. */
3603 lttng_trigger_destroy(trigger
);
3604 lttng_dynamic_buffer_reset(&trigger_buffer
);
3608 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3609 struct notification_thread_handle
*notification_thread
)
3613 ssize_t sock_recv_len
;
3614 struct lttng_trigger
*trigger
= NULL
;
3615 struct lttng_buffer_view view
;
3616 struct lttng_dynamic_buffer trigger_buffer
;
3618 lttng_dynamic_buffer_init(&trigger_buffer
);
3619 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3620 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3622 ret
= LTTNG_ERR_NOMEM
;
3626 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3628 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3629 ERR("Failed to receive \"unregister trigger\" command payload");
3630 /* TODO: should this be a new error enum ? */
3631 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3635 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3636 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3638 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3639 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3643 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3646 lttng_trigger_destroy(trigger
);
3647 lttng_dynamic_buffer_reset(&trigger_buffer
);
3652 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3653 * snapshot output is *not* set with a remote destination.
3655 * Return 0 on success or a LTTNG_ERR code.
3657 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3658 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3661 struct lttng_ht_iter iter
;
3662 struct consumer_socket
*socket
;
3665 assert(snap_output
);
3668 DBG2("Set relayd object from snapshot output");
3670 /* Ignore if snapshot consumer output is not network. */
3671 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3676 * For each consumer socket, create and send the relayd object of the
3680 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3681 socket
, node
.node
) {
3682 ret
= send_consumer_relayd_sockets(0, session
->id
,
3683 snap_output
->consumer
, socket
,
3684 session
->name
, session
->hostname
,
3685 session
->live_timer
);
3686 if (ret
!= LTTNG_OK
) {
3698 * Record a kernel snapshot.
3700 * Return LTTNG_OK on success or a LTTNG_ERR code.
3702 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3703 struct snapshot_output
*output
, struct ltt_session
*session
,
3704 int wait
, uint64_t nb_packets_per_stream
)
3714 * Copy kernel session sockets so we can communicate with the right
3715 * consumer for the snapshot record command.
3717 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3719 ret
= LTTNG_ERR_NOMEM
;
3723 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3724 if (ret
!= LTTNG_OK
) {
3725 goto error_snapshot
;
3728 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3729 if (ret
!= LTTNG_OK
) {
3730 goto error_snapshot
;
3737 /* Clean up copied sockets so this output can use some other later on. */
3738 consumer_destroy_output_sockets(output
->consumer
);
3745 * Record a UST snapshot.
3747 * Return 0 on success or a LTTNG_ERR error code.
3749 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3750 struct snapshot_output
*output
, struct ltt_session
*session
,
3751 int wait
, uint64_t nb_packets_per_stream
)
3760 * Copy UST session sockets so we can communicate with the right
3761 * consumer for the snapshot record command.
3763 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3765 ret
= LTTNG_ERR_NOMEM
;
3769 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3770 if (ret
!= LTTNG_OK
) {
3771 goto error_snapshot
;
3774 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3778 ret
= LTTNG_ERR_INVALID
;
3781 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3784 goto error_snapshot
;
3790 /* Clean up copied sockets so this output can use some other later on. */
3791 consumer_destroy_output_sockets(output
->consumer
);
3797 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3798 uint64_t cur_nr_packets
)
3800 uint64_t tot_size
= 0;
3802 if (session
->kernel_session
) {
3803 struct ltt_kernel_channel
*chan
;
3804 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3806 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3807 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3809 * Don't take channel into account if we
3810 * already grab all its packets.
3814 tot_size
+= chan
->channel
->attr
.subbuf_size
3815 * chan
->stream_count
;
3819 if (session
->ust_session
) {
3820 struct ltt_ust_session
*usess
= session
->ust_session
;
3822 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3830 * Calculate the number of packets we can grab from each stream that
3831 * fits within the overall snapshot max size.
3833 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3834 * the number of packets per stream.
3836 * TODO: this approach is not perfect: we consider the worse case
3837 * (packet filling the sub-buffers) as an upper bound, but we could do
3838 * better if we do this calculation while we actually grab the packet
3839 * content: we would know how much padding we don't actually store into
3842 * This algorithm is currently bounded by the number of packets per
3845 * Since we call this algorithm before actually grabbing the data, it's
3846 * an approximation: for instance, applications could appear/disappear
3847 * in between this call and actually grabbing data.
3850 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3853 uint64_t cur_nb_packets
= 0;
3856 return 0; /* Infinite */
3859 size_left
= max_size
;
3861 uint64_t one_more_packet_tot_size
;
3863 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3865 if (!one_more_packet_tot_size
) {
3866 /* We are already grabbing all packets. */
3869 size_left
-= one_more_packet_tot_size
;
3870 if (size_left
< 0) {
3875 if (!cur_nb_packets
) {
3876 /* Not enough room to grab one packet of each stream, error. */
3879 return cur_nb_packets
;
3883 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3885 * The wait parameter is ignored so this call always wait for the snapshot to
3886 * complete before returning.
3888 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3890 int cmd_snapshot_record(struct ltt_session
*session
,
3891 struct lttng_snapshot_output
*output
, int wait
)
3894 unsigned int use_tmp_output
= 0;
3895 struct snapshot_output tmp_output
;
3896 unsigned int snapshot_success
= 0;
3902 DBG("Cmd snapshot record for session %s", session
->name
);
3904 /* Get the datetime for the snapshot output directory. */
3905 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3908 ret
= LTTNG_ERR_INVALID
;
3913 * Permission denied to create an output if the session is not
3914 * set in no output mode.
3916 if (session
->output_traces
) {
3917 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3921 /* The session needs to be started at least once. */
3922 if (!session
->has_been_started
) {
3923 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3927 /* Use temporary output for the session. */
3928 if (*output
->ctrl_url
!= '\0') {
3929 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3930 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3933 if (ret
== -ENOMEM
) {
3934 ret
= LTTNG_ERR_NOMEM
;
3936 ret
= LTTNG_ERR_INVALID
;
3940 /* Use the global session count for the temporary snapshot. */
3941 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3943 /* Use the global datetime */
3944 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3948 if (use_tmp_output
) {
3949 int64_t nb_packets_per_stream
;
3951 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3952 tmp_output
.max_size
);
3953 if (nb_packets_per_stream
< 0) {
3954 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3958 if (session
->kernel_session
) {
3959 ret
= record_kernel_snapshot(session
->kernel_session
,
3960 &tmp_output
, session
,
3961 wait
, nb_packets_per_stream
);
3962 if (ret
!= LTTNG_OK
) {
3967 if (session
->ust_session
) {
3968 ret
= record_ust_snapshot(session
->ust_session
,
3969 &tmp_output
, session
,
3970 wait
, nb_packets_per_stream
);
3971 if (ret
!= LTTNG_OK
) {
3976 snapshot_success
= 1;
3978 struct snapshot_output
*sout
;
3979 struct lttng_ht_iter iter
;
3982 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3983 &iter
.iter
, sout
, node
.node
) {
3984 int64_t nb_packets_per_stream
;
3987 * Make a local copy of the output and assign the possible
3988 * temporary value given by the caller.
3990 memset(&tmp_output
, 0, sizeof(tmp_output
));
3991 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3993 if (output
->max_size
!= (uint64_t) -1ULL) {
3994 tmp_output
.max_size
= output
->max_size
;
3997 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3998 tmp_output
.max_size
);
3999 if (nb_packets_per_stream
< 0) {
4000 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4005 /* Use temporary name. */
4006 if (*output
->name
!= '\0') {
4007 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4008 sizeof(tmp_output
.name
))) {
4009 ret
= LTTNG_ERR_INVALID
;
4015 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4016 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4018 if (session
->kernel_session
) {
4019 ret
= record_kernel_snapshot(session
->kernel_session
,
4020 &tmp_output
, session
,
4021 wait
, nb_packets_per_stream
);
4022 if (ret
!= LTTNG_OK
) {
4028 if (session
->ust_session
) {
4029 ret
= record_ust_snapshot(session
->ust_session
,
4030 &tmp_output
, session
,
4031 wait
, nb_packets_per_stream
);
4032 if (ret
!= LTTNG_OK
) {
4037 snapshot_success
= 1;
4042 if (snapshot_success
) {
4043 session
->snapshot
.nb_snapshot
++;
4045 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4053 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4055 int cmd_set_session_shm_path(struct ltt_session
*session
,
4056 const char *shm_path
)
4062 * Can only set shm path before session is started.
4064 if (session
->has_been_started
) {
4065 return LTTNG_ERR_SESSION_STARTED
;
4068 strncpy(session
->shm_path
, shm_path
,
4069 sizeof(session
->shm_path
));
4070 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4076 * Init command subsystem.
4081 * Set network sequence index to 1 for streams to match a relayd
4082 * socket on the consumer side.
4084 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4085 relayd_net_seq_idx
= 1;
4086 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4088 DBG("Command subsystem initialized");