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>
33 #include "health-sessiond.h"
35 #include "kernel-consumer.h"
36 #include "lttng-sessiond.h"
42 * Used to keep a unique index for each relayd socket created where this value
43 * is associated with streams on the consumer so it can match the right relayd
44 * to send to. It must be accessed with the relayd_net_seq_idx_lock
47 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 static uint64_t relayd_net_seq_idx
;
51 * Create a session path used by list_lttng_sessions for the case that the
52 * session consumer is on the network.
54 static int build_network_session_path(char *dst
, size_t size
,
55 struct ltt_session
*session
)
57 int ret
, kdata_port
, udata_port
;
58 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
59 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
64 memset(tmp_urls
, 0, sizeof(tmp_urls
));
65 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
67 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
69 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
70 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
71 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
74 if (session
->ust_session
&& session
->ust_session
->consumer
) {
75 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
76 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
79 if (uuri
== NULL
&& kuri
== NULL
) {
80 uri
= &session
->consumer
->dst
.net
.control
;
81 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
82 } else if (kuri
&& uuri
) {
83 ret
= uri_compare(kuri
, uuri
);
87 /* Build uuri URL string */
88 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
95 } else if (kuri
&& uuri
== NULL
) {
97 } else if (uuri
&& kuri
== NULL
) {
101 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
107 * Do we have a UST url set. If yes, this means we have both kernel and UST
110 if (*tmp_uurl
!= '\0') {
111 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
112 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
115 if (kuri
|| (!kuri
&& !uuri
)) {
118 /* No kernel URI, use the UST port. */
121 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
129 * Fill lttng_channel array of all channels.
131 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
132 struct lttng_channel
*channels
)
135 struct ltt_kernel_channel
*kchan
;
137 DBG("Listing channels for session %s", session
->name
);
140 case LTTNG_DOMAIN_KERNEL
:
141 /* Kernel channels */
142 if (session
->kernel_session
!= NULL
) {
143 cds_list_for_each_entry(kchan
,
144 &session
->kernel_session
->channel_list
.head
, list
) {
145 /* Copy lttng_channel struct to array */
146 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
147 channels
[i
].enabled
= kchan
->enabled
;
152 case LTTNG_DOMAIN_UST
:
154 struct lttng_ht_iter iter
;
155 struct ltt_ust_channel
*uchan
;
158 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
159 &iter
.iter
, uchan
, node
.node
) {
160 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
161 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
162 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
163 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
164 channels
[i
].attr
.switch_timer_interval
=
165 uchan
->attr
.switch_timer_interval
;
166 channels
[i
].attr
.read_timer_interval
=
167 uchan
->attr
.read_timer_interval
;
168 channels
[i
].enabled
= uchan
->enabled
;
169 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
170 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
171 switch (uchan
->attr
.output
) {
174 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
188 * Create a list of agent domain events.
190 * Return number of events in list on success or else a negative value.
192 static int list_lttng_agent_events(struct agent
*agt
,
193 struct lttng_event
**events
)
196 unsigned int nb_event
= 0;
197 struct agent_event
*event
;
198 struct lttng_event
*tmp_events
;
199 struct lttng_ht_iter iter
;
204 DBG3("Listing agent events");
206 nb_event
= lttng_ht_get_count(agt
->events
);
212 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
214 PERROR("zmalloc agent events session");
215 ret
= -LTTNG_ERR_FATAL
;
220 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
221 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
222 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
223 tmp_events
[i
].enabled
= event
->enabled
;
224 tmp_events
[i
].loglevel
= event
->loglevel
;
225 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
230 *events
= tmp_events
;
234 assert(nb_event
== i
);
239 * Create a list of ust global domain events.
241 static int list_lttng_ust_global_events(char *channel_name
,
242 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
245 unsigned int nb_event
= 0;
246 struct lttng_ht_iter iter
;
247 struct lttng_ht_node_str
*node
;
248 struct ltt_ust_channel
*uchan
;
249 struct ltt_ust_event
*uevent
;
250 struct lttng_event
*tmp
;
252 DBG("Listing UST global events for channel %s", channel_name
);
256 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
257 node
= lttng_ht_iter_get_node_str(&iter
);
259 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
263 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
265 nb_event
+= lttng_ht_get_count(uchan
->events
);
272 DBG3("Listing UST global %d events", nb_event
);
274 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
276 ret
= LTTNG_ERR_FATAL
;
280 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
281 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
282 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
283 tmp
[i
].enabled
= uevent
->enabled
;
285 switch (uevent
->attr
.instrumentation
) {
286 case LTTNG_UST_TRACEPOINT
:
287 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
289 case LTTNG_UST_PROBE
:
290 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
292 case LTTNG_UST_FUNCTION
:
293 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
297 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
298 switch (uevent
->attr
.loglevel_type
) {
299 case LTTNG_UST_LOGLEVEL_ALL
:
300 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
302 case LTTNG_UST_LOGLEVEL_RANGE
:
303 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
305 case LTTNG_UST_LOGLEVEL_SINGLE
:
306 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
309 if (uevent
->filter
) {
312 if (uevent
->exclusion
) {
313 tmp
[i
].exclusion
= 1;
327 * Fill lttng_event array of all kernel events in the channel.
329 static int list_lttng_kernel_events(char *channel_name
,
330 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
333 unsigned int nb_event
;
334 struct ltt_kernel_event
*event
;
335 struct ltt_kernel_channel
*kchan
;
337 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
339 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
343 nb_event
= kchan
->event_count
;
345 DBG("Listing events for channel %s", kchan
->channel
->name
);
351 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
352 if (*events
== NULL
) {
353 ret
= LTTNG_ERR_FATAL
;
357 /* Kernel channels */
358 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
359 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
360 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
361 (*events
)[i
].enabled
= event
->enabled
;
363 switch (event
->event
->instrumentation
) {
364 case LTTNG_KERNEL_TRACEPOINT
:
365 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
367 case LTTNG_KERNEL_KRETPROBE
:
368 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
369 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
370 sizeof(struct lttng_kernel_kprobe
));
372 case LTTNG_KERNEL_KPROBE
:
373 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
374 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
375 sizeof(struct lttng_kernel_kprobe
));
377 case LTTNG_KERNEL_FUNCTION
:
378 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
379 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
380 sizeof(struct lttng_kernel_function
));
382 case LTTNG_KERNEL_NOOP
:
383 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
385 case LTTNG_KERNEL_SYSCALL
:
386 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
388 case LTTNG_KERNEL_ALL
:
399 /* Negate the error code to differentiate the size from an error */
404 * Add URI so the consumer output object. Set the correct path depending on the
405 * domain adding the default trace directory.
407 static int add_uri_to_consumer(struct consumer_output
*consumer
,
408 struct lttng_uri
*uri
, int domain
, const char *session_name
)
411 const char *default_trace_dir
;
415 if (consumer
== NULL
) {
416 DBG("No consumer detected. Don't add URI. Stopping.");
417 ret
= LTTNG_ERR_NO_CONSUMER
;
422 case LTTNG_DOMAIN_KERNEL
:
423 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
425 case LTTNG_DOMAIN_UST
:
426 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
430 * This case is possible is we try to add the URI to the global tracing
431 * session consumer object which in this case there is no subdir.
433 default_trace_dir
= "";
436 switch (uri
->dtype
) {
439 DBG2("Setting network URI to consumer");
441 if (consumer
->type
== CONSUMER_DST_NET
) {
442 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
443 consumer
->dst
.net
.control_isset
) ||
444 (uri
->stype
== LTTNG_STREAM_DATA
&&
445 consumer
->dst
.net
.data_isset
)) {
446 ret
= LTTNG_ERR_URL_EXIST
;
450 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
453 consumer
->type
= CONSUMER_DST_NET
;
455 /* Set URI into consumer output object */
456 ret
= consumer_set_network_uri(consumer
, uri
);
460 } else if (ret
== 1) {
462 * URI was the same in the consumer so we do not append the subdir
463 * again so to not duplicate output dir.
469 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
470 ret
= consumer_set_subdir(consumer
, session_name
);
472 ret
= LTTNG_ERR_FATAL
;
477 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
478 /* On a new subdir, reappend the default trace dir. */
479 strncat(consumer
->subdir
, default_trace_dir
,
480 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
481 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
486 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
487 memset(consumer
->dst
.trace_path
, 0,
488 sizeof(consumer
->dst
.trace_path
));
489 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
490 sizeof(consumer
->dst
.trace_path
));
491 /* Append default trace dir */
492 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
493 sizeof(consumer
->dst
.trace_path
) -
494 strlen(consumer
->dst
.trace_path
) - 1);
495 /* Flag consumer as local. */
496 consumer
->type
= CONSUMER_DST_LOCAL
;
507 * Init tracing by creating trace directory and sending fds kernel consumer.
509 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
512 struct lttng_ht_iter iter
;
513 struct consumer_socket
*socket
;
519 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
520 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
522 pthread_mutex_lock(socket
->lock
);
523 ret
= kernel_consumer_send_session(socket
, session
);
524 pthread_mutex_unlock(socket
->lock
);
526 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
538 * Create a socket to the relayd using the URI.
540 * On success, the relayd_sock pointer is set to the created socket.
541 * Else, it's stays untouched and a lttcomm error code is returned.
543 static int create_connect_relayd(struct lttng_uri
*uri
,
544 struct lttcomm_relayd_sock
**relayd_sock
)
547 struct lttcomm_relayd_sock
*rsock
;
549 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
550 RELAYD_VERSION_COMM_MINOR
);
552 ret
= LTTNG_ERR_FATAL
;
557 * Connect to relayd so we can proceed with a session creation. This call
558 * can possibly block for an arbitrary amount of time to set the health
559 * state to be in poll execution.
562 ret
= relayd_connect(rsock
);
565 ERR("Unable to reach lttng-relayd");
566 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
570 /* Create socket for control stream. */
571 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
572 DBG3("Creating relayd stream socket from URI");
574 /* Check relayd version */
575 ret
= relayd_version_check(rsock
);
577 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
580 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
581 DBG3("Creating relayd data socket from URI");
583 /* Command is not valid */
584 ERR("Relayd invalid stream type: %d", uri
->stype
);
585 ret
= LTTNG_ERR_INVALID
;
589 *relayd_sock
= rsock
;
594 /* The returned value is not useful since we are on an error path. */
595 (void) relayd_close(rsock
);
603 * Connect to the relayd using URI and send the socket to the right consumer.
605 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
606 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
607 struct consumer_socket
*consumer_sock
,
608 char *session_name
, char *hostname
, int session_live_timer
)
611 struct lttcomm_relayd_sock
*rsock
= NULL
;
613 /* Connect to relayd and make version check if uri is the control. */
614 ret
= create_connect_relayd(relayd_uri
, &rsock
);
615 if (ret
!= LTTNG_OK
) {
620 /* Set the network sequence index if not set. */
621 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
622 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
624 * Increment net_seq_idx because we are about to transfer the
625 * new relayd socket to the consumer.
626 * Assign unique key so the consumer can match streams.
628 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
629 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
632 /* Send relayd socket to consumer. */
633 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
634 relayd_uri
->stype
, session_id
,
635 session_name
, hostname
, session_live_timer
);
637 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
641 /* Flag that the corresponding socket was sent. */
642 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
643 consumer_sock
->control_sock_sent
= 1;
644 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
645 consumer_sock
->data_sock_sent
= 1;
651 * Close socket which was dup on the consumer side. The session daemon does
652 * NOT keep track of the relayd socket(s) once transfer to the consumer.
656 (void) relayd_close(rsock
);
660 if (ret
!= LTTNG_OK
) {
662 * The consumer output for this session should not be used anymore
663 * since the relayd connection failed thus making any tracing or/and
664 * streaming not usable.
666 consumer
->enabled
= 0;
672 * Send both relayd sockets to a specific consumer and domain. This is a
673 * helper function to facilitate sending the information to the consumer for a
676 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
677 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
678 char *session_name
, char *hostname
, int session_live_timer
)
685 /* Sending control relayd socket. */
686 if (!sock
->control_sock_sent
) {
687 ret
= send_consumer_relayd_socket(domain
, session_id
,
688 &consumer
->dst
.net
.control
, consumer
, sock
,
689 session_name
, hostname
, session_live_timer
);
690 if (ret
!= LTTNG_OK
) {
695 /* Sending data relayd socket. */
696 if (!sock
->data_sock_sent
) {
697 ret
= send_consumer_relayd_socket(domain
, session_id
,
698 &consumer
->dst
.net
.data
, consumer
, sock
,
699 session_name
, hostname
, session_live_timer
);
700 if (ret
!= LTTNG_OK
) {
710 * Setup relayd connections for a tracing session. First creates the socket to
711 * the relayd and send them to the right domain consumer. Consumer type MUST be
714 int cmd_setup_relayd(struct ltt_session
*session
)
717 struct ltt_ust_session
*usess
;
718 struct ltt_kernel_session
*ksess
;
719 struct consumer_socket
*socket
;
720 struct lttng_ht_iter iter
;
724 usess
= session
->ust_session
;
725 ksess
= session
->kernel_session
;
727 DBG("Setting relayd for session %s", session
->name
);
731 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
732 && usess
->consumer
->enabled
) {
733 /* For each consumer socket, send relayd sockets */
734 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
736 pthread_mutex_lock(socket
->lock
);
737 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
738 usess
->consumer
, socket
,
739 session
->name
, session
->hostname
,
740 session
->live_timer
);
741 pthread_mutex_unlock(socket
->lock
);
742 if (ret
!= LTTNG_OK
) {
745 /* Session is now ready for network streaming. */
746 session
->net_handle
= 1;
750 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
751 && ksess
->consumer
->enabled
) {
752 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
754 pthread_mutex_lock(socket
->lock
);
755 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
756 ksess
->consumer
, socket
,
757 session
->name
, session
->hostname
,
758 session
->live_timer
);
759 pthread_mutex_unlock(socket
->lock
);
760 if (ret
!= LTTNG_OK
) {
763 /* Session is now ready for network streaming. */
764 session
->net_handle
= 1;
774 * Start a kernel session by opening all necessary streams.
776 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
779 struct ltt_kernel_channel
*kchan
;
781 /* Open kernel metadata */
782 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
783 ret
= kernel_open_metadata(ksess
);
785 ret
= LTTNG_ERR_KERN_META_FAIL
;
790 /* Open kernel metadata stream */
791 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
792 ret
= kernel_open_metadata_stream(ksess
);
794 ERR("Kernel create metadata stream failed");
795 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
800 /* For each channel */
801 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
802 if (kchan
->stream_count
== 0) {
803 ret
= kernel_open_channel_stream(kchan
);
805 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
808 /* Update the stream global counter */
809 ksess
->stream_count_global
+= ret
;
813 /* Setup kernel consumer socket and send fds to it */
814 ret
= init_kernel_tracing(ksess
);
816 ret
= LTTNG_ERR_KERN_START_FAIL
;
820 /* This start the kernel tracing */
821 ret
= kernel_start_session(ksess
);
823 ret
= LTTNG_ERR_KERN_START_FAIL
;
827 /* Quiescent wait after starting trace */
828 kernel_wait_quiescent(kernel_tracer_fd
);
839 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
841 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
845 struct ltt_ust_session
*usess
;
847 usess
= session
->ust_session
;
852 case LTTNG_DOMAIN_KERNEL
:
854 ret
= channel_kernel_disable(session
->kernel_session
,
856 if (ret
!= LTTNG_OK
) {
860 kernel_wait_quiescent(kernel_tracer_fd
);
863 case LTTNG_DOMAIN_UST
:
865 struct ltt_ust_channel
*uchan
;
866 struct lttng_ht
*chan_ht
;
868 chan_ht
= usess
->domain_global
.channels
;
870 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
872 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
876 ret
= channel_ust_disable(usess
, uchan
);
877 if (ret
!= LTTNG_OK
) {
883 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
884 case LTTNG_DOMAIN_UST_EXEC_NAME
:
885 case LTTNG_DOMAIN_UST_PID
:
888 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
900 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
902 * The wpipe arguments is used as a notifier for the kernel thread.
904 int cmd_enable_channel(struct ltt_session
*session
,
905 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
908 struct ltt_ust_session
*usess
= session
->ust_session
;
909 struct lttng_ht
*chan_ht
;
915 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
920 * Don't try to enable a channel if the session has been started at
921 * some point in time before. The tracer does not allow it.
923 if (session
->has_been_started
) {
924 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
929 * If the session is a live session, remove the switch timer, the
930 * live timer does the same thing but sends also synchronisation
931 * beacons for inactive streams.
933 if (session
->live_timer
> 0) {
934 attr
->attr
.live_timer_interval
= session
->live_timer
;
935 attr
->attr
.switch_timer_interval
= 0;
939 * The ringbuffer (both in user space and kernel) behave badly in overwrite
940 * mode and with less than 2 subbuf so block it right away and send back an
941 * invalid attribute error.
943 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
944 ret
= LTTNG_ERR_INVALID
;
948 switch (domain
->type
) {
949 case LTTNG_DOMAIN_KERNEL
:
951 struct ltt_kernel_channel
*kchan
;
953 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
954 session
->kernel_session
);
956 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
957 if (attr
->name
[0] != '\0') {
958 session
->kernel_session
->has_non_default_channel
= 1;
961 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
964 if (ret
!= LTTNG_OK
) {
968 kernel_wait_quiescent(kernel_tracer_fd
);
971 case LTTNG_DOMAIN_UST
:
973 struct ltt_ust_channel
*uchan
;
975 chan_ht
= usess
->domain_global
.channels
;
977 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
979 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
980 if (attr
->name
[0] != '\0') {
981 usess
->has_non_default_channel
= 1;
984 ret
= channel_ust_enable(usess
, uchan
);
989 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1000 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1002 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1004 struct lttng_event
*event
)
1009 event_name
= event
->name
;
1011 if (event
->loglevel_type
|| event
->loglevel
|| event
->enabled
1012 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1013 return LTTNG_ERR_UNK
;
1019 case LTTNG_DOMAIN_KERNEL
:
1021 struct ltt_kernel_channel
*kchan
;
1022 struct ltt_kernel_session
*ksess
;
1024 ksess
= session
->kernel_session
;
1027 * If a non-default channel has been created in the
1028 * session, explicitely require that -c chan_name needs
1031 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1032 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1036 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1037 if (kchan
== NULL
) {
1038 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1042 switch (event
->type
) {
1043 case LTTNG_EVENT_ALL
:
1044 case LTTNG_EVENT_TRACEPOINT
:
1045 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1046 if (ret
!= LTTNG_OK
) {
1050 case LTTNG_EVENT_SYSCALL
:
1051 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1054 ret
= LTTNG_ERR_UNK
;
1058 kernel_wait_quiescent(kernel_tracer_fd
);
1061 case LTTNG_DOMAIN_UST
:
1063 struct ltt_ust_channel
*uchan
;
1064 struct ltt_ust_session
*usess
;
1066 usess
= session
->ust_session
;
1069 * If a non-default channel has been created in the
1070 * session, explicitely require that -c chan_name needs
1073 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1074 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1078 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1080 if (uchan
== NULL
) {
1081 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1085 switch (event
->type
) {
1086 case LTTNG_EVENT_ALL
:
1087 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1088 if (ret
!= LTTNG_OK
) {
1093 ret
= LTTNG_ERR_UNK
;
1097 DBG3("Disable UST event %s in channel %s completed", event_name
,
1101 case LTTNG_DOMAIN_LOG4J
:
1102 case LTTNG_DOMAIN_JUL
:
1105 struct ltt_ust_session
*usess
= session
->ust_session
;
1109 switch (event
->type
) {
1110 case LTTNG_EVENT_ALL
:
1113 ret
= LTTNG_ERR_UNK
;
1117 agt
= trace_ust_find_agent(usess
, domain
);
1119 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1123 ret
= event_agent_disable(usess
, agt
, event_name
);
1124 if (ret
!= LTTNG_OK
) {
1131 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1132 case LTTNG_DOMAIN_UST_PID
:
1133 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1136 ret
= LTTNG_ERR_UND
;
1148 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1150 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1152 struct lttng_event
*event
)
1157 event_name
= event
->name
;
1162 case LTTNG_DOMAIN_KERNEL
:
1164 struct ltt_kernel_session
*ksess
;
1165 struct ltt_kernel_channel
*kchan
;
1167 ksess
= session
->kernel_session
;
1170 * If a non-default channel has been created in the
1171 * session, explicitely require that -c chan_name needs
1174 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1175 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1179 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1180 if (kchan
== NULL
) {
1181 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1185 switch (event
->type
) {
1186 case LTTNG_EVENT_ALL
:
1187 ret
= event_kernel_disable_all(kchan
);
1188 if (ret
!= LTTNG_OK
) {
1192 case LTTNG_EVENT_SYSCALL
:
1193 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1196 ret
= LTTNG_ERR_UNK
;
1200 kernel_wait_quiescent(kernel_tracer_fd
);
1203 case LTTNG_DOMAIN_UST
:
1205 struct ltt_ust_session
*usess
;
1206 struct ltt_ust_channel
*uchan
;
1208 usess
= session
->ust_session
;
1211 * If a non-default channel has been created in the
1212 * session, explicitely require that -c chan_name needs
1215 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1216 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1220 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1222 if (uchan
== NULL
) {
1223 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1227 switch (event
->type
) {
1228 case LTTNG_EVENT_ALL
:
1229 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1235 ret
= LTTNG_ERR_UNK
;
1239 DBG3("Disable all UST events in channel %s completed", channel_name
);
1243 case LTTNG_DOMAIN_LOG4J
:
1244 case LTTNG_DOMAIN_JUL
:
1247 struct ltt_ust_session
*usess
= session
->ust_session
;
1251 switch (event
->type
) {
1252 case LTTNG_EVENT_ALL
:
1255 ret
= LTTNG_ERR_UNK
;
1259 agt
= trace_ust_find_agent(usess
, domain
);
1261 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1265 ret
= event_agent_disable_all(usess
, agt
);
1266 if (ret
!= LTTNG_OK
) {
1273 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1274 case LTTNG_DOMAIN_UST_PID
:
1275 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1278 ret
= LTTNG_ERR_UND
;
1290 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1292 int cmd_add_context(struct ltt_session
*session
, int domain
,
1293 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1295 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1298 case LTTNG_DOMAIN_KERNEL
:
1299 assert(session
->kernel_session
);
1301 if (session
->kernel_session
->channel_count
== 0) {
1302 /* Create default channel */
1303 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1304 if (ret
!= LTTNG_OK
) {
1307 chan_kern_created
= 1;
1309 /* Add kernel context to kernel tracer */
1310 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1311 if (ret
!= LTTNG_OK
) {
1315 case LTTNG_DOMAIN_UST
:
1317 struct ltt_ust_session
*usess
= session
->ust_session
;
1318 unsigned int chan_count
;
1322 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1323 if (chan_count
== 0) {
1324 struct lttng_channel
*attr
;
1325 /* Create default channel */
1326 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1328 ret
= LTTNG_ERR_FATAL
;
1332 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1333 if (ret
!= LTTNG_OK
) {
1338 chan_ust_created
= 1;
1341 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1342 if (ret
!= LTTNG_OK
) {
1348 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1349 case LTTNG_DOMAIN_UST_PID
:
1350 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1353 ret
= LTTNG_ERR_UND
;
1360 if (chan_kern_created
) {
1361 struct ltt_kernel_channel
*kchan
=
1362 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1363 session
->kernel_session
);
1364 /* Created previously, this should NOT fail. */
1366 kernel_destroy_channel(kchan
);
1369 if (chan_ust_created
) {
1370 struct ltt_ust_channel
*uchan
=
1371 trace_ust_find_channel_by_name(
1372 session
->ust_session
->domain_global
.channels
,
1373 DEFAULT_CHANNEL_NAME
);
1374 /* Created previously, this should NOT fail. */
1376 /* Remove from the channel list of the session. */
1377 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1379 trace_ust_destroy_channel(uchan
);
1384 static int validate_event_name(const char *name
)
1387 const char *c
= name
;
1388 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1391 * Make sure that unescaped wildcards are only used as the last
1392 * character of the event name.
1394 while (c
< event_name_end
) {
1402 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1403 /* Wildcard is not the last character */
1404 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1417 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1419 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1420 char *channel_name
, struct lttng_event
*event
,
1421 char *filter_expression
,
1422 struct lttng_filter_bytecode
*filter
,
1423 struct lttng_event_exclusion
*exclusion
,
1426 int ret
, channel_created
= 0;
1427 struct lttng_channel
*attr
;
1431 assert(channel_name
);
1433 ret
= validate_event_name(event
->name
);
1440 switch (domain
->type
) {
1441 case LTTNG_DOMAIN_KERNEL
:
1443 struct ltt_kernel_channel
*kchan
;
1446 * If a non-default channel has been created in the
1447 * session, explicitely require that -c chan_name needs
1450 if (session
->kernel_session
->has_non_default_channel
1451 && channel_name
[0] == '\0') {
1452 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1456 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1457 session
->kernel_session
);
1458 if (kchan
== NULL
) {
1459 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1460 LTTNG_BUFFER_GLOBAL
);
1462 ret
= LTTNG_ERR_FATAL
;
1465 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1467 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1468 if (ret
!= LTTNG_OK
) {
1474 channel_created
= 1;
1477 /* Get the newly created kernel channel pointer */
1478 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1479 session
->kernel_session
);
1480 if (kchan
== NULL
) {
1481 /* This sould not happen... */
1482 ret
= LTTNG_ERR_FATAL
;
1486 switch (event
->type
) {
1487 case LTTNG_EVENT_ALL
:
1488 case LTTNG_EVENT_TRACEPOINT
:
1489 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1490 if (ret
!= LTTNG_OK
) {
1491 if (channel_created
) {
1492 /* Let's not leak a useless channel. */
1493 kernel_destroy_channel(kchan
);
1498 case LTTNG_EVENT_SYSCALL
:
1499 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1502 ret
= LTTNG_ERR_UNK
;
1506 kernel_wait_quiescent(kernel_tracer_fd
);
1509 case LTTNG_DOMAIN_UST
:
1511 struct ltt_ust_channel
*uchan
;
1512 struct ltt_ust_session
*usess
= session
->ust_session
;
1517 * If a non-default channel has been created in the
1518 * session, explicitely require that -c chan_name needs
1521 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1522 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1526 /* Get channel from global UST domain */
1527 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1529 if (uchan
== NULL
) {
1530 /* Create default channel */
1531 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1532 usess
->buffer_type
);
1534 ret
= LTTNG_ERR_FATAL
;
1537 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1539 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1540 if (ret
!= LTTNG_OK
) {
1546 /* Get the newly created channel reference back */
1547 uchan
= trace_ust_find_channel_by_name(
1548 usess
->domain_global
.channels
, channel_name
);
1552 /* At this point, the session and channel exist on the tracer */
1553 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1554 filter_expression
, filter
, exclusion
);
1555 if (ret
!= LTTNG_OK
) {
1560 case LTTNG_DOMAIN_LOG4J
:
1561 case LTTNG_DOMAIN_JUL
:
1563 const char *default_event_name
, *default_chan_name
;
1565 struct lttng_event uevent
;
1566 struct lttng_domain tmp_dom
;
1567 struct ltt_ust_session
*usess
= session
->ust_session
;
1571 agt
= trace_ust_find_agent(usess
, domain
->type
);
1573 agt
= agent_create(domain
->type
);
1575 ret
= -LTTNG_ERR_NOMEM
;
1578 agent_add(agt
, usess
->agents
);
1581 /* Create the default tracepoint. */
1582 memset(&uevent
, 0, sizeof(uevent
));
1583 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1584 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1585 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1586 if (!default_event_name
) {
1587 ret
= -LTTNG_ERR_FATAL
;
1590 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1591 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1594 * The domain type is changed because we are about to enable the
1595 * default channel and event for the JUL domain that are hardcoded.
1596 * This happens in the UST domain.
1598 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1599 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1601 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1602 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1604 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1607 ret
= cmd_enable_event(session
, &tmp_dom
, (char *) default_chan_name
,
1608 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1609 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1613 /* The wild card * means that everything should be enabled. */
1614 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1615 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1617 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1619 if (ret
!= LTTNG_OK
) {
1626 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1627 case LTTNG_DOMAIN_UST_PID
:
1628 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1631 ret
= LTTNG_ERR_UND
;
1643 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1645 int cmd_enable_event_all(struct ltt_session
*session
,
1646 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1647 char *filter_expression
,
1648 struct lttng_filter_bytecode
*filter
, int wpipe
)
1651 struct lttng_channel
*attr
;
1654 assert(channel_name
);
1658 switch (domain
->type
) {
1659 case LTTNG_DOMAIN_KERNEL
:
1661 struct ltt_kernel_channel
*kchan
;
1663 assert(session
->kernel_session
);
1666 * If a non-default channel has been created in the
1667 * session, explicitely require that -c chan_name needs
1670 if (session
->kernel_session
->has_non_default_channel
1671 && channel_name
[0] == '\0') {
1672 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1676 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1677 session
->kernel_session
);
1678 if (kchan
== NULL
) {
1679 /* Create default channel */
1680 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1681 LTTNG_BUFFER_GLOBAL
);
1683 ret
= LTTNG_ERR_FATAL
;
1686 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1688 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1689 if (ret
!= LTTNG_OK
) {
1695 /* Get the newly created kernel channel pointer */
1696 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1697 session
->kernel_session
);
1701 switch (event_type
) {
1702 case LTTNG_EVENT_SYSCALL
:
1703 ret
= event_kernel_enable_syscall(kchan
, "");
1705 case LTTNG_EVENT_TRACEPOINT
:
1707 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1708 * events already registered to the channel.
1710 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1712 case LTTNG_EVENT_ALL
:
1713 /* Enable syscalls and tracepoints */
1714 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1717 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1721 /* Manage return value */
1722 if (ret
!= LTTNG_OK
) {
1724 * On error, cmd_enable_channel call will take care of destroying
1725 * the created channel if it was needed.
1730 kernel_wait_quiescent(kernel_tracer_fd
);
1733 case LTTNG_DOMAIN_UST
:
1735 struct ltt_ust_channel
*uchan
;
1736 struct ltt_ust_session
*usess
= session
->ust_session
;
1741 * If a non-default channel has been created in the
1742 * session, explicitely require that -c chan_name needs
1745 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1746 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1750 /* Get channel from global UST domain */
1751 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1753 if (uchan
== NULL
) {
1754 /* Create default channel */
1755 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1756 usess
->buffer_type
);
1758 ret
= LTTNG_ERR_FATAL
;
1761 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1763 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1764 if (ret
!= LTTNG_OK
) {
1770 /* Get the newly created channel reference back */
1771 uchan
= trace_ust_find_channel_by_name(
1772 usess
->domain_global
.channels
, channel_name
);
1776 /* At this point, the session and channel exist on the tracer */
1778 switch (event_type
) {
1779 case LTTNG_EVENT_ALL
:
1780 case LTTNG_EVENT_TRACEPOINT
:
1781 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1782 filter_expression
, filter
);
1783 if (ret
!= LTTNG_OK
) {
1788 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1792 /* Manage return value */
1793 if (ret
!= LTTNG_OK
) {
1799 case LTTNG_DOMAIN_LOG4J
:
1800 case LTTNG_DOMAIN_JUL
:
1802 struct lttng_event event
;
1803 struct ltt_ust_session
*usess
= session
->ust_session
;
1807 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1808 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1809 strncpy(event
.name
, "*", sizeof(event
.name
));
1810 event
.name
[sizeof(event
.name
) - 1] = '\0';
1812 ret
= cmd_enable_event(session
, domain
, NULL
, &event
,
1813 filter_expression
, filter
, NULL
, wpipe
);
1814 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1821 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1822 case LTTNG_DOMAIN_UST_PID
:
1823 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1826 ret
= LTTNG_ERR_UND
;
1839 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1841 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1844 ssize_t nb_events
= 0;
1847 case LTTNG_DOMAIN_KERNEL
:
1848 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1849 if (nb_events
< 0) {
1850 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1854 case LTTNG_DOMAIN_UST
:
1855 nb_events
= ust_app_list_events(events
);
1856 if (nb_events
< 0) {
1857 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1861 case LTTNG_DOMAIN_LOG4J
:
1862 case LTTNG_DOMAIN_JUL
:
1863 nb_events
= agent_list_events(events
, domain
);
1864 if (nb_events
< 0) {
1865 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1870 ret
= LTTNG_ERR_UND
;
1877 /* Return negative value to differentiate return code */
1882 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1884 ssize_t
cmd_list_tracepoint_fields(int domain
,
1885 struct lttng_event_field
**fields
)
1888 ssize_t nb_fields
= 0;
1891 case LTTNG_DOMAIN_UST
:
1892 nb_fields
= ust_app_list_event_fields(fields
);
1893 if (nb_fields
< 0) {
1894 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1898 case LTTNG_DOMAIN_KERNEL
:
1899 default: /* fall-through */
1900 ret
= LTTNG_ERR_UND
;
1907 /* Return negative value to differentiate return code */
1912 * Command LTTNG_START_TRACE processed by the client thread.
1914 int cmd_start_trace(struct ltt_session
*session
)
1917 unsigned long nb_chan
= 0;
1918 struct ltt_kernel_session
*ksession
;
1919 struct ltt_ust_session
*usess
;
1923 /* Ease our life a bit ;) */
1924 ksession
= session
->kernel_session
;
1925 usess
= session
->ust_session
;
1927 /* Is the session already started? */
1928 if (session
->active
) {
1929 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1934 * Starting a session without channel is useless since after that it's not
1935 * possible to enable channel thus inform the client.
1937 if (usess
&& usess
->domain_global
.channels
) {
1938 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1941 nb_chan
+= ksession
->channel_count
;
1944 ret
= LTTNG_ERR_NO_CHANNEL
;
1948 /* Kernel tracing */
1949 if (ksession
!= NULL
) {
1950 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1951 if (ret
!= LTTNG_OK
) {
1956 /* Flag session that trace should start automatically */
1959 * Even though the start trace might fail, flag this session active so
1960 * other application coming in are started by default.
1964 ret
= ust_app_start_trace_all(usess
);
1966 ret
= LTTNG_ERR_UST_START_FAIL
;
1971 /* Flag this after a successful start. */
1972 session
->has_been_started
= 1;
1973 session
->active
= 1;
1982 * Command LTTNG_STOP_TRACE processed by the client thread.
1984 int cmd_stop_trace(struct ltt_session
*session
)
1987 struct ltt_kernel_channel
*kchan
;
1988 struct ltt_kernel_session
*ksession
;
1989 struct ltt_ust_session
*usess
;
1994 ksession
= session
->kernel_session
;
1995 usess
= session
->ust_session
;
1997 /* Session is not active. Skip everythong and inform the client. */
1998 if (!session
->active
) {
1999 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2004 if (ksession
&& ksession
->active
) {
2005 DBG("Stop kernel tracing");
2007 /* Flush metadata if exist */
2008 if (ksession
->metadata_stream_fd
>= 0) {
2009 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2011 ERR("Kernel metadata flush failed");
2015 /* Flush all buffers before stopping */
2016 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2017 ret
= kernel_flush_buffer(kchan
);
2019 ERR("Kernel flush buffer error");
2023 ret
= kernel_stop_session(ksession
);
2025 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2029 kernel_wait_quiescent(kernel_tracer_fd
);
2031 ksession
->active
= 0;
2034 if (usess
&& usess
->active
) {
2036 * Even though the stop trace might fail, flag this session inactive so
2037 * other application coming in are not started by default.
2041 ret
= ust_app_stop_trace_all(usess
);
2043 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2048 /* Flag inactive after a successful stop. */
2049 session
->active
= 0;
2057 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2059 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
2060 size_t nb_uri
, struct lttng_uri
*uris
)
2063 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2064 struct ltt_ust_session
*usess
= session
->ust_session
;
2065 struct consumer_output
*consumer
= NULL
;
2071 /* Can't set consumer URI if the session is active. */
2072 if (session
->active
) {
2073 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2078 * This case switch makes sure the domain session has a temporary consumer
2079 * so the URL can be set.
2083 /* Code flow error. A session MUST always have a consumer object */
2084 assert(session
->consumer
);
2086 * The URL will be added to the tracing session consumer instead of a
2087 * specific domain consumer.
2089 consumer
= session
->consumer
;
2091 case LTTNG_DOMAIN_KERNEL
:
2092 /* Code flow error if we don't have a kernel session here. */
2094 assert(ksess
->consumer
);
2095 consumer
= ksess
->consumer
;
2097 case LTTNG_DOMAIN_UST
:
2098 /* Code flow error if we don't have a kernel session here. */
2100 assert(usess
->consumer
);
2101 consumer
= usess
->consumer
;
2105 for (i
= 0; i
< nb_uri
; i
++) {
2106 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
2107 if (ret
!= LTTNG_OK
) {
2113 * Make sure to set the session in output mode after we set URI since a
2114 * session can be created without URL (thus flagged in no output mode).
2116 session
->output_traces
= 1;
2118 ksess
->output_traces
= 1;
2120 usess
->output_traces
= 1;
2131 * Command LTTNG_CREATE_SESSION processed by the client thread.
2133 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2134 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2137 struct ltt_session
*session
;
2143 * Verify if the session already exist
2145 * XXX: There is no need for the session lock list here since the caller
2146 * (process_client_msg) is holding it. We might want to change that so a
2147 * single command does not lock the entire session list.
2149 session
= session_find_by_name(name
);
2150 if (session
!= NULL
) {
2151 ret
= LTTNG_ERR_EXIST_SESS
;
2155 /* Create tracing session in the registry */
2156 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2157 LTTNG_SOCK_GET_GID_CRED(creds
));
2158 if (ret
!= LTTNG_OK
) {
2163 * Get the newly created session pointer back
2165 * XXX: There is no need for the session lock list here since the caller
2166 * (process_client_msg) is holding it. We might want to change that so a
2167 * single command does not lock the entire session list.
2169 session
= session_find_by_name(name
);
2172 session
->live_timer
= live_timer
;
2173 /* Create default consumer output for the session not yet created. */
2174 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2175 if (session
->consumer
== NULL
) {
2176 ret
= LTTNG_ERR_FATAL
;
2177 goto consumer_error
;
2181 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2182 if (ret
!= LTTNG_OK
) {
2183 goto consumer_error
;
2185 session
->output_traces
= 1;
2187 session
->output_traces
= 0;
2188 DBG2("Session %s created with no output", session
->name
);
2191 session
->consumer
->enabled
= 1;
2196 session_destroy(session
);
2203 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2205 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2206 size_t nb_uri
, lttng_sock_cred
*creds
)
2209 struct ltt_session
*session
;
2210 struct snapshot_output
*new_output
= NULL
;
2216 * Create session in no output mode with URIs set to NULL. The uris we've
2217 * received are for a default snapshot output if one.
2219 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2220 if (ret
!= LTTNG_OK
) {
2224 /* Get the newly created session pointer back. This should NEVER fail. */
2225 session
= session_find_by_name(name
);
2228 /* Flag session for snapshot mode. */
2229 session
->snapshot_mode
= 1;
2231 /* Skip snapshot output creation if no URI is given. */
2236 new_output
= snapshot_output_alloc();
2238 ret
= LTTNG_ERR_NOMEM
;
2239 goto error_snapshot_alloc
;
2242 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2243 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2245 if (ret
== -ENOMEM
) {
2246 ret
= LTTNG_ERR_NOMEM
;
2248 ret
= LTTNG_ERR_INVALID
;
2250 goto error_snapshot
;
2254 snapshot_add_output(&session
->snapshot
, new_output
);
2261 snapshot_output_destroy(new_output
);
2262 error_snapshot_alloc
:
2263 session_destroy(session
);
2269 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2271 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2274 struct ltt_ust_session
*usess
;
2275 struct ltt_kernel_session
*ksess
;
2280 usess
= session
->ust_session
;
2281 ksess
= session
->kernel_session
;
2283 /* Clean kernel session teardown */
2284 kernel_destroy_session(ksess
);
2286 /* UST session teardown */
2288 /* Close any relayd session */
2289 consumer_output_send_destroy_relayd(usess
->consumer
);
2291 /* Destroy every UST application related to this session. */
2292 ret
= ust_app_destroy_trace_all(usess
);
2294 ERR("Error in ust_app_destroy_trace_all");
2297 /* Clean up the rest. */
2298 trace_ust_destroy_session(usess
);
2302 * Must notify the kernel thread here to update it's poll set in order to
2303 * remove the channel(s)' fd just destroyed.
2305 ret
= notify_thread_pipe(wpipe
);
2307 PERROR("write kernel poll pipe");
2310 ret
= session_destroy(session
);
2316 * Command LTTNG_CALIBRATE processed by the client thread.
2318 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2323 case LTTNG_DOMAIN_KERNEL
:
2325 struct lttng_kernel_calibrate kcalibrate
;
2327 switch (calibrate
->type
) {
2328 case LTTNG_CALIBRATE_FUNCTION
:
2330 /* Default and only possible calibrate option. */
2331 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2335 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2337 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2342 case LTTNG_DOMAIN_UST
:
2344 struct lttng_ust_calibrate ucalibrate
;
2346 switch (calibrate
->type
) {
2347 case LTTNG_CALIBRATE_FUNCTION
:
2349 /* Default and only possible calibrate option. */
2350 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2354 ret
= ust_app_calibrate_glb(&ucalibrate
);
2356 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2362 ret
= LTTNG_ERR_UND
;
2373 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2375 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2376 const char *sock_path
, struct consumer_data
*cdata
)
2379 struct consumer_socket
*socket
= NULL
;
2386 case LTTNG_DOMAIN_KERNEL
:
2388 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2392 /* Can't register a consumer if there is already one */
2393 if (ksess
->consumer_fds_sent
!= 0) {
2394 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2398 sock
= lttcomm_connect_unix_sock(sock_path
);
2400 ret
= LTTNG_ERR_CONNECT_FAIL
;
2403 cdata
->cmd_sock
= sock
;
2405 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2406 if (socket
== NULL
) {
2409 PERROR("close register consumer");
2411 cdata
->cmd_sock
= -1;
2412 ret
= LTTNG_ERR_FATAL
;
2416 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2417 if (socket
->lock
== NULL
) {
2418 PERROR("zmalloc pthread mutex");
2419 ret
= LTTNG_ERR_FATAL
;
2422 pthread_mutex_init(socket
->lock
, NULL
);
2423 socket
->registered
= 1;
2426 consumer_add_socket(socket
, ksess
->consumer
);
2429 pthread_mutex_lock(&cdata
->pid_mutex
);
2431 pthread_mutex_unlock(&cdata
->pid_mutex
);
2436 /* TODO: Userspace tracing */
2437 ret
= LTTNG_ERR_UND
;
2445 consumer_destroy_socket(socket
);
2451 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2453 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2454 struct lttng_domain
**domains
)
2459 struct lttng_ht_iter iter
;
2461 if (session
->kernel_session
!= NULL
) {
2462 DBG3("Listing domains found kernel domain");
2466 if (session
->ust_session
!= NULL
) {
2467 DBG3("Listing domains found UST global domain");
2470 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2472 if (agt
->being_used
) {
2478 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2479 if (*domains
== NULL
) {
2480 ret
= LTTNG_ERR_FATAL
;
2484 if (session
->kernel_session
!= NULL
) {
2485 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2489 if (session
->ust_session
!= NULL
) {
2490 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2491 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2494 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2496 if (agt
->being_used
) {
2497 (*domains
)[index
].type
= agt
->domain
;
2498 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2507 /* Return negative value to differentiate return code */
2513 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2515 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2516 struct lttng_channel
**channels
)
2519 ssize_t nb_chan
= 0;
2522 case LTTNG_DOMAIN_KERNEL
:
2523 if (session
->kernel_session
!= NULL
) {
2524 nb_chan
= session
->kernel_session
->channel_count
;
2526 DBG3("Number of kernel channels %zd", nb_chan
);
2528 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2531 case LTTNG_DOMAIN_UST
:
2532 if (session
->ust_session
!= NULL
) {
2533 nb_chan
= lttng_ht_get_count(
2534 session
->ust_session
->domain_global
.channels
);
2536 DBG3("Number of UST global channels %zd", nb_chan
);
2538 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2543 ret
= LTTNG_ERR_UND
;
2548 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2549 if (*channels
== NULL
) {
2550 ret
= LTTNG_ERR_FATAL
;
2554 list_lttng_channels(domain
, session
, *channels
);
2557 /* Ret value was set in the domain switch case */
2564 /* Return negative value to differentiate return code */
2569 * Command LTTNG_LIST_EVENTS processed by the client thread.
2571 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2572 char *channel_name
, struct lttng_event
**events
)
2575 ssize_t nb_event
= 0;
2578 case LTTNG_DOMAIN_KERNEL
:
2579 if (session
->kernel_session
!= NULL
) {
2580 nb_event
= list_lttng_kernel_events(channel_name
,
2581 session
->kernel_session
, events
);
2584 case LTTNG_DOMAIN_UST
:
2586 if (session
->ust_session
!= NULL
) {
2587 nb_event
= list_lttng_ust_global_events(channel_name
,
2588 &session
->ust_session
->domain_global
, events
);
2592 case LTTNG_DOMAIN_LOG4J
:
2593 case LTTNG_DOMAIN_JUL
:
2594 if (session
->ust_session
) {
2595 struct lttng_ht_iter iter
;
2598 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2599 &iter
.iter
, agt
, node
.node
) {
2600 nb_event
= list_lttng_agent_events(agt
, events
);
2605 ret
= LTTNG_ERR_UND
;
2612 /* Return negative value to differentiate return code */
2617 * Using the session list, filled a lttng_session array to send back to the
2618 * client for session listing.
2620 * The session list lock MUST be acquired before calling this function. Use
2621 * session_lock_list() and session_unlock_list().
2623 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2628 struct ltt_session
*session
;
2629 struct ltt_session_list
*list
= session_get_list();
2631 DBG("Getting all available session for UID %d GID %d",
2634 * Iterate over session list and append data after the control struct in
2637 cds_list_for_each_entry(session
, &list
->head
, list
) {
2639 * Only list the sessions the user can control.
2641 if (!session_access_ok(session
, uid
, gid
)) {
2645 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2646 struct ltt_ust_session
*usess
= session
->ust_session
;
2648 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2649 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2650 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2651 ret
= build_network_session_path(sessions
[i
].path
,
2652 sizeof(sessions
[i
].path
), session
);
2654 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2655 session
->consumer
->dst
.trace_path
);
2658 PERROR("snprintf session path");
2662 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2663 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2664 sessions
[i
].enabled
= session
->active
;
2665 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2666 sessions
[i
].live_timer_interval
= session
->live_timer
;
2672 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2673 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2675 int cmd_data_pending(struct ltt_session
*session
)
2678 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2679 struct ltt_ust_session
*usess
= session
->ust_session
;
2683 /* Session MUST be stopped to ask for data availability. */
2684 if (session
->active
) {
2685 ret
= LTTNG_ERR_SESSION_STARTED
;
2689 * If stopped, just make sure we've started before else the above call
2690 * will always send that there is data pending.
2692 * The consumer assumes that when the data pending command is received,
2693 * the trace has been started before or else no output data is written
2694 * by the streams which is a condition for data pending. So, this is
2695 * *VERY* important that we don't ask the consumer before a start
2698 if (!session
->has_been_started
) {
2704 if (ksess
&& ksess
->consumer
) {
2705 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2707 /* Data is still being extracted for the kernel. */
2712 if (usess
&& usess
->consumer
) {
2713 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2715 /* Data is still being extracted for the kernel. */
2720 /* Data is ready to be read by a viewer */
2728 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2730 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2732 int cmd_snapshot_add_output(struct ltt_session
*session
,
2733 struct lttng_snapshot_output
*output
, uint32_t *id
)
2736 struct snapshot_output
*new_output
;
2741 DBG("Cmd snapshot add output for session %s", session
->name
);
2744 * Permission denied to create an output if the session is not
2745 * set in no output mode.
2747 if (session
->output_traces
) {
2748 ret
= LTTNG_ERR_EPERM
;
2752 /* Only one output is allowed until we have the "tee" feature. */
2753 if (session
->snapshot
.nb_output
== 1) {
2754 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2758 new_output
= snapshot_output_alloc();
2760 ret
= LTTNG_ERR_NOMEM
;
2764 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2765 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2766 &session
->snapshot
);
2768 if (ret
== -ENOMEM
) {
2769 ret
= LTTNG_ERR_NOMEM
;
2771 ret
= LTTNG_ERR_INVALID
;
2777 snapshot_add_output(&session
->snapshot
, new_output
);
2779 *id
= new_output
->id
;
2786 snapshot_output_destroy(new_output
);
2792 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2794 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2796 int cmd_snapshot_del_output(struct ltt_session
*session
,
2797 struct lttng_snapshot_output
*output
)
2800 struct snapshot_output
*sout
= NULL
;
2808 * Permission denied to create an output if the session is not
2809 * set in no output mode.
2811 if (session
->output_traces
) {
2812 ret
= LTTNG_ERR_EPERM
;
2817 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2819 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2820 } else if (*output
->name
!= '\0') {
2821 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2823 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2826 ret
= LTTNG_ERR_INVALID
;
2830 snapshot_delete_output(&session
->snapshot
, sout
);
2831 snapshot_output_destroy(sout
);
2840 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2842 * If no output is available, outputs is untouched and 0 is returned.
2844 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2846 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2847 struct lttng_snapshot_output
**outputs
)
2850 struct lttng_snapshot_output
*list
;
2851 struct lttng_ht_iter iter
;
2852 struct snapshot_output
*output
;
2857 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2860 * Permission denied to create an output if the session is not
2861 * set in no output mode.
2863 if (session
->output_traces
) {
2864 ret
= LTTNG_ERR_EPERM
;
2868 if (session
->snapshot
.nb_output
== 0) {
2873 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2875 ret
= LTTNG_ERR_NOMEM
;
2879 /* Copy list from session to the new list object. */
2880 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2881 output
, node
.node
) {
2882 assert(output
->consumer
);
2883 list
[idx
].id
= output
->id
;
2884 list
[idx
].max_size
= output
->max_size
;
2885 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2886 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2887 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2888 sizeof(list
[idx
].ctrl_url
));
2891 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2892 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2894 ret
= LTTNG_ERR_NOMEM
;
2899 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2900 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2902 ret
= LTTNG_ERR_NOMEM
;
2910 return session
->snapshot
.nb_output
;
2919 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2920 * snapshot output is *not* set with a remote destination.
2922 * Return 0 on success or a LTTNG_ERR code.
2924 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2925 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2928 struct lttng_ht_iter iter
;
2929 struct consumer_socket
*socket
;
2932 assert(snap_output
);
2935 DBG2("Set relayd object from snapshot output");
2937 /* Ignore if snapshot consumer output is not network. */
2938 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2943 * For each consumer socket, create and send the relayd object of the
2947 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2948 socket
, node
.node
) {
2949 ret
= send_consumer_relayd_sockets(0, session
->id
,
2950 snap_output
->consumer
, socket
,
2951 session
->name
, session
->hostname
,
2952 session
->live_timer
);
2953 if (ret
!= LTTNG_OK
) {
2965 * Record a kernel snapshot.
2967 * Return LTTNG_OK on success or a LTTNG_ERR code.
2969 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2970 struct snapshot_output
*output
, struct ltt_session
*session
,
2971 int wait
, uint64_t max_stream_size
)
2979 /* Get the datetime for the snapshot output directory. */
2980 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2981 sizeof(output
->datetime
));
2983 ret
= LTTNG_ERR_INVALID
;
2988 * Copy kernel session sockets so we can communicate with the right
2989 * consumer for the snapshot record command.
2991 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2993 ret
= LTTNG_ERR_NOMEM
;
2997 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2998 if (ret
!= LTTNG_OK
) {
2999 goto error_snapshot
;
3002 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
3003 if (ret
!= LTTNG_OK
) {
3004 goto error_snapshot
;
3010 /* Clean up copied sockets so this output can use some other later on. */
3011 consumer_destroy_output_sockets(output
->consumer
);
3017 * Record a UST snapshot.
3019 * Return 0 on success or a LTTNG_ERR error code.
3021 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3022 struct snapshot_output
*output
, struct ltt_session
*session
,
3023 int wait
, uint64_t max_stream_size
)
3031 /* Get the datetime for the snapshot output directory. */
3032 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3033 sizeof(output
->datetime
));
3035 ret
= LTTNG_ERR_INVALID
;
3040 * Copy UST session sockets so we can communicate with the right
3041 * consumer for the snapshot record command.
3043 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3045 ret
= LTTNG_ERR_NOMEM
;
3049 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3050 if (ret
!= LTTNG_OK
) {
3051 goto error_snapshot
;
3054 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
3058 ret
= LTTNG_ERR_INVALID
;
3061 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3064 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3067 goto error_snapshot
;
3073 /* Clean up copied sockets so this output can use some other later on. */
3074 consumer_destroy_output_sockets(output
->consumer
);
3080 * Return the biggest subbuffer size of all channels in the given session.
3082 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
3084 uint64_t max_size
= 0;
3088 if (session
->kernel_session
) {
3089 struct ltt_kernel_channel
*chan
;
3090 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3093 * For each channel, add to the max size the size of each subbuffer
3094 * multiplied by their sized.
3096 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3097 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
3098 max_size
= chan
->channel
->attr
.subbuf_size
;
3103 if (session
->ust_session
) {
3104 struct lttng_ht_iter iter
;
3105 struct ltt_ust_channel
*uchan
;
3106 struct ltt_ust_session
*usess
= session
->ust_session
;
3108 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
3110 if (uchan
->attr
.subbuf_size
> max_size
) {
3111 max_size
= uchan
->attr
.subbuf_size
;
3120 * Returns the total number of streams for a session or a negative value
3123 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3125 unsigned int total_streams
= 0;
3127 if (session
->kernel_session
) {
3128 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3130 total_streams
+= ksess
->stream_count_global
;
3133 if (session
->ust_session
) {
3134 struct ltt_ust_session
*usess
= session
->ust_session
;
3136 total_streams
+= ust_app_get_nb_stream(usess
);
3139 return total_streams
;
3143 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3145 * The wait parameter is ignored so this call always wait for the snapshot to
3146 * complete before returning.
3148 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3150 int cmd_snapshot_record(struct ltt_session
*session
,
3151 struct lttng_snapshot_output
*output
, int wait
)
3154 unsigned int use_tmp_output
= 0;
3155 struct snapshot_output tmp_output
;
3156 unsigned int nb_streams
, snapshot_success
= 0;
3157 uint64_t session_max_size
= 0, max_stream_size
= 0;
3161 DBG("Cmd snapshot record for session %s", session
->name
);
3164 * Permission denied to create an output if the session is not
3165 * set in no output mode.
3167 if (session
->output_traces
) {
3168 ret
= LTTNG_ERR_EPERM
;
3172 /* The session needs to be started at least once. */
3173 if (!session
->has_been_started
) {
3174 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3178 /* Use temporary output for the session. */
3179 if (output
&& *output
->ctrl_url
!= '\0') {
3180 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3181 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3184 if (ret
== -ENOMEM
) {
3185 ret
= LTTNG_ERR_NOMEM
;
3187 ret
= LTTNG_ERR_INVALID
;
3191 /* Use the global session count for the temporary snapshot. */
3192 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3197 * Get the session maximum size for a snapshot meaning it will compute the
3198 * size of all streams from all domain.
3200 max_stream_size
= get_session_max_subbuf_size(session
);
3202 nb_streams
= get_session_nb_streams(session
);
3205 * The maximum size of the snapshot is the number of streams multiplied
3206 * by the biggest subbuf size of all channels in a session which is the
3207 * maximum stream size available for each stream. The session max size
3208 * is now checked against the snapshot max size value given by the user
3209 * and if lower, an error is returned.
3211 session_max_size
= max_stream_size
* nb_streams
;
3214 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3215 session_max_size
, max_stream_size
);
3218 * If we use a temporary output, check right away if the max size fits else
3219 * for each output the max size will be checked.
3221 if (use_tmp_output
&&
3222 (tmp_output
.max_size
!= 0 &&
3223 tmp_output
.max_size
< session_max_size
)) {
3224 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3228 if (session
->kernel_session
) {
3229 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3231 if (use_tmp_output
) {
3232 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3233 wait
, max_stream_size
);
3234 if (ret
!= LTTNG_OK
) {
3237 snapshot_success
= 1;
3239 struct snapshot_output
*sout
;
3240 struct lttng_ht_iter iter
;
3243 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3244 &iter
.iter
, sout
, node
.node
) {
3246 * Make a local copy of the output and assign the possible
3247 * temporary value given by the caller.
3249 memset(&tmp_output
, 0, sizeof(tmp_output
));
3250 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3252 /* Use temporary max size. */
3253 if (output
->max_size
!= (uint64_t) -1ULL) {
3254 tmp_output
.max_size
= output
->max_size
;
3257 if (tmp_output
.max_size
!= 0 &&
3258 tmp_output
.max_size
< session_max_size
) {
3260 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3264 /* Use temporary name. */
3265 if (*output
->name
!= '\0') {
3266 strncpy(tmp_output
.name
, output
->name
,
3267 sizeof(tmp_output
.name
));
3270 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3272 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3273 session
, wait
, max_stream_size
);
3274 if (ret
!= LTTNG_OK
) {
3278 snapshot_success
= 1;
3284 if (session
->ust_session
) {
3285 struct ltt_ust_session
*usess
= session
->ust_session
;
3287 if (use_tmp_output
) {
3288 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3289 wait
, max_stream_size
);
3290 if (ret
!= LTTNG_OK
) {
3293 snapshot_success
= 1;
3295 struct snapshot_output
*sout
;
3296 struct lttng_ht_iter iter
;
3299 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3300 &iter
.iter
, sout
, node
.node
) {
3302 * Make a local copy of the output and assign the possible
3303 * temporary value given by the caller.
3305 memset(&tmp_output
, 0, sizeof(tmp_output
));
3306 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3308 /* Use temporary max size. */
3309 if (output
->max_size
!= (uint64_t) -1ULL) {
3310 tmp_output
.max_size
= output
->max_size
;
3313 if (tmp_output
.max_size
!= 0 &&
3314 tmp_output
.max_size
< session_max_size
) {
3316 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3320 /* Use temporary name. */
3321 if (*output
->name
!= '\0') {
3322 strncpy(tmp_output
.name
, output
->name
,
3323 sizeof(tmp_output
.name
));
3326 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3328 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3329 wait
, max_stream_size
);
3330 if (ret
!= LTTNG_OK
) {
3334 snapshot_success
= 1;
3340 if (snapshot_success
) {
3341 session
->snapshot
.nb_snapshot
++;
3343 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3351 * Init command subsystem.
3356 * Set network sequence index to 1 for streams to match a relayd
3357 * socket on the consumer side.
3359 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3360 relayd_net_seq_idx
= 1;
3361 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3363 DBG("Command subsystem initialized");