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;
1362 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1364 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1365 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1366 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1370 case LTTNG_DOMAIN_KERNEL
:
1371 assert(session
->kernel_session
);
1373 if (session
->kernel_session
->channel_count
== 0) {
1374 /* Create default channel */
1375 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1376 if (ret
!= LTTNG_OK
) {
1379 chan_kern_created
= 1;
1381 /* Add kernel context to kernel tracer */
1382 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1383 if (ret
!= LTTNG_OK
) {
1387 case LTTNG_DOMAIN_JUL
:
1388 case LTTNG_DOMAIN_LOG4J
:
1391 * Validate channel name.
1392 * If no channel name is given and the domain is JUL or LOG4J,
1393 * set it to the appropriate domain-specific channel name. If
1394 * a name is provided but does not match the expexted channel
1395 * name, return an error.
1397 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1398 strcmp(channel_name
,
1399 DEFAULT_JUL_CHANNEL_NAME
)) {
1400 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1402 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1403 strcmp(channel_name
,
1404 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1405 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1409 case LTTNG_DOMAIN_UST
:
1411 struct ltt_ust_session
*usess
= session
->ust_session
;
1412 unsigned int chan_count
;
1416 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1417 if (chan_count
== 0) {
1418 struct lttng_channel
*attr
;
1419 /* Create default channel */
1420 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1422 ret
= LTTNG_ERR_FATAL
;
1426 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1427 if (ret
!= LTTNG_OK
) {
1432 chan_ust_created
= 1;
1435 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1436 free(app_ctx_provider_name
);
1438 app_ctx_name
= NULL
;
1439 app_ctx_provider_name
= NULL
;
1440 if (ret
!= LTTNG_OK
) {
1446 ret
= LTTNG_ERR_UND
;
1454 if (chan_kern_created
) {
1455 struct ltt_kernel_channel
*kchan
=
1456 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1457 session
->kernel_session
);
1458 /* Created previously, this should NOT fail. */
1460 kernel_destroy_channel(kchan
);
1463 if (chan_ust_created
) {
1464 struct ltt_ust_channel
*uchan
=
1465 trace_ust_find_channel_by_name(
1466 session
->ust_session
->domain_global
.channels
,
1467 DEFAULT_CHANNEL_NAME
);
1468 /* Created previously, this should NOT fail. */
1470 /* Remove from the channel list of the session. */
1471 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1473 trace_ust_destroy_channel(uchan
);
1476 free(app_ctx_provider_name
);
1481 static int validate_event_name(const char *name
)
1484 const char *c
= name
;
1485 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1486 bool null_terminated
= false;
1489 * Make sure that unescaped wildcards are only used as the last
1490 * character of the event name.
1492 while (c
< event_name_end
) {
1495 null_terminated
= true;
1501 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1502 /* Wildcard is not the last character */
1503 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1512 if (!ret
&& !null_terminated
) {
1513 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1518 static inline bool name_starts_with(const char *name
, const char *prefix
)
1520 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1522 return !strncmp(name
, prefix
, max_cmp_len
);
1525 /* Perform userspace-specific event name validation */
1526 static int validate_ust_event_name(const char *name
)
1536 * Check name against all internal UST event component namespaces used
1539 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1540 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1541 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1550 * Internal version of cmd_enable_event() with a supplemental
1551 * "internal_event" flag which is used to enable internal events which should
1552 * be hidden from clients. Such events are used in the agent implementation to
1553 * enable the events through which all "agent" events are funeled.
1555 static int _cmd_enable_event(struct ltt_session
*session
,
1556 struct lttng_domain
*domain
,
1557 char *channel_name
, struct lttng_event
*event
,
1558 char *filter_expression
,
1559 struct lttng_filter_bytecode
*filter
,
1560 struct lttng_event_exclusion
*exclusion
,
1561 int wpipe
, bool internal_event
)
1563 int ret
, channel_created
= 0;
1564 struct lttng_channel
*attr
;
1568 assert(channel_name
);
1570 /* If we have a filter, we must have its filter expression */
1571 assert(!(!!filter_expression
^ !!filter
));
1573 DBG("Enable event command for event \'%s\'", event
->name
);
1577 ret
= validate_event_name(event
->name
);
1582 switch (domain
->type
) {
1583 case LTTNG_DOMAIN_KERNEL
:
1585 struct ltt_kernel_channel
*kchan
;
1588 * If a non-default channel has been created in the
1589 * session, explicitely require that -c chan_name needs
1592 if (session
->kernel_session
->has_non_default_channel
1593 && channel_name
[0] == '\0') {
1594 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1598 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1599 session
->kernel_session
);
1600 if (kchan
== NULL
) {
1601 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1602 LTTNG_BUFFER_GLOBAL
);
1604 ret
= LTTNG_ERR_FATAL
;
1607 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1609 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1610 if (ret
!= LTTNG_OK
) {
1616 channel_created
= 1;
1619 /* Get the newly created kernel channel pointer */
1620 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1621 session
->kernel_session
);
1622 if (kchan
== NULL
) {
1623 /* This sould not happen... */
1624 ret
= LTTNG_ERR_FATAL
;
1628 switch (event
->type
) {
1629 case LTTNG_EVENT_ALL
:
1631 char *filter_expression_a
= NULL
;
1632 struct lttng_filter_bytecode
*filter_a
= NULL
;
1635 * We need to duplicate filter_expression and filter,
1636 * because ownership is passed to first enable
1639 if (filter_expression
) {
1640 filter_expression_a
= strdup(filter_expression
);
1641 if (!filter_expression_a
) {
1642 ret
= LTTNG_ERR_FATAL
;
1647 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1649 free(filter_expression_a
);
1650 ret
= LTTNG_ERR_FATAL
;
1653 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1655 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1656 ret
= event_kernel_enable_event(kchan
, event
,
1657 filter_expression
, filter
);
1658 /* We have passed ownership */
1659 filter_expression
= NULL
;
1661 if (ret
!= LTTNG_OK
) {
1662 if (channel_created
) {
1663 /* Let's not leak a useless channel. */
1664 kernel_destroy_channel(kchan
);
1666 free(filter_expression_a
);
1670 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1671 ret
= event_kernel_enable_event(kchan
, event
,
1672 filter_expression_a
, filter_a
);
1673 /* We have passed ownership */
1674 filter_expression_a
= NULL
;
1676 if (ret
!= LTTNG_OK
) {
1681 case LTTNG_EVENT_PROBE
:
1682 case LTTNG_EVENT_FUNCTION
:
1683 case LTTNG_EVENT_FUNCTION_ENTRY
:
1684 case LTTNG_EVENT_TRACEPOINT
:
1685 ret
= event_kernel_enable_event(kchan
, event
,
1686 filter_expression
, filter
);
1687 /* We have passed ownership */
1688 filter_expression
= NULL
;
1690 if (ret
!= LTTNG_OK
) {
1691 if (channel_created
) {
1692 /* Let's not leak a useless channel. */
1693 kernel_destroy_channel(kchan
);
1698 case LTTNG_EVENT_SYSCALL
:
1699 ret
= event_kernel_enable_event(kchan
, event
,
1700 filter_expression
, filter
);
1701 /* We have passed ownership */
1702 filter_expression
= NULL
;
1704 if (ret
!= LTTNG_OK
) {
1709 ret
= LTTNG_ERR_UNK
;
1713 kernel_wait_quiescent(kernel_tracer_fd
);
1716 case LTTNG_DOMAIN_UST
:
1718 struct ltt_ust_channel
*uchan
;
1719 struct ltt_ust_session
*usess
= session
->ust_session
;
1724 * If a non-default channel has been created in the
1725 * session, explicitely require that -c chan_name needs
1728 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1729 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1733 /* Get channel from global UST domain */
1734 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1736 if (uchan
== NULL
) {
1737 /* Create default channel */
1738 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1739 usess
->buffer_type
);
1741 ret
= LTTNG_ERR_FATAL
;
1744 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1746 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1747 if (ret
!= LTTNG_OK
) {
1753 /* Get the newly created channel reference back */
1754 uchan
= trace_ust_find_channel_by_name(
1755 usess
->domain_global
.channels
, channel_name
);
1759 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1761 * Don't allow users to add UST events to channels which
1762 * are assigned to a userspace subdomain (JUL, Log4J,
1765 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
1769 if (!internal_event
) {
1771 * Ensure the event name is not reserved for internal
1774 ret
= validate_ust_event_name(event
->name
);
1776 WARN("Userspace event name %s failed validation.",
1778 event
->name
: "NULL");
1779 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1784 /* At this point, the session and channel exist on the tracer */
1785 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1786 filter_expression
, filter
, exclusion
,
1788 /* We have passed ownership */
1789 filter_expression
= NULL
;
1792 if (ret
!= LTTNG_OK
) {
1797 case LTTNG_DOMAIN_LOG4J
:
1798 case LTTNG_DOMAIN_JUL
:
1799 case LTTNG_DOMAIN_PYTHON
:
1801 const char *default_event_name
, *default_chan_name
;
1803 struct lttng_event uevent
;
1804 struct lttng_domain tmp_dom
;
1805 struct ltt_ust_session
*usess
= session
->ust_session
;
1809 agt
= trace_ust_find_agent(usess
, domain
->type
);
1811 agt
= agent_create(domain
->type
);
1813 ret
= LTTNG_ERR_NOMEM
;
1816 agent_add(agt
, usess
->agents
);
1819 /* Create the default tracepoint. */
1820 memset(&uevent
, 0, sizeof(uevent
));
1821 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1822 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1823 default_event_name
= event_get_default_agent_ust_name(
1825 if (!default_event_name
) {
1826 ret
= LTTNG_ERR_FATAL
;
1829 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1830 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1833 * The domain type is changed because we are about to enable the
1834 * default channel and event for the JUL domain that are hardcoded.
1835 * This happens in the UST domain.
1837 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1838 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1840 switch (domain
->type
) {
1841 case LTTNG_DOMAIN_LOG4J
:
1842 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1844 case LTTNG_DOMAIN_JUL
:
1845 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1847 case LTTNG_DOMAIN_PYTHON
:
1848 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1851 /* The switch/case we are in makes this impossible */
1856 char *filter_expression_copy
= NULL
;
1857 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1860 const size_t filter_size
= sizeof(
1861 struct lttng_filter_bytecode
)
1864 filter_copy
= zmalloc(filter_size
);
1866 ret
= LTTNG_ERR_NOMEM
;
1869 memcpy(filter_copy
, filter
, filter_size
);
1871 filter_expression_copy
=
1872 strdup(filter_expression
);
1873 if (!filter_expression
) {
1874 ret
= LTTNG_ERR_NOMEM
;
1877 if (!filter_expression_copy
|| !filter_copy
) {
1878 free(filter_expression_copy
);
1884 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1885 (char *) default_chan_name
,
1886 &uevent
, filter_expression_copy
,
1887 filter_copy
, NULL
, wpipe
);
1890 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1894 /* The wild card * means that everything should be enabled. */
1895 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1896 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1899 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1903 filter_expression
= NULL
;
1904 if (ret
!= LTTNG_OK
) {
1911 ret
= LTTNG_ERR_UND
;
1918 free(filter_expression
);
1926 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1927 * We own filter, exclusion, and filter_expression.
1929 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1930 char *channel_name
, struct lttng_event
*event
,
1931 char *filter_expression
,
1932 struct lttng_filter_bytecode
*filter
,
1933 struct lttng_event_exclusion
*exclusion
,
1936 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1937 filter_expression
, filter
, exclusion
, wpipe
, false);
1941 * Enable an event which is internal to LTTng. An internal should
1942 * never be made visible to clients and are immune to checks such as
1945 static int cmd_enable_event_internal(struct ltt_session
*session
,
1946 struct lttng_domain
*domain
,
1947 char *channel_name
, struct lttng_event
*event
,
1948 char *filter_expression
,
1949 struct lttng_filter_bytecode
*filter
,
1950 struct lttng_event_exclusion
*exclusion
,
1953 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1954 filter_expression
, filter
, exclusion
, wpipe
, true);
1958 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1960 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
1961 struct lttng_event
**events
)
1964 ssize_t nb_events
= 0;
1967 case LTTNG_DOMAIN_KERNEL
:
1968 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1969 if (nb_events
< 0) {
1970 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1974 case LTTNG_DOMAIN_UST
:
1975 nb_events
= ust_app_list_events(events
);
1976 if (nb_events
< 0) {
1977 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1981 case LTTNG_DOMAIN_LOG4J
:
1982 case LTTNG_DOMAIN_JUL
:
1983 case LTTNG_DOMAIN_PYTHON
:
1984 nb_events
= agent_list_events(events
, domain
);
1985 if (nb_events
< 0) {
1986 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1991 ret
= LTTNG_ERR_UND
;
1998 /* Return negative value to differentiate return code */
2003 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2005 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2006 struct lttng_event_field
**fields
)
2009 ssize_t nb_fields
= 0;
2012 case LTTNG_DOMAIN_UST
:
2013 nb_fields
= ust_app_list_event_fields(fields
);
2014 if (nb_fields
< 0) {
2015 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2019 case LTTNG_DOMAIN_KERNEL
:
2020 default: /* fall-through */
2021 ret
= LTTNG_ERR_UND
;
2028 /* Return negative value to differentiate return code */
2032 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2034 return syscall_table_list(events
);
2038 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2040 * Called with session lock held.
2042 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2043 enum lttng_domain_type domain
, int32_t **pids
)
2046 ssize_t nr_pids
= 0;
2049 case LTTNG_DOMAIN_KERNEL
:
2051 struct ltt_kernel_session
*ksess
;
2053 ksess
= session
->kernel_session
;
2054 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2056 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2061 case LTTNG_DOMAIN_UST
:
2063 struct ltt_ust_session
*usess
;
2065 usess
= session
->ust_session
;
2066 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2068 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2073 case LTTNG_DOMAIN_LOG4J
:
2074 case LTTNG_DOMAIN_JUL
:
2075 case LTTNG_DOMAIN_PYTHON
:
2077 ret
= LTTNG_ERR_UND
;
2084 /* Return negative value to differentiate return code */
2089 * Command LTTNG_START_TRACE processed by the client thread.
2091 * Called with session mutex held.
2093 int cmd_start_trace(struct ltt_session
*session
)
2096 unsigned long nb_chan
= 0;
2097 struct ltt_kernel_session
*ksession
;
2098 struct ltt_ust_session
*usess
;
2102 /* Ease our life a bit ;) */
2103 ksession
= session
->kernel_session
;
2104 usess
= session
->ust_session
;
2106 /* Is the session already started? */
2107 if (session
->active
) {
2108 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2113 * Starting a session without channel is useless since after that it's not
2114 * possible to enable channel thus inform the client.
2116 if (usess
&& usess
->domain_global
.channels
) {
2117 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2120 nb_chan
+= ksession
->channel_count
;
2123 ret
= LTTNG_ERR_NO_CHANNEL
;
2127 /* Kernel tracing */
2128 if (ksession
!= NULL
) {
2129 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2130 if (ret
!= LTTNG_OK
) {
2135 /* Flag session that trace should start automatically */
2138 * Even though the start trace might fail, flag this session active so
2139 * other application coming in are started by default.
2143 ret
= ust_app_start_trace_all(usess
);
2145 ret
= LTTNG_ERR_UST_START_FAIL
;
2150 /* Flag this after a successful start. */
2151 session
->has_been_started
= 1;
2152 session
->active
= 1;
2161 * Command LTTNG_STOP_TRACE processed by the client thread.
2163 int cmd_stop_trace(struct ltt_session
*session
)
2166 struct ltt_kernel_channel
*kchan
;
2167 struct ltt_kernel_session
*ksession
;
2168 struct ltt_ust_session
*usess
;
2173 ksession
= session
->kernel_session
;
2174 usess
= session
->ust_session
;
2176 /* Session is not active. Skip everythong and inform the client. */
2177 if (!session
->active
) {
2178 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2183 if (ksession
&& ksession
->active
) {
2184 DBG("Stop kernel tracing");
2186 /* Flush metadata if exist */
2187 if (ksession
->metadata_stream_fd
>= 0) {
2188 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2190 ERR("Kernel metadata flush failed");
2194 /* Flush all buffers before stopping */
2195 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2196 ret
= kernel_flush_buffer(kchan
);
2198 ERR("Kernel flush buffer error");
2202 ret
= kernel_stop_session(ksession
);
2204 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2208 kernel_wait_quiescent(kernel_tracer_fd
);
2210 ksession
->active
= 0;
2213 if (usess
&& usess
->active
) {
2215 * Even though the stop trace might fail, flag this session inactive so
2216 * other application coming in are not started by default.
2220 ret
= ust_app_stop_trace_all(usess
);
2222 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2227 /* Flag inactive after a successful stop. */
2228 session
->active
= 0;
2236 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2238 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2239 struct lttng_uri
*uris
)
2242 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2243 struct ltt_ust_session
*usess
= session
->ust_session
;
2249 /* Can't set consumer URI if the session is active. */
2250 if (session
->active
) {
2251 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2255 /* Set the "global" consumer URIs */
2256 for (i
= 0; i
< nb_uri
; i
++) {
2257 ret
= add_uri_to_consumer(session
->consumer
,
2258 &uris
[i
], 0, session
->name
);
2259 if (ret
!= LTTNG_OK
) {
2264 /* Set UST session URIs */
2265 if (session
->ust_session
) {
2266 for (i
= 0; i
< nb_uri
; i
++) {
2267 ret
= add_uri_to_consumer(
2268 session
->ust_session
->consumer
,
2269 &uris
[i
], LTTNG_DOMAIN_UST
,
2271 if (ret
!= LTTNG_OK
) {
2277 /* Set kernel session URIs */
2278 if (session
->kernel_session
) {
2279 for (i
= 0; i
< nb_uri
; i
++) {
2280 ret
= add_uri_to_consumer(
2281 session
->kernel_session
->consumer
,
2282 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2284 if (ret
!= LTTNG_OK
) {
2291 * Make sure to set the session in output mode after we set URI since a
2292 * session can be created without URL (thus flagged in no output mode).
2294 session
->output_traces
= 1;
2296 ksess
->output_traces
= 1;
2300 usess
->output_traces
= 1;
2311 * Command LTTNG_CREATE_SESSION processed by the client thread.
2313 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2314 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2317 struct ltt_session
*session
;
2323 * Verify if the session already exist
2325 * XXX: There is no need for the session lock list here since the caller
2326 * (process_client_msg) is holding it. We might want to change that so a
2327 * single command does not lock the entire session list.
2329 session
= session_find_by_name(name
);
2330 if (session
!= NULL
) {
2331 ret
= LTTNG_ERR_EXIST_SESS
;
2335 /* Create tracing session in the registry */
2336 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2337 LTTNG_SOCK_GET_GID_CRED(creds
));
2338 if (ret
!= LTTNG_OK
) {
2343 * Get the newly created session pointer back
2345 * XXX: There is no need for the session lock list here since the caller
2346 * (process_client_msg) is holding it. We might want to change that so a
2347 * single command does not lock the entire session list.
2349 session
= session_find_by_name(name
);
2352 session
->live_timer
= live_timer
;
2353 /* Create default consumer output for the session not yet created. */
2354 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2355 if (session
->consumer
== NULL
) {
2356 ret
= LTTNG_ERR_FATAL
;
2357 goto consumer_error
;
2361 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2362 if (ret
!= LTTNG_OK
) {
2363 goto consumer_error
;
2365 session
->output_traces
= 1;
2367 session
->output_traces
= 0;
2368 DBG2("Session %s created with no output", session
->name
);
2371 session
->consumer
->enabled
= 1;
2376 session_destroy(session
);
2383 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2385 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2386 size_t nb_uri
, lttng_sock_cred
*creds
)
2389 struct ltt_session
*session
;
2390 struct snapshot_output
*new_output
= NULL
;
2396 * Create session in no output mode with URIs set to NULL. The uris we've
2397 * received are for a default snapshot output if one.
2399 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2400 if (ret
!= LTTNG_OK
) {
2404 /* Get the newly created session pointer back. This should NEVER fail. */
2405 session
= session_find_by_name(name
);
2408 /* Flag session for snapshot mode. */
2409 session
->snapshot_mode
= 1;
2411 /* Skip snapshot output creation if no URI is given. */
2416 new_output
= snapshot_output_alloc();
2418 ret
= LTTNG_ERR_NOMEM
;
2419 goto error_snapshot_alloc
;
2422 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2423 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2425 if (ret
== -ENOMEM
) {
2426 ret
= LTTNG_ERR_NOMEM
;
2428 ret
= LTTNG_ERR_INVALID
;
2430 goto error_snapshot
;
2434 snapshot_add_output(&session
->snapshot
, new_output
);
2441 snapshot_output_destroy(new_output
);
2442 error_snapshot_alloc
:
2443 session_destroy(session
);
2449 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2451 * Called with session lock held.
2453 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2456 struct ltt_ust_session
*usess
;
2457 struct ltt_kernel_session
*ksess
;
2462 usess
= session
->ust_session
;
2463 ksess
= session
->kernel_session
;
2465 /* Clean kernel session teardown */
2466 kernel_destroy_session(ksess
);
2468 /* UST session teardown */
2470 /* Close any relayd session */
2471 consumer_output_send_destroy_relayd(usess
->consumer
);
2473 /* Destroy every UST application related to this session. */
2474 ret
= ust_app_destroy_trace_all(usess
);
2476 ERR("Error in ust_app_destroy_trace_all");
2479 /* Clean up the rest. */
2480 trace_ust_destroy_session(usess
);
2484 * Must notify the kernel thread here to update it's poll set in order to
2485 * remove the channel(s)' fd just destroyed.
2487 ret
= notify_thread_pipe(wpipe
);
2489 PERROR("write kernel poll pipe");
2492 ret
= session_destroy(session
);
2498 * Command LTTNG_CALIBRATE processed by the client thread.
2500 int cmd_calibrate(enum lttng_domain_type domain
,
2501 struct lttng_calibrate
*calibrate
)
2506 case LTTNG_DOMAIN_KERNEL
:
2508 struct lttng_kernel_calibrate kcalibrate
;
2510 switch (calibrate
->type
) {
2511 case LTTNG_CALIBRATE_FUNCTION
:
2513 /* Default and only possible calibrate option. */
2514 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2518 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2520 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2525 case LTTNG_DOMAIN_UST
:
2527 struct lttng_ust_calibrate ucalibrate
;
2529 switch (calibrate
->type
) {
2530 case LTTNG_CALIBRATE_FUNCTION
:
2532 /* Default and only possible calibrate option. */
2533 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2537 ret
= ust_app_calibrate_glb(&ucalibrate
);
2539 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2545 ret
= LTTNG_ERR_UND
;
2556 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2558 int cmd_register_consumer(struct ltt_session
*session
,
2559 enum lttng_domain_type domain
, const char *sock_path
,
2560 struct consumer_data
*cdata
)
2563 struct consumer_socket
*socket
= NULL
;
2570 case LTTNG_DOMAIN_KERNEL
:
2572 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2576 /* Can't register a consumer if there is already one */
2577 if (ksess
->consumer_fds_sent
!= 0) {
2578 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2582 sock
= lttcomm_connect_unix_sock(sock_path
);
2584 ret
= LTTNG_ERR_CONNECT_FAIL
;
2587 cdata
->cmd_sock
= sock
;
2589 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2590 if (socket
== NULL
) {
2593 PERROR("close register consumer");
2595 cdata
->cmd_sock
= -1;
2596 ret
= LTTNG_ERR_FATAL
;
2600 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2601 if (socket
->lock
== NULL
) {
2602 PERROR("zmalloc pthread mutex");
2603 ret
= LTTNG_ERR_FATAL
;
2606 pthread_mutex_init(socket
->lock
, NULL
);
2607 socket
->registered
= 1;
2610 consumer_add_socket(socket
, ksess
->consumer
);
2613 pthread_mutex_lock(&cdata
->pid_mutex
);
2615 pthread_mutex_unlock(&cdata
->pid_mutex
);
2620 /* TODO: Userspace tracing */
2621 ret
= LTTNG_ERR_UND
;
2629 consumer_destroy_socket(socket
);
2635 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2637 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2638 struct lttng_domain
**domains
)
2643 struct lttng_ht_iter iter
;
2645 if (session
->kernel_session
!= NULL
) {
2646 DBG3("Listing domains found kernel domain");
2650 if (session
->ust_session
!= NULL
) {
2651 DBG3("Listing domains found UST global domain");
2655 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2657 if (agt
->being_used
) {
2668 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2669 if (*domains
== NULL
) {
2670 ret
= LTTNG_ERR_FATAL
;
2674 if (session
->kernel_session
!= NULL
) {
2675 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2677 /* Kernel session buffer type is always GLOBAL */
2678 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2683 if (session
->ust_session
!= NULL
) {
2684 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2685 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2689 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2691 if (agt
->being_used
) {
2692 (*domains
)[index
].type
= agt
->domain
;
2693 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2703 /* Return negative value to differentiate return code */
2709 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2711 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2712 struct ltt_session
*session
, struct lttng_channel
**channels
)
2715 ssize_t nb_chan
= 0;
2718 case LTTNG_DOMAIN_KERNEL
:
2719 if (session
->kernel_session
!= NULL
) {
2720 nb_chan
= session
->kernel_session
->channel_count
;
2722 DBG3("Number of kernel channels %zd", nb_chan
);
2724 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2727 case LTTNG_DOMAIN_UST
:
2728 if (session
->ust_session
!= NULL
) {
2730 nb_chan
= lttng_ht_get_count(
2731 session
->ust_session
->domain_global
.channels
);
2734 DBG3("Number of UST global channels %zd", nb_chan
);
2736 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2741 ret
= LTTNG_ERR_UND
;
2746 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2747 if (*channels
== NULL
) {
2748 ret
= LTTNG_ERR_FATAL
;
2752 list_lttng_channels(domain
, session
, *channels
);
2758 /* Return negative value to differentiate return code */
2763 * Command LTTNG_LIST_EVENTS processed by the client thread.
2765 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2766 struct ltt_session
*session
, char *channel_name
,
2767 struct lttng_event
**events
)
2770 ssize_t nb_event
= 0;
2773 case LTTNG_DOMAIN_KERNEL
:
2774 if (session
->kernel_session
!= NULL
) {
2775 nb_event
= list_lttng_kernel_events(channel_name
,
2776 session
->kernel_session
, events
);
2779 case LTTNG_DOMAIN_UST
:
2781 if (session
->ust_session
!= NULL
) {
2782 nb_event
= list_lttng_ust_global_events(channel_name
,
2783 &session
->ust_session
->domain_global
, events
);
2787 case LTTNG_DOMAIN_LOG4J
:
2788 case LTTNG_DOMAIN_JUL
:
2789 case LTTNG_DOMAIN_PYTHON
:
2790 if (session
->ust_session
) {
2791 struct lttng_ht_iter iter
;
2795 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2796 &iter
.iter
, agt
, node
.node
) {
2797 if (agt
->domain
== domain
) {
2798 nb_event
= list_lttng_agent_events(
2807 ret
= LTTNG_ERR_UND
;
2814 /* Return negative value to differentiate return code */
2819 * Using the session list, filled a lttng_session array to send back to the
2820 * client for session listing.
2822 * The session list lock MUST be acquired before calling this function. Use
2823 * session_lock_list() and session_unlock_list().
2825 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2830 struct ltt_session
*session
;
2831 struct ltt_session_list
*list
= session_get_list();
2833 DBG("Getting all available session for UID %d GID %d",
2836 * Iterate over session list and append data after the control struct in
2839 cds_list_for_each_entry(session
, &list
->head
, list
) {
2841 * Only list the sessions the user can control.
2843 if (!session_access_ok(session
, uid
, gid
)) {
2847 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2848 struct ltt_ust_session
*usess
= session
->ust_session
;
2850 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2851 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2852 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2853 ret
= build_network_session_path(sessions
[i
].path
,
2854 sizeof(sessions
[i
].path
), session
);
2856 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2857 session
->consumer
->dst
.trace_path
);
2860 PERROR("snprintf session path");
2864 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2865 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2866 sessions
[i
].enabled
= session
->active
;
2867 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2868 sessions
[i
].live_timer_interval
= session
->live_timer
;
2874 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2875 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2877 int cmd_data_pending(struct ltt_session
*session
)
2880 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2881 struct ltt_ust_session
*usess
= session
->ust_session
;
2885 /* Session MUST be stopped to ask for data availability. */
2886 if (session
->active
) {
2887 ret
= LTTNG_ERR_SESSION_STARTED
;
2891 * If stopped, just make sure we've started before else the above call
2892 * will always send that there is data pending.
2894 * The consumer assumes that when the data pending command is received,
2895 * the trace has been started before or else no output data is written
2896 * by the streams which is a condition for data pending. So, this is
2897 * *VERY* important that we don't ask the consumer before a start
2900 if (!session
->has_been_started
) {
2906 if (ksess
&& ksess
->consumer
) {
2907 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2909 /* Data is still being extracted for the kernel. */
2914 if (usess
&& usess
->consumer
) {
2915 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2917 /* Data is still being extracted for the kernel. */
2922 /* Data is ready to be read by a viewer */
2930 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2932 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2934 int cmd_snapshot_add_output(struct ltt_session
*session
,
2935 struct lttng_snapshot_output
*output
, uint32_t *id
)
2938 struct snapshot_output
*new_output
;
2943 DBG("Cmd snapshot add output for session %s", session
->name
);
2946 * Permission denied to create an output if the session is not
2947 * set in no output mode.
2949 if (session
->output_traces
) {
2950 ret
= LTTNG_ERR_EPERM
;
2954 /* Only one output is allowed until we have the "tee" feature. */
2955 if (session
->snapshot
.nb_output
== 1) {
2956 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2960 new_output
= snapshot_output_alloc();
2962 ret
= LTTNG_ERR_NOMEM
;
2966 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2967 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2968 &session
->snapshot
);
2970 if (ret
== -ENOMEM
) {
2971 ret
= LTTNG_ERR_NOMEM
;
2973 ret
= LTTNG_ERR_INVALID
;
2979 snapshot_add_output(&session
->snapshot
, new_output
);
2981 *id
= new_output
->id
;
2988 snapshot_output_destroy(new_output
);
2994 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2996 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2998 int cmd_snapshot_del_output(struct ltt_session
*session
,
2999 struct lttng_snapshot_output
*output
)
3002 struct snapshot_output
*sout
= NULL
;
3010 * Permission denied to create an output if the session is not
3011 * set in no output mode.
3013 if (session
->output_traces
) {
3014 ret
= LTTNG_ERR_EPERM
;
3019 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3021 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3022 } else if (*output
->name
!= '\0') {
3023 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3025 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3028 ret
= LTTNG_ERR_INVALID
;
3032 snapshot_delete_output(&session
->snapshot
, sout
);
3033 snapshot_output_destroy(sout
);
3042 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3044 * If no output is available, outputs is untouched and 0 is returned.
3046 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3048 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3049 struct lttng_snapshot_output
**outputs
)
3052 struct lttng_snapshot_output
*list
= NULL
;
3053 struct lttng_ht_iter iter
;
3054 struct snapshot_output
*output
;
3059 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3062 * Permission denied to create an output if the session is not
3063 * set in no output mode.
3065 if (session
->output_traces
) {
3066 ret
= -LTTNG_ERR_EPERM
;
3070 if (session
->snapshot
.nb_output
== 0) {
3075 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3077 ret
= -LTTNG_ERR_NOMEM
;
3081 /* Copy list from session to the new list object. */
3083 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3084 output
, node
.node
) {
3085 assert(output
->consumer
);
3086 list
[idx
].id
= output
->id
;
3087 list
[idx
].max_size
= output
->max_size
;
3088 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3089 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3090 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3091 sizeof(list
[idx
].ctrl_url
));
3094 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3095 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3097 ret
= -LTTNG_ERR_NOMEM
;
3102 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3103 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3105 ret
= -LTTNG_ERR_NOMEM
;
3114 ret
= session
->snapshot
.nb_output
;
3122 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3123 * snapshot output is *not* set with a remote destination.
3125 * Return 0 on success or a LTTNG_ERR code.
3127 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3128 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3131 struct lttng_ht_iter iter
;
3132 struct consumer_socket
*socket
;
3135 assert(snap_output
);
3138 DBG2("Set relayd object from snapshot output");
3140 /* Ignore if snapshot consumer output is not network. */
3141 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3146 * For each consumer socket, create and send the relayd object of the
3150 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3151 socket
, node
.node
) {
3152 ret
= send_consumer_relayd_sockets(0, session
->id
,
3153 snap_output
->consumer
, socket
,
3154 session
->name
, session
->hostname
,
3155 session
->live_timer
);
3156 if (ret
!= LTTNG_OK
) {
3168 * Record a kernel snapshot.
3170 * Return LTTNG_OK on success or a LTTNG_ERR code.
3172 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3173 struct snapshot_output
*output
, struct ltt_session
*session
,
3174 int wait
, uint64_t nb_packets_per_stream
)
3182 /* Get the datetime for the snapshot output directory. */
3183 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3184 sizeof(output
->datetime
));
3186 ret
= LTTNG_ERR_INVALID
;
3191 * Copy kernel session sockets so we can communicate with the right
3192 * consumer for the snapshot record command.
3194 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3196 ret
= LTTNG_ERR_NOMEM
;
3200 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3201 if (ret
!= LTTNG_OK
) {
3202 goto error_snapshot
;
3205 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3206 if (ret
!= LTTNG_OK
) {
3207 goto error_snapshot
;
3214 /* Clean up copied sockets so this output can use some other later on. */
3215 consumer_destroy_output_sockets(output
->consumer
);
3222 * Record a UST snapshot.
3224 * Return 0 on success or a LTTNG_ERR error code.
3226 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3227 struct snapshot_output
*output
, struct ltt_session
*session
,
3228 int wait
, uint64_t nb_packets_per_stream
)
3236 /* Get the datetime for the snapshot output directory. */
3237 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3238 sizeof(output
->datetime
));
3240 ret
= LTTNG_ERR_INVALID
;
3245 * Copy UST session sockets so we can communicate with the right
3246 * consumer for the snapshot record command.
3248 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3250 ret
= LTTNG_ERR_NOMEM
;
3254 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3255 if (ret
!= LTTNG_OK
) {
3256 goto error_snapshot
;
3259 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3263 ret
= LTTNG_ERR_INVALID
;
3266 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3269 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3272 goto error_snapshot
;
3278 /* Clean up copied sockets so this output can use some other later on. */
3279 consumer_destroy_output_sockets(output
->consumer
);
3285 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3286 uint64_t cur_nr_packets
)
3288 uint64_t tot_size
= 0;
3290 if (session
->kernel_session
) {
3291 struct ltt_kernel_channel
*chan
;
3292 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3294 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3295 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3297 * Don't take channel into account if we
3298 * already grab all its packets.
3302 tot_size
+= chan
->channel
->attr
.subbuf_size
3303 * chan
->stream_count
;
3307 if (session
->ust_session
) {
3308 struct ltt_ust_session
*usess
= session
->ust_session
;
3310 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3318 * Calculate the number of packets we can grab from each stream that
3319 * fits within the overall snapshot max size.
3321 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3322 * the number of packets per stream.
3324 * TODO: this approach is not perfect: we consider the worse case
3325 * (packet filling the sub-buffers) as an upper bound, but we could do
3326 * better if we do this calculation while we actually grab the packet
3327 * content: we would know how much padding we don't actually store into
3330 * This algorithm is currently bounded by the number of packets per
3333 * Since we call this algorithm before actually grabbing the data, it's
3334 * an approximation: for instance, applications could appear/disappear
3335 * in between this call and actually grabbing data.
3338 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3341 uint64_t cur_nb_packets
= 0;
3344 return 0; /* Infinite */
3347 size_left
= max_size
;
3349 uint64_t one_more_packet_tot_size
;
3351 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3353 if (!one_more_packet_tot_size
) {
3354 /* We are already grabbing all packets. */
3357 size_left
-= one_more_packet_tot_size
;
3358 if (size_left
< 0) {
3363 if (!cur_nb_packets
) {
3364 /* Not enough room to grab one packet of each stream, error. */
3367 return cur_nb_packets
;
3371 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3373 * The wait parameter is ignored so this call always wait for the snapshot to
3374 * complete before returning.
3376 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3378 int cmd_snapshot_record(struct ltt_session
*session
,
3379 struct lttng_snapshot_output
*output
, int wait
)
3382 unsigned int use_tmp_output
= 0;
3383 struct snapshot_output tmp_output
;
3384 unsigned int snapshot_success
= 0;
3389 DBG("Cmd snapshot record for session %s", session
->name
);
3392 * Permission denied to create an output if the session is not
3393 * set in no output mode.
3395 if (session
->output_traces
) {
3396 ret
= LTTNG_ERR_EPERM
;
3400 /* The session needs to be started at least once. */
3401 if (!session
->has_been_started
) {
3402 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3406 /* Use temporary output for the session. */
3407 if (*output
->ctrl_url
!= '\0') {
3408 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3409 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3412 if (ret
== -ENOMEM
) {
3413 ret
= LTTNG_ERR_NOMEM
;
3415 ret
= LTTNG_ERR_INVALID
;
3419 /* Use the global session count for the temporary snapshot. */
3420 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3424 if (session
->kernel_session
) {
3425 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3427 if (use_tmp_output
) {
3428 int64_t nb_packets_per_stream
;
3430 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3431 tmp_output
.max_size
);
3432 if (nb_packets_per_stream
< 0) {
3433 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3436 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3437 wait
, nb_packets_per_stream
);
3438 if (ret
!= LTTNG_OK
) {
3441 snapshot_success
= 1;
3443 struct snapshot_output
*sout
;
3444 struct lttng_ht_iter iter
;
3447 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3448 &iter
.iter
, sout
, node
.node
) {
3449 int64_t nb_packets_per_stream
;
3452 * Make a local copy of the output and assign the possible
3453 * temporary value given by the caller.
3455 memset(&tmp_output
, 0, sizeof(tmp_output
));
3456 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3458 if (output
->max_size
!= (uint64_t) -1ULL) {
3459 tmp_output
.max_size
= output
->max_size
;
3462 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3463 tmp_output
.max_size
);
3464 if (nb_packets_per_stream
< 0) {
3465 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3469 /* Use temporary name. */
3470 if (*output
->name
!= '\0') {
3471 strncpy(tmp_output
.name
, output
->name
,
3472 sizeof(tmp_output
.name
));
3475 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3477 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3478 session
, wait
, nb_packets_per_stream
);
3479 if (ret
!= LTTNG_OK
) {
3483 snapshot_success
= 1;
3489 if (session
->ust_session
) {
3490 struct ltt_ust_session
*usess
= session
->ust_session
;
3492 if (use_tmp_output
) {
3493 int64_t nb_packets_per_stream
;
3495 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3496 tmp_output
.max_size
);
3497 if (nb_packets_per_stream
< 0) {
3498 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3501 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3502 wait
, nb_packets_per_stream
);
3503 if (ret
!= LTTNG_OK
) {
3506 snapshot_success
= 1;
3508 struct snapshot_output
*sout
;
3509 struct lttng_ht_iter iter
;
3512 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3513 &iter
.iter
, sout
, node
.node
) {
3514 int64_t nb_packets_per_stream
;
3517 * Make a local copy of the output and assign the possible
3518 * temporary value given by the caller.
3520 memset(&tmp_output
, 0, sizeof(tmp_output
));
3521 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3523 if (output
->max_size
!= (uint64_t) -1ULL) {
3524 tmp_output
.max_size
= output
->max_size
;
3527 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3528 tmp_output
.max_size
);
3529 if (nb_packets_per_stream
< 0) {
3530 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3535 /* Use temporary name. */
3536 if (*output
->name
!= '\0') {
3537 strncpy(tmp_output
.name
, output
->name
,
3538 sizeof(tmp_output
.name
));
3541 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3543 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3544 wait
, nb_packets_per_stream
);
3545 if (ret
!= LTTNG_OK
) {
3549 snapshot_success
= 1;
3555 if (snapshot_success
) {
3556 session
->snapshot
.nb_snapshot
++;
3558 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3566 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3568 int cmd_set_session_shm_path(struct ltt_session
*session
,
3569 const char *shm_path
)
3575 * Can only set shm path before session is started.
3577 if (session
->has_been_started
) {
3578 return LTTNG_ERR_SESSION_STARTED
;
3581 strncpy(session
->shm_path
, shm_path
,
3582 sizeof(session
->shm_path
));
3583 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3589 * Init command subsystem.
3594 * Set network sequence index to 1 for streams to match a relayd
3595 * socket on the consumer side.
3597 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3598 relayd_net_seq_idx
= 1;
3599 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3601 DBG("Command subsystem initialized");