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.
23 #include <urcu/list.h>
24 #include <urcu/uatomic.h>
26 #include <common/defaults.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/relayd/relayd.h>
30 #include <common/utils.h>
35 #include "health-sessiond.h"
37 #include "kernel-consumer.h"
38 #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(int domain
, struct ltt_session
*session
,
145 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 /* Negate the error code to differentiate the size from an error */
426 * Add URI so the consumer output object. Set the correct path depending on the
427 * domain adding the default trace directory.
429 static int add_uri_to_consumer(struct consumer_output
*consumer
,
430 struct lttng_uri
*uri
, int domain
, const char *session_name
)
433 const char *default_trace_dir
;
437 if (consumer
== NULL
) {
438 DBG("No consumer detected. Don't add URI. Stopping.");
439 ret
= LTTNG_ERR_NO_CONSUMER
;
444 case LTTNG_DOMAIN_KERNEL
:
445 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
447 case LTTNG_DOMAIN_UST
:
448 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
452 * This case is possible is we try to add the URI to the global tracing
453 * session consumer object which in this case there is no subdir.
455 default_trace_dir
= "";
458 switch (uri
->dtype
) {
461 DBG2("Setting network URI to consumer");
463 if (consumer
->type
== CONSUMER_DST_NET
) {
464 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
465 consumer
->dst
.net
.control_isset
) ||
466 (uri
->stype
== LTTNG_STREAM_DATA
&&
467 consumer
->dst
.net
.data_isset
)) {
468 ret
= LTTNG_ERR_URL_EXIST
;
472 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
475 consumer
->type
= CONSUMER_DST_NET
;
477 /* Set URI into consumer output object */
478 ret
= consumer_set_network_uri(consumer
, uri
);
482 } else if (ret
== 1) {
484 * URI was the same in the consumer so we do not append the subdir
485 * again so to not duplicate output dir.
491 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
492 ret
= consumer_set_subdir(consumer
, session_name
);
494 ret
= LTTNG_ERR_FATAL
;
499 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
500 /* On a new subdir, reappend the default trace dir. */
501 strncat(consumer
->subdir
, default_trace_dir
,
502 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
503 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
508 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
509 memset(consumer
->dst
.trace_path
, 0,
510 sizeof(consumer
->dst
.trace_path
));
511 /* Explicit length checks for strcpy and strcat. */
512 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
513 >= sizeof(consumer
->dst
.trace_path
)) {
514 ret
= LTTNG_ERR_FATAL
;
517 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
518 /* Append default trace dir */
519 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
520 /* Flag consumer as local. */
521 consumer
->type
= CONSUMER_DST_LOCAL
;
532 * Init tracing by creating trace directory and sending fds kernel consumer.
534 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
537 struct lttng_ht_iter iter
;
538 struct consumer_socket
*socket
;
544 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
545 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
547 pthread_mutex_lock(socket
->lock
);
548 ret
= kernel_consumer_send_session(socket
, session
);
549 pthread_mutex_unlock(socket
->lock
);
551 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
563 * Create a socket to the relayd using the URI.
565 * On success, the relayd_sock pointer is set to the created socket.
566 * Else, it's stays untouched and a lttcomm error code is returned.
568 static int create_connect_relayd(struct lttng_uri
*uri
,
569 struct lttcomm_relayd_sock
**relayd_sock
)
572 struct lttcomm_relayd_sock
*rsock
;
574 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
575 RELAYD_VERSION_COMM_MINOR
);
577 ret
= LTTNG_ERR_FATAL
;
582 * Connect to relayd so we can proceed with a session creation. This call
583 * can possibly block for an arbitrary amount of time to set the health
584 * state to be in poll execution.
587 ret
= relayd_connect(rsock
);
590 ERR("Unable to reach lttng-relayd");
591 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
595 /* Create socket for control stream. */
596 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
597 DBG3("Creating relayd stream socket from URI");
599 /* Check relayd version */
600 ret
= relayd_version_check(rsock
);
602 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
605 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
606 DBG3("Creating relayd data socket from URI");
608 /* Command is not valid */
609 ERR("Relayd invalid stream type: %d", uri
->stype
);
610 ret
= LTTNG_ERR_INVALID
;
614 *relayd_sock
= rsock
;
619 /* The returned value is not useful since we are on an error path. */
620 (void) relayd_close(rsock
);
628 * Connect to the relayd using URI and send the socket to the right consumer.
630 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
631 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
632 struct consumer_socket
*consumer_sock
,
633 char *session_name
, char *hostname
, int session_live_timer
)
636 struct lttcomm_relayd_sock
*rsock
= NULL
;
638 /* Connect to relayd and make version check if uri is the control. */
639 ret
= create_connect_relayd(relayd_uri
, &rsock
);
640 if (ret
!= LTTNG_OK
) {
645 /* Set the network sequence index if not set. */
646 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
647 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
649 * Increment net_seq_idx because we are about to transfer the
650 * new relayd socket to the consumer.
651 * Assign unique key so the consumer can match streams.
653 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
654 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
657 /* Send relayd socket to consumer. */
658 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
659 relayd_uri
->stype
, session_id
,
660 session_name
, hostname
, session_live_timer
);
662 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
666 /* Flag that the corresponding socket was sent. */
667 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
668 consumer_sock
->control_sock_sent
= 1;
669 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
670 consumer_sock
->data_sock_sent
= 1;
676 * Close socket which was dup on the consumer side. The session daemon does
677 * NOT keep track of the relayd socket(s) once transfer to the consumer.
681 (void) relayd_close(rsock
);
685 if (ret
!= LTTNG_OK
) {
687 * The consumer output for this session should not be used anymore
688 * since the relayd connection failed thus making any tracing or/and
689 * streaming not usable.
691 consumer
->enabled
= 0;
697 * Send both relayd sockets to a specific consumer and domain. This is a
698 * helper function to facilitate sending the information to the consumer for a
701 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
702 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
703 char *session_name
, char *hostname
, int session_live_timer
)
710 /* Sending control relayd socket. */
711 if (!sock
->control_sock_sent
) {
712 ret
= send_consumer_relayd_socket(domain
, session_id
,
713 &consumer
->dst
.net
.control
, consumer
, sock
,
714 session_name
, hostname
, session_live_timer
);
715 if (ret
!= LTTNG_OK
) {
720 /* Sending data relayd socket. */
721 if (!sock
->data_sock_sent
) {
722 ret
= send_consumer_relayd_socket(domain
, session_id
,
723 &consumer
->dst
.net
.data
, consumer
, sock
,
724 session_name
, hostname
, session_live_timer
);
725 if (ret
!= LTTNG_OK
) {
735 * Setup relayd connections for a tracing session. First creates the socket to
736 * the relayd and send them to the right domain consumer. Consumer type MUST be
739 int cmd_setup_relayd(struct ltt_session
*session
)
742 struct ltt_ust_session
*usess
;
743 struct ltt_kernel_session
*ksess
;
744 struct consumer_socket
*socket
;
745 struct lttng_ht_iter iter
;
749 usess
= session
->ust_session
;
750 ksess
= session
->kernel_session
;
752 DBG("Setting relayd for session %s", session
->name
);
756 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
757 && usess
->consumer
->enabled
) {
758 /* For each consumer socket, send relayd sockets */
759 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
761 pthread_mutex_lock(socket
->lock
);
762 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
763 usess
->consumer
, socket
,
764 session
->name
, session
->hostname
,
765 session
->live_timer
);
766 pthread_mutex_unlock(socket
->lock
);
767 if (ret
!= LTTNG_OK
) {
770 /* Session is now ready for network streaming. */
771 session
->net_handle
= 1;
775 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
776 && ksess
->consumer
->enabled
) {
777 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
779 pthread_mutex_lock(socket
->lock
);
780 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
781 ksess
->consumer
, socket
,
782 session
->name
, session
->hostname
,
783 session
->live_timer
);
784 pthread_mutex_unlock(socket
->lock
);
785 if (ret
!= LTTNG_OK
) {
788 /* Session is now ready for network streaming. */
789 session
->net_handle
= 1;
799 * Start a kernel session by opening all necessary streams.
801 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
804 struct ltt_kernel_channel
*kchan
;
806 /* Open kernel metadata */
807 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
808 ret
= kernel_open_metadata(ksess
);
810 ret
= LTTNG_ERR_KERN_META_FAIL
;
815 /* Open kernel metadata stream */
816 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
817 ret
= kernel_open_metadata_stream(ksess
);
819 ERR("Kernel create metadata stream failed");
820 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
825 /* For each channel */
826 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
827 if (kchan
->stream_count
== 0) {
828 ret
= kernel_open_channel_stream(kchan
);
830 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
833 /* Update the stream global counter */
834 ksess
->stream_count_global
+= ret
;
838 /* Setup kernel consumer socket and send fds to it */
839 ret
= init_kernel_tracing(ksess
);
841 ret
= LTTNG_ERR_KERN_START_FAIL
;
845 /* This start the kernel tracing */
846 ret
= kernel_start_session(ksess
);
848 ret
= LTTNG_ERR_KERN_START_FAIL
;
852 /* Quiescent wait after starting trace */
853 kernel_wait_quiescent(kernel_tracer_fd
);
864 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
866 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
870 struct ltt_ust_session
*usess
;
872 usess
= session
->ust_session
;
877 case LTTNG_DOMAIN_KERNEL
:
879 ret
= channel_kernel_disable(session
->kernel_session
,
881 if (ret
!= LTTNG_OK
) {
885 kernel_wait_quiescent(kernel_tracer_fd
);
888 case LTTNG_DOMAIN_UST
:
890 struct ltt_ust_channel
*uchan
;
891 struct lttng_ht
*chan_ht
;
893 chan_ht
= usess
->domain_global
.channels
;
895 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
897 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
901 ret
= channel_ust_disable(usess
, uchan
);
902 if (ret
!= LTTNG_OK
) {
908 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
920 * Command LTTNG_TRACK_PID processed by the client thread.
922 * Called with session lock held.
924 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
931 case LTTNG_DOMAIN_KERNEL
:
933 struct ltt_kernel_session
*ksess
;
935 ksess
= session
->kernel_session
;
937 ret
= kernel_track_pid(ksess
, pid
);
938 if (ret
!= LTTNG_OK
) {
942 kernel_wait_quiescent(kernel_tracer_fd
);
945 case LTTNG_DOMAIN_UST
:
947 struct ltt_ust_session
*usess
;
949 usess
= session
->ust_session
;
951 ret
= trace_ust_track_pid(usess
, pid
);
952 if (ret
!= LTTNG_OK
) {
958 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
970 * Command LTTNG_UNTRACK_PID processed by the client thread.
972 * Called with session lock held.
974 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
981 case LTTNG_DOMAIN_KERNEL
:
983 struct ltt_kernel_session
*ksess
;
985 ksess
= session
->kernel_session
;
987 ret
= kernel_untrack_pid(ksess
, pid
);
988 if (ret
!= LTTNG_OK
) {
992 kernel_wait_quiescent(kernel_tracer_fd
);
995 case LTTNG_DOMAIN_UST
:
997 struct ltt_ust_session
*usess
;
999 usess
= session
->ust_session
;
1001 ret
= trace_ust_untrack_pid(usess
, pid
);
1002 if (ret
!= LTTNG_OK
) {
1008 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1020 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1022 * The wpipe arguments is used as a notifier for the kernel thread.
1024 int cmd_enable_channel(struct ltt_session
*session
,
1025 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1028 struct ltt_ust_session
*usess
= session
->ust_session
;
1029 struct lttng_ht
*chan_ht
;
1036 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1038 /* Validate channel name */
1039 if (attr
->name
[0] == '.' ||
1040 memchr(attr
->name
, '/', len
) != NULL
) {
1041 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1045 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1050 * Don't try to enable a channel if the session has been started at
1051 * some point in time before. The tracer does not allow it.
1053 if (session
->has_been_started
) {
1054 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1059 * If the session is a live session, remove the switch timer, the
1060 * live timer does the same thing but sends also synchronisation
1061 * beacons for inactive streams.
1063 if (session
->live_timer
> 0) {
1064 attr
->attr
.live_timer_interval
= session
->live_timer
;
1065 attr
->attr
.switch_timer_interval
= 0;
1069 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1070 * mode and with less than 2 subbuf so block it right away and send back an
1071 * invalid attribute error.
1073 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1074 ret
= LTTNG_ERR_INVALID
;
1078 switch (domain
->type
) {
1079 case LTTNG_DOMAIN_KERNEL
:
1081 struct ltt_kernel_channel
*kchan
;
1083 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1084 session
->kernel_session
);
1085 if (kchan
== NULL
) {
1086 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1087 if (attr
->name
[0] != '\0') {
1088 session
->kernel_session
->has_non_default_channel
= 1;
1091 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1094 if (ret
!= LTTNG_OK
) {
1098 kernel_wait_quiescent(kernel_tracer_fd
);
1101 case LTTNG_DOMAIN_UST
:
1102 case LTTNG_DOMAIN_JUL
:
1103 case LTTNG_DOMAIN_LOG4J
:
1104 case LTTNG_DOMAIN_PYTHON
:
1106 struct ltt_ust_channel
*uchan
;
1111 * Current agent implementation limitations force us to allow
1112 * only one channel at once in "agent" subdomains. Each
1113 * subdomain has a default channel name which must be strictly
1116 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1117 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1118 LTTNG_SYMBOL_NAME_LEN
)) {
1119 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1122 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1123 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1124 LTTNG_SYMBOL_NAME_LEN
)) {
1125 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1128 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1129 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1130 LTTNG_SYMBOL_NAME_LEN
)) {
1131 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1136 chan_ht
= usess
->domain_global
.channels
;
1138 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1139 if (uchan
== NULL
) {
1140 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1141 if (attr
->name
[0] != '\0') {
1142 usess
->has_non_default_channel
= 1;
1145 ret
= channel_ust_enable(usess
, uchan
);
1150 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1161 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1163 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1165 struct lttng_event
*event
)
1170 DBG("Disable event command for event \'%s\'", event
->name
);
1172 event_name
= event
->name
;
1173 if (validate_event_name(event_name
)) {
1174 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1178 /* Error out on unhandled search criteria */
1179 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1180 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1181 ret
= LTTNG_ERR_UNK
;
1188 case LTTNG_DOMAIN_KERNEL
:
1190 struct ltt_kernel_channel
*kchan
;
1191 struct ltt_kernel_session
*ksess
;
1193 ksess
= session
->kernel_session
;
1196 * If a non-default channel has been created in the
1197 * session, explicitely require that -c chan_name needs
1200 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1201 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1205 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1206 if (kchan
== NULL
) {
1207 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1211 switch (event
->type
) {
1212 case LTTNG_EVENT_ALL
:
1213 case LTTNG_EVENT_TRACEPOINT
:
1214 case LTTNG_EVENT_SYSCALL
:
1215 case LTTNG_EVENT_PROBE
:
1216 case LTTNG_EVENT_FUNCTION
:
1217 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1218 if (event_name
[0] == '\0') {
1219 ret
= event_kernel_disable_event(kchan
,
1222 ret
= event_kernel_disable_event(kchan
,
1223 event_name
, event
->type
);
1225 if (ret
!= LTTNG_OK
) {
1230 ret
= LTTNG_ERR_UNK
;
1234 kernel_wait_quiescent(kernel_tracer_fd
);
1237 case LTTNG_DOMAIN_UST
:
1239 struct ltt_ust_channel
*uchan
;
1240 struct ltt_ust_session
*usess
;
1242 usess
= session
->ust_session
;
1244 if (validate_ust_event_name(event_name
)) {
1245 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1250 * If a non-default channel has been created in the
1251 * session, explicitly require that -c chan_name needs
1254 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1255 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1259 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1261 if (uchan
== NULL
) {
1262 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1266 switch (event
->type
) {
1267 case LTTNG_EVENT_ALL
:
1269 * An empty event name means that everything
1270 * should be disabled.
1272 if (event
->name
[0] == '\0') {
1273 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1275 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1278 if (ret
!= LTTNG_OK
) {
1283 ret
= LTTNG_ERR_UNK
;
1287 DBG3("Disable UST event %s in channel %s completed", event_name
,
1291 case LTTNG_DOMAIN_LOG4J
:
1292 case LTTNG_DOMAIN_JUL
:
1293 case LTTNG_DOMAIN_PYTHON
:
1296 struct ltt_ust_session
*usess
= session
->ust_session
;
1300 switch (event
->type
) {
1301 case LTTNG_EVENT_ALL
:
1304 ret
= LTTNG_ERR_UNK
;
1308 agt
= trace_ust_find_agent(usess
, domain
);
1310 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1314 * An empty event name means that everything
1315 * should be disabled.
1317 if (event
->name
[0] == '\0') {
1318 ret
= event_agent_disable_all(usess
, agt
);
1320 ret
= event_agent_disable(usess
, agt
, event_name
);
1322 if (ret
!= LTTNG_OK
) {
1329 ret
= LTTNG_ERR_UND
;
1342 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1344 int cmd_add_context(struct ltt_session
*session
, int domain
,
1345 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1347 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1350 case LTTNG_DOMAIN_KERNEL
:
1351 assert(session
->kernel_session
);
1353 if (session
->kernel_session
->channel_count
== 0) {
1354 /* Create default channel */
1355 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1356 if (ret
!= LTTNG_OK
) {
1359 chan_kern_created
= 1;
1361 /* Add kernel context to kernel tracer */
1362 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1363 if (ret
!= LTTNG_OK
) {
1367 case LTTNG_DOMAIN_UST
:
1369 struct ltt_ust_session
*usess
= session
->ust_session
;
1370 unsigned int chan_count
;
1374 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1375 if (chan_count
== 0) {
1376 struct lttng_channel
*attr
;
1377 /* Create default channel */
1378 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1380 ret
= LTTNG_ERR_FATAL
;
1384 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1385 if (ret
!= LTTNG_OK
) {
1390 chan_ust_created
= 1;
1393 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1394 if (ret
!= LTTNG_OK
) {
1400 ret
= LTTNG_ERR_UND
;
1407 if (chan_kern_created
) {
1408 struct ltt_kernel_channel
*kchan
=
1409 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1410 session
->kernel_session
);
1411 /* Created previously, this should NOT fail. */
1413 kernel_destroy_channel(kchan
);
1416 if (chan_ust_created
) {
1417 struct ltt_ust_channel
*uchan
=
1418 trace_ust_find_channel_by_name(
1419 session
->ust_session
->domain_global
.channels
,
1420 DEFAULT_CHANNEL_NAME
);
1421 /* Created previously, this should NOT fail. */
1423 /* Remove from the channel list of the session. */
1424 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1426 trace_ust_destroy_channel(uchan
);
1431 static int validate_event_name(const char *name
)
1434 const char *c
= name
;
1435 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1438 * Make sure that unescaped wildcards are only used as the last
1439 * character of the event name.
1441 while (c
< event_name_end
) {
1449 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1450 /* Wildcard is not the last character */
1451 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1463 static inline bool name_starts_with(const char *name
, const char *prefix
)
1465 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1467 return !strncmp(name
, prefix
, max_cmp_len
);
1470 /* Perform userspace-specific event name validation */
1471 static int validate_ust_event_name(const char *name
)
1481 * Check name against all internal UST event component namespaces used
1484 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1485 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1486 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1495 * Internal version of cmd_enable_event() with a supplemental
1496 * "internal_event" flag which is used to enable internal events which should
1497 * be hidden from clients. Such events are used in the agent implementation to
1498 * enable the events through which all "agent" events are funeled.
1500 static int _cmd_enable_event(struct ltt_session
*session
,
1501 struct lttng_domain
*domain
,
1502 char *channel_name
, struct lttng_event
*event
,
1503 char *filter_expression
,
1504 struct lttng_filter_bytecode
*filter
,
1505 struct lttng_event_exclusion
*exclusion
,
1506 int wpipe
, bool internal_event
)
1508 int ret
, channel_created
= 0;
1509 struct lttng_channel
*attr
;
1513 assert(channel_name
);
1515 DBG("Enable event command for event \'%s\'", event
->name
);
1519 ret
= validate_event_name(event
->name
);
1524 switch (domain
->type
) {
1525 case LTTNG_DOMAIN_KERNEL
:
1527 struct ltt_kernel_channel
*kchan
;
1530 * If a non-default channel has been created in the
1531 * session, explicitely require that -c chan_name needs
1534 if (session
->kernel_session
->has_non_default_channel
1535 && channel_name
[0] == '\0') {
1536 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1540 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1541 session
->kernel_session
);
1542 if (kchan
== NULL
) {
1543 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1544 LTTNG_BUFFER_GLOBAL
);
1546 ret
= LTTNG_ERR_FATAL
;
1549 if (lttng_strncpy(attr
->name
, channel_name
,
1550 sizeof(attr
->name
))) {
1551 ret
= LTTNG_ERR_INVALID
;
1556 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1557 if (ret
!= LTTNG_OK
) {
1563 channel_created
= 1;
1566 /* Get the newly created kernel channel pointer */
1567 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1568 session
->kernel_session
);
1569 if (kchan
== NULL
) {
1570 /* This sould not happen... */
1571 ret
= LTTNG_ERR_FATAL
;
1575 switch (event
->type
) {
1576 case LTTNG_EVENT_ALL
:
1578 char *filter_expression_a
= NULL
;
1579 struct lttng_filter_bytecode
*filter_a
= NULL
;
1582 * We need to duplicate filter_expression and filter,
1583 * because ownership is passed to first enable
1586 if (filter_expression
) {
1587 filter_expression_a
= strdup(filter_expression
);
1588 if (!filter_expression_a
) {
1589 ret
= LTTNG_ERR_FATAL
;
1594 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1596 free(filter_expression_a
);
1597 ret
= LTTNG_ERR_FATAL
;
1600 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1602 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1603 ret
= event_kernel_enable_event(kchan
, event
,
1604 filter_expression
, filter
);
1605 /* We have passed ownership */
1606 filter_expression
= NULL
;
1608 if (ret
!= LTTNG_OK
) {
1609 if (channel_created
) {
1610 /* Let's not leak a useless channel. */
1611 kernel_destroy_channel(kchan
);
1613 free(filter_expression_a
);
1617 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1618 ret
= event_kernel_enable_event(kchan
, event
,
1619 filter_expression_a
, filter_a
);
1620 /* We have passed ownership */
1621 filter_expression_a
= NULL
;
1623 if (ret
!= LTTNG_OK
) {
1628 case LTTNG_EVENT_PROBE
:
1629 case LTTNG_EVENT_FUNCTION
:
1630 case LTTNG_EVENT_FUNCTION_ENTRY
:
1631 case LTTNG_EVENT_TRACEPOINT
:
1632 ret
= event_kernel_enable_event(kchan
, event
,
1633 filter_expression
, filter
);
1634 /* We have passed ownership */
1635 filter_expression
= NULL
;
1637 if (ret
!= LTTNG_OK
) {
1638 if (channel_created
) {
1639 /* Let's not leak a useless channel. */
1640 kernel_destroy_channel(kchan
);
1645 case LTTNG_EVENT_SYSCALL
:
1646 ret
= event_kernel_enable_event(kchan
, event
,
1647 filter_expression
, filter
);
1648 /* We have passed ownership */
1649 filter_expression
= NULL
;
1651 if (ret
!= LTTNG_OK
) {
1656 ret
= LTTNG_ERR_UNK
;
1660 kernel_wait_quiescent(kernel_tracer_fd
);
1663 case LTTNG_DOMAIN_UST
:
1665 struct ltt_ust_channel
*uchan
;
1666 struct ltt_ust_session
*usess
= session
->ust_session
;
1671 * If a non-default channel has been created in the
1672 * session, explicitely require that -c chan_name needs
1675 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1676 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1680 /* Get channel from global UST domain */
1681 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1683 if (uchan
== NULL
) {
1684 /* Create default channel */
1685 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1686 usess
->buffer_type
);
1688 ret
= LTTNG_ERR_FATAL
;
1691 if (lttng_strncpy(attr
->name
, channel_name
,
1692 sizeof(attr
->name
))) {
1693 ret
= LTTNG_ERR_INVALID
;
1698 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1699 if (ret
!= LTTNG_OK
) {
1705 /* Get the newly created channel reference back */
1706 uchan
= trace_ust_find_channel_by_name(
1707 usess
->domain_global
.channels
, channel_name
);
1711 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1713 * Don't allow users to add UST events to channels which
1714 * are assigned to a userspace subdomain (JUL, Log4J,
1717 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
1721 if (!internal_event
) {
1723 * Ensure the event name is not reserved for internal
1726 ret
= validate_ust_event_name(event
->name
);
1728 WARN("Userspace event name %s failed validation.",
1730 event
->name
: "NULL");
1731 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1736 /* At this point, the session and channel exist on the tracer */
1737 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1738 filter_expression
, filter
, exclusion
,
1740 /* We have passed ownership */
1741 filter_expression
= NULL
;
1744 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1745 goto already_enabled
;
1746 } else if (ret
!= LTTNG_OK
) {
1751 case LTTNG_DOMAIN_LOG4J
:
1752 case LTTNG_DOMAIN_JUL
:
1753 case LTTNG_DOMAIN_PYTHON
:
1755 const char *default_event_name
, *default_chan_name
;
1757 struct lttng_event uevent
;
1758 struct lttng_domain tmp_dom
;
1759 struct ltt_ust_session
*usess
= session
->ust_session
;
1763 agt
= trace_ust_find_agent(usess
, domain
->type
);
1765 agt
= agent_create(domain
->type
);
1767 ret
= LTTNG_ERR_NOMEM
;
1770 agent_add(agt
, usess
->agents
);
1773 /* Create the default tracepoint. */
1774 memset(&uevent
, 0, sizeof(uevent
));
1775 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1776 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1777 default_event_name
= event_get_default_agent_ust_name(
1779 if (!default_event_name
) {
1780 ret
= LTTNG_ERR_FATAL
;
1783 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1784 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1787 * The domain type is changed because we are about to enable the
1788 * default channel and event for the JUL domain that are hardcoded.
1789 * This happens in the UST domain.
1791 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1792 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1794 switch (domain
->type
) {
1795 case LTTNG_DOMAIN_LOG4J
:
1796 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1798 case LTTNG_DOMAIN_JUL
:
1799 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1801 case LTTNG_DOMAIN_PYTHON
:
1802 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1805 /* The switch/case we are in should avoid this else big problem */
1810 char *filter_expression_copy
= NULL
;
1811 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1814 const size_t filter_size
= sizeof(
1815 struct lttng_filter_bytecode
)
1818 filter_copy
= zmalloc(filter_size
);
1820 ret
= LTTNG_ERR_NOMEM
;
1823 memcpy(filter_copy
, filter
, filter_size
);
1825 filter_expression_copy
=
1826 strdup(filter_expression
);
1827 if (!filter_expression
) {
1828 ret
= LTTNG_ERR_NOMEM
;
1831 if (!filter_expression_copy
|| !filter_copy
) {
1832 free(filter_expression_copy
);
1838 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1839 (char *) default_chan_name
,
1840 &uevent
, filter_expression_copy
,
1841 filter_copy
, NULL
, wpipe
);
1844 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1845 goto already_enabled
;
1846 } else if (ret
!= LTTNG_OK
) {
1850 /* The wild card * means that everything should be enabled. */
1851 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1852 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1855 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1859 filter_expression
= NULL
;
1860 if (ret
!= LTTNG_OK
) {
1867 ret
= LTTNG_ERR_UND
;
1875 free(filter_expression
);
1883 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1884 * We own filter, exclusion, and filter_expression.
1886 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1887 char *channel_name
, struct lttng_event
*event
,
1888 char *filter_expression
,
1889 struct lttng_filter_bytecode
*filter
,
1890 struct lttng_event_exclusion
*exclusion
,
1893 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1894 filter_expression
, filter
, exclusion
, wpipe
, false);
1898 * Enable an event which is internal to LTTng. An internal should
1899 * never be made visible to clients and are immune to checks such as
1902 static int cmd_enable_event_internal(struct ltt_session
*session
,
1903 struct lttng_domain
*domain
,
1904 char *channel_name
, struct lttng_event
*event
,
1905 char *filter_expression
,
1906 struct lttng_filter_bytecode
*filter
,
1907 struct lttng_event_exclusion
*exclusion
,
1910 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1911 filter_expression
, filter
, exclusion
, wpipe
, true);
1915 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1917 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1920 ssize_t nb_events
= 0;
1923 case LTTNG_DOMAIN_KERNEL
:
1924 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1925 if (nb_events
< 0) {
1926 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1930 case LTTNG_DOMAIN_UST
:
1931 nb_events
= ust_app_list_events(events
);
1932 if (nb_events
< 0) {
1933 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1937 case LTTNG_DOMAIN_LOG4J
:
1938 case LTTNG_DOMAIN_JUL
:
1939 case LTTNG_DOMAIN_PYTHON
:
1940 nb_events
= agent_list_events(events
, domain
);
1941 if (nb_events
< 0) {
1942 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1947 ret
= LTTNG_ERR_UND
;
1954 /* Return negative value to differentiate return code */
1959 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1961 ssize_t
cmd_list_tracepoint_fields(int domain
,
1962 struct lttng_event_field
**fields
)
1965 ssize_t nb_fields
= 0;
1968 case LTTNG_DOMAIN_UST
:
1969 nb_fields
= ust_app_list_event_fields(fields
);
1970 if (nb_fields
< 0) {
1971 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1975 case LTTNG_DOMAIN_KERNEL
:
1976 default: /* fall-through */
1977 ret
= LTTNG_ERR_UND
;
1984 /* Return negative value to differentiate return code */
1988 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1990 return syscall_table_list(events
);
1994 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
1996 * Called with session lock held.
1998 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
1999 int domain
, int32_t **pids
)
2002 ssize_t nr_pids
= 0;
2005 case LTTNG_DOMAIN_KERNEL
:
2007 struct ltt_kernel_session
*ksess
;
2009 ksess
= session
->kernel_session
;
2010 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2012 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2017 case LTTNG_DOMAIN_UST
:
2019 struct ltt_ust_session
*usess
;
2021 usess
= session
->ust_session
;
2022 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2024 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2029 case LTTNG_DOMAIN_LOG4J
:
2030 case LTTNG_DOMAIN_JUL
:
2031 case LTTNG_DOMAIN_PYTHON
:
2033 ret
= LTTNG_ERR_UND
;
2040 /* Return negative value to differentiate return code */
2045 * Command LTTNG_START_TRACE processed by the client thread.
2047 * Called with session mutex held.
2049 int cmd_start_trace(struct ltt_session
*session
)
2052 unsigned long nb_chan
= 0;
2053 struct ltt_kernel_session
*ksession
;
2054 struct ltt_ust_session
*usess
;
2058 /* Ease our life a bit ;) */
2059 ksession
= session
->kernel_session
;
2060 usess
= session
->ust_session
;
2062 /* Is the session already started? */
2063 if (session
->active
) {
2064 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2069 * Starting a session without channel is useless since after that it's not
2070 * possible to enable channel thus inform the client.
2072 if (usess
&& usess
->domain_global
.channels
) {
2074 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2078 nb_chan
+= ksession
->channel_count
;
2081 ret
= LTTNG_ERR_NO_CHANNEL
;
2085 /* Kernel tracing */
2086 if (ksession
!= NULL
) {
2087 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2088 if (ret
!= LTTNG_OK
) {
2093 /* Flag session that trace should start automatically */
2096 * Even though the start trace might fail, flag this session active so
2097 * other application coming in are started by default.
2101 ret
= ust_app_start_trace_all(usess
);
2103 ret
= LTTNG_ERR_UST_START_FAIL
;
2108 /* Flag this after a successful start. */
2109 session
->has_been_started
= 1;
2110 session
->active
= 1;
2119 * Command LTTNG_STOP_TRACE processed by the client thread.
2121 int cmd_stop_trace(struct ltt_session
*session
)
2124 struct ltt_kernel_channel
*kchan
;
2125 struct ltt_kernel_session
*ksession
;
2126 struct ltt_ust_session
*usess
;
2131 ksession
= session
->kernel_session
;
2132 usess
= session
->ust_session
;
2134 /* Session is not active. Skip everythong and inform the client. */
2135 if (!session
->active
) {
2136 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2141 if (ksession
&& ksession
->active
) {
2142 DBG("Stop kernel tracing");
2144 /* Flush metadata if exist */
2145 if (ksession
->metadata_stream_fd
>= 0) {
2146 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2148 ERR("Kernel metadata flush failed");
2152 /* Flush all buffers before stopping */
2153 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2154 ret
= kernel_flush_buffer(kchan
);
2156 ERR("Kernel flush buffer error");
2160 ret
= kernel_stop_session(ksession
);
2162 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2166 kernel_wait_quiescent(kernel_tracer_fd
);
2168 ksession
->active
= 0;
2171 if (usess
&& usess
->active
) {
2173 * Even though the stop trace might fail, flag this session inactive so
2174 * other application coming in are not started by default.
2178 ret
= ust_app_stop_trace_all(usess
);
2180 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2185 /* Flag inactive after a successful stop. */
2186 session
->active
= 0;
2194 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2196 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2197 struct lttng_uri
*uris
)
2200 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2201 struct ltt_ust_session
*usess
= session
->ust_session
;
2207 /* Can't set consumer URI if the session is active. */
2208 if (session
->active
) {
2209 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2213 /* Set the "global" consumer URIs */
2214 for (i
= 0; i
< nb_uri
; i
++) {
2215 ret
= add_uri_to_consumer(session
->consumer
,
2216 &uris
[i
], 0, session
->name
);
2217 if (ret
!= LTTNG_OK
) {
2222 /* Set UST session URIs */
2223 if (session
->ust_session
) {
2224 for (i
= 0; i
< nb_uri
; i
++) {
2225 ret
= add_uri_to_consumer(
2226 session
->ust_session
->consumer
,
2227 &uris
[i
], LTTNG_DOMAIN_UST
,
2229 if (ret
!= LTTNG_OK
) {
2235 /* Set kernel session URIs */
2236 if (session
->kernel_session
) {
2237 for (i
= 0; i
< nb_uri
; i
++) {
2238 ret
= add_uri_to_consumer(
2239 session
->kernel_session
->consumer
,
2240 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2242 if (ret
!= LTTNG_OK
) {
2249 * Make sure to set the session in output mode after we set URI since a
2250 * session can be created without URL (thus flagged in no output mode).
2252 session
->output_traces
= 1;
2254 ksess
->output_traces
= 1;
2258 usess
->output_traces
= 1;
2269 * Command LTTNG_CREATE_SESSION processed by the client thread.
2271 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2272 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2275 struct ltt_session
*session
;
2281 * Verify if the session already exist
2283 * XXX: There is no need for the session lock list here since the caller
2284 * (process_client_msg) is holding it. We might want to change that so a
2285 * single command does not lock the entire session list.
2287 session
= session_find_by_name(name
);
2288 if (session
!= NULL
) {
2289 ret
= LTTNG_ERR_EXIST_SESS
;
2293 /* Create tracing session in the registry */
2294 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2295 LTTNG_SOCK_GET_GID_CRED(creds
));
2296 if (ret
!= LTTNG_OK
) {
2301 * Get the newly created session pointer back
2303 * XXX: There is no need for the session lock list here since the caller
2304 * (process_client_msg) is holding it. We might want to change that so a
2305 * single command does not lock the entire session list.
2307 session
= session_find_by_name(name
);
2310 session
->live_timer
= live_timer
;
2311 /* Create default consumer output for the session not yet created. */
2312 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2313 if (session
->consumer
== NULL
) {
2314 ret
= LTTNG_ERR_FATAL
;
2315 goto consumer_error
;
2319 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2320 if (ret
!= LTTNG_OK
) {
2321 goto consumer_error
;
2323 session
->output_traces
= 1;
2325 session
->output_traces
= 0;
2326 DBG2("Session %s created with no output", session
->name
);
2329 session
->consumer
->enabled
= 1;
2334 session_destroy(session
);
2341 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2343 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2344 size_t nb_uri
, lttng_sock_cred
*creds
)
2347 struct ltt_session
*session
;
2348 struct snapshot_output
*new_output
= NULL
;
2354 * Create session in no output mode with URIs set to NULL. The uris we've
2355 * received are for a default snapshot output if one.
2357 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2358 if (ret
!= LTTNG_OK
) {
2362 /* Get the newly created session pointer back. This should NEVER fail. */
2363 session
= session_find_by_name(name
);
2366 /* Flag session for snapshot mode. */
2367 session
->snapshot_mode
= 1;
2369 /* Skip snapshot output creation if no URI is given. */
2374 new_output
= snapshot_output_alloc();
2376 ret
= LTTNG_ERR_NOMEM
;
2377 goto error_snapshot_alloc
;
2380 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2381 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2383 if (ret
== -ENOMEM
) {
2384 ret
= LTTNG_ERR_NOMEM
;
2386 ret
= LTTNG_ERR_INVALID
;
2388 goto error_snapshot
;
2392 snapshot_add_output(&session
->snapshot
, new_output
);
2399 snapshot_output_destroy(new_output
);
2400 error_snapshot_alloc
:
2401 session_destroy(session
);
2407 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2409 * Called with session lock held.
2411 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2414 struct ltt_ust_session
*usess
;
2415 struct ltt_kernel_session
*ksess
;
2420 usess
= session
->ust_session
;
2421 ksess
= session
->kernel_session
;
2423 /* Clean kernel session teardown */
2424 kernel_destroy_session(ksess
);
2426 /* UST session teardown */
2428 /* Close any relayd session */
2429 consumer_output_send_destroy_relayd(usess
->consumer
);
2431 /* Destroy every UST application related to this session. */
2432 ret
= ust_app_destroy_trace_all(usess
);
2434 ERR("Error in ust_app_destroy_trace_all");
2437 /* Clean up the rest. */
2438 trace_ust_destroy_session(usess
);
2442 * Must notify the kernel thread here to update it's poll set in order to
2443 * remove the channel(s)' fd just destroyed.
2445 ret
= notify_thread_pipe(wpipe
);
2447 PERROR("write kernel poll pipe");
2450 ret
= session_destroy(session
);
2456 * Command LTTNG_CALIBRATE processed by the client thread.
2458 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2463 case LTTNG_DOMAIN_KERNEL
:
2465 struct lttng_kernel_calibrate kcalibrate
;
2467 switch (calibrate
->type
) {
2468 case LTTNG_CALIBRATE_FUNCTION
:
2470 /* Default and only possible calibrate option. */
2471 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2475 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2477 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2482 case LTTNG_DOMAIN_UST
:
2484 struct lttng_ust_calibrate ucalibrate
;
2486 switch (calibrate
->type
) {
2487 case LTTNG_CALIBRATE_FUNCTION
:
2489 /* Default and only possible calibrate option. */
2490 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2494 ret
= ust_app_calibrate_glb(&ucalibrate
);
2496 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2502 ret
= LTTNG_ERR_UND
;
2513 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2515 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2516 const char *sock_path
, struct consumer_data
*cdata
)
2519 struct consumer_socket
*socket
= NULL
;
2526 case LTTNG_DOMAIN_KERNEL
:
2528 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2532 /* Can't register a consumer if there is already one */
2533 if (ksess
->consumer_fds_sent
!= 0) {
2534 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2538 sock
= lttcomm_connect_unix_sock(sock_path
);
2540 ret
= LTTNG_ERR_CONNECT_FAIL
;
2543 cdata
->cmd_sock
= sock
;
2545 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2546 if (socket
== NULL
) {
2549 PERROR("close register consumer");
2551 cdata
->cmd_sock
= -1;
2552 ret
= LTTNG_ERR_FATAL
;
2556 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2557 if (socket
->lock
== NULL
) {
2558 PERROR("zmalloc pthread mutex");
2559 ret
= LTTNG_ERR_FATAL
;
2562 pthread_mutex_init(socket
->lock
, NULL
);
2563 socket
->registered
= 1;
2566 consumer_add_socket(socket
, ksess
->consumer
);
2569 pthread_mutex_lock(&cdata
->pid_mutex
);
2571 pthread_mutex_unlock(&cdata
->pid_mutex
);
2576 /* TODO: Userspace tracing */
2577 ret
= LTTNG_ERR_UND
;
2585 consumer_destroy_socket(socket
);
2591 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2593 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2594 struct lttng_domain
**domains
)
2599 struct lttng_ht_iter iter
;
2601 if (session
->kernel_session
!= NULL
) {
2602 DBG3("Listing domains found kernel domain");
2606 if (session
->ust_session
!= NULL
) {
2607 DBG3("Listing domains found UST global domain");
2611 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2613 if (agt
->being_used
) {
2624 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2625 if (*domains
== NULL
) {
2626 ret
= LTTNG_ERR_FATAL
;
2630 if (session
->kernel_session
!= NULL
) {
2631 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2633 /* Kernel session buffer type is always GLOBAL */
2634 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2639 if (session
->ust_session
!= NULL
) {
2640 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2641 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2645 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2647 if (agt
->being_used
) {
2648 (*domains
)[index
].type
= agt
->domain
;
2649 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2659 /* Return negative value to differentiate return code */
2665 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2667 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2668 struct lttng_channel
**channels
)
2671 ssize_t nb_chan
= 0;
2674 case LTTNG_DOMAIN_KERNEL
:
2675 if (session
->kernel_session
!= NULL
) {
2676 nb_chan
= session
->kernel_session
->channel_count
;
2678 DBG3("Number of kernel channels %zd", nb_chan
);
2680 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2683 case LTTNG_DOMAIN_UST
:
2684 if (session
->ust_session
!= NULL
) {
2686 nb_chan
= lttng_ht_get_count(
2687 session
->ust_session
->domain_global
.channels
);
2690 DBG3("Number of UST global channels %zd", nb_chan
);
2692 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2697 ret
= LTTNG_ERR_UND
;
2702 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2703 if (*channels
== NULL
) {
2704 ret
= LTTNG_ERR_FATAL
;
2708 list_lttng_channels(domain
, session
, *channels
);
2714 /* Return negative value to differentiate return code */
2719 * Command LTTNG_LIST_EVENTS processed by the client thread.
2721 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2722 char *channel_name
, struct lttng_event
**events
)
2725 ssize_t nb_event
= 0;
2728 case LTTNG_DOMAIN_KERNEL
:
2729 if (session
->kernel_session
!= NULL
) {
2730 nb_event
= list_lttng_kernel_events(channel_name
,
2731 session
->kernel_session
, events
);
2734 case LTTNG_DOMAIN_UST
:
2736 if (session
->ust_session
!= NULL
) {
2737 nb_event
= list_lttng_ust_global_events(channel_name
,
2738 &session
->ust_session
->domain_global
, events
);
2742 case LTTNG_DOMAIN_LOG4J
:
2743 case LTTNG_DOMAIN_JUL
:
2744 case LTTNG_DOMAIN_PYTHON
:
2745 if (session
->ust_session
) {
2746 struct lttng_ht_iter iter
;
2750 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2751 &iter
.iter
, agt
, node
.node
) {
2752 if (agt
->domain
== domain
) {
2753 nb_event
= list_lttng_agent_events(
2762 ret
= LTTNG_ERR_UND
;
2769 /* Return negative value to differentiate return code */
2774 * Using the session list, filled a lttng_session array to send back to the
2775 * client for session listing.
2777 * The session list lock MUST be acquired before calling this function. Use
2778 * session_lock_list() and session_unlock_list().
2780 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2785 struct ltt_session
*session
;
2786 struct ltt_session_list
*list
= session_get_list();
2788 DBG("Getting all available session for UID %d GID %d",
2791 * Iterate over session list and append data after the control struct in
2794 cds_list_for_each_entry(session
, &list
->head
, list
) {
2796 * Only list the sessions the user can control.
2798 if (!session_access_ok(session
, uid
, gid
)) {
2802 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2803 struct ltt_ust_session
*usess
= session
->ust_session
;
2805 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2806 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2807 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2808 ret
= build_network_session_path(sessions
[i
].path
,
2809 sizeof(sessions
[i
].path
), session
);
2811 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2812 session
->consumer
->dst
.trace_path
);
2815 PERROR("snprintf session path");
2819 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2820 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2821 sessions
[i
].enabled
= session
->active
;
2822 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2823 sessions
[i
].live_timer_interval
= session
->live_timer
;
2829 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2830 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2832 int cmd_data_pending(struct ltt_session
*session
)
2835 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2836 struct ltt_ust_session
*usess
= session
->ust_session
;
2840 /* Session MUST be stopped to ask for data availability. */
2841 if (session
->active
) {
2842 ret
= LTTNG_ERR_SESSION_STARTED
;
2846 * If stopped, just make sure we've started before else the above call
2847 * will always send that there is data pending.
2849 * The consumer assumes that when the data pending command is received,
2850 * the trace has been started before or else no output data is written
2851 * by the streams which is a condition for data pending. So, this is
2852 * *VERY* important that we don't ask the consumer before a start
2855 if (!session
->has_been_started
) {
2861 if (ksess
&& ksess
->consumer
) {
2862 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2864 /* Data is still being extracted for the kernel. */
2869 if (usess
&& usess
->consumer
) {
2870 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2872 /* Data is still being extracted for the kernel. */
2877 /* Data is ready to be read by a viewer */
2885 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2887 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2889 int cmd_snapshot_add_output(struct ltt_session
*session
,
2890 struct lttng_snapshot_output
*output
, uint32_t *id
)
2893 struct snapshot_output
*new_output
;
2898 DBG("Cmd snapshot add output for session %s", session
->name
);
2901 * Permission denied to create an output if the session is not
2902 * set in no output mode.
2904 if (session
->output_traces
) {
2905 ret
= LTTNG_ERR_EPERM
;
2909 /* Only one output is allowed until we have the "tee" feature. */
2910 if (session
->snapshot
.nb_output
== 1) {
2911 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2915 new_output
= snapshot_output_alloc();
2917 ret
= LTTNG_ERR_NOMEM
;
2921 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2922 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2923 &session
->snapshot
);
2925 if (ret
== -ENOMEM
) {
2926 ret
= LTTNG_ERR_NOMEM
;
2928 ret
= LTTNG_ERR_INVALID
;
2934 snapshot_add_output(&session
->snapshot
, new_output
);
2936 *id
= new_output
->id
;
2943 snapshot_output_destroy(new_output
);
2949 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2951 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2953 int cmd_snapshot_del_output(struct ltt_session
*session
,
2954 struct lttng_snapshot_output
*output
)
2957 struct snapshot_output
*sout
= NULL
;
2965 * Permission denied to create an output if the session is not
2966 * set in no output mode.
2968 if (session
->output_traces
) {
2969 ret
= LTTNG_ERR_EPERM
;
2974 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2976 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2977 } else if (*output
->name
!= '\0') {
2978 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2980 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2983 ret
= LTTNG_ERR_INVALID
;
2987 snapshot_delete_output(&session
->snapshot
, sout
);
2988 snapshot_output_destroy(sout
);
2997 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2999 * If no output is available, outputs is untouched and 0 is returned.
3001 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3003 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3004 struct lttng_snapshot_output
**outputs
)
3007 struct lttng_snapshot_output
*list
= NULL
;
3008 struct lttng_ht_iter iter
;
3009 struct snapshot_output
*output
;
3014 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3017 * Permission denied to create an output if the session is not
3018 * set in no output mode.
3020 if (session
->output_traces
) {
3021 ret
= -LTTNG_ERR_EPERM
;
3025 if (session
->snapshot
.nb_output
== 0) {
3030 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3032 ret
= -LTTNG_ERR_NOMEM
;
3036 /* Copy list from session to the new list object. */
3038 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3039 output
, node
.node
) {
3040 assert(output
->consumer
);
3041 list
[idx
].id
= output
->id
;
3042 list
[idx
].max_size
= output
->max_size
;
3043 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3044 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3045 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3046 sizeof(list
[idx
].ctrl_url
));
3049 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3050 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3052 ret
= -LTTNG_ERR_NOMEM
;
3057 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3058 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3060 ret
= -LTTNG_ERR_NOMEM
;
3069 ret
= session
->snapshot
.nb_output
;
3077 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3078 * snapshot output is *not* set with a remote destination.
3080 * Return 0 on success or a LTTNG_ERR code.
3082 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3083 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3086 struct lttng_ht_iter iter
;
3087 struct consumer_socket
*socket
;
3090 assert(snap_output
);
3093 DBG2("Set relayd object from snapshot output");
3095 /* Ignore if snapshot consumer output is not network. */
3096 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3101 * For each consumer socket, create and send the relayd object of the
3105 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3106 socket
, node
.node
) {
3107 ret
= send_consumer_relayd_sockets(0, session
->id
,
3108 snap_output
->consumer
, socket
,
3109 session
->name
, session
->hostname
,
3110 session
->live_timer
);
3111 if (ret
!= LTTNG_OK
) {
3123 * Record a kernel snapshot.
3125 * Return LTTNG_OK on success or a LTTNG_ERR code.
3127 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3128 struct snapshot_output
*output
, struct ltt_session
*session
,
3129 int wait
, uint64_t nb_packets_per_stream
)
3139 * Copy kernel session sockets so we can communicate with the right
3140 * consumer for the snapshot record command.
3142 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3144 ret
= LTTNG_ERR_NOMEM
;
3148 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3149 if (ret
!= LTTNG_OK
) {
3150 goto error_snapshot
;
3153 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3154 if (ret
!= LTTNG_OK
) {
3155 goto error_snapshot
;
3162 /* Clean up copied sockets so this output can use some other later on. */
3163 consumer_destroy_output_sockets(output
->consumer
);
3170 * Record a UST snapshot.
3172 * Return 0 on success or a LTTNG_ERR error code.
3174 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3175 struct snapshot_output
*output
, struct ltt_session
*session
,
3176 int wait
, uint64_t nb_packets_per_stream
)
3185 * Copy UST session sockets so we can communicate with the right
3186 * consumer for the snapshot record command.
3188 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3190 ret
= LTTNG_ERR_NOMEM
;
3194 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3195 if (ret
!= LTTNG_OK
) {
3196 goto error_snapshot
;
3199 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3203 ret
= LTTNG_ERR_INVALID
;
3206 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3209 goto error_snapshot
;
3215 /* Clean up copied sockets so this output can use some other later on. */
3216 consumer_destroy_output_sockets(output
->consumer
);
3222 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3223 uint64_t cur_nr_packets
)
3225 uint64_t tot_size
= 0;
3227 if (session
->kernel_session
) {
3228 struct ltt_kernel_channel
*chan
;
3229 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3231 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3232 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3234 * Don't take channel into account if we
3235 * already grab all its packets.
3239 tot_size
+= chan
->channel
->attr
.subbuf_size
3240 * chan
->stream_count
;
3244 if (session
->ust_session
) {
3245 struct ltt_ust_session
*usess
= session
->ust_session
;
3247 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3255 * Calculate the number of packets we can grab from each stream that
3256 * fits within the overall snapshot max size.
3258 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3259 * the number of packets per stream.
3261 * TODO: this approach is not perfect: we consider the worse case
3262 * (packet filling the sub-buffers) as an upper bound, but we could do
3263 * better if we do this calculation while we actually grab the packet
3264 * content: we would know how much padding we don't actually store into
3267 * This algorithm is currently bounded by the number of packets per
3270 * Since we call this algorithm before actually grabbing the data, it's
3271 * an approximation: for instance, applications could appear/disappear
3272 * in between this call and actually grabbing data.
3275 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3278 uint64_t cur_nb_packets
= 0;
3281 return 0; /* Infinite */
3284 size_left
= max_size
;
3286 uint64_t one_more_packet_tot_size
;
3288 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3290 if (!one_more_packet_tot_size
) {
3291 /* We are already grabbing all packets. */
3294 size_left
-= one_more_packet_tot_size
;
3295 if (size_left
< 0) {
3300 if (!cur_nb_packets
) {
3301 /* Not enough room to grab one packet of each stream, error. */
3304 return cur_nb_packets
;
3308 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3310 * The wait parameter is ignored so this call always wait for the snapshot to
3311 * complete before returning.
3313 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3315 int cmd_snapshot_record(struct ltt_session
*session
,
3316 struct lttng_snapshot_output
*output
, int wait
)
3319 unsigned int use_tmp_output
= 0;
3320 struct snapshot_output tmp_output
;
3321 unsigned int snapshot_success
= 0;
3327 DBG("Cmd snapshot record for session %s", session
->name
);
3329 /* Get the datetime for the snapshot output directory. */
3330 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3333 ret
= LTTNG_ERR_INVALID
;
3338 * Permission denied to create an output if the session is not
3339 * set in no output mode.
3341 if (session
->output_traces
) {
3342 ret
= LTTNG_ERR_EPERM
;
3346 /* The session needs to be started at least once. */
3347 if (!session
->has_been_started
) {
3348 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3352 /* Use temporary output for the session. */
3353 if (*output
->ctrl_url
!= '\0') {
3354 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3355 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3358 if (ret
== -ENOMEM
) {
3359 ret
= LTTNG_ERR_NOMEM
;
3361 ret
= LTTNG_ERR_INVALID
;
3365 /* Use the global session count for the temporary snapshot. */
3366 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3368 /* Use the global datetime */
3369 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3373 if (use_tmp_output
) {
3374 int64_t nb_packets_per_stream
;
3376 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3377 tmp_output
.max_size
);
3378 if (nb_packets_per_stream
< 0) {
3379 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3383 if (session
->kernel_session
) {
3384 ret
= record_kernel_snapshot(session
->kernel_session
,
3385 &tmp_output
, session
,
3386 wait
, nb_packets_per_stream
);
3387 if (ret
!= LTTNG_OK
) {
3392 if (session
->ust_session
) {
3393 ret
= record_ust_snapshot(session
->ust_session
,
3394 &tmp_output
, session
,
3395 wait
, nb_packets_per_stream
);
3396 if (ret
!= LTTNG_OK
) {
3401 snapshot_success
= 1;
3403 struct snapshot_output
*sout
;
3404 struct lttng_ht_iter iter
;
3407 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3408 &iter
.iter
, sout
, node
.node
) {
3409 int64_t nb_packets_per_stream
;
3412 * Make a local copy of the output and assign the possible
3413 * temporary value given by the caller.
3415 memset(&tmp_output
, 0, sizeof(tmp_output
));
3416 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3418 if (output
->max_size
!= (uint64_t) -1ULL) {
3419 tmp_output
.max_size
= output
->max_size
;
3422 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3423 tmp_output
.max_size
);
3424 if (nb_packets_per_stream
< 0) {
3425 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3430 /* Use temporary name. */
3431 if (*output
->name
!= '\0') {
3432 strncpy(tmp_output
.name
, output
->name
,
3433 sizeof(tmp_output
.name
));
3436 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3437 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3439 if (session
->kernel_session
) {
3440 ret
= record_kernel_snapshot(session
->kernel_session
,
3441 &tmp_output
, session
,
3442 wait
, nb_packets_per_stream
);
3443 if (ret
!= LTTNG_OK
) {
3449 if (session
->ust_session
) {
3450 ret
= record_ust_snapshot(session
->ust_session
,
3451 &tmp_output
, session
,
3452 wait
, nb_packets_per_stream
);
3453 if (ret
!= LTTNG_OK
) {
3458 snapshot_success
= 1;
3463 if (snapshot_success
) {
3464 session
->snapshot
.nb_snapshot
++;
3466 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3474 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3476 int cmd_set_session_shm_path(struct ltt_session
*session
,
3477 const char *shm_path
)
3483 * Can only set shm path before session is started.
3485 if (session
->has_been_started
) {
3486 return LTTNG_ERR_SESSION_STARTED
;
3489 strncpy(session
->shm_path
, shm_path
,
3490 sizeof(session
->shm_path
));
3491 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3497 * Init command subsystem.
3502 * Set network sequence index to 1 for streams to match a relayd
3503 * socket on the consumer side.
3505 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3506 relayd_net_seq_idx
= 1;
3507 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3509 DBG("Command subsystem initialized");