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.h>
38 #include <lttng/channel-internal.h>
39 #include <common/string-utils/string-utils.h>
44 #include "health-sessiond.h"
46 #include "kernel-consumer.h"
47 #include "lttng-sessiond.h"
51 #include "buffer-registry.h"
52 #include "notification-thread.h"
53 #include "notification-thread-commands.h"
55 #include "rotation-thread.h"
60 * Used to keep a unique index for each relayd socket created where this value
61 * is associated with streams on the consumer so it can match the right relayd
62 * to send to. It must be accessed with the relayd_net_seq_idx_lock
65 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
66 static uint64_t relayd_net_seq_idx
;
68 static int validate_ust_event_name(const char *);
69 static int cmd_enable_event_internal(struct ltt_session
*session
,
70 struct lttng_domain
*domain
,
71 char *channel_name
, struct lttng_event
*event
,
72 char *filter_expression
,
73 struct lttng_filter_bytecode
*filter
,
74 struct lttng_event_exclusion
*exclusion
,
78 * Create a session path used by list_lttng_sessions for the case that the
79 * session consumer is on the network.
81 static int build_network_session_path(char *dst
, size_t size
,
82 struct ltt_session
*session
)
84 int ret
, kdata_port
, udata_port
;
85 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
86 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
91 memset(tmp_urls
, 0, sizeof(tmp_urls
));
92 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
94 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
96 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
97 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
98 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
101 if (session
->ust_session
&& session
->ust_session
->consumer
) {
102 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
103 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
106 if (uuri
== NULL
&& kuri
== NULL
) {
107 uri
= &session
->consumer
->dst
.net
.control
;
108 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
109 } else if (kuri
&& uuri
) {
110 ret
= uri_compare(kuri
, uuri
);
114 /* Build uuri URL string */
115 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
122 } else if (kuri
&& uuri
== NULL
) {
124 } else if (uuri
&& kuri
== NULL
) {
128 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
134 * Do we have a UST url set. If yes, this means we have both kernel and UST
137 if (*tmp_uurl
!= '\0') {
138 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
139 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
142 if (kuri
|| (!kuri
&& !uuri
)) {
145 /* No kernel URI, use the UST port. */
148 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
156 * Get run-time attributes if the session has been started (discarded events,
159 static int get_kernel_runtime_stats(struct ltt_session
*session
,
160 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
161 uint64_t *lost_packets
)
165 if (!session
->has_been_started
) {
167 *discarded_events
= 0;
172 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
173 session
->kernel_session
->consumer
,
179 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
180 session
->kernel_session
->consumer
,
191 * Get run-time attributes if the session has been started (discarded events,
194 static int get_ust_runtime_stats(struct ltt_session
*session
,
195 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
196 uint64_t *lost_packets
)
199 struct ltt_ust_session
*usess
;
201 if (!discarded_events
|| !lost_packets
) {
206 usess
= session
->ust_session
;
207 assert(discarded_events
);
208 assert(lost_packets
);
210 if (!usess
|| !session
->has_been_started
) {
211 *discarded_events
= 0;
217 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
218 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
219 &usess
->buffer_reg_uid_list
,
220 usess
->consumer
, uchan
->id
,
221 uchan
->attr
.overwrite
,
224 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
225 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
226 uchan
, usess
->consumer
,
227 uchan
->attr
.overwrite
,
233 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
234 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
236 ERR("Unsupported buffer type");
247 * Fill lttng_channel array of all channels.
249 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
250 struct ltt_session
*session
, struct lttng_channel
*channels
,
251 struct lttng_channel_extended
*chan_exts
)
254 struct ltt_kernel_channel
*kchan
;
256 DBG("Listing channels for session %s", session
->name
);
259 case LTTNG_DOMAIN_KERNEL
:
260 /* Kernel channels */
261 if (session
->kernel_session
!= NULL
) {
262 cds_list_for_each_entry(kchan
,
263 &session
->kernel_session
->channel_list
.head
, list
) {
264 uint64_t discarded_events
, lost_packets
;
265 struct lttng_channel_extended
*extended
;
267 extended
= (struct lttng_channel_extended
*)
268 kchan
->channel
->attr
.extended
.ptr
;
270 ret
= get_kernel_runtime_stats(session
, kchan
,
271 &discarded_events
, &lost_packets
);
275 /* Copy lttng_channel struct to array */
276 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
277 channels
[i
].enabled
= kchan
->enabled
;
278 chan_exts
[i
].discarded_events
=
280 chan_exts
[i
].lost_packets
= lost_packets
;
281 chan_exts
[i
].monitor_timer_interval
=
282 extended
->monitor_timer_interval
;
283 chan_exts
[i
].blocking_timeout
= 0;
288 case LTTNG_DOMAIN_UST
:
290 struct lttng_ht_iter iter
;
291 struct ltt_ust_channel
*uchan
;
294 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
295 &iter
.iter
, uchan
, node
.node
) {
296 uint64_t discarded_events
= 0, lost_packets
= 0;
298 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
299 LTTNG_SYMBOL_NAME_LEN
)) {
302 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
303 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
304 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
305 channels
[i
].attr
.switch_timer_interval
=
306 uchan
->attr
.switch_timer_interval
;
307 channels
[i
].attr
.read_timer_interval
=
308 uchan
->attr
.read_timer_interval
;
309 channels
[i
].enabled
= uchan
->enabled
;
310 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
311 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
314 * Map enum lttng_ust_output to enum lttng_event_output.
316 switch (uchan
->attr
.output
) {
318 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
322 * LTTNG_UST_MMAP is the only supported UST
329 chan_exts
[i
].monitor_timer_interval
=
330 uchan
->monitor_timer_interval
;
331 chan_exts
[i
].blocking_timeout
=
332 uchan
->attr
.u
.s
.blocking_timeout
;
334 ret
= get_ust_runtime_stats(session
, uchan
,
335 &discarded_events
, &lost_packets
);
339 chan_exts
[i
].discarded_events
= discarded_events
;
340 chan_exts
[i
].lost_packets
= lost_packets
;
352 return -LTTNG_ERR_FATAL
;
358 static void increment_extended_len(const char *filter_expression
,
359 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
361 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
363 if (filter_expression
) {
364 *extended_len
+= strlen(filter_expression
) + 1;
368 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
372 static void append_extended_info(const char *filter_expression
,
373 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
375 struct lttcomm_event_extended_header extended_header
;
376 size_t filter_len
= 0;
377 size_t nb_exclusions
= 0;
379 if (filter_expression
) {
380 filter_len
= strlen(filter_expression
) + 1;
384 nb_exclusions
= exclusion
->count
;
387 /* Set header fields */
388 extended_header
.filter_len
= filter_len
;
389 extended_header
.nb_exclusions
= nb_exclusions
;
392 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
393 *extended_at
+= sizeof(extended_header
);
395 /* Copy filter string */
396 if (filter_expression
) {
397 memcpy(*extended_at
, filter_expression
, filter_len
);
398 *extended_at
+= filter_len
;
401 /* Copy exclusion names */
403 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
405 memcpy(*extended_at
, &exclusion
->names
, len
);
411 * Create a list of agent domain events.
413 * Return number of events in list on success or else a negative value.
415 static int list_lttng_agent_events(struct agent
*agt
,
416 struct lttng_event
**events
, size_t *total_size
)
419 unsigned int nb_event
= 0;
420 struct agent_event
*event
;
421 struct lttng_event
*tmp_events
;
422 struct lttng_ht_iter iter
;
423 size_t extended_len
= 0;
429 DBG3("Listing agent events");
432 nb_event
= lttng_ht_get_count(agt
->events
);
440 /* Compute required extended infos size */
441 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
444 * This is only valid because the commands which add events are
445 * processed in the same thread as the listing.
448 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
449 increment_extended_len(event
->filter_expression
, NULL
,
454 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
455 tmp_events
= zmalloc(*total_size
);
457 PERROR("zmalloc agent events session");
458 ret
= -LTTNG_ERR_FATAL
;
462 extended_at
= ((uint8_t *) tmp_events
) +
463 nb_event
* sizeof(struct lttng_event
);
466 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
467 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
468 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
469 tmp_events
[i
].enabled
= event
->enabled
;
470 tmp_events
[i
].loglevel
= event
->loglevel_value
;
471 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
474 /* Append extended info */
475 append_extended_info(event
->filter_expression
, NULL
,
480 *events
= tmp_events
;
484 assert(nb_event
== i
);
489 * Create a list of ust global domain events.
491 static int list_lttng_ust_global_events(char *channel_name
,
492 struct ltt_ust_domain_global
*ust_global
,
493 struct lttng_event
**events
, size_t *total_size
)
496 unsigned int nb_event
= 0;
497 struct lttng_ht_iter iter
;
498 struct lttng_ht_node_str
*node
;
499 struct ltt_ust_channel
*uchan
;
500 struct ltt_ust_event
*uevent
;
501 struct lttng_event
*tmp
;
502 size_t extended_len
= 0;
505 DBG("Listing UST global events for channel %s", channel_name
);
509 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
510 node
= lttng_ht_iter_get_node_str(&iter
);
512 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
516 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
518 nb_event
= lttng_ht_get_count(uchan
->events
);
525 DBG3("Listing UST global %d events", nb_event
);
527 /* Compute required extended infos size */
528 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
529 if (uevent
->internal
) {
534 increment_extended_len(uevent
->filter_expression
,
535 uevent
->exclusion
, &extended_len
);
538 /* All events are internal, skip. */
544 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
545 tmp
= zmalloc(*total_size
);
547 ret
= -LTTNG_ERR_FATAL
;
551 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
553 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
554 if (uevent
->internal
) {
555 /* This event should remain hidden from clients */
558 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
559 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
560 tmp
[i
].enabled
= uevent
->enabled
;
562 switch (uevent
->attr
.instrumentation
) {
563 case LTTNG_UST_TRACEPOINT
:
564 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
566 case LTTNG_UST_PROBE
:
567 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
569 case LTTNG_UST_FUNCTION
:
570 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
574 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
575 switch (uevent
->attr
.loglevel_type
) {
576 case LTTNG_UST_LOGLEVEL_ALL
:
577 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
579 case LTTNG_UST_LOGLEVEL_RANGE
:
580 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
582 case LTTNG_UST_LOGLEVEL_SINGLE
:
583 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
586 if (uevent
->filter
) {
589 if (uevent
->exclusion
) {
590 tmp
[i
].exclusion
= 1;
594 /* Append extended info */
595 append_extended_info(uevent
->filter_expression
,
596 uevent
->exclusion
, &extended_at
);
607 * Fill lttng_event array of all kernel events in the channel.
609 static int list_lttng_kernel_events(char *channel_name
,
610 struct ltt_kernel_session
*kernel_session
,
611 struct lttng_event
**events
, size_t *total_size
)
614 unsigned int nb_event
;
615 struct ltt_kernel_event
*event
;
616 struct ltt_kernel_channel
*kchan
;
617 size_t extended_len
= 0;
620 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
622 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
626 nb_event
= kchan
->event_count
;
628 DBG("Listing events for channel %s", kchan
->channel
->name
);
636 /* Compute required extended infos size */
637 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
638 increment_extended_len(event
->filter_expression
, NULL
,
642 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
643 *events
= zmalloc(*total_size
);
644 if (*events
== NULL
) {
645 ret
= LTTNG_ERR_FATAL
;
649 extended_at
= ((void *) *events
) +
650 nb_event
* sizeof(struct lttng_event
);
652 /* Kernel channels */
653 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
654 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
655 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
656 (*events
)[i
].enabled
= event
->enabled
;
657 (*events
)[i
].filter
=
658 (unsigned char) !!event
->filter_expression
;
660 switch (event
->event
->instrumentation
) {
661 case LTTNG_KERNEL_TRACEPOINT
:
662 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
664 case LTTNG_KERNEL_KRETPROBE
:
665 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
666 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
667 sizeof(struct lttng_kernel_kprobe
));
669 case LTTNG_KERNEL_KPROBE
:
670 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
671 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
672 sizeof(struct lttng_kernel_kprobe
));
674 case LTTNG_KERNEL_FUNCTION
:
675 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
676 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
677 sizeof(struct lttng_kernel_function
));
679 case LTTNG_KERNEL_NOOP
:
680 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
682 case LTTNG_KERNEL_SYSCALL
:
683 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
685 case LTTNG_KERNEL_ALL
:
691 /* Append extended info */
692 append_extended_info(event
->filter_expression
, NULL
,
700 /* Negate the error code to differentiate the size from an error */
705 * Add URI so the consumer output object. Set the correct path depending on the
706 * domain adding the default trace directory.
708 static int add_uri_to_consumer(struct consumer_output
*consumer
,
709 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
710 const char *session_name
)
713 const char *default_trace_dir
;
717 if (consumer
== NULL
) {
718 DBG("No consumer detected. Don't add URI. Stopping.");
719 ret
= LTTNG_ERR_NO_CONSUMER
;
724 case LTTNG_DOMAIN_KERNEL
:
725 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
727 case LTTNG_DOMAIN_UST
:
728 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
732 * This case is possible is we try to add the URI to the global tracing
733 * session consumer object which in this case there is no subdir.
735 default_trace_dir
= "";
738 switch (uri
->dtype
) {
741 DBG2("Setting network URI to consumer");
743 if (consumer
->type
== CONSUMER_DST_NET
) {
744 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
745 consumer
->dst
.net
.control_isset
) ||
746 (uri
->stype
== LTTNG_STREAM_DATA
&&
747 consumer
->dst
.net
.data_isset
)) {
748 ret
= LTTNG_ERR_URL_EXIST
;
752 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
755 consumer
->type
= CONSUMER_DST_NET
;
757 /* Set URI into consumer output object */
758 ret
= consumer_set_network_uri(consumer
, uri
);
762 } else if (ret
== 1) {
764 * URI was the same in the consumer so we do not append the subdir
765 * again so to not duplicate output dir.
771 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
772 ret
= consumer_set_subdir(consumer
, session_name
);
774 ret
= LTTNG_ERR_FATAL
;
779 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
780 /* On a new subdir, reappend the default trace dir. */
781 strncat(consumer
->subdir
, default_trace_dir
,
782 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
783 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
788 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
789 memset(consumer
->dst
.session_root_path
, 0,
790 sizeof(consumer
->dst
.session_root_path
));
791 /* Explicit length checks for strcpy and strcat. */
792 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
793 >= sizeof(consumer
->dst
.session_root_path
)) {
794 ret
= LTTNG_ERR_FATAL
;
797 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
798 /* Append default trace dir */
799 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
800 /* Flag consumer as local. */
801 consumer
->type
= CONSUMER_DST_LOCAL
;
812 * Init tracing by creating trace directory and sending fds kernel consumer.
814 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
817 struct lttng_ht_iter iter
;
818 struct consumer_socket
*socket
;
824 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
825 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
827 pthread_mutex_lock(socket
->lock
);
828 ret
= kernel_consumer_send_session(socket
, session
);
829 pthread_mutex_unlock(socket
->lock
);
831 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
843 * Create a socket to the relayd using the URI.
845 * On success, the relayd_sock pointer is set to the created socket.
846 * Else, it's stays untouched and a lttcomm error code is returned.
848 static int create_connect_relayd(struct lttng_uri
*uri
,
849 struct lttcomm_relayd_sock
**relayd_sock
,
850 struct consumer_output
*consumer
)
853 struct lttcomm_relayd_sock
*rsock
;
855 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
856 RELAYD_VERSION_COMM_MINOR
);
858 ret
= LTTNG_ERR_FATAL
;
863 * Connect to relayd so we can proceed with a session creation. This call
864 * can possibly block for an arbitrary amount of time to set the health
865 * state to be in poll execution.
868 ret
= relayd_connect(rsock
);
871 ERR("Unable to reach lttng-relayd");
872 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
876 /* Create socket for control stream. */
877 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
878 DBG3("Creating relayd stream socket from URI");
880 /* Check relayd version */
881 ret
= relayd_version_check(rsock
);
883 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
886 consumer
->relay_major_version
= rsock
->major
;
887 consumer
->relay_minor_version
= rsock
->minor
;
888 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
889 DBG3("Creating relayd data socket from URI");
891 /* Command is not valid */
892 ERR("Relayd invalid stream type: %d", uri
->stype
);
893 ret
= LTTNG_ERR_INVALID
;
897 *relayd_sock
= rsock
;
902 /* The returned value is not useful since we are on an error path. */
903 (void) relayd_close(rsock
);
911 * Connect to the relayd using URI and send the socket to the right consumer.
913 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
914 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
915 struct consumer_output
*consumer
,
916 struct consumer_socket
*consumer_sock
,
917 char *session_name
, char *hostname
, int session_live_timer
)
920 struct lttcomm_relayd_sock
*rsock
= NULL
;
922 /* Connect to relayd and make version check if uri is the control. */
923 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
924 if (ret
!= LTTNG_OK
) {
929 /* Set the network sequence index if not set. */
930 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
931 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
933 * Increment net_seq_idx because we are about to transfer the
934 * new relayd socket to the consumer.
935 * Assign unique key so the consumer can match streams.
937 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
938 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
941 /* Send relayd socket to consumer. */
942 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
943 relayd_uri
->stype
, session_id
,
944 session_name
, hostname
, session_live_timer
);
946 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
950 /* Flag that the corresponding socket was sent. */
951 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
952 consumer_sock
->control_sock_sent
= 1;
953 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
954 consumer_sock
->data_sock_sent
= 1;
960 * Close socket which was dup on the consumer side. The session daemon does
961 * NOT keep track of the relayd socket(s) once transfer to the consumer.
965 (void) relayd_close(rsock
);
969 if (ret
!= LTTNG_OK
) {
971 * The consumer output for this session should not be used anymore
972 * since the relayd connection failed thus making any tracing or/and
973 * streaming not usable.
975 consumer
->enabled
= 0;
981 * Send both relayd sockets to a specific consumer and domain. This is a
982 * helper function to facilitate sending the information to the consumer for a
985 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
986 unsigned int session_id
, struct consumer_output
*consumer
,
987 struct consumer_socket
*sock
, char *session_name
,
988 char *hostname
, int session_live_timer
)
995 /* Sending control relayd socket. */
996 if (!sock
->control_sock_sent
) {
997 ret
= send_consumer_relayd_socket(domain
, session_id
,
998 &consumer
->dst
.net
.control
, consumer
, sock
,
999 session_name
, hostname
, session_live_timer
);
1000 if (ret
!= LTTNG_OK
) {
1005 /* Sending data relayd socket. */
1006 if (!sock
->data_sock_sent
) {
1007 ret
= send_consumer_relayd_socket(domain
, session_id
,
1008 &consumer
->dst
.net
.data
, consumer
, sock
,
1009 session_name
, hostname
, session_live_timer
);
1010 if (ret
!= LTTNG_OK
) {
1020 * Setup relayd connections for a tracing session. First creates the socket to
1021 * the relayd and send them to the right domain consumer. Consumer type MUST be
1024 int cmd_setup_relayd(struct ltt_session
*session
)
1027 struct ltt_ust_session
*usess
;
1028 struct ltt_kernel_session
*ksess
;
1029 struct consumer_socket
*socket
;
1030 struct lttng_ht_iter iter
;
1034 usess
= session
->ust_session
;
1035 ksess
= session
->kernel_session
;
1037 DBG("Setting relayd for session %s", session
->name
);
1041 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1042 && usess
->consumer
->enabled
) {
1043 /* For each consumer socket, send relayd sockets */
1044 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1045 socket
, node
.node
) {
1046 pthread_mutex_lock(socket
->lock
);
1047 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1048 usess
->consumer
, socket
,
1049 session
->name
, session
->hostname
,
1050 session
->live_timer
);
1051 pthread_mutex_unlock(socket
->lock
);
1052 if (ret
!= LTTNG_OK
) {
1055 /* Session is now ready for network streaming. */
1056 session
->net_handle
= 1;
1058 session
->consumer
->relay_major_version
=
1059 usess
->consumer
->relay_major_version
;
1060 session
->consumer
->relay_minor_version
=
1061 usess
->consumer
->relay_minor_version
;
1064 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1065 && ksess
->consumer
->enabled
) {
1066 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1067 socket
, node
.node
) {
1068 pthread_mutex_lock(socket
->lock
);
1069 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1070 ksess
->consumer
, socket
,
1071 session
->name
, session
->hostname
,
1072 session
->live_timer
);
1073 pthread_mutex_unlock(socket
->lock
);
1074 if (ret
!= LTTNG_OK
) {
1077 /* Session is now ready for network streaming. */
1078 session
->net_handle
= 1;
1080 session
->consumer
->relay_major_version
=
1081 ksess
->consumer
->relay_major_version
;
1082 session
->consumer
->relay_minor_version
=
1083 ksess
->consumer
->relay_minor_version
;
1092 * Start a kernel session by opening all necessary streams.
1094 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1097 struct ltt_kernel_channel
*kchan
;
1099 /* Open kernel metadata */
1100 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1101 ret
= kernel_open_metadata(ksess
);
1103 ret
= LTTNG_ERR_KERN_META_FAIL
;
1108 /* Open kernel metadata stream */
1109 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1110 ret
= kernel_open_metadata_stream(ksess
);
1112 ERR("Kernel create metadata stream failed");
1113 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1118 /* For each channel */
1119 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1120 if (kchan
->stream_count
== 0) {
1121 ret
= kernel_open_channel_stream(kchan
);
1123 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1126 /* Update the stream global counter */
1127 ksess
->stream_count_global
+= ret
;
1131 /* Setup kernel consumer socket and send fds to it */
1132 ret
= init_kernel_tracing(ksess
);
1134 ret
= LTTNG_ERR_KERN_START_FAIL
;
1138 /* This start the kernel tracing */
1139 ret
= kernel_start_session(ksess
);
1141 ret
= LTTNG_ERR_KERN_START_FAIL
;
1145 /* Quiescent wait after starting trace */
1146 kernel_wait_quiescent(kernel_tracer_fd
);
1157 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1159 int cmd_disable_channel(struct ltt_session
*session
,
1160 enum lttng_domain_type domain
, char *channel_name
)
1163 struct ltt_ust_session
*usess
;
1165 usess
= session
->ust_session
;
1170 case LTTNG_DOMAIN_KERNEL
:
1172 ret
= channel_kernel_disable(session
->kernel_session
,
1174 if (ret
!= LTTNG_OK
) {
1178 kernel_wait_quiescent(kernel_tracer_fd
);
1181 case LTTNG_DOMAIN_UST
:
1183 struct ltt_ust_channel
*uchan
;
1184 struct lttng_ht
*chan_ht
;
1186 chan_ht
= usess
->domain_global
.channels
;
1188 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1189 if (uchan
== NULL
) {
1190 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1194 ret
= channel_ust_disable(usess
, uchan
);
1195 if (ret
!= LTTNG_OK
) {
1201 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1213 * Command LTTNG_TRACK_PID processed by the client thread.
1215 * Called with session lock held.
1217 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1225 case LTTNG_DOMAIN_KERNEL
:
1227 struct ltt_kernel_session
*ksess
;
1229 ksess
= session
->kernel_session
;
1231 ret
= kernel_track_pid(ksess
, pid
);
1232 if (ret
!= LTTNG_OK
) {
1236 kernel_wait_quiescent(kernel_tracer_fd
);
1239 case LTTNG_DOMAIN_UST
:
1241 struct ltt_ust_session
*usess
;
1243 usess
= session
->ust_session
;
1245 ret
= trace_ust_track_pid(usess
, pid
);
1246 if (ret
!= LTTNG_OK
) {
1252 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1264 * Command LTTNG_UNTRACK_PID processed by the client thread.
1266 * Called with session lock held.
1268 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1276 case LTTNG_DOMAIN_KERNEL
:
1278 struct ltt_kernel_session
*ksess
;
1280 ksess
= session
->kernel_session
;
1282 ret
= kernel_untrack_pid(ksess
, pid
);
1283 if (ret
!= LTTNG_OK
) {
1287 kernel_wait_quiescent(kernel_tracer_fd
);
1290 case LTTNG_DOMAIN_UST
:
1292 struct ltt_ust_session
*usess
;
1294 usess
= session
->ust_session
;
1296 ret
= trace_ust_untrack_pid(usess
, pid
);
1297 if (ret
!= LTTNG_OK
) {
1303 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1315 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1317 * The wpipe arguments is used as a notifier for the kernel thread.
1319 int cmd_enable_channel(struct ltt_session
*session
,
1320 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1323 struct ltt_ust_session
*usess
= session
->ust_session
;
1324 struct lttng_ht
*chan_ht
;
1331 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1333 /* Validate channel name */
1334 if (attr
->name
[0] == '.' ||
1335 memchr(attr
->name
, '/', len
) != NULL
) {
1336 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1340 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1345 * Don't try to enable a channel if the session has been started at
1346 * some point in time before. The tracer does not allow it.
1348 if (session
->has_been_started
) {
1349 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1354 * If the session is a live session, remove the switch timer, the
1355 * live timer does the same thing but sends also synchronisation
1356 * beacons for inactive streams.
1358 if (session
->live_timer
> 0) {
1359 attr
->attr
.live_timer_interval
= session
->live_timer
;
1360 attr
->attr
.switch_timer_interval
= 0;
1363 /* Check for feature support */
1364 switch (domain
->type
) {
1365 case LTTNG_DOMAIN_KERNEL
:
1367 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1368 /* Sampling position of buffer is not supported */
1369 WARN("Kernel tracer does not support buffer monitoring. "
1370 "Setting the monitor interval timer to 0 "
1371 "(disabled) for channel '%s' of session '%s'",
1372 attr
-> name
, session
->name
);
1373 lttng_channel_set_monitor_timer_interval(attr
, 0);
1377 case LTTNG_DOMAIN_UST
:
1378 case LTTNG_DOMAIN_JUL
:
1379 case LTTNG_DOMAIN_LOG4J
:
1380 case LTTNG_DOMAIN_PYTHON
:
1383 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1387 switch (domain
->type
) {
1388 case LTTNG_DOMAIN_KERNEL
:
1390 struct ltt_kernel_channel
*kchan
;
1392 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1393 session
->kernel_session
);
1394 if (kchan
== NULL
) {
1395 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1396 if (attr
->name
[0] != '\0') {
1397 session
->kernel_session
->has_non_default_channel
= 1;
1400 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1403 if (ret
!= LTTNG_OK
) {
1407 kernel_wait_quiescent(kernel_tracer_fd
);
1410 case LTTNG_DOMAIN_UST
:
1411 case LTTNG_DOMAIN_JUL
:
1412 case LTTNG_DOMAIN_LOG4J
:
1413 case LTTNG_DOMAIN_PYTHON
:
1415 struct ltt_ust_channel
*uchan
;
1420 * Current agent implementation limitations force us to allow
1421 * only one channel at once in "agent" subdomains. Each
1422 * subdomain has a default channel name which must be strictly
1425 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1426 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1427 LTTNG_SYMBOL_NAME_LEN
)) {
1428 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1431 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1432 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1433 LTTNG_SYMBOL_NAME_LEN
)) {
1434 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1437 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1438 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1439 LTTNG_SYMBOL_NAME_LEN
)) {
1440 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1445 chan_ht
= usess
->domain_global
.channels
;
1447 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1448 if (uchan
== NULL
) {
1449 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1450 if (attr
->name
[0] != '\0') {
1451 usess
->has_non_default_channel
= 1;
1454 ret
= channel_ust_enable(usess
, uchan
);
1459 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1470 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1472 int cmd_disable_event(struct ltt_session
*session
,
1473 enum lttng_domain_type domain
, char *channel_name
,
1474 struct lttng_event
*event
)
1479 DBG("Disable event command for event \'%s\'", event
->name
);
1481 event_name
= event
->name
;
1483 /* Error out on unhandled search criteria */
1484 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1485 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1486 ret
= LTTNG_ERR_UNK
;
1493 case LTTNG_DOMAIN_KERNEL
:
1495 struct ltt_kernel_channel
*kchan
;
1496 struct ltt_kernel_session
*ksess
;
1498 ksess
= session
->kernel_session
;
1501 * If a non-default channel has been created in the
1502 * session, explicitely require that -c chan_name needs
1505 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1506 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1510 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1511 if (kchan
== NULL
) {
1512 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1516 switch (event
->type
) {
1517 case LTTNG_EVENT_ALL
:
1518 case LTTNG_EVENT_TRACEPOINT
:
1519 case LTTNG_EVENT_SYSCALL
:
1520 case LTTNG_EVENT_PROBE
:
1521 case LTTNG_EVENT_FUNCTION
:
1522 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1523 if (event_name
[0] == '\0') {
1524 ret
= event_kernel_disable_event(kchan
,
1527 ret
= event_kernel_disable_event(kchan
,
1528 event_name
, event
->type
);
1530 if (ret
!= LTTNG_OK
) {
1535 ret
= LTTNG_ERR_UNK
;
1539 kernel_wait_quiescent(kernel_tracer_fd
);
1542 case LTTNG_DOMAIN_UST
:
1544 struct ltt_ust_channel
*uchan
;
1545 struct ltt_ust_session
*usess
;
1547 usess
= session
->ust_session
;
1549 if (validate_ust_event_name(event_name
)) {
1550 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1555 * If a non-default channel has been created in the
1556 * session, explicitly require that -c chan_name needs
1559 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1560 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1564 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1566 if (uchan
== NULL
) {
1567 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1571 switch (event
->type
) {
1572 case LTTNG_EVENT_ALL
:
1574 * An empty event name means that everything
1575 * should be disabled.
1577 if (event
->name
[0] == '\0') {
1578 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1580 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1583 if (ret
!= LTTNG_OK
) {
1588 ret
= LTTNG_ERR_UNK
;
1592 DBG3("Disable UST event %s in channel %s completed", event_name
,
1596 case LTTNG_DOMAIN_LOG4J
:
1597 case LTTNG_DOMAIN_JUL
:
1598 case LTTNG_DOMAIN_PYTHON
:
1601 struct ltt_ust_session
*usess
= session
->ust_session
;
1605 switch (event
->type
) {
1606 case LTTNG_EVENT_ALL
:
1609 ret
= LTTNG_ERR_UNK
;
1613 agt
= trace_ust_find_agent(usess
, domain
);
1615 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1619 * An empty event name means that everything
1620 * should be disabled.
1622 if (event
->name
[0] == '\0') {
1623 ret
= event_agent_disable_all(usess
, agt
);
1625 ret
= event_agent_disable(usess
, agt
, event_name
);
1627 if (ret
!= LTTNG_OK
) {
1634 ret
= LTTNG_ERR_UND
;
1647 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1649 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1650 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1652 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1653 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1655 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1656 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1657 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1661 case LTTNG_DOMAIN_KERNEL
:
1662 assert(session
->kernel_session
);
1664 if (session
->kernel_session
->channel_count
== 0) {
1665 /* Create default channel */
1666 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1667 if (ret
!= LTTNG_OK
) {
1670 chan_kern_created
= 1;
1672 /* Add kernel context to kernel tracer */
1673 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1674 if (ret
!= LTTNG_OK
) {
1678 case LTTNG_DOMAIN_JUL
:
1679 case LTTNG_DOMAIN_LOG4J
:
1682 * Validate channel name.
1683 * If no channel name is given and the domain is JUL or LOG4J,
1684 * set it to the appropriate domain-specific channel name. If
1685 * a name is provided but does not match the expexted channel
1686 * name, return an error.
1688 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1689 strcmp(channel_name
,
1690 DEFAULT_JUL_CHANNEL_NAME
)) {
1691 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1693 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1694 strcmp(channel_name
,
1695 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1696 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1699 /* break is _not_ missing here. */
1701 case LTTNG_DOMAIN_UST
:
1703 struct ltt_ust_session
*usess
= session
->ust_session
;
1704 unsigned int chan_count
;
1708 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1709 if (chan_count
== 0) {
1710 struct lttng_channel
*attr
;
1711 /* Create default channel */
1712 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1714 ret
= LTTNG_ERR_FATAL
;
1718 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1719 if (ret
!= LTTNG_OK
) {
1723 channel_attr_destroy(attr
);
1724 chan_ust_created
= 1;
1727 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1728 free(app_ctx_provider_name
);
1730 app_ctx_name
= NULL
;
1731 app_ctx_provider_name
= NULL
;
1732 if (ret
!= LTTNG_OK
) {
1738 ret
= LTTNG_ERR_UND
;
1746 if (chan_kern_created
) {
1747 struct ltt_kernel_channel
*kchan
=
1748 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1749 session
->kernel_session
);
1750 /* Created previously, this should NOT fail. */
1752 kernel_destroy_channel(kchan
);
1755 if (chan_ust_created
) {
1756 struct ltt_ust_channel
*uchan
=
1757 trace_ust_find_channel_by_name(
1758 session
->ust_session
->domain_global
.channels
,
1759 DEFAULT_CHANNEL_NAME
);
1760 /* Created previously, this should NOT fail. */
1762 /* Remove from the channel list of the session. */
1763 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1765 trace_ust_destroy_channel(uchan
);
1768 free(app_ctx_provider_name
);
1773 static inline bool name_starts_with(const char *name
, const char *prefix
)
1775 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1777 return !strncmp(name
, prefix
, max_cmp_len
);
1780 /* Perform userspace-specific event name validation */
1781 static int validate_ust_event_name(const char *name
)
1791 * Check name against all internal UST event component namespaces used
1794 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1795 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1796 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1805 * Internal version of cmd_enable_event() with a supplemental
1806 * "internal_event" flag which is used to enable internal events which should
1807 * be hidden from clients. Such events are used in the agent implementation to
1808 * enable the events through which all "agent" events are funeled.
1810 static int _cmd_enable_event(struct ltt_session
*session
,
1811 struct lttng_domain
*domain
,
1812 char *channel_name
, struct lttng_event
*event
,
1813 char *filter_expression
,
1814 struct lttng_filter_bytecode
*filter
,
1815 struct lttng_event_exclusion
*exclusion
,
1816 int wpipe
, bool internal_event
)
1818 int ret
= 0, channel_created
= 0;
1819 struct lttng_channel
*attr
= NULL
;
1823 assert(channel_name
);
1825 /* If we have a filter, we must have its filter expression */
1826 assert(!(!!filter_expression
^ !!filter
));
1828 /* Normalize event name as a globbing pattern */
1829 strutils_normalize_star_glob_pattern(event
->name
);
1831 /* Normalize exclusion names as globbing patterns */
1835 for (i
= 0; i
< exclusion
->count
; i
++) {
1836 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1838 strutils_normalize_star_glob_pattern(name
);
1842 DBG("Enable event command for event \'%s\'", event
->name
);
1846 switch (domain
->type
) {
1847 case LTTNG_DOMAIN_KERNEL
:
1849 struct ltt_kernel_channel
*kchan
;
1852 * If a non-default channel has been created in the
1853 * session, explicitely require that -c chan_name needs
1856 if (session
->kernel_session
->has_non_default_channel
1857 && channel_name
[0] == '\0') {
1858 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1862 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1863 session
->kernel_session
);
1864 if (kchan
== NULL
) {
1865 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1866 LTTNG_BUFFER_GLOBAL
);
1868 ret
= LTTNG_ERR_FATAL
;
1871 if (lttng_strncpy(attr
->name
, channel_name
,
1872 sizeof(attr
->name
))) {
1873 ret
= LTTNG_ERR_INVALID
;
1877 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1878 if (ret
!= LTTNG_OK
) {
1881 channel_created
= 1;
1884 /* Get the newly created kernel channel pointer */
1885 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1886 session
->kernel_session
);
1887 if (kchan
== NULL
) {
1888 /* This sould not happen... */
1889 ret
= LTTNG_ERR_FATAL
;
1893 switch (event
->type
) {
1894 case LTTNG_EVENT_ALL
:
1896 char *filter_expression_a
= NULL
;
1897 struct lttng_filter_bytecode
*filter_a
= NULL
;
1900 * We need to duplicate filter_expression and filter,
1901 * because ownership is passed to first enable
1904 if (filter_expression
) {
1905 filter_expression_a
= strdup(filter_expression
);
1906 if (!filter_expression_a
) {
1907 ret
= LTTNG_ERR_FATAL
;
1912 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1914 free(filter_expression_a
);
1915 ret
= LTTNG_ERR_FATAL
;
1918 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1920 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1921 ret
= event_kernel_enable_event(kchan
, event
,
1922 filter_expression
, filter
);
1923 /* We have passed ownership */
1924 filter_expression
= NULL
;
1926 if (ret
!= LTTNG_OK
) {
1927 if (channel_created
) {
1928 /* Let's not leak a useless channel. */
1929 kernel_destroy_channel(kchan
);
1931 free(filter_expression_a
);
1935 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1936 ret
= event_kernel_enable_event(kchan
, event
,
1937 filter_expression_a
, filter_a
);
1938 /* We have passed ownership */
1939 filter_expression_a
= NULL
;
1941 if (ret
!= LTTNG_OK
) {
1946 case LTTNG_EVENT_PROBE
:
1947 case LTTNG_EVENT_FUNCTION
:
1948 case LTTNG_EVENT_FUNCTION_ENTRY
:
1949 case LTTNG_EVENT_TRACEPOINT
:
1950 ret
= event_kernel_enable_event(kchan
, event
,
1951 filter_expression
, filter
);
1952 /* We have passed ownership */
1953 filter_expression
= NULL
;
1955 if (ret
!= LTTNG_OK
) {
1956 if (channel_created
) {
1957 /* Let's not leak a useless channel. */
1958 kernel_destroy_channel(kchan
);
1963 case LTTNG_EVENT_SYSCALL
:
1964 ret
= event_kernel_enable_event(kchan
, event
,
1965 filter_expression
, filter
);
1966 /* We have passed ownership */
1967 filter_expression
= NULL
;
1969 if (ret
!= LTTNG_OK
) {
1974 ret
= LTTNG_ERR_UNK
;
1978 kernel_wait_quiescent(kernel_tracer_fd
);
1981 case LTTNG_DOMAIN_UST
:
1983 struct ltt_ust_channel
*uchan
;
1984 struct ltt_ust_session
*usess
= session
->ust_session
;
1989 * If a non-default channel has been created in the
1990 * session, explicitely require that -c chan_name needs
1993 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1994 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1998 /* Get channel from global UST domain */
1999 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2001 if (uchan
== NULL
) {
2002 /* Create default channel */
2003 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2004 usess
->buffer_type
);
2006 ret
= LTTNG_ERR_FATAL
;
2009 if (lttng_strncpy(attr
->name
, channel_name
,
2010 sizeof(attr
->name
))) {
2011 ret
= LTTNG_ERR_INVALID
;
2015 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2016 if (ret
!= LTTNG_OK
) {
2020 /* Get the newly created channel reference back */
2021 uchan
= trace_ust_find_channel_by_name(
2022 usess
->domain_global
.channels
, channel_name
);
2026 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2028 * Don't allow users to add UST events to channels which
2029 * are assigned to a userspace subdomain (JUL, Log4J,
2032 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2036 if (!internal_event
) {
2038 * Ensure the event name is not reserved for internal
2041 ret
= validate_ust_event_name(event
->name
);
2043 WARN("Userspace event name %s failed validation.",
2045 event
->name
: "NULL");
2046 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2051 /* At this point, the session and channel exist on the tracer */
2052 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2053 filter_expression
, filter
, exclusion
,
2055 /* We have passed ownership */
2056 filter_expression
= NULL
;
2059 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2060 goto already_enabled
;
2061 } else if (ret
!= LTTNG_OK
) {
2066 case LTTNG_DOMAIN_LOG4J
:
2067 case LTTNG_DOMAIN_JUL
:
2068 case LTTNG_DOMAIN_PYTHON
:
2070 const char *default_event_name
, *default_chan_name
;
2072 struct lttng_event uevent
;
2073 struct lttng_domain tmp_dom
;
2074 struct ltt_ust_session
*usess
= session
->ust_session
;
2078 agt
= trace_ust_find_agent(usess
, domain
->type
);
2080 agt
= agent_create(domain
->type
);
2082 ret
= LTTNG_ERR_NOMEM
;
2085 agent_add(agt
, usess
->agents
);
2088 /* Create the default tracepoint. */
2089 memset(&uevent
, 0, sizeof(uevent
));
2090 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2091 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2092 default_event_name
= event_get_default_agent_ust_name(
2094 if (!default_event_name
) {
2095 ret
= LTTNG_ERR_FATAL
;
2098 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2099 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2102 * The domain type is changed because we are about to enable the
2103 * default channel and event for the JUL domain that are hardcoded.
2104 * This happens in the UST domain.
2106 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2107 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2109 switch (domain
->type
) {
2110 case LTTNG_DOMAIN_LOG4J
:
2111 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2113 case LTTNG_DOMAIN_JUL
:
2114 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2116 case LTTNG_DOMAIN_PYTHON
:
2117 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2120 /* The switch/case we are in makes this impossible */
2125 char *filter_expression_copy
= NULL
;
2126 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2129 const size_t filter_size
= sizeof(
2130 struct lttng_filter_bytecode
)
2133 filter_copy
= zmalloc(filter_size
);
2135 ret
= LTTNG_ERR_NOMEM
;
2138 memcpy(filter_copy
, filter
, filter_size
);
2140 filter_expression_copy
=
2141 strdup(filter_expression
);
2142 if (!filter_expression
) {
2143 ret
= LTTNG_ERR_NOMEM
;
2146 if (!filter_expression_copy
|| !filter_copy
) {
2147 free(filter_expression_copy
);
2153 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2154 (char *) default_chan_name
,
2155 &uevent
, filter_expression_copy
,
2156 filter_copy
, NULL
, wpipe
);
2159 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2160 goto already_enabled
;
2161 } else if (ret
!= LTTNG_OK
) {
2165 /* The wild card * means that everything should be enabled. */
2166 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2167 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2170 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2174 filter_expression
= NULL
;
2175 if (ret
!= LTTNG_OK
) {
2182 ret
= LTTNG_ERR_UND
;
2190 free(filter_expression
);
2193 channel_attr_destroy(attr
);
2199 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2200 * We own filter, exclusion, and filter_expression.
2202 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2203 char *channel_name
, struct lttng_event
*event
,
2204 char *filter_expression
,
2205 struct lttng_filter_bytecode
*filter
,
2206 struct lttng_event_exclusion
*exclusion
,
2209 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2210 filter_expression
, filter
, exclusion
, wpipe
, false);
2214 * Enable an event which is internal to LTTng. An internal should
2215 * never be made visible to clients and are immune to checks such as
2218 static int cmd_enable_event_internal(struct ltt_session
*session
,
2219 struct lttng_domain
*domain
,
2220 char *channel_name
, struct lttng_event
*event
,
2221 char *filter_expression
,
2222 struct lttng_filter_bytecode
*filter
,
2223 struct lttng_event_exclusion
*exclusion
,
2226 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2227 filter_expression
, filter
, exclusion
, wpipe
, true);
2231 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2233 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2234 struct lttng_event
**events
)
2237 ssize_t nb_events
= 0;
2240 case LTTNG_DOMAIN_KERNEL
:
2241 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2242 if (nb_events
< 0) {
2243 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2247 case LTTNG_DOMAIN_UST
:
2248 nb_events
= ust_app_list_events(events
);
2249 if (nb_events
< 0) {
2250 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2254 case LTTNG_DOMAIN_LOG4J
:
2255 case LTTNG_DOMAIN_JUL
:
2256 case LTTNG_DOMAIN_PYTHON
:
2257 nb_events
= agent_list_events(events
, domain
);
2258 if (nb_events
< 0) {
2259 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2264 ret
= LTTNG_ERR_UND
;
2271 /* Return negative value to differentiate return code */
2276 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2278 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2279 struct lttng_event_field
**fields
)
2282 ssize_t nb_fields
= 0;
2285 case LTTNG_DOMAIN_UST
:
2286 nb_fields
= ust_app_list_event_fields(fields
);
2287 if (nb_fields
< 0) {
2288 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2292 case LTTNG_DOMAIN_KERNEL
:
2293 default: /* fall-through */
2294 ret
= LTTNG_ERR_UND
;
2301 /* Return negative value to differentiate return code */
2305 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2307 return syscall_table_list(events
);
2311 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2313 * Called with session lock held.
2315 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2316 enum lttng_domain_type domain
, int32_t **pids
)
2319 ssize_t nr_pids
= 0;
2322 case LTTNG_DOMAIN_KERNEL
:
2324 struct ltt_kernel_session
*ksess
;
2326 ksess
= session
->kernel_session
;
2327 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2329 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2334 case LTTNG_DOMAIN_UST
:
2336 struct ltt_ust_session
*usess
;
2338 usess
= session
->ust_session
;
2339 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2341 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2346 case LTTNG_DOMAIN_LOG4J
:
2347 case LTTNG_DOMAIN_JUL
:
2348 case LTTNG_DOMAIN_PYTHON
:
2350 ret
= LTTNG_ERR_UND
;
2357 /* Return negative value to differentiate return code */
2362 * Command LTTNG_START_TRACE processed by the client thread.
2364 * Called with session mutex held.
2366 int cmd_start_trace(struct ltt_session
*session
)
2369 unsigned long nb_chan
= 0;
2370 struct ltt_kernel_session
*ksession
;
2371 struct ltt_ust_session
*usess
;
2375 /* Ease our life a bit ;) */
2376 ksession
= session
->kernel_session
;
2377 usess
= session
->ust_session
;
2379 /* Is the session already started? */
2380 if (session
->active
) {
2381 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2386 * Starting a session without channel is useless since after that it's not
2387 * possible to enable channel thus inform the client.
2389 if (usess
&& usess
->domain_global
.channels
) {
2390 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2393 nb_chan
+= ksession
->channel_count
;
2396 ret
= LTTNG_ERR_NO_CHANNEL
;
2401 * Record the timestamp of the first time the session is started for
2402 * an eventual session rotation call.
2404 if (!session
->has_been_started
) {
2405 session
->session_start_ts
= time(NULL
);
2406 if (session
->session_start_ts
== (time_t) -1) {
2407 PERROR("Get start time");
2408 ret
= LTTNG_ERR_FATAL
;
2413 /* Kernel tracing */
2414 if (ksession
!= NULL
) {
2415 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2416 if (ret
!= LTTNG_OK
) {
2421 /* Flag session that trace should start automatically */
2424 * Even though the start trace might fail, flag this session active so
2425 * other application coming in are started by default.
2429 ret
= ust_app_start_trace_all(usess
);
2431 ret
= LTTNG_ERR_UST_START_FAIL
;
2436 /* Flag this after a successful start. */
2437 session
->has_been_started
= 1;
2438 session
->active
= 1;
2447 * Command LTTNG_STOP_TRACE processed by the client thread.
2449 int cmd_stop_trace(struct ltt_session
*session
)
2452 struct ltt_kernel_channel
*kchan
;
2453 struct ltt_kernel_session
*ksession
;
2454 struct ltt_ust_session
*usess
;
2459 ksession
= session
->kernel_session
;
2460 usess
= session
->ust_session
;
2462 /* Session is not active. Skip everythong and inform the client. */
2463 if (!session
->active
) {
2464 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2469 if (ksession
&& ksession
->active
) {
2470 DBG("Stop kernel tracing");
2472 ret
= kernel_stop_session(ksession
);
2474 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2478 kernel_wait_quiescent(kernel_tracer_fd
);
2480 /* Flush metadata after stopping (if exists) */
2481 if (ksession
->metadata_stream_fd
>= 0) {
2482 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2484 ERR("Kernel metadata flush failed");
2488 /* Flush all buffers after stopping */
2489 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2490 ret
= kernel_flush_buffer(kchan
);
2492 ERR("Kernel flush buffer error");
2496 ksession
->active
= 0;
2499 if (usess
&& usess
->active
) {
2501 * Even though the stop trace might fail, flag this session inactive so
2502 * other application coming in are not started by default.
2506 ret
= ust_app_stop_trace_all(usess
);
2508 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2513 session
->session_last_stop_ts
= time(NULL
);
2515 /* Flag inactive after a successful stop. */
2516 session
->active
= 0;
2524 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2526 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2527 struct lttng_uri
*uris
)
2530 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2531 struct ltt_ust_session
*usess
= session
->ust_session
;
2537 /* Can't set consumer URI if the session is active. */
2538 if (session
->active
) {
2539 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2543 /* Set the "global" consumer URIs */
2544 for (i
= 0; i
< nb_uri
; i
++) {
2545 ret
= add_uri_to_consumer(session
->consumer
,
2546 &uris
[i
], 0, session
->name
);
2547 if (ret
!= LTTNG_OK
) {
2552 /* Set UST session URIs */
2553 if (session
->ust_session
) {
2554 for (i
= 0; i
< nb_uri
; i
++) {
2555 ret
= add_uri_to_consumer(
2556 session
->ust_session
->consumer
,
2557 &uris
[i
], LTTNG_DOMAIN_UST
,
2559 if (ret
!= LTTNG_OK
) {
2565 /* Set kernel session URIs */
2566 if (session
->kernel_session
) {
2567 for (i
= 0; i
< nb_uri
; i
++) {
2568 ret
= add_uri_to_consumer(
2569 session
->kernel_session
->consumer
,
2570 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2572 if (ret
!= LTTNG_OK
) {
2579 * Make sure to set the session in output mode after we set URI since a
2580 * session can be created without URL (thus flagged in no output mode).
2582 session
->output_traces
= 1;
2584 ksess
->output_traces
= 1;
2588 usess
->output_traces
= 1;
2599 * Command LTTNG_CREATE_SESSION processed by the client thread.
2601 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2602 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2605 struct ltt_session
*session
;
2611 * Verify if the session already exist
2613 * XXX: There is no need for the session lock list here since the caller
2614 * (process_client_msg) is holding it. We might want to change that so a
2615 * single command does not lock the entire session list.
2617 session
= session_find_by_name(name
);
2618 if (session
!= NULL
) {
2619 ret
= LTTNG_ERR_EXIST_SESS
;
2623 /* Create tracing session in the registry */
2624 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2625 LTTNG_SOCK_GET_GID_CRED(creds
));
2626 if (ret
!= LTTNG_OK
) {
2631 * Get the newly created session pointer back
2633 * XXX: There is no need for the session lock list here since the caller
2634 * (process_client_msg) is holding it. We might want to change that so a
2635 * single command does not lock the entire session list.
2637 session
= session_find_by_name(name
);
2640 session
->live_timer
= live_timer
;
2641 /* Create default consumer output for the session not yet created. */
2642 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2643 if (session
->consumer
== NULL
) {
2644 ret
= LTTNG_ERR_FATAL
;
2645 goto consumer_error
;
2649 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2650 if (ret
!= LTTNG_OK
) {
2651 goto consumer_error
;
2653 session
->output_traces
= 1;
2655 session
->output_traces
= 0;
2656 DBG2("Session %s created with no output", session
->name
);
2659 session
->consumer
->enabled
= 1;
2664 session_destroy(session
);
2671 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2673 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2674 size_t nb_uri
, lttng_sock_cred
*creds
)
2677 struct ltt_session
*session
;
2678 struct snapshot_output
*new_output
= NULL
;
2684 * Create session in no output mode with URIs set to NULL. The uris we've
2685 * received are for a default snapshot output if one.
2687 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2688 if (ret
!= LTTNG_OK
) {
2692 /* Get the newly created session pointer back. This should NEVER fail. */
2693 session
= session_find_by_name(name
);
2696 /* Flag session for snapshot mode. */
2697 session
->snapshot_mode
= 1;
2699 /* Skip snapshot output creation if no URI is given. */
2704 new_output
= snapshot_output_alloc();
2706 ret
= LTTNG_ERR_NOMEM
;
2707 goto error_snapshot_alloc
;
2710 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2711 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2713 if (ret
== -ENOMEM
) {
2714 ret
= LTTNG_ERR_NOMEM
;
2716 ret
= LTTNG_ERR_INVALID
;
2718 goto error_snapshot
;
2722 snapshot_add_output(&session
->snapshot
, new_output
);
2729 snapshot_output_destroy(new_output
);
2730 error_snapshot_alloc
:
2731 session_destroy(session
);
2737 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2739 * Called with session lock held.
2741 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2744 struct ltt_ust_session
*usess
;
2745 struct ltt_kernel_session
*ksess
;
2750 usess
= session
->ust_session
;
2751 ksess
= session
->kernel_session
;
2753 if (session
->rotate_count
> 0) {
2754 session
->rotate_count
++;
2756 * The currently active tracing path is now the folder we
2759 snprintf(session
->rotation_chunk
.current_rotate_path
,
2761 session
->rotation_chunk
.active_tracing_path
);
2762 ret
= rename_complete_chunk(session
,
2763 session
->session_last_stop_ts
);
2765 ERR("Renaming session on destroy");
2769 /* Clean kernel session teardown */
2770 kernel_destroy_session(ksess
);
2772 /* UST session teardown */
2774 /* Close any relayd session */
2775 consumer_output_send_destroy_relayd(usess
->consumer
);
2777 /* Destroy every UST application related to this session. */
2778 ret
= ust_app_destroy_trace_all(usess
);
2780 ERR("Error in ust_app_destroy_trace_all");
2783 /* Clean up the rest. */
2784 trace_ust_destroy_session(usess
);
2788 * Must notify the kernel thread here to update it's poll set in order to
2789 * remove the channel(s)' fd just destroyed.
2791 ret
= notify_thread_pipe(wpipe
);
2793 PERROR("write kernel poll pipe");
2796 ret
= session_destroy(session
);
2802 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2804 int cmd_register_consumer(struct ltt_session
*session
,
2805 enum lttng_domain_type domain
, const char *sock_path
,
2806 struct consumer_data
*cdata
)
2809 struct consumer_socket
*socket
= NULL
;
2816 case LTTNG_DOMAIN_KERNEL
:
2818 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2822 /* Can't register a consumer if there is already one */
2823 if (ksess
->consumer_fds_sent
!= 0) {
2824 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2828 sock
= lttcomm_connect_unix_sock(sock_path
);
2830 ret
= LTTNG_ERR_CONNECT_FAIL
;
2833 cdata
->cmd_sock
= sock
;
2835 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2836 if (socket
== NULL
) {
2839 PERROR("close register consumer");
2841 cdata
->cmd_sock
= -1;
2842 ret
= LTTNG_ERR_FATAL
;
2846 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2847 if (socket
->lock
== NULL
) {
2848 PERROR("zmalloc pthread mutex");
2849 ret
= LTTNG_ERR_FATAL
;
2852 pthread_mutex_init(socket
->lock
, NULL
);
2853 socket
->registered
= 1;
2856 consumer_add_socket(socket
, ksess
->consumer
);
2859 pthread_mutex_lock(&cdata
->pid_mutex
);
2861 pthread_mutex_unlock(&cdata
->pid_mutex
);
2866 /* TODO: Userspace tracing */
2867 ret
= LTTNG_ERR_UND
;
2875 consumer_destroy_socket(socket
);
2881 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2883 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2884 struct lttng_domain
**domains
)
2889 struct lttng_ht_iter iter
;
2891 if (session
->kernel_session
!= NULL
) {
2892 DBG3("Listing domains found kernel domain");
2896 if (session
->ust_session
!= NULL
) {
2897 DBG3("Listing domains found UST global domain");
2901 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2903 if (agt
->being_used
) {
2914 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2915 if (*domains
== NULL
) {
2916 ret
= LTTNG_ERR_FATAL
;
2920 if (session
->kernel_session
!= NULL
) {
2921 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2923 /* Kernel session buffer type is always GLOBAL */
2924 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2929 if (session
->ust_session
!= NULL
) {
2930 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2931 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2935 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2937 if (agt
->being_used
) {
2938 (*domains
)[index
].type
= agt
->domain
;
2939 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2949 /* Return negative value to differentiate return code */
2955 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2957 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2958 struct ltt_session
*session
, struct lttng_channel
**channels
)
2960 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2963 case LTTNG_DOMAIN_KERNEL
:
2964 if (session
->kernel_session
!= NULL
) {
2965 nb_chan
= session
->kernel_session
->channel_count
;
2967 DBG3("Number of kernel channels %zd", nb_chan
);
2969 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2973 case LTTNG_DOMAIN_UST
:
2974 if (session
->ust_session
!= NULL
) {
2976 nb_chan
= lttng_ht_get_count(
2977 session
->ust_session
->domain_global
.channels
);
2980 DBG3("Number of UST global channels %zd", nb_chan
);
2982 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2987 ret
= -LTTNG_ERR_UND
;
2992 const size_t channel_size
= sizeof(struct lttng_channel
) +
2993 sizeof(struct lttng_channel_extended
);
2994 struct lttng_channel_extended
*channel_exts
;
2996 payload_size
= nb_chan
* channel_size
;
2997 *channels
= zmalloc(payload_size
);
2998 if (*channels
== NULL
) {
2999 ret
= -LTTNG_ERR_FATAL
;
3003 channel_exts
= ((void *) *channels
) +
3004 (nb_chan
* sizeof(struct lttng_channel
));
3005 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3006 if (ret
!= LTTNG_OK
) {
3021 * Command LTTNG_LIST_EVENTS processed by the client thread.
3023 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3024 struct ltt_session
*session
, char *channel_name
,
3025 struct lttng_event
**events
, size_t *total_size
)
3028 ssize_t nb_event
= 0;
3031 case LTTNG_DOMAIN_KERNEL
:
3032 if (session
->kernel_session
!= NULL
) {
3033 nb_event
= list_lttng_kernel_events(channel_name
,
3034 session
->kernel_session
, events
,
3038 case LTTNG_DOMAIN_UST
:
3040 if (session
->ust_session
!= NULL
) {
3041 nb_event
= list_lttng_ust_global_events(channel_name
,
3042 &session
->ust_session
->domain_global
, events
,
3047 case LTTNG_DOMAIN_LOG4J
:
3048 case LTTNG_DOMAIN_JUL
:
3049 case LTTNG_DOMAIN_PYTHON
:
3050 if (session
->ust_session
) {
3051 struct lttng_ht_iter iter
;
3055 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3056 &iter
.iter
, agt
, node
.node
) {
3057 if (agt
->domain
== domain
) {
3058 nb_event
= list_lttng_agent_events(
3068 ret
= LTTNG_ERR_UND
;
3075 /* Return negative value to differentiate return code */
3080 * Using the session list, filled a lttng_session array to send back to the
3081 * client for session listing.
3083 * The session list lock MUST be acquired before calling this function. Use
3084 * session_lock_list() and session_unlock_list().
3086 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3091 struct ltt_session
*session
;
3092 struct ltt_session_list
*list
= session_get_list();
3094 DBG("Getting all available session for UID %d GID %d",
3097 * Iterate over session list and append data after the control struct in
3100 cds_list_for_each_entry(session
, &list
->head
, list
) {
3102 * Only list the sessions the user can control.
3104 if (!session_access_ok(session
, uid
, gid
)) {
3108 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3109 struct ltt_ust_session
*usess
= session
->ust_session
;
3111 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3112 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3113 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3114 ret
= build_network_session_path(sessions
[i
].path
,
3115 sizeof(sessions
[i
].path
), session
);
3117 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3118 session
->consumer
->dst
.session_root_path
);
3121 PERROR("snprintf session path");
3125 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3126 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3127 sessions
[i
].enabled
= session
->active
;
3128 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3129 sessions
[i
].live_timer_interval
= session
->live_timer
;
3135 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3136 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3138 int cmd_data_pending(struct ltt_session
*session
)
3141 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3142 struct ltt_ust_session
*usess
= session
->ust_session
;
3146 /* Session MUST be stopped to ask for data availability. */
3147 if (session
->active
) {
3148 ret
= LTTNG_ERR_SESSION_STARTED
;
3152 * If stopped, just make sure we've started before else the above call
3153 * will always send that there is data pending.
3155 * The consumer assumes that when the data pending command is received,
3156 * the trace has been started before or else no output data is written
3157 * by the streams which is a condition for data pending. So, this is
3158 * *VERY* important that we don't ask the consumer before a start
3161 if (!session
->has_been_started
) {
3167 if (ksess
&& ksess
->consumer
) {
3168 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3170 /* Data is still being extracted for the kernel. */
3175 if (usess
&& usess
->consumer
) {
3176 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3178 /* Data is still being extracted for the kernel. */
3183 /* Data is ready to be read by a viewer */
3191 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3193 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3195 int cmd_snapshot_add_output(struct ltt_session
*session
,
3196 struct lttng_snapshot_output
*output
, uint32_t *id
)
3199 struct snapshot_output
*new_output
;
3204 DBG("Cmd snapshot add output for session %s", session
->name
);
3207 * Can't create an output if the session is not set in no-output mode.
3209 if (session
->output_traces
) {
3210 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3214 /* Only one output is allowed until we have the "tee" feature. */
3215 if (session
->snapshot
.nb_output
== 1) {
3216 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3220 new_output
= snapshot_output_alloc();
3222 ret
= LTTNG_ERR_NOMEM
;
3226 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3227 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3228 &session
->snapshot
);
3230 if (ret
== -ENOMEM
) {
3231 ret
= LTTNG_ERR_NOMEM
;
3233 ret
= LTTNG_ERR_INVALID
;
3239 snapshot_add_output(&session
->snapshot
, new_output
);
3241 *id
= new_output
->id
;
3248 snapshot_output_destroy(new_output
);
3254 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3256 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3258 int cmd_snapshot_del_output(struct ltt_session
*session
,
3259 struct lttng_snapshot_output
*output
)
3262 struct snapshot_output
*sout
= NULL
;
3270 * Permission denied to create an output if the session is not
3271 * set in no output mode.
3273 if (session
->output_traces
) {
3274 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3279 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3281 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3282 } else if (*output
->name
!= '\0') {
3283 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3285 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3288 ret
= LTTNG_ERR_INVALID
;
3292 snapshot_delete_output(&session
->snapshot
, sout
);
3293 snapshot_output_destroy(sout
);
3302 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3304 * If no output is available, outputs is untouched and 0 is returned.
3306 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3308 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3309 struct lttng_snapshot_output
**outputs
)
3312 struct lttng_snapshot_output
*list
= NULL
;
3313 struct lttng_ht_iter iter
;
3314 struct snapshot_output
*output
;
3319 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3322 * Permission denied to create an output if the session is not
3323 * set in no output mode.
3325 if (session
->output_traces
) {
3326 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3330 if (session
->snapshot
.nb_output
== 0) {
3335 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3337 ret
= -LTTNG_ERR_NOMEM
;
3341 /* Copy list from session to the new list object. */
3343 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3344 output
, node
.node
) {
3345 assert(output
->consumer
);
3346 list
[idx
].id
= output
->id
;
3347 list
[idx
].max_size
= output
->max_size
;
3348 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3349 sizeof(list
[idx
].name
))) {
3350 ret
= -LTTNG_ERR_INVALID
;
3353 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3354 if (lttng_strncpy(list
[idx
].ctrl_url
,
3355 output
->consumer
->dst
.session_root_path
,
3356 sizeof(list
[idx
].ctrl_url
))) {
3357 ret
= -LTTNG_ERR_INVALID
;
3362 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3363 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3365 ret
= -LTTNG_ERR_NOMEM
;
3370 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3371 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3373 ret
= -LTTNG_ERR_NOMEM
;
3382 ret
= session
->snapshot
.nb_output
;
3391 * Check if we can regenerate the metadata for this session.
3392 * Only kernel, UST per-uid and non-live sessions are supported.
3394 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3397 int check_regenerate_metadata_support(struct ltt_session
*session
)
3403 if (session
->live_timer
!= 0) {
3404 ret
= LTTNG_ERR_LIVE_SESSION
;
3407 if (!session
->active
) {
3408 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3411 if (session
->ust_session
) {
3412 switch (session
->ust_session
->buffer_type
) {
3413 case LTTNG_BUFFER_PER_UID
:
3415 case LTTNG_BUFFER_PER_PID
:
3416 ret
= LTTNG_ERR_PER_PID_SESSION
;
3420 ret
= LTTNG_ERR_UNK
;
3424 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3425 session
->consumer
->relay_minor_version
< 8) {
3426 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3436 int clear_metadata_file(int fd
)
3440 ret
= lseek(fd
, 0, SEEK_SET
);
3446 ret
= ftruncate(fd
, 0);
3448 PERROR("ftruncate");
3457 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3460 struct buffer_reg_uid
*uid_reg
= NULL
;
3461 struct buffer_reg_session
*session_reg
= NULL
;
3464 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3465 struct ust_registry_session
*registry
;
3466 struct ust_registry_channel
*chan
;
3467 struct lttng_ht_iter iter_chan
;
3469 session_reg
= uid_reg
->registry
;
3470 registry
= session_reg
->reg
.ust
;
3472 pthread_mutex_lock(®istry
->lock
);
3473 registry
->metadata_len_sent
= 0;
3474 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3475 registry
->metadata_len
= 0;
3476 registry
->metadata_version
++;
3477 if (registry
->metadata_fd
> 0) {
3478 /* Clear the metadata file's content. */
3479 ret
= clear_metadata_file(registry
->metadata_fd
);
3481 pthread_mutex_unlock(®istry
->lock
);
3486 ret
= ust_metadata_session_statedump(registry
, NULL
,
3487 registry
->major
, registry
->minor
);
3489 pthread_mutex_unlock(®istry
->lock
);
3490 ERR("Failed to generate session metadata (err = %d)",
3494 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3496 struct ust_registry_event
*event
;
3497 struct lttng_ht_iter iter_event
;
3499 ret
= ust_metadata_channel_statedump(registry
, chan
);
3501 pthread_mutex_unlock(®istry
->lock
);
3502 ERR("Failed to generate channel metadata "
3506 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3508 ret
= ust_metadata_event_statedump(registry
,
3511 pthread_mutex_unlock(®istry
->lock
);
3512 ERR("Failed to generate event metadata "
3518 pthread_mutex_unlock(®istry
->lock
);
3527 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3529 * Ask the consumer to truncate the existing metadata file(s) and
3530 * then regenerate the metadata. Live and per-pid sessions are not
3531 * supported and return an error.
3533 * Return 0 on success or else a LTTNG_ERR code.
3535 int cmd_regenerate_metadata(struct ltt_session
*session
)
3541 ret
= check_regenerate_metadata_support(session
);
3546 if (session
->kernel_session
) {
3547 ret
= kernctl_session_regenerate_metadata(
3548 session
->kernel_session
->fd
);
3550 ERR("Failed to regenerate the kernel metadata");
3555 if (session
->ust_session
) {
3556 ret
= ust_regenerate_metadata(session
->ust_session
);
3558 ERR("Failed to regenerate the UST metadata");
3562 DBG("Cmd metadata regenerate for session %s", session
->name
);
3570 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3572 * Ask the tracer to regenerate a new statedump.
3574 * Return 0 on success or else a LTTNG_ERR code.
3576 int cmd_regenerate_statedump(struct ltt_session
*session
)
3582 if (!session
->active
) {
3583 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3587 if (session
->kernel_session
) {
3588 ret
= kernctl_session_regenerate_statedump(
3589 session
->kernel_session
->fd
);
3591 * Currently, the statedump in kernel can only fail if out
3595 if (ret
== -ENOMEM
) {
3596 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3598 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3600 ERR("Failed to regenerate the kernel statedump");
3605 if (session
->ust_session
) {
3606 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3608 * Currently, the statedump in UST always returns 0.
3611 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3612 ERR("Failed to regenerate the UST statedump");
3616 DBG("Cmd regenerate statedump for session %s", session
->name
);
3623 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3624 struct notification_thread_handle
*notification_thread
)
3628 ssize_t sock_recv_len
;
3629 struct lttng_trigger
*trigger
= NULL
;
3630 struct lttng_buffer_view view
;
3631 struct lttng_dynamic_buffer trigger_buffer
;
3633 lttng_dynamic_buffer_init(&trigger_buffer
);
3634 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3635 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3637 ret
= LTTNG_ERR_NOMEM
;
3641 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3643 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3644 ERR("Failed to receive \"register trigger\" command payload");
3645 /* TODO: should this be a new error enum ? */
3646 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3650 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3651 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3653 ERR("Invalid trigger payload received in \"register trigger\" command");
3654 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3658 ret
= notification_thread_command_register_trigger(notification_thread
,
3660 /* Ownership of trigger was transferred. */
3663 lttng_trigger_destroy(trigger
);
3664 lttng_dynamic_buffer_reset(&trigger_buffer
);
3668 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3669 struct notification_thread_handle
*notification_thread
)
3673 ssize_t sock_recv_len
;
3674 struct lttng_trigger
*trigger
= NULL
;
3675 struct lttng_buffer_view view
;
3676 struct lttng_dynamic_buffer trigger_buffer
;
3678 lttng_dynamic_buffer_init(&trigger_buffer
);
3679 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3680 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3682 ret
= LTTNG_ERR_NOMEM
;
3686 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3688 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3689 ERR("Failed to receive \"unregister trigger\" command payload");
3690 /* TODO: should this be a new error enum ? */
3691 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3695 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3696 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3698 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3699 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3703 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3706 lttng_trigger_destroy(trigger
);
3707 lttng_dynamic_buffer_reset(&trigger_buffer
);
3712 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3713 * snapshot output is *not* set with a remote destination.
3715 * Return 0 on success or a LTTNG_ERR code.
3717 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3718 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3721 struct lttng_ht_iter iter
;
3722 struct consumer_socket
*socket
;
3725 assert(snap_output
);
3728 DBG2("Set relayd object from snapshot output");
3730 /* Ignore if snapshot consumer output is not network. */
3731 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3736 * For each consumer socket, create and send the relayd object of the
3740 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3741 socket
, node
.node
) {
3742 ret
= send_consumer_relayd_sockets(0, session
->id
,
3743 snap_output
->consumer
, socket
,
3744 session
->name
, session
->hostname
,
3745 session
->live_timer
);
3746 if (ret
!= LTTNG_OK
) {
3758 * Record a kernel snapshot.
3760 * Return LTTNG_OK on success or a LTTNG_ERR code.
3762 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3763 struct snapshot_output
*output
, struct ltt_session
*session
,
3764 int wait
, uint64_t nb_packets_per_stream
)
3774 * Copy kernel session sockets so we can communicate with the right
3775 * consumer for the snapshot record command.
3777 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3779 ret
= LTTNG_ERR_NOMEM
;
3783 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3784 if (ret
!= LTTNG_OK
) {
3785 goto error_snapshot
;
3788 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3789 if (ret
!= LTTNG_OK
) {
3790 goto error_snapshot
;
3797 /* Clean up copied sockets so this output can use some other later on. */
3798 consumer_destroy_output_sockets(output
->consumer
);
3805 * Record a UST snapshot.
3807 * Return 0 on success or a LTTNG_ERR error code.
3809 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3810 struct snapshot_output
*output
, struct ltt_session
*session
,
3811 int wait
, uint64_t nb_packets_per_stream
)
3820 * Copy UST session sockets so we can communicate with the right
3821 * consumer for the snapshot record command.
3823 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3825 ret
= LTTNG_ERR_NOMEM
;
3829 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3830 if (ret
!= LTTNG_OK
) {
3831 goto error_snapshot
;
3834 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3838 ret
= LTTNG_ERR_INVALID
;
3841 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3844 goto error_snapshot
;
3850 /* Clean up copied sockets so this output can use some other later on. */
3851 consumer_destroy_output_sockets(output
->consumer
);
3857 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3858 uint64_t cur_nr_packets
)
3860 uint64_t tot_size
= 0;
3862 if (session
->kernel_session
) {
3863 struct ltt_kernel_channel
*chan
;
3864 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3866 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3867 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3869 * Don't take channel into account if we
3870 * already grab all its packets.
3874 tot_size
+= chan
->channel
->attr
.subbuf_size
3875 * chan
->stream_count
;
3879 if (session
->ust_session
) {
3880 struct ltt_ust_session
*usess
= session
->ust_session
;
3882 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3890 * Calculate the number of packets we can grab from each stream that
3891 * fits within the overall snapshot max size.
3893 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3894 * the number of packets per stream.
3896 * TODO: this approach is not perfect: we consider the worse case
3897 * (packet filling the sub-buffers) as an upper bound, but we could do
3898 * better if we do this calculation while we actually grab the packet
3899 * content: we would know how much padding we don't actually store into
3902 * This algorithm is currently bounded by the number of packets per
3905 * Since we call this algorithm before actually grabbing the data, it's
3906 * an approximation: for instance, applications could appear/disappear
3907 * in between this call and actually grabbing data.
3910 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3913 uint64_t cur_nb_packets
= 0;
3916 return 0; /* Infinite */
3919 size_left
= max_size
;
3921 uint64_t one_more_packet_tot_size
;
3923 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3925 if (!one_more_packet_tot_size
) {
3926 /* We are already grabbing all packets. */
3929 size_left
-= one_more_packet_tot_size
;
3930 if (size_left
< 0) {
3935 if (!cur_nb_packets
) {
3936 /* Not enough room to grab one packet of each stream, error. */
3939 return cur_nb_packets
;
3943 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3945 * The wait parameter is ignored so this call always wait for the snapshot to
3946 * complete before returning.
3948 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3950 int cmd_snapshot_record(struct ltt_session
*session
,
3951 struct lttng_snapshot_output
*output
, int wait
)
3954 unsigned int use_tmp_output
= 0;
3955 struct snapshot_output tmp_output
;
3956 unsigned int snapshot_success
= 0;
3962 DBG("Cmd snapshot record for session %s", session
->name
);
3964 /* Get the datetime for the snapshot output directory. */
3965 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3968 ret
= LTTNG_ERR_INVALID
;
3973 * Permission denied to create an output if the session is not
3974 * set in no output mode.
3976 if (session
->output_traces
) {
3977 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3981 /* The session needs to be started at least once. */
3982 if (!session
->has_been_started
) {
3983 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3987 /* Use temporary output for the session. */
3988 if (*output
->ctrl_url
!= '\0') {
3989 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3990 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3993 if (ret
== -ENOMEM
) {
3994 ret
= LTTNG_ERR_NOMEM
;
3996 ret
= LTTNG_ERR_INVALID
;
4000 /* Use the global session count for the temporary snapshot. */
4001 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4003 /* Use the global datetime */
4004 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4008 if (use_tmp_output
) {
4009 int64_t nb_packets_per_stream
;
4011 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4012 tmp_output
.max_size
);
4013 if (nb_packets_per_stream
< 0) {
4014 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
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
) {
4027 if (session
->ust_session
) {
4028 ret
= record_ust_snapshot(session
->ust_session
,
4029 &tmp_output
, session
,
4030 wait
, nb_packets_per_stream
);
4031 if (ret
!= LTTNG_OK
) {
4036 snapshot_success
= 1;
4038 struct snapshot_output
*sout
;
4039 struct lttng_ht_iter iter
;
4042 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4043 &iter
.iter
, sout
, node
.node
) {
4044 int64_t nb_packets_per_stream
;
4047 * Make a local copy of the output and assign the possible
4048 * temporary value given by the caller.
4050 memset(&tmp_output
, 0, sizeof(tmp_output
));
4051 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4053 if (output
->max_size
!= (uint64_t) -1ULL) {
4054 tmp_output
.max_size
= output
->max_size
;
4057 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4058 tmp_output
.max_size
);
4059 if (nb_packets_per_stream
< 0) {
4060 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4065 /* Use temporary name. */
4066 if (*output
->name
!= '\0') {
4067 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4068 sizeof(tmp_output
.name
))) {
4069 ret
= LTTNG_ERR_INVALID
;
4075 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4076 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4078 if (session
->kernel_session
) {
4079 ret
= record_kernel_snapshot(session
->kernel_session
,
4080 &tmp_output
, session
,
4081 wait
, nb_packets_per_stream
);
4082 if (ret
!= LTTNG_OK
) {
4088 if (session
->ust_session
) {
4089 ret
= record_ust_snapshot(session
->ust_session
,
4090 &tmp_output
, session
,
4091 wait
, nb_packets_per_stream
);
4092 if (ret
!= LTTNG_OK
) {
4097 snapshot_success
= 1;
4102 if (snapshot_success
) {
4103 session
->snapshot
.nb_snapshot
++;
4105 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4113 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4115 int cmd_set_session_shm_path(struct ltt_session
*session
,
4116 const char *shm_path
)
4122 * Can only set shm path before session is started.
4124 if (session
->has_been_started
) {
4125 return LTTNG_ERR_SESSION_STARTED
;
4128 strncpy(session
->shm_path
, shm_path
,
4129 sizeof(session
->shm_path
));
4130 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4136 int rename_first_chunk(struct ltt_session
*session
,
4137 struct consumer_output
*consumer
, char *datetime
)
4140 char *tmppath
= NULL
, *tmppath2
= NULL
;
4142 tmppath
= zmalloc(PATH_MAX
* sizeof(char));
4144 ret
= -LTTNG_ERR_NOMEM
;
4147 tmppath2
= zmalloc(PATH_MAX
* sizeof(char));
4149 ret
= -LTTNG_ERR_NOMEM
;
4153 /* Current domain path: <session>/kernel */
4154 snprintf(tmppath
, PATH_MAX
, "%s/%s",
4155 consumer
->dst
.session_root_path
, consumer
->subdir
);
4156 /* New domain path: <session>/<start-date>-/kernel */
4157 snprintf(tmppath2
, PATH_MAX
, "%s/%s-/%s",
4158 consumer
->dst
.session_root_path
, datetime
,
4161 * Move the per-domain folder inside the first rotation
4164 ret
= session_rename_chunk(session
, tmppath
, tmppath2
, 1);
4166 PERROR("Rename first trace directory");
4167 ret
= -LTTNG_ERR_ROTATE_NO_DATA
;
4181 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4183 * Ask the consumer to rotate the session output directory.
4185 * Return 0 on success or else a LTTNG_ERR code.
4187 int cmd_rotate_session(struct ltt_session
*session
,
4188 struct lttng_rotate_session_return
**rotate_return
)
4191 struct tm
*timeinfo
;
4197 *rotate_return
= zmalloc(sizeof(struct lttng_rotate_session_return
));
4198 if (!*rotate_return
) {
4203 if (session
->live_timer
|| session
->snapshot_mode
||
4204 !session
->output_traces
) {
4205 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4209 if (session
->rotate_pending
) {
4210 ret
= -LTTNG_ERR_ROTATE_PENDING
;
4214 /* Special case for the first rotation. */
4215 if (session
->rotate_count
== 0) {
4216 timeinfo
= localtime(&session
->session_start_ts
);
4217 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
4218 /* Either one of the two sessions is enough to get the root path. */
4219 if (session
->kernel_session
) {
4220 snprintf(session
->rotation_chunk
.current_rotate_path
,
4222 session
->kernel_session
->consumer
->dst
.session_root_path
,
4224 } else if (session
->ust_session
) {
4225 snprintf(session
->rotation_chunk
.current_rotate_path
,
4227 session
->ust_session
->consumer
->dst
.session_root_path
,
4234 * Create the first rotation folder to move the existing
4235 * kernel/ust folders into.
4237 ret
= run_as_mkdir_recursive(session
->rotation_chunk
.current_rotate_path
,
4238 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
4240 if (errno
!= EEXIST
) {
4241 ERR("Trace directory creation error");
4242 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4246 if (session
->kernel_session
) {
4247 ret
= rename_first_chunk(session
,
4248 session
->kernel_session
->consumer
,
4254 if (session
->ust_session
) {
4255 ret
= rename_first_chunk(session
,
4256 session
->ust_session
->consumer
,
4264 * The currently active tracing path is now the folder we
4267 snprintf(session
->rotation_chunk
.current_rotate_path
,
4269 session
->rotation_chunk
.active_tracing_path
);
4272 session
->rotate_count
++;
4273 session
->rotate_pending
= 1;
4276 * Create the path name for the next chunk.
4279 if (now
== (time_t) -1) {
4280 ret
= -LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
4284 timeinfo
= localtime(&now
);
4285 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
4286 if (session
->kernel_session
) {
4287 /* The active path for the next rotation/destroy. */
4288 snprintf(session
->rotation_chunk
.active_tracing_path
,
4290 session
->kernel_session
->consumer
->dst
.session_root_path
,
4292 /* The sub-directory for the consumer. */
4293 snprintf(session
->kernel_session
->consumer
->chunk_path
,
4294 PATH_MAX
, "/%s-/%s/", datetime
,
4295 session
->kernel_session
->consumer
->subdir
);
4296 ret
= kernel_rotate_session(session
);
4297 if (ret
!= LTTNG_OK
) {
4301 if (session
->ust_session
) {
4302 snprintf(session
->rotation_chunk
.active_tracing_path
,
4304 session
->ust_session
->consumer
->dst
.session_root_path
,
4306 snprintf(session
->ust_session
->consumer
->chunk_path
,
4307 PATH_MAX
, "/%s-/", datetime
);
4308 ret
= ust_app_rotate_session(session
);
4309 if (ret
!= LTTNG_OK
) {
4314 (*rotate_return
)->rotate_id
= session
->rotate_count
;
4315 (*rotate_return
)->status
= LTTNG_ROTATE_STARTED
;
4317 DBG("Cmd rotate session %s, rotate_id %" PRIu64
, session
->name
,
4318 session
->rotate_count
);
4324 (*rotate_return
)->status
= LTTNG_ROTATE_ERROR
;
4330 * Command LTTNG_ROTATE_PENDING from the lttng-ctl library.
4332 * Check if the session has finished its rotation.
4334 * Return 0 on success or else a LTTNG_ERR code.
4336 int cmd_rotate_pending(struct ltt_session
*session
,
4337 struct lttng_rotate_pending_return
**pending_return
,
4344 DBG("Cmd rotate pending session %s, rotate_id %" PRIu64
, session
->name
,
4345 session
->rotate_count
);
4347 *pending_return
= zmalloc(sizeof(struct lttng_rotate_pending_return
));
4348 if (!*pending_return
) {
4353 if (session
->rotate_count
!= rotate_id
) {
4354 (*pending_return
)->status
= LTTNG_ROTATE_EXPIRED
;
4359 if (session
->rotate_pending
) {
4360 DBG("Session %s, rotate_id %" PRIu64
" still pending",
4361 session
->name
, session
->rotate_count
);
4362 (*pending_return
)->status
= LTTNG_ROTATE_STARTED
;
4364 DBG("Session %s, rotate_id %" PRIu64
" finished",
4365 session
->name
, session
->rotate_count
);
4366 (*pending_return
)->status
= LTTNG_ROTATE_COMPLETED
;
4367 snprintf((*pending_return
)->output_path
, PATH_MAX
, "%s",
4368 session
->rotation_chunk
.current_rotate_path
);
4380 * Init command subsystem.
4385 * Set network sequence index to 1 for streams to match a relayd
4386 * socket on the consumer side.
4388 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4389 relayd_net_seq_idx
= 1;
4390 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4392 DBG("Command subsystem initialized");