2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <urcu/list.h>
22 #include <urcu/uatomic.h>
24 #include <common/defaults.h>
25 #include <common/common.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/relayd/relayd.h>
28 #include <common/utils.h>
29 #include <common/compat/string.h>
34 #include "health-sessiond.h"
36 #include "kernel-consumer.h"
37 #include "lttng-sessiond.h"
45 * Used to keep a unique index for each relayd socket created where this value
46 * is associated with streams on the consumer so it can match the right relayd
47 * to send to. It must be accessed with the relayd_net_seq_idx_lock
50 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 static uint64_t relayd_net_seq_idx
;
53 static int validate_event_name(const char *);
54 static int validate_ust_event_name(const char *);
55 static int cmd_enable_event_internal(struct ltt_session
*session
,
56 struct lttng_domain
*domain
,
57 char *channel_name
, struct lttng_event
*event
,
58 char *filter_expression
,
59 struct lttng_filter_bytecode
*filter
,
60 struct lttng_event_exclusion
*exclusion
,
64 * Create a session path used by list_lttng_sessions for the case that the
65 * session consumer is on the network.
67 static int build_network_session_path(char *dst
, size_t size
,
68 struct ltt_session
*session
)
70 int ret
, kdata_port
, udata_port
;
71 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
72 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
77 memset(tmp_urls
, 0, sizeof(tmp_urls
));
78 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
80 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
82 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
83 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
84 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
87 if (session
->ust_session
&& session
->ust_session
->consumer
) {
88 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
89 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
92 if (uuri
== NULL
&& kuri
== NULL
) {
93 uri
= &session
->consumer
->dst
.net
.control
;
94 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
95 } else if (kuri
&& uuri
) {
96 ret
= uri_compare(kuri
, uuri
);
100 /* Build uuri URL string */
101 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
108 } else if (kuri
&& uuri
== NULL
) {
110 } else if (uuri
&& kuri
== NULL
) {
114 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
120 * Do we have a UST url set. If yes, this means we have both kernel and UST
123 if (*tmp_uurl
!= '\0') {
124 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
125 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
128 if (kuri
|| (!kuri
&& !uuri
)) {
131 /* No kernel URI, use the UST port. */
134 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
142 * Fill lttng_channel array of all channels.
144 static void list_lttng_channels(enum lttng_domain_type domain
,
145 struct ltt_session
*session
, struct lttng_channel
*channels
)
148 struct ltt_kernel_channel
*kchan
;
150 DBG("Listing channels for session %s", session
->name
);
153 case LTTNG_DOMAIN_KERNEL
:
154 /* Kernel channels */
155 if (session
->kernel_session
!= NULL
) {
156 cds_list_for_each_entry(kchan
,
157 &session
->kernel_session
->channel_list
.head
, list
) {
158 /* Copy lttng_channel struct to array */
159 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
160 channels
[i
].enabled
= kchan
->enabled
;
165 case LTTNG_DOMAIN_UST
:
167 struct lttng_ht_iter iter
;
168 struct ltt_ust_channel
*uchan
;
171 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
172 &iter
.iter
, uchan
, node
.node
) {
173 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
174 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
175 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
176 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
177 channels
[i
].attr
.switch_timer_interval
=
178 uchan
->attr
.switch_timer_interval
;
179 channels
[i
].attr
.read_timer_interval
=
180 uchan
->attr
.read_timer_interval
;
181 channels
[i
].enabled
= uchan
->enabled
;
182 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
183 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
184 switch (uchan
->attr
.output
) {
187 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
201 * Create a list of agent domain events.
203 * Return number of events in list on success or else a negative value.
205 static int list_lttng_agent_events(struct agent
*agt
,
206 struct lttng_event
**events
)
209 unsigned int nb_event
= 0;
210 struct agent_event
*event
;
211 struct lttng_event
*tmp_events
;
212 struct lttng_ht_iter iter
;
217 DBG3("Listing agent events");
220 nb_event
= lttng_ht_get_count(agt
->events
);
227 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
229 PERROR("zmalloc agent events session");
230 ret
= -LTTNG_ERR_FATAL
;
235 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
236 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
237 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
238 tmp_events
[i
].enabled
= event
->enabled
;
239 tmp_events
[i
].loglevel
= event
->loglevel_value
;
240 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
245 *events
= tmp_events
;
249 assert(nb_event
== i
);
254 * Create a list of ust global domain events.
256 static int list_lttng_ust_global_events(char *channel_name
,
257 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
260 unsigned int nb_event
= 0;
261 struct lttng_ht_iter iter
;
262 struct lttng_ht_node_str
*node
;
263 struct ltt_ust_channel
*uchan
;
264 struct ltt_ust_event
*uevent
;
265 struct lttng_event
*tmp
;
267 DBG("Listing UST global events for channel %s", channel_name
);
271 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
272 node
= lttng_ht_iter_get_node_str(&iter
);
274 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
278 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
280 nb_event
= lttng_ht_get_count(uchan
->events
);
286 DBG3("Listing UST global %d events", nb_event
);
288 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
290 ret
= LTTNG_ERR_FATAL
;
294 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
295 if (uevent
->internal
) {
296 /* This event should remain hidden from clients */
300 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
301 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
302 tmp
[i
].enabled
= uevent
->enabled
;
304 switch (uevent
->attr
.instrumentation
) {
305 case LTTNG_UST_TRACEPOINT
:
306 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
308 case LTTNG_UST_PROBE
:
309 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
311 case LTTNG_UST_FUNCTION
:
312 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
316 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
317 switch (uevent
->attr
.loglevel_type
) {
318 case LTTNG_UST_LOGLEVEL_ALL
:
319 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
321 case LTTNG_UST_LOGLEVEL_RANGE
:
322 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
324 case LTTNG_UST_LOGLEVEL_SINGLE
:
325 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
328 if (uevent
->filter
) {
331 if (uevent
->exclusion
) {
332 tmp
[i
].exclusion
= 1;
346 * Fill lttng_event array of all kernel events in the channel.
348 static int list_lttng_kernel_events(char *channel_name
,
349 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
352 unsigned int nb_event
;
353 struct ltt_kernel_event
*event
;
354 struct ltt_kernel_channel
*kchan
;
356 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
358 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
362 nb_event
= kchan
->event_count
;
364 DBG("Listing events for channel %s", kchan
->channel
->name
);
371 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
372 if (*events
== NULL
) {
373 ret
= LTTNG_ERR_FATAL
;
377 /* Kernel channels */
378 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
379 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
380 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
381 (*events
)[i
].enabled
= event
->enabled
;
382 (*events
)[i
].filter
=
383 (unsigned char) !!event
->filter_expression
;
385 switch (event
->event
->instrumentation
) {
386 case LTTNG_KERNEL_TRACEPOINT
:
387 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
389 case LTTNG_KERNEL_KRETPROBE
:
390 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
391 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
392 sizeof(struct lttng_kernel_kprobe
));
394 case LTTNG_KERNEL_KPROBE
:
395 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
396 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
397 sizeof(struct lttng_kernel_kprobe
));
399 case LTTNG_KERNEL_FUNCTION
:
400 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
401 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
402 sizeof(struct lttng_kernel_function
));
404 case LTTNG_KERNEL_NOOP
:
405 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
407 case LTTNG_KERNEL_SYSCALL
:
408 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
410 case LTTNG_KERNEL_ALL
:
421 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
433 /* Negate the error code to differentiate the size from an error */
438 * Add URI so the consumer output object. Set the correct path depending on the
439 * domain adding the default trace directory.
441 static int add_uri_to_consumer(struct consumer_output
*consumer
,
442 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
443 const char *session_name
)
446 const char *default_trace_dir
;
450 if (consumer
== NULL
) {
451 DBG("No consumer detected. Don't add URI. Stopping.");
452 ret
= LTTNG_ERR_NO_CONSUMER
;
457 case LTTNG_DOMAIN_KERNEL
:
458 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
460 case LTTNG_DOMAIN_UST
:
461 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
465 * This case is possible is we try to add the URI to the global tracing
466 * session consumer object which in this case there is no subdir.
468 default_trace_dir
= "";
471 switch (uri
->dtype
) {
474 DBG2("Setting network URI to consumer");
476 if (consumer
->type
== CONSUMER_DST_NET
) {
477 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
478 consumer
->dst
.net
.control_isset
) ||
479 (uri
->stype
== LTTNG_STREAM_DATA
&&
480 consumer
->dst
.net
.data_isset
)) {
481 ret
= LTTNG_ERR_URL_EXIST
;
485 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
488 consumer
->type
= CONSUMER_DST_NET
;
490 /* Set URI into consumer output object */
491 ret
= consumer_set_network_uri(consumer
, uri
);
495 } else if (ret
== 1) {
497 * URI was the same in the consumer so we do not append the subdir
498 * again so to not duplicate output dir.
504 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
505 ret
= consumer_set_subdir(consumer
, session_name
);
507 ret
= LTTNG_ERR_FATAL
;
512 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
513 /* On a new subdir, reappend the default trace dir. */
514 strncat(consumer
->subdir
, default_trace_dir
,
515 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
516 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
521 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
522 memset(consumer
->dst
.trace_path
, 0,
523 sizeof(consumer
->dst
.trace_path
));
524 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
525 sizeof(consumer
->dst
.trace_path
));
526 /* Append default trace dir */
527 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
528 sizeof(consumer
->dst
.trace_path
) -
529 strlen(consumer
->dst
.trace_path
) - 1);
530 /* Flag consumer as local. */
531 consumer
->type
= CONSUMER_DST_LOCAL
;
542 * Init tracing by creating trace directory and sending fds kernel consumer.
544 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
547 struct lttng_ht_iter iter
;
548 struct consumer_socket
*socket
;
554 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
555 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
557 pthread_mutex_lock(socket
->lock
);
558 ret
= kernel_consumer_send_session(socket
, session
);
559 pthread_mutex_unlock(socket
->lock
);
561 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
573 * Create a socket to the relayd using the URI.
575 * On success, the relayd_sock pointer is set to the created socket.
576 * Else, it's stays untouched and a lttcomm error code is returned.
578 static int create_connect_relayd(struct lttng_uri
*uri
,
579 struct lttcomm_relayd_sock
**relayd_sock
)
582 struct lttcomm_relayd_sock
*rsock
;
584 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
585 RELAYD_VERSION_COMM_MINOR
);
587 ret
= LTTNG_ERR_FATAL
;
592 * Connect to relayd so we can proceed with a session creation. This call
593 * can possibly block for an arbitrary amount of time to set the health
594 * state to be in poll execution.
597 ret
= relayd_connect(rsock
);
600 ERR("Unable to reach lttng-relayd");
601 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
605 /* Create socket for control stream. */
606 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
607 DBG3("Creating relayd stream socket from URI");
609 /* Check relayd version */
610 ret
= relayd_version_check(rsock
);
612 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
615 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
616 DBG3("Creating relayd data socket from URI");
618 /* Command is not valid */
619 ERR("Relayd invalid stream type: %d", uri
->stype
);
620 ret
= LTTNG_ERR_INVALID
;
624 *relayd_sock
= rsock
;
629 /* The returned value is not useful since we are on an error path. */
630 (void) relayd_close(rsock
);
638 * Connect to the relayd using URI and send the socket to the right consumer.
640 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
641 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
642 struct consumer_output
*consumer
,
643 struct consumer_socket
*consumer_sock
,
644 char *session_name
, char *hostname
, int session_live_timer
)
647 struct lttcomm_relayd_sock
*rsock
= NULL
;
649 /* Connect to relayd and make version check if uri is the control. */
650 ret
= create_connect_relayd(relayd_uri
, &rsock
);
651 if (ret
!= LTTNG_OK
) {
656 /* Set the network sequence index if not set. */
657 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
658 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
660 * Increment net_seq_idx because we are about to transfer the
661 * new relayd socket to the consumer.
662 * Assign unique key so the consumer can match streams.
664 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
665 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
668 /* Send relayd socket to consumer. */
669 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
670 relayd_uri
->stype
, session_id
,
671 session_name
, hostname
, session_live_timer
);
673 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
677 /* Flag that the corresponding socket was sent. */
678 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
679 consumer_sock
->control_sock_sent
= 1;
680 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
681 consumer_sock
->data_sock_sent
= 1;
687 * Close socket which was dup on the consumer side. The session daemon does
688 * NOT keep track of the relayd socket(s) once transfer to the consumer.
692 (void) relayd_close(rsock
);
696 if (ret
!= LTTNG_OK
) {
698 * The consumer output for this session should not be used anymore
699 * since the relayd connection failed thus making any tracing or/and
700 * streaming not usable.
702 consumer
->enabled
= 0;
708 * Send both relayd sockets to a specific consumer and domain. This is a
709 * helper function to facilitate sending the information to the consumer for a
712 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
713 unsigned int session_id
, struct consumer_output
*consumer
,
714 struct consumer_socket
*sock
, char *session_name
,
715 char *hostname
, int session_live_timer
)
722 /* Sending control relayd socket. */
723 if (!sock
->control_sock_sent
) {
724 ret
= send_consumer_relayd_socket(domain
, session_id
,
725 &consumer
->dst
.net
.control
, consumer
, sock
,
726 session_name
, hostname
, session_live_timer
);
727 if (ret
!= LTTNG_OK
) {
732 /* Sending data relayd socket. */
733 if (!sock
->data_sock_sent
) {
734 ret
= send_consumer_relayd_socket(domain
, session_id
,
735 &consumer
->dst
.net
.data
, consumer
, sock
,
736 session_name
, hostname
, session_live_timer
);
737 if (ret
!= LTTNG_OK
) {
747 * Setup relayd connections for a tracing session. First creates the socket to
748 * the relayd and send them to the right domain consumer. Consumer type MUST be
751 int cmd_setup_relayd(struct ltt_session
*session
)
754 struct ltt_ust_session
*usess
;
755 struct ltt_kernel_session
*ksess
;
756 struct consumer_socket
*socket
;
757 struct lttng_ht_iter iter
;
761 usess
= session
->ust_session
;
762 ksess
= session
->kernel_session
;
764 DBG("Setting relayd for session %s", session
->name
);
768 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
769 && usess
->consumer
->enabled
) {
770 /* For each consumer socket, send relayd sockets */
771 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
773 pthread_mutex_lock(socket
->lock
);
774 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
775 usess
->consumer
, socket
,
776 session
->name
, session
->hostname
,
777 session
->live_timer
);
778 pthread_mutex_unlock(socket
->lock
);
779 if (ret
!= LTTNG_OK
) {
782 /* Session is now ready for network streaming. */
783 session
->net_handle
= 1;
787 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
788 && ksess
->consumer
->enabled
) {
789 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
791 pthread_mutex_lock(socket
->lock
);
792 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
793 ksess
->consumer
, socket
,
794 session
->name
, session
->hostname
,
795 session
->live_timer
);
796 pthread_mutex_unlock(socket
->lock
);
797 if (ret
!= LTTNG_OK
) {
800 /* Session is now ready for network streaming. */
801 session
->net_handle
= 1;
811 * Start a kernel session by opening all necessary streams.
813 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
816 struct ltt_kernel_channel
*kchan
;
818 /* Open kernel metadata */
819 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
820 ret
= kernel_open_metadata(ksess
);
822 ret
= LTTNG_ERR_KERN_META_FAIL
;
827 /* Open kernel metadata stream */
828 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
829 ret
= kernel_open_metadata_stream(ksess
);
831 ERR("Kernel create metadata stream failed");
832 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
837 /* For each channel */
838 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
839 if (kchan
->stream_count
== 0) {
840 ret
= kernel_open_channel_stream(kchan
);
842 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
845 /* Update the stream global counter */
846 ksess
->stream_count_global
+= ret
;
850 /* Setup kernel consumer socket and send fds to it */
851 ret
= init_kernel_tracing(ksess
);
853 ret
= LTTNG_ERR_KERN_START_FAIL
;
857 /* This start the kernel tracing */
858 ret
= kernel_start_session(ksess
);
860 ret
= LTTNG_ERR_KERN_START_FAIL
;
864 /* Quiescent wait after starting trace */
865 kernel_wait_quiescent(kernel_tracer_fd
);
876 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
878 int cmd_disable_channel(struct ltt_session
*session
,
879 enum lttng_domain_type domain
, char *channel_name
)
882 struct ltt_ust_session
*usess
;
884 usess
= session
->ust_session
;
889 case LTTNG_DOMAIN_KERNEL
:
891 ret
= channel_kernel_disable(session
->kernel_session
,
893 if (ret
!= LTTNG_OK
) {
897 kernel_wait_quiescent(kernel_tracer_fd
);
900 case LTTNG_DOMAIN_UST
:
902 struct ltt_ust_channel
*uchan
;
903 struct lttng_ht
*chan_ht
;
905 chan_ht
= usess
->domain_global
.channels
;
907 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
909 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
913 ret
= channel_ust_disable(usess
, uchan
);
914 if (ret
!= LTTNG_OK
) {
920 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
932 * Command LTTNG_TRACK_PID processed by the client thread.
934 * Called with session lock held.
936 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
944 case LTTNG_DOMAIN_KERNEL
:
946 struct ltt_kernel_session
*ksess
;
948 ksess
= session
->kernel_session
;
950 ret
= kernel_track_pid(ksess
, pid
);
951 if (ret
!= LTTNG_OK
) {
955 kernel_wait_quiescent(kernel_tracer_fd
);
958 case LTTNG_DOMAIN_UST
:
960 struct ltt_ust_session
*usess
;
962 usess
= session
->ust_session
;
964 ret
= trace_ust_track_pid(usess
, pid
);
965 if (ret
!= LTTNG_OK
) {
971 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
983 * Command LTTNG_UNTRACK_PID processed by the client thread.
985 * Called with session lock held.
987 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
995 case LTTNG_DOMAIN_KERNEL
:
997 struct ltt_kernel_session
*ksess
;
999 ksess
= session
->kernel_session
;
1001 ret
= kernel_untrack_pid(ksess
, pid
);
1002 if (ret
!= LTTNG_OK
) {
1006 kernel_wait_quiescent(kernel_tracer_fd
);
1009 case LTTNG_DOMAIN_UST
:
1011 struct ltt_ust_session
*usess
;
1013 usess
= session
->ust_session
;
1015 ret
= trace_ust_untrack_pid(usess
, pid
);
1016 if (ret
!= LTTNG_OK
) {
1022 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1034 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1036 * The wpipe arguments is used as a notifier for the kernel thread.
1038 int cmd_enable_channel(struct ltt_session
*session
,
1039 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1042 struct ltt_ust_session
*usess
= session
->ust_session
;
1043 struct lttng_ht
*chan_ht
;
1050 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1052 /* Validate channel name */
1053 if (attr
->name
[0] == '.' ||
1054 memchr(attr
->name
, '/', len
) != NULL
) {
1055 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1059 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1064 * Don't try to enable a channel if the session has been started at
1065 * some point in time before. The tracer does not allow it.
1067 if (session
->has_been_started
) {
1068 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1073 * If the session is a live session, remove the switch timer, the
1074 * live timer does the same thing but sends also synchronisation
1075 * beacons for inactive streams.
1077 if (session
->live_timer
> 0) {
1078 attr
->attr
.live_timer_interval
= session
->live_timer
;
1079 attr
->attr
.switch_timer_interval
= 0;
1083 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1084 * mode and with less than 2 subbuf so block it right away and send back an
1085 * invalid attribute error.
1087 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1088 ret
= LTTNG_ERR_INVALID
;
1092 switch (domain
->type
) {
1093 case LTTNG_DOMAIN_KERNEL
:
1095 struct ltt_kernel_channel
*kchan
;
1097 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1098 session
->kernel_session
);
1099 if (kchan
== NULL
) {
1100 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1101 if (attr
->name
[0] != '\0') {
1102 session
->kernel_session
->has_non_default_channel
= 1;
1105 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1108 if (ret
!= LTTNG_OK
) {
1112 kernel_wait_quiescent(kernel_tracer_fd
);
1115 case LTTNG_DOMAIN_UST
:
1116 case LTTNG_DOMAIN_JUL
:
1117 case LTTNG_DOMAIN_LOG4J
:
1118 case LTTNG_DOMAIN_PYTHON
:
1120 struct ltt_ust_channel
*uchan
;
1125 * Current agent implementation limitations force us to allow
1126 * only one channel at once in "agent" subdomains. Each
1127 * subdomain has a default channel name which must be strictly
1130 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1131 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1132 LTTNG_SYMBOL_NAME_LEN
)) {
1133 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1136 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1137 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1138 LTTNG_SYMBOL_NAME_LEN
)) {
1139 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1142 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1143 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1144 LTTNG_SYMBOL_NAME_LEN
)) {
1145 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1150 chan_ht
= usess
->domain_global
.channels
;
1152 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1153 if (uchan
== NULL
) {
1154 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1155 if (attr
->name
[0] != '\0') {
1156 usess
->has_non_default_channel
= 1;
1159 ret
= channel_ust_enable(usess
, uchan
);
1164 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1175 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1177 int cmd_disable_event(struct ltt_session
*session
,
1178 enum lttng_domain_type domain
, char *channel_name
,
1179 struct lttng_event
*event
)
1184 DBG("Disable event command for event \'%s\'", event
->name
);
1186 event_name
= event
->name
;
1187 if (validate_event_name(event_name
)) {
1188 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1192 /* Error out on unhandled search criteria */
1193 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1194 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1195 ret
= LTTNG_ERR_UNK
;
1202 case LTTNG_DOMAIN_KERNEL
:
1204 struct ltt_kernel_channel
*kchan
;
1205 struct ltt_kernel_session
*ksess
;
1207 ksess
= session
->kernel_session
;
1210 * If a non-default channel has been created in the
1211 * session, explicitely require that -c chan_name needs
1214 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1215 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1219 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1220 if (kchan
== NULL
) {
1221 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1225 switch (event
->type
) {
1226 case LTTNG_EVENT_ALL
:
1227 case LTTNG_EVENT_TRACEPOINT
:
1228 case LTTNG_EVENT_SYSCALL
:
1229 case LTTNG_EVENT_PROBE
:
1230 case LTTNG_EVENT_FUNCTION
:
1231 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1232 if (event_name
[0] == '\0') {
1233 ret
= event_kernel_disable_event(kchan
,
1236 ret
= event_kernel_disable_event(kchan
,
1237 event_name
, event
->type
);
1239 if (ret
!= LTTNG_OK
) {
1244 ret
= LTTNG_ERR_UNK
;
1248 kernel_wait_quiescent(kernel_tracer_fd
);
1251 case LTTNG_DOMAIN_UST
:
1253 struct ltt_ust_channel
*uchan
;
1254 struct ltt_ust_session
*usess
;
1256 usess
= session
->ust_session
;
1258 if (validate_ust_event_name(event_name
)) {
1259 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1264 * If a non-default channel has been created in the
1265 * session, explicitly require that -c chan_name needs
1268 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1269 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1273 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1275 if (uchan
== NULL
) {
1276 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1280 switch (event
->type
) {
1281 case LTTNG_EVENT_ALL
:
1283 * An empty event name means that everything
1284 * should be disabled.
1286 if (event
->name
[0] == '\0') {
1287 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1289 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1292 if (ret
!= LTTNG_OK
) {
1297 ret
= LTTNG_ERR_UNK
;
1301 DBG3("Disable UST event %s in channel %s completed", event_name
,
1305 case LTTNG_DOMAIN_LOG4J
:
1306 case LTTNG_DOMAIN_JUL
:
1307 case LTTNG_DOMAIN_PYTHON
:
1310 struct ltt_ust_session
*usess
= session
->ust_session
;
1314 switch (event
->type
) {
1315 case LTTNG_EVENT_ALL
:
1318 ret
= LTTNG_ERR_UNK
;
1322 agt
= trace_ust_find_agent(usess
, domain
);
1324 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1328 * An empty event name means that everything
1329 * should be disabled.
1331 if (event
->name
[0] == '\0') {
1332 ret
= event_agent_disable_all(usess
, agt
);
1334 ret
= event_agent_disable(usess
, agt
, event_name
);
1336 if (ret
!= LTTNG_OK
) {
1343 ret
= LTTNG_ERR_UND
;
1356 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1358 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1359 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1361 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1364 case LTTNG_DOMAIN_KERNEL
:
1365 assert(session
->kernel_session
);
1367 if (session
->kernel_session
->channel_count
== 0) {
1368 /* Create default channel */
1369 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1370 if (ret
!= LTTNG_OK
) {
1373 chan_kern_created
= 1;
1375 /* Add kernel context to kernel tracer */
1376 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1377 if (ret
!= LTTNG_OK
) {
1381 case LTTNG_DOMAIN_UST
:
1383 struct ltt_ust_session
*usess
= session
->ust_session
;
1384 unsigned int chan_count
;
1388 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1389 if (chan_count
== 0) {
1390 struct lttng_channel
*attr
;
1391 /* Create default channel */
1392 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1394 ret
= LTTNG_ERR_FATAL
;
1398 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1399 if (ret
!= LTTNG_OK
) {
1404 chan_ust_created
= 1;
1407 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1408 if (ret
!= LTTNG_OK
) {
1414 ret
= LTTNG_ERR_UND
;
1421 if (chan_kern_created
) {
1422 struct ltt_kernel_channel
*kchan
=
1423 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1424 session
->kernel_session
);
1425 /* Created previously, this should NOT fail. */
1427 kernel_destroy_channel(kchan
);
1430 if (chan_ust_created
) {
1431 struct ltt_ust_channel
*uchan
=
1432 trace_ust_find_channel_by_name(
1433 session
->ust_session
->domain_global
.channels
,
1434 DEFAULT_CHANNEL_NAME
);
1435 /* Created previously, this should NOT fail. */
1437 /* Remove from the channel list of the session. */
1438 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1440 trace_ust_destroy_channel(uchan
);
1445 static int validate_event_name(const char *name
)
1448 const char *c
= name
;
1449 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1450 bool null_terminated
= false;
1453 * Make sure that unescaped wildcards are only used as the last
1454 * character of the event name.
1456 while (c
< event_name_end
) {
1459 null_terminated
= true;
1465 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1466 /* Wildcard is not the last character */
1467 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1476 if (!ret
&& !null_terminated
) {
1477 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1482 static inline bool name_starts_with(const char *name
, const char *prefix
)
1484 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1486 return !strncmp(name
, prefix
, max_cmp_len
);
1489 /* Perform userspace-specific event name validation */
1490 static int validate_ust_event_name(const char *name
)
1500 * Check name against all internal UST event component namespaces used
1503 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1504 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1505 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1514 * Internal version of cmd_enable_event() with a supplemental
1515 * "internal_event" flag which is used to enable internal events which should
1516 * be hidden from clients. Such events are used in the agent implementation to
1517 * enable the events through which all "agent" events are funeled.
1519 static int _cmd_enable_event(struct ltt_session
*session
,
1520 struct lttng_domain
*domain
,
1521 char *channel_name
, struct lttng_event
*event
,
1522 char *filter_expression
,
1523 struct lttng_filter_bytecode
*filter
,
1524 struct lttng_event_exclusion
*exclusion
,
1525 int wpipe
, bool internal_event
)
1527 int ret
, channel_created
= 0;
1528 struct lttng_channel
*attr
;
1532 assert(channel_name
);
1534 /* If we have a filter, we must have its filter expression */
1535 assert(!(!!filter_expression
^ !!filter
));
1537 DBG("Enable event command for event \'%s\'", event
->name
);
1541 ret
= validate_event_name(event
->name
);
1546 switch (domain
->type
) {
1547 case LTTNG_DOMAIN_KERNEL
:
1549 struct ltt_kernel_channel
*kchan
;
1552 * If a non-default channel has been created in the
1553 * session, explicitely require that -c chan_name needs
1556 if (session
->kernel_session
->has_non_default_channel
1557 && channel_name
[0] == '\0') {
1558 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1562 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1563 session
->kernel_session
);
1564 if (kchan
== NULL
) {
1565 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1566 LTTNG_BUFFER_GLOBAL
);
1568 ret
= LTTNG_ERR_FATAL
;
1571 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1573 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1574 if (ret
!= LTTNG_OK
) {
1580 channel_created
= 1;
1583 /* Get the newly created kernel channel pointer */
1584 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1585 session
->kernel_session
);
1586 if (kchan
== NULL
) {
1587 /* This sould not happen... */
1588 ret
= LTTNG_ERR_FATAL
;
1592 switch (event
->type
) {
1593 case LTTNG_EVENT_ALL
:
1595 char *filter_expression_a
= NULL
;
1596 struct lttng_filter_bytecode
*filter_a
= NULL
;
1599 * We need to duplicate filter_expression and filter,
1600 * because ownership is passed to first enable
1603 if (filter_expression
) {
1604 filter_expression_a
= strdup(filter_expression
);
1605 if (!filter_expression_a
) {
1606 ret
= LTTNG_ERR_FATAL
;
1611 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1613 free(filter_expression_a
);
1614 ret
= LTTNG_ERR_FATAL
;
1617 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1619 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1620 ret
= event_kernel_enable_event(kchan
, event
,
1621 filter_expression
, filter
);
1622 /* We have passed ownership */
1623 filter_expression
= NULL
;
1625 if (ret
!= LTTNG_OK
) {
1626 if (channel_created
) {
1627 /* Let's not leak a useless channel. */
1628 kernel_destroy_channel(kchan
);
1630 free(filter_expression_a
);
1634 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1635 ret
= event_kernel_enable_event(kchan
, event
,
1636 filter_expression_a
, filter_a
);
1637 /* We have passed ownership */
1638 filter_expression_a
= NULL
;
1640 if (ret
!= LTTNG_OK
) {
1645 case LTTNG_EVENT_PROBE
:
1646 case LTTNG_EVENT_FUNCTION
:
1647 case LTTNG_EVENT_FUNCTION_ENTRY
:
1648 case LTTNG_EVENT_TRACEPOINT
:
1649 ret
= event_kernel_enable_event(kchan
, event
,
1650 filter_expression
, filter
);
1651 /* We have passed ownership */
1652 filter_expression
= NULL
;
1654 if (ret
!= LTTNG_OK
) {
1655 if (channel_created
) {
1656 /* Let's not leak a useless channel. */
1657 kernel_destroy_channel(kchan
);
1662 case LTTNG_EVENT_SYSCALL
:
1663 ret
= event_kernel_enable_event(kchan
, event
,
1664 filter_expression
, filter
);
1665 /* We have passed ownership */
1666 filter_expression
= NULL
;
1668 if (ret
!= LTTNG_OK
) {
1673 ret
= LTTNG_ERR_UNK
;
1677 kernel_wait_quiescent(kernel_tracer_fd
);
1680 case LTTNG_DOMAIN_UST
:
1682 struct ltt_ust_channel
*uchan
;
1683 struct ltt_ust_session
*usess
= session
->ust_session
;
1688 * If a non-default channel has been created in the
1689 * session, explicitely require that -c chan_name needs
1692 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1693 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1697 /* Get channel from global UST domain */
1698 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1700 if (uchan
== NULL
) {
1701 /* Create default channel */
1702 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1703 usess
->buffer_type
);
1705 ret
= LTTNG_ERR_FATAL
;
1708 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1710 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1711 if (ret
!= LTTNG_OK
) {
1717 /* Get the newly created channel reference back */
1718 uchan
= trace_ust_find_channel_by_name(
1719 usess
->domain_global
.channels
, channel_name
);
1723 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1725 * Don't allow users to add UST events to channels which
1726 * are assigned to a userspace subdomain (JUL, Log4J,
1729 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
1733 if (!internal_event
) {
1735 * Ensure the event name is not reserved for internal
1738 ret
= validate_ust_event_name(event
->name
);
1740 WARN("Userspace event name %s failed validation.",
1742 event
->name
: "NULL");
1743 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1748 /* At this point, the session and channel exist on the tracer */
1749 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1750 filter_expression
, filter
, exclusion
,
1752 /* We have passed ownership */
1753 filter_expression
= NULL
;
1756 if (ret
!= LTTNG_OK
) {
1761 case LTTNG_DOMAIN_LOG4J
:
1762 case LTTNG_DOMAIN_JUL
:
1763 case LTTNG_DOMAIN_PYTHON
:
1765 const char *default_event_name
, *default_chan_name
;
1767 struct lttng_event uevent
;
1768 struct lttng_domain tmp_dom
;
1769 struct ltt_ust_session
*usess
= session
->ust_session
;
1773 agt
= trace_ust_find_agent(usess
, domain
->type
);
1775 agt
= agent_create(domain
->type
);
1777 ret
= LTTNG_ERR_NOMEM
;
1780 agent_add(agt
, usess
->agents
);
1783 /* Create the default tracepoint. */
1784 memset(&uevent
, 0, sizeof(uevent
));
1785 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1786 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1787 default_event_name
= event_get_default_agent_ust_name(
1789 if (!default_event_name
) {
1790 ret
= LTTNG_ERR_FATAL
;
1793 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1794 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1797 * The domain type is changed because we are about to enable the
1798 * default channel and event for the JUL domain that are hardcoded.
1799 * This happens in the UST domain.
1801 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1802 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1804 switch (domain
->type
) {
1805 case LTTNG_DOMAIN_LOG4J
:
1806 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1808 case LTTNG_DOMAIN_JUL
:
1809 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1811 case LTTNG_DOMAIN_PYTHON
:
1812 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1815 /* The switch/case we are in makes this impossible */
1820 char *filter_expression_copy
= NULL
;
1821 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1824 const size_t filter_size
= sizeof(
1825 struct lttng_filter_bytecode
)
1828 filter_copy
= zmalloc(filter_size
);
1830 ret
= LTTNG_ERR_NOMEM
;
1833 memcpy(filter_copy
, filter
, filter_size
);
1835 filter_expression_copy
=
1836 strdup(filter_expression
);
1837 if (!filter_expression
) {
1838 ret
= LTTNG_ERR_NOMEM
;
1841 if (!filter_expression_copy
|| !filter_copy
) {
1842 free(filter_expression_copy
);
1848 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1849 (char *) default_chan_name
,
1850 &uevent
, filter_expression_copy
,
1851 filter_copy
, NULL
, wpipe
);
1854 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1858 /* The wild card * means that everything should be enabled. */
1859 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1860 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1863 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1867 filter_expression
= NULL
;
1868 if (ret
!= LTTNG_OK
) {
1875 ret
= LTTNG_ERR_UND
;
1882 free(filter_expression
);
1890 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1891 * We own filter, exclusion, and filter_expression.
1893 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1894 char *channel_name
, struct lttng_event
*event
,
1895 char *filter_expression
,
1896 struct lttng_filter_bytecode
*filter
,
1897 struct lttng_event_exclusion
*exclusion
,
1900 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1901 filter_expression
, filter
, exclusion
, wpipe
, false);
1905 * Enable an event which is internal to LTTng. An internal should
1906 * never be made visible to clients and are immune to checks such as
1909 static int cmd_enable_event_internal(struct ltt_session
*session
,
1910 struct lttng_domain
*domain
,
1911 char *channel_name
, struct lttng_event
*event
,
1912 char *filter_expression
,
1913 struct lttng_filter_bytecode
*filter
,
1914 struct lttng_event_exclusion
*exclusion
,
1917 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1918 filter_expression
, filter
, exclusion
, wpipe
, true);
1922 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1924 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
1925 struct lttng_event
**events
)
1928 ssize_t nb_events
= 0;
1931 case LTTNG_DOMAIN_KERNEL
:
1932 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1933 if (nb_events
< 0) {
1934 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1938 case LTTNG_DOMAIN_UST
:
1939 nb_events
= ust_app_list_events(events
);
1940 if (nb_events
< 0) {
1941 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1945 case LTTNG_DOMAIN_LOG4J
:
1946 case LTTNG_DOMAIN_JUL
:
1947 case LTTNG_DOMAIN_PYTHON
:
1948 nb_events
= agent_list_events(events
, domain
);
1949 if (nb_events
< 0) {
1950 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1955 ret
= LTTNG_ERR_UND
;
1962 /* Return negative value to differentiate return code */
1967 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1969 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
1970 struct lttng_event_field
**fields
)
1973 ssize_t nb_fields
= 0;
1976 case LTTNG_DOMAIN_UST
:
1977 nb_fields
= ust_app_list_event_fields(fields
);
1978 if (nb_fields
< 0) {
1979 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1983 case LTTNG_DOMAIN_KERNEL
:
1984 default: /* fall-through */
1985 ret
= LTTNG_ERR_UND
;
1992 /* Return negative value to differentiate return code */
1996 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1998 return syscall_table_list(events
);
2002 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2004 * Called with session lock held.
2006 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2007 enum lttng_domain_type domain
, int32_t **pids
)
2010 ssize_t nr_pids
= 0;
2013 case LTTNG_DOMAIN_KERNEL
:
2015 struct ltt_kernel_session
*ksess
;
2017 ksess
= session
->kernel_session
;
2018 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2020 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2025 case LTTNG_DOMAIN_UST
:
2027 struct ltt_ust_session
*usess
;
2029 usess
= session
->ust_session
;
2030 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2032 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2037 case LTTNG_DOMAIN_LOG4J
:
2038 case LTTNG_DOMAIN_JUL
:
2039 case LTTNG_DOMAIN_PYTHON
:
2041 ret
= LTTNG_ERR_UND
;
2048 /* Return negative value to differentiate return code */
2053 * Command LTTNG_START_TRACE processed by the client thread.
2055 * Called with session mutex held.
2057 int cmd_start_trace(struct ltt_session
*session
)
2060 unsigned long nb_chan
= 0;
2061 struct ltt_kernel_session
*ksession
;
2062 struct ltt_ust_session
*usess
;
2066 /* Ease our life a bit ;) */
2067 ksession
= session
->kernel_session
;
2068 usess
= session
->ust_session
;
2070 /* Is the session already started? */
2071 if (session
->active
) {
2072 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2077 * Starting a session without channel is useless since after that it's not
2078 * possible to enable channel thus inform the client.
2080 if (usess
&& usess
->domain_global
.channels
) {
2081 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2084 nb_chan
+= ksession
->channel_count
;
2087 ret
= LTTNG_ERR_NO_CHANNEL
;
2091 /* Kernel tracing */
2092 if (ksession
!= NULL
) {
2093 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2094 if (ret
!= LTTNG_OK
) {
2099 /* Flag session that trace should start automatically */
2102 * Even though the start trace might fail, flag this session active so
2103 * other application coming in are started by default.
2107 ret
= ust_app_start_trace_all(usess
);
2109 ret
= LTTNG_ERR_UST_START_FAIL
;
2114 /* Flag this after a successful start. */
2115 session
->has_been_started
= 1;
2116 session
->active
= 1;
2125 * Command LTTNG_STOP_TRACE processed by the client thread.
2127 int cmd_stop_trace(struct ltt_session
*session
)
2130 struct ltt_kernel_channel
*kchan
;
2131 struct ltt_kernel_session
*ksession
;
2132 struct ltt_ust_session
*usess
;
2137 ksession
= session
->kernel_session
;
2138 usess
= session
->ust_session
;
2140 /* Session is not active. Skip everythong and inform the client. */
2141 if (!session
->active
) {
2142 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2147 if (ksession
&& ksession
->active
) {
2148 DBG("Stop kernel tracing");
2150 /* Flush metadata if exist */
2151 if (ksession
->metadata_stream_fd
>= 0) {
2152 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2154 ERR("Kernel metadata flush failed");
2158 /* Flush all buffers before stopping */
2159 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2160 ret
= kernel_flush_buffer(kchan
);
2162 ERR("Kernel flush buffer error");
2166 ret
= kernel_stop_session(ksession
);
2168 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2172 kernel_wait_quiescent(kernel_tracer_fd
);
2174 ksession
->active
= 0;
2177 if (usess
&& usess
->active
) {
2179 * Even though the stop trace might fail, flag this session inactive so
2180 * other application coming in are not started by default.
2184 ret
= ust_app_stop_trace_all(usess
);
2186 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2191 /* Flag inactive after a successful stop. */
2192 session
->active
= 0;
2200 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2202 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2203 struct lttng_uri
*uris
)
2206 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2207 struct ltt_ust_session
*usess
= session
->ust_session
;
2213 /* Can't set consumer URI if the session is active. */
2214 if (session
->active
) {
2215 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2219 /* Set the "global" consumer URIs */
2220 for (i
= 0; i
< nb_uri
; i
++) {
2221 ret
= add_uri_to_consumer(session
->consumer
,
2222 &uris
[i
], 0, session
->name
);
2223 if (ret
!= LTTNG_OK
) {
2228 /* Set UST session URIs */
2229 if (session
->ust_session
) {
2230 for (i
= 0; i
< nb_uri
; i
++) {
2231 ret
= add_uri_to_consumer(
2232 session
->ust_session
->consumer
,
2233 &uris
[i
], LTTNG_DOMAIN_UST
,
2235 if (ret
!= LTTNG_OK
) {
2241 /* Set kernel session URIs */
2242 if (session
->kernel_session
) {
2243 for (i
= 0; i
< nb_uri
; i
++) {
2244 ret
= add_uri_to_consumer(
2245 session
->kernel_session
->consumer
,
2246 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2248 if (ret
!= LTTNG_OK
) {
2255 * Make sure to set the session in output mode after we set URI since a
2256 * session can be created without URL (thus flagged in no output mode).
2258 session
->output_traces
= 1;
2260 ksess
->output_traces
= 1;
2264 usess
->output_traces
= 1;
2275 * Command LTTNG_CREATE_SESSION processed by the client thread.
2277 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2278 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2281 struct ltt_session
*session
;
2287 * Verify if the session already exist
2289 * XXX: There is no need for the session lock list here since the caller
2290 * (process_client_msg) is holding it. We might want to change that so a
2291 * single command does not lock the entire session list.
2293 session
= session_find_by_name(name
);
2294 if (session
!= NULL
) {
2295 ret
= LTTNG_ERR_EXIST_SESS
;
2299 /* Create tracing session in the registry */
2300 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2301 LTTNG_SOCK_GET_GID_CRED(creds
));
2302 if (ret
!= LTTNG_OK
) {
2307 * Get the newly created session pointer back
2309 * XXX: There is no need for the session lock list here since the caller
2310 * (process_client_msg) is holding it. We might want to change that so a
2311 * single command does not lock the entire session list.
2313 session
= session_find_by_name(name
);
2316 session
->live_timer
= live_timer
;
2317 /* Create default consumer output for the session not yet created. */
2318 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2319 if (session
->consumer
== NULL
) {
2320 ret
= LTTNG_ERR_FATAL
;
2321 goto consumer_error
;
2325 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2326 if (ret
!= LTTNG_OK
) {
2327 goto consumer_error
;
2329 session
->output_traces
= 1;
2331 session
->output_traces
= 0;
2332 DBG2("Session %s created with no output", session
->name
);
2335 session
->consumer
->enabled
= 1;
2340 session_destroy(session
);
2347 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2349 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2350 size_t nb_uri
, lttng_sock_cred
*creds
)
2353 struct ltt_session
*session
;
2354 struct snapshot_output
*new_output
= NULL
;
2360 * Create session in no output mode with URIs set to NULL. The uris we've
2361 * received are for a default snapshot output if one.
2363 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2364 if (ret
!= LTTNG_OK
) {
2368 /* Get the newly created session pointer back. This should NEVER fail. */
2369 session
= session_find_by_name(name
);
2372 /* Flag session for snapshot mode. */
2373 session
->snapshot_mode
= 1;
2375 /* Skip snapshot output creation if no URI is given. */
2380 new_output
= snapshot_output_alloc();
2382 ret
= LTTNG_ERR_NOMEM
;
2383 goto error_snapshot_alloc
;
2386 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2387 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2389 if (ret
== -ENOMEM
) {
2390 ret
= LTTNG_ERR_NOMEM
;
2392 ret
= LTTNG_ERR_INVALID
;
2394 goto error_snapshot
;
2398 snapshot_add_output(&session
->snapshot
, new_output
);
2405 snapshot_output_destroy(new_output
);
2406 error_snapshot_alloc
:
2407 session_destroy(session
);
2413 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2415 * Called with session lock held.
2417 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2420 struct ltt_ust_session
*usess
;
2421 struct ltt_kernel_session
*ksess
;
2426 usess
= session
->ust_session
;
2427 ksess
= session
->kernel_session
;
2429 /* Clean kernel session teardown */
2430 kernel_destroy_session(ksess
);
2432 /* UST session teardown */
2434 /* Close any relayd session */
2435 consumer_output_send_destroy_relayd(usess
->consumer
);
2437 /* Destroy every UST application related to this session. */
2438 ret
= ust_app_destroy_trace_all(usess
);
2440 ERR("Error in ust_app_destroy_trace_all");
2443 /* Clean up the rest. */
2444 trace_ust_destroy_session(usess
);
2448 * Must notify the kernel thread here to update it's poll set in order to
2449 * remove the channel(s)' fd just destroyed.
2451 ret
= notify_thread_pipe(wpipe
);
2453 PERROR("write kernel poll pipe");
2456 ret
= session_destroy(session
);
2462 * Command LTTNG_CALIBRATE processed by the client thread.
2464 int cmd_calibrate(enum lttng_domain_type domain
,
2465 struct lttng_calibrate
*calibrate
)
2470 case LTTNG_DOMAIN_KERNEL
:
2472 struct lttng_kernel_calibrate kcalibrate
;
2474 switch (calibrate
->type
) {
2475 case LTTNG_CALIBRATE_FUNCTION
:
2477 /* Default and only possible calibrate option. */
2478 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2482 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2484 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2489 case LTTNG_DOMAIN_UST
:
2491 struct lttng_ust_calibrate ucalibrate
;
2493 switch (calibrate
->type
) {
2494 case LTTNG_CALIBRATE_FUNCTION
:
2496 /* Default and only possible calibrate option. */
2497 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2501 ret
= ust_app_calibrate_glb(&ucalibrate
);
2503 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2509 ret
= LTTNG_ERR_UND
;
2520 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2522 int cmd_register_consumer(struct ltt_session
*session
,
2523 enum lttng_domain_type domain
, const char *sock_path
,
2524 struct consumer_data
*cdata
)
2527 struct consumer_socket
*socket
= NULL
;
2534 case LTTNG_DOMAIN_KERNEL
:
2536 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2540 /* Can't register a consumer if there is already one */
2541 if (ksess
->consumer_fds_sent
!= 0) {
2542 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2546 sock
= lttcomm_connect_unix_sock(sock_path
);
2548 ret
= LTTNG_ERR_CONNECT_FAIL
;
2551 cdata
->cmd_sock
= sock
;
2553 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2554 if (socket
== NULL
) {
2557 PERROR("close register consumer");
2559 cdata
->cmd_sock
= -1;
2560 ret
= LTTNG_ERR_FATAL
;
2564 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2565 if (socket
->lock
== NULL
) {
2566 PERROR("zmalloc pthread mutex");
2567 ret
= LTTNG_ERR_FATAL
;
2570 pthread_mutex_init(socket
->lock
, NULL
);
2571 socket
->registered
= 1;
2574 consumer_add_socket(socket
, ksess
->consumer
);
2577 pthread_mutex_lock(&cdata
->pid_mutex
);
2579 pthread_mutex_unlock(&cdata
->pid_mutex
);
2584 /* TODO: Userspace tracing */
2585 ret
= LTTNG_ERR_UND
;
2593 consumer_destroy_socket(socket
);
2599 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2601 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2602 struct lttng_domain
**domains
)
2607 struct lttng_ht_iter iter
;
2609 if (session
->kernel_session
!= NULL
) {
2610 DBG3("Listing domains found kernel domain");
2614 if (session
->ust_session
!= NULL
) {
2615 DBG3("Listing domains found UST global domain");
2619 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2621 if (agt
->being_used
) {
2632 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2633 if (*domains
== NULL
) {
2634 ret
= LTTNG_ERR_FATAL
;
2638 if (session
->kernel_session
!= NULL
) {
2639 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2641 /* Kernel session buffer type is always GLOBAL */
2642 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2647 if (session
->ust_session
!= NULL
) {
2648 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2649 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2653 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2655 if (agt
->being_used
) {
2656 (*domains
)[index
].type
= agt
->domain
;
2657 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2667 /* Return negative value to differentiate return code */
2673 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2675 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2676 struct ltt_session
*session
, struct lttng_channel
**channels
)
2679 ssize_t nb_chan
= 0;
2682 case LTTNG_DOMAIN_KERNEL
:
2683 if (session
->kernel_session
!= NULL
) {
2684 nb_chan
= session
->kernel_session
->channel_count
;
2686 DBG3("Number of kernel channels %zd", nb_chan
);
2688 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2691 case LTTNG_DOMAIN_UST
:
2692 if (session
->ust_session
!= NULL
) {
2694 nb_chan
= lttng_ht_get_count(
2695 session
->ust_session
->domain_global
.channels
);
2698 DBG3("Number of UST global channels %zd", nb_chan
);
2700 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2705 ret
= LTTNG_ERR_UND
;
2710 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2711 if (*channels
== NULL
) {
2712 ret
= LTTNG_ERR_FATAL
;
2716 list_lttng_channels(domain
, session
, *channels
);
2722 /* Return negative value to differentiate return code */
2727 * Command LTTNG_LIST_EVENTS processed by the client thread.
2729 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2730 struct ltt_session
*session
, char *channel_name
,
2731 struct lttng_event
**events
)
2734 ssize_t nb_event
= 0;
2737 case LTTNG_DOMAIN_KERNEL
:
2738 if (session
->kernel_session
!= NULL
) {
2739 nb_event
= list_lttng_kernel_events(channel_name
,
2740 session
->kernel_session
, events
);
2743 case LTTNG_DOMAIN_UST
:
2745 if (session
->ust_session
!= NULL
) {
2746 nb_event
= list_lttng_ust_global_events(channel_name
,
2747 &session
->ust_session
->domain_global
, events
);
2751 case LTTNG_DOMAIN_LOG4J
:
2752 case LTTNG_DOMAIN_JUL
:
2753 case LTTNG_DOMAIN_PYTHON
:
2754 if (session
->ust_session
) {
2755 struct lttng_ht_iter iter
;
2759 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2760 &iter
.iter
, agt
, node
.node
) {
2761 if (agt
->domain
== domain
) {
2762 nb_event
= list_lttng_agent_events(
2771 ret
= LTTNG_ERR_UND
;
2778 /* Return negative value to differentiate return code */
2783 * Using the session list, filled a lttng_session array to send back to the
2784 * client for session listing.
2786 * The session list lock MUST be acquired before calling this function. Use
2787 * session_lock_list() and session_unlock_list().
2789 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2794 struct ltt_session
*session
;
2795 struct ltt_session_list
*list
= session_get_list();
2797 DBG("Getting all available session for UID %d GID %d",
2800 * Iterate over session list and append data after the control struct in
2803 cds_list_for_each_entry(session
, &list
->head
, list
) {
2805 * Only list the sessions the user can control.
2807 if (!session_access_ok(session
, uid
, gid
)) {
2811 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2812 struct ltt_ust_session
*usess
= session
->ust_session
;
2814 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2815 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2816 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2817 ret
= build_network_session_path(sessions
[i
].path
,
2818 sizeof(sessions
[i
].path
), session
);
2820 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2821 session
->consumer
->dst
.trace_path
);
2824 PERROR("snprintf session path");
2828 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2829 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2830 sessions
[i
].enabled
= session
->active
;
2831 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2832 sessions
[i
].live_timer_interval
= session
->live_timer
;
2838 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2839 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2841 int cmd_data_pending(struct ltt_session
*session
)
2844 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2845 struct ltt_ust_session
*usess
= session
->ust_session
;
2849 /* Session MUST be stopped to ask for data availability. */
2850 if (session
->active
) {
2851 ret
= LTTNG_ERR_SESSION_STARTED
;
2855 * If stopped, just make sure we've started before else the above call
2856 * will always send that there is data pending.
2858 * The consumer assumes that when the data pending command is received,
2859 * the trace has been started before or else no output data is written
2860 * by the streams which is a condition for data pending. So, this is
2861 * *VERY* important that we don't ask the consumer before a start
2864 if (!session
->has_been_started
) {
2870 if (ksess
&& ksess
->consumer
) {
2871 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2873 /* Data is still being extracted for the kernel. */
2878 if (usess
&& usess
->consumer
) {
2879 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2881 /* Data is still being extracted for the kernel. */
2886 /* Data is ready to be read by a viewer */
2894 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2896 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2898 int cmd_snapshot_add_output(struct ltt_session
*session
,
2899 struct lttng_snapshot_output
*output
, uint32_t *id
)
2902 struct snapshot_output
*new_output
;
2907 DBG("Cmd snapshot add output for session %s", session
->name
);
2910 * Permission denied to create an output if the session is not
2911 * set in no output mode.
2913 if (session
->output_traces
) {
2914 ret
= LTTNG_ERR_EPERM
;
2918 /* Only one output is allowed until we have the "tee" feature. */
2919 if (session
->snapshot
.nb_output
== 1) {
2920 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2924 new_output
= snapshot_output_alloc();
2926 ret
= LTTNG_ERR_NOMEM
;
2930 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2931 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2932 &session
->snapshot
);
2934 if (ret
== -ENOMEM
) {
2935 ret
= LTTNG_ERR_NOMEM
;
2937 ret
= LTTNG_ERR_INVALID
;
2943 snapshot_add_output(&session
->snapshot
, new_output
);
2945 *id
= new_output
->id
;
2952 snapshot_output_destroy(new_output
);
2958 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2960 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2962 int cmd_snapshot_del_output(struct ltt_session
*session
,
2963 struct lttng_snapshot_output
*output
)
2966 struct snapshot_output
*sout
= NULL
;
2974 * Permission denied to create an output if the session is not
2975 * set in no output mode.
2977 if (session
->output_traces
) {
2978 ret
= LTTNG_ERR_EPERM
;
2983 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2985 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2986 } else if (*output
->name
!= '\0') {
2987 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2989 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2992 ret
= LTTNG_ERR_INVALID
;
2996 snapshot_delete_output(&session
->snapshot
, sout
);
2997 snapshot_output_destroy(sout
);
3006 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3008 * If no output is available, outputs is untouched and 0 is returned.
3010 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3012 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3013 struct lttng_snapshot_output
**outputs
)
3016 struct lttng_snapshot_output
*list
= NULL
;
3017 struct lttng_ht_iter iter
;
3018 struct snapshot_output
*output
;
3023 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3026 * Permission denied to create an output if the session is not
3027 * set in no output mode.
3029 if (session
->output_traces
) {
3030 ret
= -LTTNG_ERR_EPERM
;
3034 if (session
->snapshot
.nb_output
== 0) {
3039 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3041 ret
= -LTTNG_ERR_NOMEM
;
3045 /* Copy list from session to the new list object. */
3047 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3048 output
, node
.node
) {
3049 assert(output
->consumer
);
3050 list
[idx
].id
= output
->id
;
3051 list
[idx
].max_size
= output
->max_size
;
3052 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3053 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3054 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3055 sizeof(list
[idx
].ctrl_url
));
3058 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3059 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3061 ret
= -LTTNG_ERR_NOMEM
;
3066 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3067 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3069 ret
= -LTTNG_ERR_NOMEM
;
3078 ret
= session
->snapshot
.nb_output
;
3086 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3087 * snapshot output is *not* set with a remote destination.
3089 * Return 0 on success or a LTTNG_ERR code.
3091 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3092 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3095 struct lttng_ht_iter iter
;
3096 struct consumer_socket
*socket
;
3099 assert(snap_output
);
3102 DBG2("Set relayd object from snapshot output");
3104 /* Ignore if snapshot consumer output is not network. */
3105 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3110 * For each consumer socket, create and send the relayd object of the
3114 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3115 socket
, node
.node
) {
3116 ret
= send_consumer_relayd_sockets(0, session
->id
,
3117 snap_output
->consumer
, socket
,
3118 session
->name
, session
->hostname
,
3119 session
->live_timer
);
3120 if (ret
!= LTTNG_OK
) {
3132 * Record a kernel snapshot.
3134 * Return LTTNG_OK on success or a LTTNG_ERR code.
3136 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3137 struct snapshot_output
*output
, struct ltt_session
*session
,
3138 int wait
, uint64_t nb_packets_per_stream
)
3146 /* Get the datetime for the snapshot output directory. */
3147 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3148 sizeof(output
->datetime
));
3150 ret
= LTTNG_ERR_INVALID
;
3155 * Copy kernel session sockets so we can communicate with the right
3156 * consumer for the snapshot record command.
3158 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3160 ret
= LTTNG_ERR_NOMEM
;
3164 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3165 if (ret
!= LTTNG_OK
) {
3166 goto error_snapshot
;
3169 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3170 if (ret
!= LTTNG_OK
) {
3171 goto error_snapshot
;
3178 /* Clean up copied sockets so this output can use some other later on. */
3179 consumer_destroy_output_sockets(output
->consumer
);
3186 * Record a UST snapshot.
3188 * Return 0 on success or a LTTNG_ERR error code.
3190 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3191 struct snapshot_output
*output
, struct ltt_session
*session
,
3192 int wait
, uint64_t nb_packets_per_stream
)
3200 /* Get the datetime for the snapshot output directory. */
3201 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3202 sizeof(output
->datetime
));
3204 ret
= LTTNG_ERR_INVALID
;
3209 * Copy UST session sockets so we can communicate with the right
3210 * consumer for the snapshot record command.
3212 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3214 ret
= LTTNG_ERR_NOMEM
;
3218 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3219 if (ret
!= LTTNG_OK
) {
3220 goto error_snapshot
;
3223 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3227 ret
= LTTNG_ERR_INVALID
;
3230 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3233 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3236 goto error_snapshot
;
3242 /* Clean up copied sockets so this output can use some other later on. */
3243 consumer_destroy_output_sockets(output
->consumer
);
3249 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3250 uint64_t cur_nr_packets
)
3252 uint64_t tot_size
= 0;
3254 if (session
->kernel_session
) {
3255 struct ltt_kernel_channel
*chan
;
3256 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3258 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3259 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3261 * Don't take channel into account if we
3262 * already grab all its packets.
3266 tot_size
+= chan
->channel
->attr
.subbuf_size
3267 * chan
->stream_count
;
3271 if (session
->ust_session
) {
3272 struct ltt_ust_session
*usess
= session
->ust_session
;
3274 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3282 * Calculate the number of packets we can grab from each stream that
3283 * fits within the overall snapshot max size.
3285 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3286 * the number of packets per stream.
3288 * TODO: this approach is not perfect: we consider the worse case
3289 * (packet filling the sub-buffers) as an upper bound, but we could do
3290 * better if we do this calculation while we actually grab the packet
3291 * content: we would know how much padding we don't actually store into
3294 * This algorithm is currently bounded by the number of packets per
3297 * Since we call this algorithm before actually grabbing the data, it's
3298 * an approximation: for instance, applications could appear/disappear
3299 * in between this call and actually grabbing data.
3302 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3305 uint64_t cur_nb_packets
= 0;
3308 return 0; /* Infinite */
3311 size_left
= max_size
;
3313 uint64_t one_more_packet_tot_size
;
3315 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3317 if (!one_more_packet_tot_size
) {
3318 /* We are already grabbing all packets. */
3321 size_left
-= one_more_packet_tot_size
;
3322 if (size_left
< 0) {
3327 if (!cur_nb_packets
) {
3328 /* Not enough room to grab one packet of each stream, error. */
3331 return cur_nb_packets
;
3335 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3337 * The wait parameter is ignored so this call always wait for the snapshot to
3338 * complete before returning.
3340 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3342 int cmd_snapshot_record(struct ltt_session
*session
,
3343 struct lttng_snapshot_output
*output
, int wait
)
3346 unsigned int use_tmp_output
= 0;
3347 struct snapshot_output tmp_output
;
3348 unsigned int snapshot_success
= 0;
3353 DBG("Cmd snapshot record for session %s", session
->name
);
3356 * Permission denied to create an output if the session is not
3357 * set in no output mode.
3359 if (session
->output_traces
) {
3360 ret
= LTTNG_ERR_EPERM
;
3364 /* The session needs to be started at least once. */
3365 if (!session
->has_been_started
) {
3366 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3370 /* Use temporary output for the session. */
3371 if (*output
->ctrl_url
!= '\0') {
3372 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3373 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3376 if (ret
== -ENOMEM
) {
3377 ret
= LTTNG_ERR_NOMEM
;
3379 ret
= LTTNG_ERR_INVALID
;
3383 /* Use the global session count for the temporary snapshot. */
3384 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3388 if (session
->kernel_session
) {
3389 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3391 if (use_tmp_output
) {
3392 int64_t nb_packets_per_stream
;
3394 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3395 tmp_output
.max_size
);
3396 if (nb_packets_per_stream
< 0) {
3397 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3400 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3401 wait
, nb_packets_per_stream
);
3402 if (ret
!= LTTNG_OK
) {
3405 snapshot_success
= 1;
3407 struct snapshot_output
*sout
;
3408 struct lttng_ht_iter iter
;
3411 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3412 &iter
.iter
, sout
, node
.node
) {
3413 int64_t nb_packets_per_stream
;
3416 * Make a local copy of the output and assign the possible
3417 * temporary value given by the caller.
3419 memset(&tmp_output
, 0, sizeof(tmp_output
));
3420 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3422 if (output
->max_size
!= (uint64_t) -1ULL) {
3423 tmp_output
.max_size
= output
->max_size
;
3426 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3427 tmp_output
.max_size
);
3428 if (nb_packets_per_stream
< 0) {
3429 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3433 /* Use temporary name. */
3434 if (*output
->name
!= '\0') {
3435 strncpy(tmp_output
.name
, output
->name
,
3436 sizeof(tmp_output
.name
));
3439 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3441 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3442 session
, wait
, nb_packets_per_stream
);
3443 if (ret
!= LTTNG_OK
) {
3447 snapshot_success
= 1;
3453 if (session
->ust_session
) {
3454 struct ltt_ust_session
*usess
= session
->ust_session
;
3456 if (use_tmp_output
) {
3457 int64_t nb_packets_per_stream
;
3459 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3460 tmp_output
.max_size
);
3461 if (nb_packets_per_stream
< 0) {
3462 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3465 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3466 wait
, nb_packets_per_stream
);
3467 if (ret
!= LTTNG_OK
) {
3470 snapshot_success
= 1;
3472 struct snapshot_output
*sout
;
3473 struct lttng_ht_iter iter
;
3476 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3477 &iter
.iter
, sout
, node
.node
) {
3478 int64_t nb_packets_per_stream
;
3481 * Make a local copy of the output and assign the possible
3482 * temporary value given by the caller.
3484 memset(&tmp_output
, 0, sizeof(tmp_output
));
3485 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3487 if (output
->max_size
!= (uint64_t) -1ULL) {
3488 tmp_output
.max_size
= output
->max_size
;
3491 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3492 tmp_output
.max_size
);
3493 if (nb_packets_per_stream
< 0) {
3494 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3499 /* Use temporary name. */
3500 if (*output
->name
!= '\0') {
3501 strncpy(tmp_output
.name
, output
->name
,
3502 sizeof(tmp_output
.name
));
3505 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3507 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3508 wait
, nb_packets_per_stream
);
3509 if (ret
!= LTTNG_OK
) {
3513 snapshot_success
= 1;
3519 if (snapshot_success
) {
3520 session
->snapshot
.nb_snapshot
++;
3522 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3530 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3532 int cmd_set_session_shm_path(struct ltt_session
*session
,
3533 const char *shm_path
)
3539 * Can only set shm path before session is started.
3541 if (session
->has_been_started
) {
3542 return LTTNG_ERR_SESSION_STARTED
;
3545 strncpy(session
->shm_path
, shm_path
,
3546 sizeof(session
->shm_path
));
3547 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3553 * Init command subsystem.
3558 * Set network sequence index to 1 for streams to match a relayd
3559 * socket on the consumer side.
3561 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3562 relayd_net_seq_idx
= 1;
3563 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3565 DBG("Command subsystem initialized");