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
;
54 * Create a session path used by list_lttng_sessions for the case that the
55 * session consumer is on the network.
57 static int build_network_session_path(char *dst
, size_t size
,
58 struct ltt_session
*session
)
60 int ret
, kdata_port
, udata_port
;
61 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
62 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
67 memset(tmp_urls
, 0, sizeof(tmp_urls
));
68 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
70 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
72 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
73 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
74 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
77 if (session
->ust_session
&& session
->ust_session
->consumer
) {
78 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
79 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
82 if (uuri
== NULL
&& kuri
== NULL
) {
83 uri
= &session
->consumer
->dst
.net
.control
;
84 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
85 } else if (kuri
&& uuri
) {
86 ret
= uri_compare(kuri
, uuri
);
90 /* Build uuri URL string */
91 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
98 } else if (kuri
&& uuri
== NULL
) {
100 } else if (uuri
&& kuri
== NULL
) {
104 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
110 * Do we have a UST url set. If yes, this means we have both kernel and UST
113 if (*tmp_uurl
!= '\0') {
114 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
115 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
118 if (kuri
|| (!kuri
&& !uuri
)) {
121 /* No kernel URI, use the UST port. */
124 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
132 * Fill lttng_channel array of all channels.
134 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
135 struct lttng_channel
*channels
)
138 struct ltt_kernel_channel
*kchan
;
140 DBG("Listing channels for session %s", session
->name
);
143 case LTTNG_DOMAIN_KERNEL
:
144 /* Kernel channels */
145 if (session
->kernel_session
!= NULL
) {
146 cds_list_for_each_entry(kchan
,
147 &session
->kernel_session
->channel_list
.head
, list
) {
148 /* Copy lttng_channel struct to array */
149 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
150 channels
[i
].enabled
= kchan
->enabled
;
155 case LTTNG_DOMAIN_UST
:
157 struct lttng_ht_iter iter
;
158 struct ltt_ust_channel
*uchan
;
161 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
162 &iter
.iter
, uchan
, node
.node
) {
163 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
164 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
165 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
166 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
167 channels
[i
].attr
.switch_timer_interval
=
168 uchan
->attr
.switch_timer_interval
;
169 channels
[i
].attr
.read_timer_interval
=
170 uchan
->attr
.read_timer_interval
;
171 channels
[i
].enabled
= uchan
->enabled
;
172 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
173 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
174 switch (uchan
->attr
.output
) {
177 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
191 * Create a list of agent domain events.
193 * Return number of events in list on success or else a negative value.
195 static int list_lttng_agent_events(struct agent
*agt
,
196 struct lttng_event
**events
)
199 unsigned int nb_event
= 0;
200 struct agent_event
*event
;
201 struct lttng_event
*tmp_events
;
202 struct lttng_ht_iter iter
;
207 DBG3("Listing agent events");
210 nb_event
= lttng_ht_get_count(agt
->events
);
217 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
219 PERROR("zmalloc agent events session");
220 ret
= -LTTNG_ERR_FATAL
;
225 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
226 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
227 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
228 tmp_events
[i
].enabled
= event
->enabled
;
229 tmp_events
[i
].loglevel
= event
->loglevel
;
230 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
235 *events
= tmp_events
;
239 assert(nb_event
== i
);
244 * Create a list of ust global domain events.
246 static int list_lttng_ust_global_events(char *channel_name
,
247 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
250 unsigned int nb_event
= 0;
251 struct lttng_ht_iter iter
;
252 struct lttng_ht_node_str
*node
;
253 struct ltt_ust_channel
*uchan
;
254 struct ltt_ust_event
*uevent
;
255 struct lttng_event
*tmp
;
257 DBG("Listing UST global events for channel %s", channel_name
);
261 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
262 node
= lttng_ht_iter_get_node_str(&iter
);
264 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
268 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
270 nb_event
+= lttng_ht_get_count(uchan
->events
);
277 DBG3("Listing UST global %d events", nb_event
);
279 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
281 ret
= LTTNG_ERR_FATAL
;
285 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
286 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
287 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
288 tmp
[i
].enabled
= uevent
->enabled
;
290 switch (uevent
->attr
.instrumentation
) {
291 case LTTNG_UST_TRACEPOINT
:
292 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
294 case LTTNG_UST_PROBE
:
295 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
297 case LTTNG_UST_FUNCTION
:
298 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
302 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
303 switch (uevent
->attr
.loglevel_type
) {
304 case LTTNG_UST_LOGLEVEL_ALL
:
305 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
307 case LTTNG_UST_LOGLEVEL_RANGE
:
308 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
310 case LTTNG_UST_LOGLEVEL_SINGLE
:
311 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
314 if (uevent
->filter
) {
317 if (uevent
->exclusion
) {
318 tmp
[i
].exclusion
= 1;
332 * Fill lttng_event array of all kernel events in the channel.
334 static int list_lttng_kernel_events(char *channel_name
,
335 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
338 unsigned int nb_event
;
339 struct ltt_kernel_event
*event
;
340 struct ltt_kernel_channel
*kchan
;
342 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
344 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
348 nb_event
= kchan
->event_count
;
350 DBG("Listing events for channel %s", kchan
->channel
->name
);
357 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
358 if (*events
== NULL
) {
359 ret
= LTTNG_ERR_FATAL
;
363 /* Kernel channels */
364 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
365 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
366 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
367 (*events
)[i
].enabled
= event
->enabled
;
369 switch (event
->event
->instrumentation
) {
370 case LTTNG_KERNEL_TRACEPOINT
:
371 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
373 case LTTNG_KERNEL_KRETPROBE
:
374 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
375 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
376 sizeof(struct lttng_kernel_kprobe
));
378 case LTTNG_KERNEL_KPROBE
:
379 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
380 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
381 sizeof(struct lttng_kernel_kprobe
));
383 case LTTNG_KERNEL_FUNCTION
:
384 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
385 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
386 sizeof(struct lttng_kernel_function
));
388 case LTTNG_KERNEL_NOOP
:
389 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
391 case LTTNG_KERNEL_SYSCALL
:
392 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
394 case LTTNG_KERNEL_ALL
:
405 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
417 /* Negate the error code to differentiate the size from an error */
422 * Add URI so the consumer output object. Set the correct path depending on the
423 * domain adding the default trace directory.
425 static int add_uri_to_consumer(struct consumer_output
*consumer
,
426 struct lttng_uri
*uri
, int domain
, const char *session_name
)
429 const char *default_trace_dir
;
433 if (consumer
== NULL
) {
434 DBG("No consumer detected. Don't add URI. Stopping.");
435 ret
= LTTNG_ERR_NO_CONSUMER
;
440 case LTTNG_DOMAIN_KERNEL
:
441 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
443 case LTTNG_DOMAIN_UST
:
444 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
448 * This case is possible is we try to add the URI to the global tracing
449 * session consumer object which in this case there is no subdir.
451 default_trace_dir
= "";
454 switch (uri
->dtype
) {
457 DBG2("Setting network URI to consumer");
459 if (consumer
->type
== CONSUMER_DST_NET
) {
460 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
461 consumer
->dst
.net
.control_isset
) ||
462 (uri
->stype
== LTTNG_STREAM_DATA
&&
463 consumer
->dst
.net
.data_isset
)) {
464 ret
= LTTNG_ERR_URL_EXIST
;
468 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
471 consumer
->type
= CONSUMER_DST_NET
;
473 /* Set URI into consumer output object */
474 ret
= consumer_set_network_uri(consumer
, uri
);
478 } else if (ret
== 1) {
480 * URI was the same in the consumer so we do not append the subdir
481 * again so to not duplicate output dir.
487 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
488 ret
= consumer_set_subdir(consumer
, session_name
);
490 ret
= LTTNG_ERR_FATAL
;
495 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
496 /* On a new subdir, reappend the default trace dir. */
497 strncat(consumer
->subdir
, default_trace_dir
,
498 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
499 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
504 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
505 memset(consumer
->dst
.trace_path
, 0,
506 sizeof(consumer
->dst
.trace_path
));
507 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
508 sizeof(consumer
->dst
.trace_path
));
509 /* Append default trace dir */
510 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
511 sizeof(consumer
->dst
.trace_path
) -
512 strlen(consumer
->dst
.trace_path
) - 1);
513 /* Flag consumer as local. */
514 consumer
->type
= CONSUMER_DST_LOCAL
;
525 * Init tracing by creating trace directory and sending fds kernel consumer.
527 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
530 struct lttng_ht_iter iter
;
531 struct consumer_socket
*socket
;
537 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
538 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
540 pthread_mutex_lock(socket
->lock
);
541 ret
= kernel_consumer_send_session(socket
, session
);
542 pthread_mutex_unlock(socket
->lock
);
544 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
556 * Create a socket to the relayd using the URI.
558 * On success, the relayd_sock pointer is set to the created socket.
559 * Else, it's stays untouched and a lttcomm error code is returned.
561 static int create_connect_relayd(struct lttng_uri
*uri
,
562 struct lttcomm_relayd_sock
**relayd_sock
)
565 struct lttcomm_relayd_sock
*rsock
;
567 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
568 RELAYD_VERSION_COMM_MINOR
);
570 ret
= LTTNG_ERR_FATAL
;
575 * Connect to relayd so we can proceed with a session creation. This call
576 * can possibly block for an arbitrary amount of time to set the health
577 * state to be in poll execution.
580 ret
= relayd_connect(rsock
);
583 ERR("Unable to reach lttng-relayd");
584 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
588 /* Create socket for control stream. */
589 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
590 DBG3("Creating relayd stream socket from URI");
592 /* Check relayd version */
593 ret
= relayd_version_check(rsock
);
595 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
598 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
599 DBG3("Creating relayd data socket from URI");
601 /* Command is not valid */
602 ERR("Relayd invalid stream type: %d", uri
->stype
);
603 ret
= LTTNG_ERR_INVALID
;
607 *relayd_sock
= rsock
;
612 /* The returned value is not useful since we are on an error path. */
613 (void) relayd_close(rsock
);
621 * Connect to the relayd using URI and send the socket to the right consumer.
623 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
624 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
625 struct consumer_socket
*consumer_sock
,
626 char *session_name
, char *hostname
, int session_live_timer
)
629 struct lttcomm_relayd_sock
*rsock
= NULL
;
631 /* Connect to relayd and make version check if uri is the control. */
632 ret
= create_connect_relayd(relayd_uri
, &rsock
);
633 if (ret
!= LTTNG_OK
) {
638 /* Set the network sequence index if not set. */
639 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
640 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
642 * Increment net_seq_idx because we are about to transfer the
643 * new relayd socket to the consumer.
644 * Assign unique key so the consumer can match streams.
646 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
647 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
650 /* Send relayd socket to consumer. */
651 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
652 relayd_uri
->stype
, session_id
,
653 session_name
, hostname
, session_live_timer
);
655 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
659 /* Flag that the corresponding socket was sent. */
660 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
661 consumer_sock
->control_sock_sent
= 1;
662 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
663 consumer_sock
->data_sock_sent
= 1;
669 * Close socket which was dup on the consumer side. The session daemon does
670 * NOT keep track of the relayd socket(s) once transfer to the consumer.
674 (void) relayd_close(rsock
);
678 if (ret
!= LTTNG_OK
) {
680 * The consumer output for this session should not be used anymore
681 * since the relayd connection failed thus making any tracing or/and
682 * streaming not usable.
684 consumer
->enabled
= 0;
690 * Send both relayd sockets to a specific consumer and domain. This is a
691 * helper function to facilitate sending the information to the consumer for a
694 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
695 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
696 char *session_name
, char *hostname
, int session_live_timer
)
703 /* Sending control relayd socket. */
704 if (!sock
->control_sock_sent
) {
705 ret
= send_consumer_relayd_socket(domain
, session_id
,
706 &consumer
->dst
.net
.control
, consumer
, sock
,
707 session_name
, hostname
, session_live_timer
);
708 if (ret
!= LTTNG_OK
) {
713 /* Sending data relayd socket. */
714 if (!sock
->data_sock_sent
) {
715 ret
= send_consumer_relayd_socket(domain
, session_id
,
716 &consumer
->dst
.net
.data
, consumer
, sock
,
717 session_name
, hostname
, session_live_timer
);
718 if (ret
!= LTTNG_OK
) {
728 * Setup relayd connections for a tracing session. First creates the socket to
729 * the relayd and send them to the right domain consumer. Consumer type MUST be
732 int cmd_setup_relayd(struct ltt_session
*session
)
735 struct ltt_ust_session
*usess
;
736 struct ltt_kernel_session
*ksess
;
737 struct consumer_socket
*socket
;
738 struct lttng_ht_iter iter
;
742 usess
= session
->ust_session
;
743 ksess
= session
->kernel_session
;
745 DBG("Setting relayd for session %s", session
->name
);
749 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
750 && usess
->consumer
->enabled
) {
751 /* For each consumer socket, send relayd sockets */
752 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
754 pthread_mutex_lock(socket
->lock
);
755 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
756 usess
->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;
768 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
769 && ksess
->consumer
->enabled
) {
770 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
772 pthread_mutex_lock(socket
->lock
);
773 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
774 ksess
->consumer
, socket
,
775 session
->name
, session
->hostname
,
776 session
->live_timer
);
777 pthread_mutex_unlock(socket
->lock
);
778 if (ret
!= LTTNG_OK
) {
781 /* Session is now ready for network streaming. */
782 session
->net_handle
= 1;
792 * Start a kernel session by opening all necessary streams.
794 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
797 struct ltt_kernel_channel
*kchan
;
799 /* Open kernel metadata */
800 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
801 ret
= kernel_open_metadata(ksess
);
803 ret
= LTTNG_ERR_KERN_META_FAIL
;
808 /* Open kernel metadata stream */
809 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
810 ret
= kernel_open_metadata_stream(ksess
);
812 ERR("Kernel create metadata stream failed");
813 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
818 /* For each channel */
819 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
820 if (kchan
->stream_count
== 0) {
821 ret
= kernel_open_channel_stream(kchan
);
823 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
826 /* Update the stream global counter */
827 ksess
->stream_count_global
+= ret
;
831 /* Setup kernel consumer socket and send fds to it */
832 ret
= init_kernel_tracing(ksess
);
834 ret
= LTTNG_ERR_KERN_START_FAIL
;
838 /* This start the kernel tracing */
839 ret
= kernel_start_session(ksess
);
841 ret
= LTTNG_ERR_KERN_START_FAIL
;
845 /* Quiescent wait after starting trace */
846 kernel_wait_quiescent(kernel_tracer_fd
);
857 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
859 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
863 struct ltt_ust_session
*usess
;
865 usess
= session
->ust_session
;
870 case LTTNG_DOMAIN_KERNEL
:
872 ret
= channel_kernel_disable(session
->kernel_session
,
874 if (ret
!= LTTNG_OK
) {
878 kernel_wait_quiescent(kernel_tracer_fd
);
881 case LTTNG_DOMAIN_UST
:
883 struct ltt_ust_channel
*uchan
;
884 struct lttng_ht
*chan_ht
;
886 chan_ht
= usess
->domain_global
.channels
;
888 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
890 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
894 ret
= channel_ust_disable(usess
, uchan
);
895 if (ret
!= LTTNG_OK
) {
901 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
913 * Command LTTNG_TRACK_PID processed by the client thread.
915 * Called with session lock held.
917 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
924 case LTTNG_DOMAIN_KERNEL
:
926 struct ltt_kernel_session
*ksess
;
928 ksess
= session
->kernel_session
;
930 ret
= kernel_track_pid(ksess
, pid
);
931 if (ret
!= LTTNG_OK
) {
935 kernel_wait_quiescent(kernel_tracer_fd
);
938 case LTTNG_DOMAIN_UST
:
940 struct ltt_ust_session
*usess
;
942 usess
= session
->ust_session
;
944 ret
= trace_ust_track_pid(usess
, pid
);
945 if (ret
!= LTTNG_OK
) {
951 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
963 * Command LTTNG_UNTRACK_PID processed by the client thread.
965 * Called with session lock held.
967 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
974 case LTTNG_DOMAIN_KERNEL
:
976 struct ltt_kernel_session
*ksess
;
978 ksess
= session
->kernel_session
;
980 ret
= kernel_untrack_pid(ksess
, pid
);
981 if (ret
!= LTTNG_OK
) {
985 kernel_wait_quiescent(kernel_tracer_fd
);
988 case LTTNG_DOMAIN_UST
:
990 struct ltt_ust_session
*usess
;
992 usess
= session
->ust_session
;
994 ret
= trace_ust_untrack_pid(usess
, pid
);
995 if (ret
!= LTTNG_OK
) {
1001 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1013 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1015 * The wpipe arguments is used as a notifier for the kernel thread.
1017 int cmd_enable_channel(struct ltt_session
*session
,
1018 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1021 struct ltt_ust_session
*usess
= session
->ust_session
;
1022 struct lttng_ht
*chan_ht
;
1029 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1031 /* Validate channel name */
1032 if (attr
->name
[0] == '.' ||
1033 memchr(attr
->name
, '/', len
) != NULL
) {
1034 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1038 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1043 * Don't try to enable a channel if the session has been started at
1044 * some point in time before. The tracer does not allow it.
1046 if (session
->has_been_started
) {
1047 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1052 * If the session is a live session, remove the switch timer, the
1053 * live timer does the same thing but sends also synchronisation
1054 * beacons for inactive streams.
1056 if (session
->live_timer
> 0) {
1057 attr
->attr
.live_timer_interval
= session
->live_timer
;
1058 attr
->attr
.switch_timer_interval
= 0;
1062 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1063 * mode and with less than 2 subbuf so block it right away and send back an
1064 * invalid attribute error.
1066 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1067 ret
= LTTNG_ERR_INVALID
;
1071 switch (domain
->type
) {
1072 case LTTNG_DOMAIN_KERNEL
:
1074 struct ltt_kernel_channel
*kchan
;
1076 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1077 session
->kernel_session
);
1078 if (kchan
== NULL
) {
1079 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1080 if (attr
->name
[0] != '\0') {
1081 session
->kernel_session
->has_non_default_channel
= 1;
1084 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1087 if (ret
!= LTTNG_OK
) {
1091 kernel_wait_quiescent(kernel_tracer_fd
);
1094 case LTTNG_DOMAIN_UST
:
1096 struct ltt_ust_channel
*uchan
;
1098 chan_ht
= usess
->domain_global
.channels
;
1100 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1101 if (uchan
== NULL
) {
1102 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1103 if (attr
->name
[0] != '\0') {
1104 usess
->has_non_default_channel
= 1;
1107 ret
= channel_ust_enable(usess
, uchan
);
1112 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1123 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1125 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1127 struct lttng_event
*event
)
1132 DBG("Disable event command for event \'%s\'", event
->name
);
1134 event_name
= event
->name
;
1136 /* Error out on unhandled search criteria */
1137 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1138 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1139 return LTTNG_ERR_UNK
;
1145 case LTTNG_DOMAIN_KERNEL
:
1147 struct ltt_kernel_channel
*kchan
;
1148 struct ltt_kernel_session
*ksess
;
1150 ksess
= session
->kernel_session
;
1153 * If a non-default channel has been created in the
1154 * session, explicitely require that -c chan_name needs
1157 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1158 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1162 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1163 if (kchan
== NULL
) {
1164 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1168 switch (event
->type
) {
1169 case LTTNG_EVENT_ALL
:
1170 ret
= event_kernel_disable_event_all(kchan
);
1171 if (ret
!= LTTNG_OK
) {
1175 case LTTNG_EVENT_TRACEPOINT
: /* fall-through */
1176 case LTTNG_EVENT_SYSCALL
:
1177 if (!strcmp(event_name
, "*")) {
1178 ret
= event_kernel_disable_event_type(kchan
,
1181 ret
= event_kernel_disable_event(kchan
,
1184 if (ret
!= LTTNG_OK
) {
1188 case LTTNG_EVENT_PROBE
:
1189 case LTTNG_EVENT_FUNCTION
:
1190 case LTTNG_EVENT_FUNCTION_ENTRY
:
1191 ret
= event_kernel_disable_event(kchan
, event_name
);
1192 if (ret
!= LTTNG_OK
) {
1197 ret
= LTTNG_ERR_UNK
;
1201 kernel_wait_quiescent(kernel_tracer_fd
);
1204 case LTTNG_DOMAIN_UST
:
1206 struct ltt_ust_channel
*uchan
;
1207 struct ltt_ust_session
*usess
;
1209 usess
= session
->ust_session
;
1212 * If a non-default channel has been created in the
1213 * session, explicitely require that -c chan_name needs
1216 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1217 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1221 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1223 if (uchan
== NULL
) {
1224 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1228 switch (event
->type
) {
1229 case LTTNG_EVENT_ALL
:
1230 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1231 if (ret
!= LTTNG_OK
) {
1236 ret
= LTTNG_ERR_UNK
;
1240 DBG3("Disable UST event %s in channel %s completed", event_name
,
1244 case LTTNG_DOMAIN_LOG4J
:
1245 case LTTNG_DOMAIN_JUL
:
1246 case LTTNG_DOMAIN_PYTHON
:
1249 struct ltt_ust_session
*usess
= session
->ust_session
;
1253 switch (event
->type
) {
1254 case LTTNG_EVENT_ALL
:
1257 ret
= LTTNG_ERR_UNK
;
1261 agt
= trace_ust_find_agent(usess
, domain
);
1263 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1266 /* The wild card * means that everything should be disabled. */
1267 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1268 ret
= event_agent_disable_all(usess
, agt
);
1270 ret
= event_agent_disable(usess
, agt
, event_name
);
1272 if (ret
!= LTTNG_OK
) {
1279 ret
= LTTNG_ERR_UND
;
1291 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1293 int cmd_add_context(struct ltt_session
*session
, int domain
,
1294 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1296 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1299 case LTTNG_DOMAIN_KERNEL
:
1300 assert(session
->kernel_session
);
1302 if (session
->kernel_session
->channel_count
== 0) {
1303 /* Create default channel */
1304 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1305 if (ret
!= LTTNG_OK
) {
1308 chan_kern_created
= 1;
1310 /* Add kernel context to kernel tracer */
1311 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1312 if (ret
!= LTTNG_OK
) {
1316 case LTTNG_DOMAIN_UST
:
1318 struct ltt_ust_session
*usess
= session
->ust_session
;
1319 unsigned int chan_count
;
1323 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1324 if (chan_count
== 0) {
1325 struct lttng_channel
*attr
;
1326 /* Create default channel */
1327 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1329 ret
= LTTNG_ERR_FATAL
;
1333 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1334 if (ret
!= LTTNG_OK
) {
1339 chan_ust_created
= 1;
1342 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1343 if (ret
!= LTTNG_OK
) {
1349 ret
= LTTNG_ERR_UND
;
1356 if (chan_kern_created
) {
1357 struct ltt_kernel_channel
*kchan
=
1358 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1359 session
->kernel_session
);
1360 /* Created previously, this should NOT fail. */
1362 kernel_destroy_channel(kchan
);
1365 if (chan_ust_created
) {
1366 struct ltt_ust_channel
*uchan
=
1367 trace_ust_find_channel_by_name(
1368 session
->ust_session
->domain_global
.channels
,
1369 DEFAULT_CHANNEL_NAME
);
1370 /* Created previously, this should NOT fail. */
1372 /* Remove from the channel list of the session. */
1373 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1375 trace_ust_destroy_channel(uchan
);
1380 static int validate_event_name(const char *name
)
1383 const char *c
= name
;
1384 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1387 * Make sure that unescaped wildcards are only used as the last
1388 * character of the event name.
1390 while (c
< event_name_end
) {
1398 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1399 /* Wildcard is not the last character */
1400 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1413 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1414 * We own filter, exclusion, and filter_expression.
1416 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1417 char *channel_name
, struct lttng_event
*event
,
1418 char *filter_expression
,
1419 struct lttng_filter_bytecode
*filter
,
1420 struct lttng_event_exclusion
*exclusion
,
1423 int ret
, channel_created
= 0;
1424 struct lttng_channel
*attr
;
1428 assert(channel_name
);
1430 DBG("Enable event command for event \'%s\'", event
->name
);
1432 ret
= validate_event_name(event
->name
);
1439 switch (domain
->type
) {
1440 case LTTNG_DOMAIN_KERNEL
:
1442 struct ltt_kernel_channel
*kchan
;
1445 * If a non-default channel has been created in the
1446 * session, explicitely require that -c chan_name needs
1449 if (session
->kernel_session
->has_non_default_channel
1450 && channel_name
[0] == '\0') {
1451 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1455 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1456 session
->kernel_session
);
1457 if (kchan
== NULL
) {
1458 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1459 LTTNG_BUFFER_GLOBAL
);
1461 ret
= LTTNG_ERR_FATAL
;
1464 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1466 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1467 if (ret
!= LTTNG_OK
) {
1473 channel_created
= 1;
1476 /* Get the newly created kernel channel pointer */
1477 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1478 session
->kernel_session
);
1479 if (kchan
== NULL
) {
1480 /* This sould not happen... */
1481 ret
= LTTNG_ERR_FATAL
;
1485 switch (event
->type
) {
1486 case LTTNG_EVENT_ALL
:
1488 char *filter_expression_a
= NULL
;
1489 struct lttng_filter_bytecode
*filter_a
= NULL
;
1492 * We need to duplicate filter_expression and filter,
1493 * because ownership is passed to first enable
1496 if (filter_expression
) {
1497 filter_expression_a
= strdup(filter_expression
);
1498 if (!filter_expression_a
) {
1499 ret
= LTTNG_ERR_FATAL
;
1504 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1506 free(filter_expression_a
);
1507 ret
= LTTNG_ERR_FATAL
;
1510 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1512 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1513 ret
= event_kernel_enable_event(kchan
, event
,
1514 filter_expression
, filter
);
1515 /* We have passed ownership */
1516 filter_expression
= NULL
;
1518 if (ret
!= LTTNG_OK
) {
1519 if (channel_created
) {
1520 /* Let's not leak a useless channel. */
1521 kernel_destroy_channel(kchan
);
1523 free(filter_expression_a
);
1527 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1528 ret
= event_kernel_enable_event(kchan
, event
,
1529 filter_expression_a
, filter_a
);
1530 if (ret
!= LTTNG_OK
) {
1531 free(filter_expression_a
);
1537 case LTTNG_EVENT_PROBE
:
1538 case LTTNG_EVENT_FUNCTION
:
1539 case LTTNG_EVENT_FUNCTION_ENTRY
:
1540 case LTTNG_EVENT_TRACEPOINT
:
1541 ret
= event_kernel_enable_event(kchan
, event
,
1542 filter_expression
, filter
);
1543 /* We have passed ownership */
1544 filter_expression
= NULL
;
1546 if (ret
!= LTTNG_OK
) {
1547 if (channel_created
) {
1548 /* Let's not leak a useless channel. */
1549 kernel_destroy_channel(kchan
);
1554 case LTTNG_EVENT_SYSCALL
:
1555 ret
= event_kernel_enable_event(kchan
, event
,
1556 filter_expression
, filter
);
1557 /* We have passed ownership */
1558 filter_expression
= NULL
;
1560 if (ret
!= LTTNG_OK
) {
1565 ret
= LTTNG_ERR_UNK
;
1569 kernel_wait_quiescent(kernel_tracer_fd
);
1572 case LTTNG_DOMAIN_UST
:
1574 struct ltt_ust_channel
*uchan
;
1575 struct ltt_ust_session
*usess
= session
->ust_session
;
1580 * If a non-default channel has been created in the
1581 * session, explicitely require that -c chan_name needs
1584 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1585 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1589 /* Get channel from global UST domain */
1590 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1592 if (uchan
== NULL
) {
1593 /* Create default channel */
1594 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1595 usess
->buffer_type
);
1597 ret
= LTTNG_ERR_FATAL
;
1600 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1602 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1603 if (ret
!= LTTNG_OK
) {
1609 /* Get the newly created channel reference back */
1610 uchan
= trace_ust_find_channel_by_name(
1611 usess
->domain_global
.channels
, channel_name
);
1615 /* At this point, the session and channel exist on the tracer */
1616 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1617 filter_expression
, filter
, exclusion
);
1618 /* We have passed ownership */
1619 filter_expression
= NULL
;
1622 if (ret
!= LTTNG_OK
) {
1627 case LTTNG_DOMAIN_LOG4J
:
1628 case LTTNG_DOMAIN_JUL
:
1629 case LTTNG_DOMAIN_PYTHON
:
1631 const char *default_event_name
, *default_chan_name
;
1633 struct lttng_event uevent
;
1634 struct lttng_domain tmp_dom
;
1635 struct ltt_ust_session
*usess
= session
->ust_session
;
1639 agt
= trace_ust_find_agent(usess
, domain
->type
);
1641 agt
= agent_create(domain
->type
);
1643 ret
= -LTTNG_ERR_NOMEM
;
1646 agent_add(agt
, usess
->agents
);
1649 /* Create the default tracepoint. */
1650 memset(&uevent
, 0, sizeof(uevent
));
1651 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1652 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1653 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1654 if (!default_event_name
) {
1655 ret
= -LTTNG_ERR_FATAL
;
1658 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1659 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1662 * The domain type is changed because we are about to enable the
1663 * default channel and event for the JUL domain that are hardcoded.
1664 * This happens in the UST domain.
1666 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1667 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1669 switch (domain
->type
) {
1670 case LTTNG_DOMAIN_LOG4J
:
1671 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1673 case LTTNG_DOMAIN_JUL
:
1674 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1676 case LTTNG_DOMAIN_PYTHON
:
1677 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1680 /* The switch/case we are in should avoid this else big problem */
1685 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1688 filter_copy
= zmalloc(
1689 sizeof(struct lttng_filter_bytecode
)
1695 memcpy(filter_copy
, filter
,
1696 sizeof(struct lttng_filter_bytecode
)
1700 ret
= cmd_enable_event(session
, &tmp_dom
,
1701 (char *) default_chan_name
,
1702 &uevent
, filter_expression
, filter_copy
,
1704 /* We have passed ownership */
1705 filter_expression
= NULL
;
1708 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1712 /* The wild card * means that everything should be enabled. */
1713 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1714 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1717 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1720 if (ret
!= LTTNG_OK
) {
1727 ret
= LTTNG_ERR_UND
;
1734 free(filter_expression
);
1742 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1744 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1747 ssize_t nb_events
= 0;
1750 case LTTNG_DOMAIN_KERNEL
:
1751 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1752 if (nb_events
< 0) {
1753 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1757 case LTTNG_DOMAIN_UST
:
1758 nb_events
= ust_app_list_events(events
);
1759 if (nb_events
< 0) {
1760 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1764 case LTTNG_DOMAIN_LOG4J
:
1765 case LTTNG_DOMAIN_JUL
:
1766 case LTTNG_DOMAIN_PYTHON
:
1767 nb_events
= agent_list_events(events
, domain
);
1768 if (nb_events
< 0) {
1769 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1774 ret
= LTTNG_ERR_UND
;
1781 /* Return negative value to differentiate return code */
1786 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1788 ssize_t
cmd_list_tracepoint_fields(int domain
,
1789 struct lttng_event_field
**fields
)
1792 ssize_t nb_fields
= 0;
1795 case LTTNG_DOMAIN_UST
:
1796 nb_fields
= ust_app_list_event_fields(fields
);
1797 if (nb_fields
< 0) {
1798 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1802 case LTTNG_DOMAIN_KERNEL
:
1803 default: /* fall-through */
1804 ret
= LTTNG_ERR_UND
;
1811 /* Return negative value to differentiate return code */
1815 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1817 return syscall_table_list(events
);
1821 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
1823 * Called with session lock held.
1825 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
1826 int domain
, int32_t **pids
)
1829 ssize_t nr_pids
= 0;
1832 case LTTNG_DOMAIN_KERNEL
:
1834 struct ltt_kernel_session
*ksess
;
1836 ksess
= session
->kernel_session
;
1837 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
1839 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1844 case LTTNG_DOMAIN_UST
:
1846 struct ltt_ust_session
*usess
;
1848 usess
= session
->ust_session
;
1849 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
1851 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1856 case LTTNG_DOMAIN_LOG4J
:
1857 case LTTNG_DOMAIN_JUL
:
1858 case LTTNG_DOMAIN_PYTHON
:
1860 ret
= LTTNG_ERR_UND
;
1867 /* Return negative value to differentiate return code */
1872 * Command LTTNG_START_TRACE processed by the client thread.
1874 * Called with session mutex held.
1876 int cmd_start_trace(struct ltt_session
*session
)
1879 unsigned long nb_chan
= 0;
1880 struct ltt_kernel_session
*ksession
;
1881 struct ltt_ust_session
*usess
;
1885 /* Ease our life a bit ;) */
1886 ksession
= session
->kernel_session
;
1887 usess
= session
->ust_session
;
1889 /* Is the session already started? */
1890 if (session
->active
) {
1891 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1896 * Starting a session without channel is useless since after that it's not
1897 * possible to enable channel thus inform the client.
1899 if (usess
&& usess
->domain_global
.channels
) {
1901 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1905 nb_chan
+= ksession
->channel_count
;
1908 ret
= LTTNG_ERR_NO_CHANNEL
;
1912 /* Kernel tracing */
1913 if (ksession
!= NULL
) {
1914 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1915 if (ret
!= LTTNG_OK
) {
1920 /* Flag session that trace should start automatically */
1923 * Even though the start trace might fail, flag this session active so
1924 * other application coming in are started by default.
1928 ret
= ust_app_start_trace_all(usess
);
1930 ret
= LTTNG_ERR_UST_START_FAIL
;
1935 /* Flag this after a successful start. */
1936 session
->has_been_started
= 1;
1937 session
->active
= 1;
1946 * Command LTTNG_STOP_TRACE processed by the client thread.
1948 int cmd_stop_trace(struct ltt_session
*session
)
1951 struct ltt_kernel_channel
*kchan
;
1952 struct ltt_kernel_session
*ksession
;
1953 struct ltt_ust_session
*usess
;
1958 ksession
= session
->kernel_session
;
1959 usess
= session
->ust_session
;
1961 /* Session is not active. Skip everythong and inform the client. */
1962 if (!session
->active
) {
1963 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1968 if (ksession
&& ksession
->active
) {
1969 DBG("Stop kernel tracing");
1971 /* Flush metadata if exist */
1972 if (ksession
->metadata_stream_fd
>= 0) {
1973 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1975 ERR("Kernel metadata flush failed");
1979 /* Flush all buffers before stopping */
1980 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1981 ret
= kernel_flush_buffer(kchan
);
1983 ERR("Kernel flush buffer error");
1987 ret
= kernel_stop_session(ksession
);
1989 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1993 kernel_wait_quiescent(kernel_tracer_fd
);
1995 ksession
->active
= 0;
1998 if (usess
&& usess
->active
) {
2000 * Even though the stop trace might fail, flag this session inactive so
2001 * other application coming in are not started by default.
2005 ret
= ust_app_stop_trace_all(usess
);
2007 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2012 /* Flag inactive after a successful stop. */
2013 session
->active
= 0;
2021 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2023 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2024 struct lttng_uri
*uris
)
2027 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2028 struct ltt_ust_session
*usess
= session
->ust_session
;
2034 /* Can't set consumer URI if the session is active. */
2035 if (session
->active
) {
2036 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2040 /* Set the "global" consumer URIs */
2041 for (i
= 0; i
< nb_uri
; i
++) {
2042 ret
= add_uri_to_consumer(session
->consumer
,
2043 &uris
[i
], 0, session
->name
);
2044 if (ret
!= LTTNG_OK
) {
2049 /* Set UST session URIs */
2050 if (session
->ust_session
) {
2051 for (i
= 0; i
< nb_uri
; i
++) {
2052 ret
= add_uri_to_consumer(
2053 session
->ust_session
->consumer
,
2054 &uris
[i
], LTTNG_DOMAIN_UST
,
2056 if (ret
!= LTTNG_OK
) {
2062 /* Set kernel session URIs */
2063 if (session
->kernel_session
) {
2064 for (i
= 0; i
< nb_uri
; i
++) {
2065 ret
= add_uri_to_consumer(
2066 session
->kernel_session
->consumer
,
2067 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2069 if (ret
!= LTTNG_OK
) {
2076 * Make sure to set the session in output mode after we set URI since a
2077 * session can be created without URL (thus flagged in no output mode).
2079 session
->output_traces
= 1;
2081 ksess
->output_traces
= 1;
2085 usess
->output_traces
= 1;
2096 * Command LTTNG_CREATE_SESSION processed by the client thread.
2098 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2099 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2102 struct ltt_session
*session
;
2108 * Verify if the session already exist
2110 * XXX: There is no need for the session lock list here since the caller
2111 * (process_client_msg) is holding it. We might want to change that so a
2112 * single command does not lock the entire session list.
2114 session
= session_find_by_name(name
);
2115 if (session
!= NULL
) {
2116 ret
= LTTNG_ERR_EXIST_SESS
;
2120 /* Create tracing session in the registry */
2121 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2122 LTTNG_SOCK_GET_GID_CRED(creds
));
2123 if (ret
!= LTTNG_OK
) {
2128 * Get the newly created session pointer back
2130 * XXX: There is no need for the session lock list here since the caller
2131 * (process_client_msg) is holding it. We might want to change that so a
2132 * single command does not lock the entire session list.
2134 session
= session_find_by_name(name
);
2137 session
->live_timer
= live_timer
;
2138 /* Create default consumer output for the session not yet created. */
2139 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2140 if (session
->consumer
== NULL
) {
2141 ret
= LTTNG_ERR_FATAL
;
2142 goto consumer_error
;
2146 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2147 if (ret
!= LTTNG_OK
) {
2148 goto consumer_error
;
2150 session
->output_traces
= 1;
2152 session
->output_traces
= 0;
2153 DBG2("Session %s created with no output", session
->name
);
2156 session
->consumer
->enabled
= 1;
2161 session_destroy(session
);
2168 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2170 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2171 size_t nb_uri
, lttng_sock_cred
*creds
)
2174 struct ltt_session
*session
;
2175 struct snapshot_output
*new_output
= NULL
;
2181 * Create session in no output mode with URIs set to NULL. The uris we've
2182 * received are for a default snapshot output if one.
2184 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2185 if (ret
!= LTTNG_OK
) {
2189 /* Get the newly created session pointer back. This should NEVER fail. */
2190 session
= session_find_by_name(name
);
2193 /* Flag session for snapshot mode. */
2194 session
->snapshot_mode
= 1;
2196 /* Skip snapshot output creation if no URI is given. */
2201 new_output
= snapshot_output_alloc();
2203 ret
= LTTNG_ERR_NOMEM
;
2204 goto error_snapshot_alloc
;
2207 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2208 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2210 if (ret
== -ENOMEM
) {
2211 ret
= LTTNG_ERR_NOMEM
;
2213 ret
= LTTNG_ERR_INVALID
;
2215 goto error_snapshot
;
2219 snapshot_add_output(&session
->snapshot
, new_output
);
2226 snapshot_output_destroy(new_output
);
2227 error_snapshot_alloc
:
2228 session_destroy(session
);
2234 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2236 * Called with session lock held.
2238 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2241 struct ltt_ust_session
*usess
;
2242 struct ltt_kernel_session
*ksess
;
2247 usess
= session
->ust_session
;
2248 ksess
= session
->kernel_session
;
2250 /* Clean kernel session teardown */
2251 kernel_destroy_session(ksess
);
2253 /* UST session teardown */
2255 /* Close any relayd session */
2256 consumer_output_send_destroy_relayd(usess
->consumer
);
2258 /* Destroy every UST application related to this session. */
2259 ret
= ust_app_destroy_trace_all(usess
);
2261 ERR("Error in ust_app_destroy_trace_all");
2264 /* Clean up the rest. */
2265 trace_ust_destroy_session(usess
);
2269 * Must notify the kernel thread here to update it's poll set in order to
2270 * remove the channel(s)' fd just destroyed.
2272 ret
= notify_thread_pipe(wpipe
);
2274 PERROR("write kernel poll pipe");
2277 ret
= session_destroy(session
);
2283 * Command LTTNG_CALIBRATE processed by the client thread.
2285 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2290 case LTTNG_DOMAIN_KERNEL
:
2292 struct lttng_kernel_calibrate kcalibrate
;
2294 switch (calibrate
->type
) {
2295 case LTTNG_CALIBRATE_FUNCTION
:
2297 /* Default and only possible calibrate option. */
2298 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2302 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2304 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2309 case LTTNG_DOMAIN_UST
:
2311 struct lttng_ust_calibrate ucalibrate
;
2313 switch (calibrate
->type
) {
2314 case LTTNG_CALIBRATE_FUNCTION
:
2316 /* Default and only possible calibrate option. */
2317 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2321 ret
= ust_app_calibrate_glb(&ucalibrate
);
2323 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2329 ret
= LTTNG_ERR_UND
;
2340 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2342 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2343 const char *sock_path
, struct consumer_data
*cdata
)
2346 struct consumer_socket
*socket
= NULL
;
2353 case LTTNG_DOMAIN_KERNEL
:
2355 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2359 /* Can't register a consumer if there is already one */
2360 if (ksess
->consumer_fds_sent
!= 0) {
2361 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2365 sock
= lttcomm_connect_unix_sock(sock_path
);
2367 ret
= LTTNG_ERR_CONNECT_FAIL
;
2370 cdata
->cmd_sock
= sock
;
2372 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2373 if (socket
== NULL
) {
2376 PERROR("close register consumer");
2378 cdata
->cmd_sock
= -1;
2379 ret
= LTTNG_ERR_FATAL
;
2383 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2384 if (socket
->lock
== NULL
) {
2385 PERROR("zmalloc pthread mutex");
2386 ret
= LTTNG_ERR_FATAL
;
2389 pthread_mutex_init(socket
->lock
, NULL
);
2390 socket
->registered
= 1;
2393 consumer_add_socket(socket
, ksess
->consumer
);
2396 pthread_mutex_lock(&cdata
->pid_mutex
);
2398 pthread_mutex_unlock(&cdata
->pid_mutex
);
2403 /* TODO: Userspace tracing */
2404 ret
= LTTNG_ERR_UND
;
2412 consumer_destroy_socket(socket
);
2418 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2420 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2421 struct lttng_domain
**domains
)
2426 struct lttng_ht_iter iter
;
2428 if (session
->kernel_session
!= NULL
) {
2429 DBG3("Listing domains found kernel domain");
2433 if (session
->ust_session
!= NULL
) {
2434 DBG3("Listing domains found UST global domain");
2438 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2440 if (agt
->being_used
) {
2451 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2452 if (*domains
== NULL
) {
2453 ret
= LTTNG_ERR_FATAL
;
2457 if (session
->kernel_session
!= NULL
) {
2458 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2462 if (session
->ust_session
!= NULL
) {
2463 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2464 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2468 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2470 if (agt
->being_used
) {
2471 (*domains
)[index
].type
= agt
->domain
;
2472 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2482 /* Return negative value to differentiate return code */
2488 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2490 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2491 struct lttng_channel
**channels
)
2494 ssize_t nb_chan
= 0;
2497 case LTTNG_DOMAIN_KERNEL
:
2498 if (session
->kernel_session
!= NULL
) {
2499 nb_chan
= session
->kernel_session
->channel_count
;
2501 DBG3("Number of kernel channels %zd", nb_chan
);
2503 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2506 case LTTNG_DOMAIN_UST
:
2507 if (session
->ust_session
!= NULL
) {
2509 nb_chan
= lttng_ht_get_count(
2510 session
->ust_session
->domain_global
.channels
);
2513 DBG3("Number of UST global channels %zd", nb_chan
);
2515 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2520 ret
= LTTNG_ERR_UND
;
2525 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2526 if (*channels
== NULL
) {
2527 ret
= LTTNG_ERR_FATAL
;
2531 list_lttng_channels(domain
, session
, *channels
);
2537 /* Return negative value to differentiate return code */
2542 * Command LTTNG_LIST_EVENTS processed by the client thread.
2544 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2545 char *channel_name
, struct lttng_event
**events
)
2548 ssize_t nb_event
= 0;
2551 case LTTNG_DOMAIN_KERNEL
:
2552 if (session
->kernel_session
!= NULL
) {
2553 nb_event
= list_lttng_kernel_events(channel_name
,
2554 session
->kernel_session
, events
);
2557 case LTTNG_DOMAIN_UST
:
2559 if (session
->ust_session
!= NULL
) {
2560 nb_event
= list_lttng_ust_global_events(channel_name
,
2561 &session
->ust_session
->domain_global
, events
);
2565 case LTTNG_DOMAIN_LOG4J
:
2566 case LTTNG_DOMAIN_JUL
:
2567 case LTTNG_DOMAIN_PYTHON
:
2568 if (session
->ust_session
) {
2569 struct lttng_ht_iter iter
;
2573 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2574 &iter
.iter
, agt
, node
.node
) {
2575 nb_event
= list_lttng_agent_events(agt
, events
);
2581 ret
= LTTNG_ERR_UND
;
2588 /* Return negative value to differentiate return code */
2593 * Using the session list, filled a lttng_session array to send back to the
2594 * client for session listing.
2596 * The session list lock MUST be acquired before calling this function. Use
2597 * session_lock_list() and session_unlock_list().
2599 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2604 struct ltt_session
*session
;
2605 struct ltt_session_list
*list
= session_get_list();
2607 DBG("Getting all available session for UID %d GID %d",
2610 * Iterate over session list and append data after the control struct in
2613 cds_list_for_each_entry(session
, &list
->head
, list
) {
2615 * Only list the sessions the user can control.
2617 if (!session_access_ok(session
, uid
, gid
)) {
2621 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2622 struct ltt_ust_session
*usess
= session
->ust_session
;
2624 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2625 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2626 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2627 ret
= build_network_session_path(sessions
[i
].path
,
2628 sizeof(sessions
[i
].path
), session
);
2630 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2631 session
->consumer
->dst
.trace_path
);
2634 PERROR("snprintf session path");
2638 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2639 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2640 sessions
[i
].enabled
= session
->active
;
2641 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2642 sessions
[i
].live_timer_interval
= session
->live_timer
;
2648 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2649 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2651 int cmd_data_pending(struct ltt_session
*session
)
2654 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2655 struct ltt_ust_session
*usess
= session
->ust_session
;
2659 /* Session MUST be stopped to ask for data availability. */
2660 if (session
->active
) {
2661 ret
= LTTNG_ERR_SESSION_STARTED
;
2665 * If stopped, just make sure we've started before else the above call
2666 * will always send that there is data pending.
2668 * The consumer assumes that when the data pending command is received,
2669 * the trace has been started before or else no output data is written
2670 * by the streams which is a condition for data pending. So, this is
2671 * *VERY* important that we don't ask the consumer before a start
2674 if (!session
->has_been_started
) {
2680 if (ksess
&& ksess
->consumer
) {
2681 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2683 /* Data is still being extracted for the kernel. */
2688 if (usess
&& usess
->consumer
) {
2689 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2691 /* Data is still being extracted for the kernel. */
2696 /* Data is ready to be read by a viewer */
2704 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2706 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2708 int cmd_snapshot_add_output(struct ltt_session
*session
,
2709 struct lttng_snapshot_output
*output
, uint32_t *id
)
2712 struct snapshot_output
*new_output
;
2717 DBG("Cmd snapshot add output for session %s", session
->name
);
2720 * Permission denied to create an output if the session is not
2721 * set in no output mode.
2723 if (session
->output_traces
) {
2724 ret
= LTTNG_ERR_EPERM
;
2728 /* Only one output is allowed until we have the "tee" feature. */
2729 if (session
->snapshot
.nb_output
== 1) {
2730 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2734 new_output
= snapshot_output_alloc();
2736 ret
= LTTNG_ERR_NOMEM
;
2740 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2741 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2742 &session
->snapshot
);
2744 if (ret
== -ENOMEM
) {
2745 ret
= LTTNG_ERR_NOMEM
;
2747 ret
= LTTNG_ERR_INVALID
;
2753 snapshot_add_output(&session
->snapshot
, new_output
);
2755 *id
= new_output
->id
;
2762 snapshot_output_destroy(new_output
);
2768 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2770 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2772 int cmd_snapshot_del_output(struct ltt_session
*session
,
2773 struct lttng_snapshot_output
*output
)
2776 struct snapshot_output
*sout
= NULL
;
2784 * Permission denied to create an output if the session is not
2785 * set in no output mode.
2787 if (session
->output_traces
) {
2788 ret
= LTTNG_ERR_EPERM
;
2793 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2795 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2796 } else if (*output
->name
!= '\0') {
2797 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2799 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2802 ret
= LTTNG_ERR_INVALID
;
2806 snapshot_delete_output(&session
->snapshot
, sout
);
2807 snapshot_output_destroy(sout
);
2816 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2818 * If no output is available, outputs is untouched and 0 is returned.
2820 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2822 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2823 struct lttng_snapshot_output
**outputs
)
2826 struct lttng_snapshot_output
*list
= NULL
;
2827 struct lttng_ht_iter iter
;
2828 struct snapshot_output
*output
;
2833 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2836 * Permission denied to create an output if the session is not
2837 * set in no output mode.
2839 if (session
->output_traces
) {
2840 ret
= -LTTNG_ERR_EPERM
;
2844 if (session
->snapshot
.nb_output
== 0) {
2849 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2851 ret
= -LTTNG_ERR_NOMEM
;
2855 /* Copy list from session to the new list object. */
2857 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2858 output
, node
.node
) {
2859 assert(output
->consumer
);
2860 list
[idx
].id
= output
->id
;
2861 list
[idx
].max_size
= output
->max_size
;
2862 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2863 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2864 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2865 sizeof(list
[idx
].ctrl_url
));
2868 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2869 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2871 ret
= -LTTNG_ERR_NOMEM
;
2876 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2877 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2879 ret
= -LTTNG_ERR_NOMEM
;
2888 ret
= session
->snapshot
.nb_output
;
2896 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2897 * snapshot output is *not* set with a remote destination.
2899 * Return 0 on success or a LTTNG_ERR code.
2901 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2902 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2905 struct lttng_ht_iter iter
;
2906 struct consumer_socket
*socket
;
2909 assert(snap_output
);
2912 DBG2("Set relayd object from snapshot output");
2914 /* Ignore if snapshot consumer output is not network. */
2915 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2920 * For each consumer socket, create and send the relayd object of the
2924 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2925 socket
, node
.node
) {
2926 ret
= send_consumer_relayd_sockets(0, session
->id
,
2927 snap_output
->consumer
, socket
,
2928 session
->name
, session
->hostname
,
2929 session
->live_timer
);
2930 if (ret
!= LTTNG_OK
) {
2942 * Record a kernel snapshot.
2944 * Return LTTNG_OK on success or a LTTNG_ERR code.
2946 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2947 struct snapshot_output
*output
, struct ltt_session
*session
,
2948 int wait
, uint64_t nb_packets_per_stream
)
2956 /* Get the datetime for the snapshot output directory. */
2957 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2958 sizeof(output
->datetime
));
2960 ret
= LTTNG_ERR_INVALID
;
2965 * Copy kernel session sockets so we can communicate with the right
2966 * consumer for the snapshot record command.
2968 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2970 ret
= LTTNG_ERR_NOMEM
;
2974 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2975 if (ret
!= LTTNG_OK
) {
2976 goto error_snapshot
;
2979 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
2980 if (ret
!= LTTNG_OK
) {
2981 goto error_snapshot
;
2988 /* Clean up copied sockets so this output can use some other later on. */
2989 consumer_destroy_output_sockets(output
->consumer
);
2996 * Record a UST snapshot.
2998 * Return 0 on success or a LTTNG_ERR error code.
3000 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3001 struct snapshot_output
*output
, struct ltt_session
*session
,
3002 int wait
, uint64_t nb_packets_per_stream
)
3010 /* Get the datetime for the snapshot output directory. */
3011 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3012 sizeof(output
->datetime
));
3014 ret
= LTTNG_ERR_INVALID
;
3019 * Copy UST session sockets so we can communicate with the right
3020 * consumer for the snapshot record command.
3022 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3024 ret
= LTTNG_ERR_NOMEM
;
3028 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3029 if (ret
!= LTTNG_OK
) {
3030 goto error_snapshot
;
3033 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3037 ret
= LTTNG_ERR_INVALID
;
3040 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3043 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3046 goto error_snapshot
;
3052 /* Clean up copied sockets so this output can use some other later on. */
3053 consumer_destroy_output_sockets(output
->consumer
);
3059 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3060 uint64_t cur_nr_packets
)
3062 uint64_t tot_size
= 0;
3064 if (session
->kernel_session
) {
3065 struct ltt_kernel_channel
*chan
;
3066 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3068 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3069 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3071 * Don't take channel into account if we
3072 * already grab all its packets.
3076 tot_size
+= chan
->channel
->attr
.subbuf_size
3077 * chan
->stream_count
;
3081 if (session
->ust_session
) {
3082 struct ltt_ust_session
*usess
= session
->ust_session
;
3084 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3092 * Calculate the number of packets we can grab from each stream that
3093 * fits within the overall snapshot max size.
3095 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3096 * the number of packets per stream.
3098 * TODO: this approach is not perfect: we consider the worse case
3099 * (packet filling the sub-buffers) as an upper bound, but we could do
3100 * better if we do this calculation while we actually grab the packet
3101 * content: we would know how much padding we don't actually store into
3104 * This algorithm is currently bounded by the number of packets per
3107 * Since we call this algorithm before actually grabbing the data, it's
3108 * an approximation: for instance, applications could appear/disappear
3109 * in between this call and actually grabbing data.
3112 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3115 uint64_t cur_nb_packets
= 0;
3118 return 0; /* Infinite */
3121 size_left
= max_size
;
3123 uint64_t one_more_packet_tot_size
;
3125 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3127 if (!one_more_packet_tot_size
) {
3128 /* We are already grabbing all packets. */
3131 size_left
-= one_more_packet_tot_size
;
3132 if (size_left
< 0) {
3137 if (!cur_nb_packets
) {
3138 /* Not enough room to grab one packet of each stream, error. */
3141 return cur_nb_packets
;
3145 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3147 * The wait parameter is ignored so this call always wait for the snapshot to
3148 * complete before returning.
3150 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3152 int cmd_snapshot_record(struct ltt_session
*session
,
3153 struct lttng_snapshot_output
*output
, int wait
)
3156 unsigned int use_tmp_output
= 0;
3157 struct snapshot_output tmp_output
;
3158 unsigned int snapshot_success
= 0;
3163 DBG("Cmd snapshot record for session %s", session
->name
);
3166 * Permission denied to create an output if the session is not
3167 * set in no output mode.
3169 if (session
->output_traces
) {
3170 ret
= LTTNG_ERR_EPERM
;
3174 /* The session needs to be started at least once. */
3175 if (!session
->has_been_started
) {
3176 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3180 /* Use temporary output for the session. */
3181 if (*output
->ctrl_url
!= '\0') {
3182 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3183 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3186 if (ret
== -ENOMEM
) {
3187 ret
= LTTNG_ERR_NOMEM
;
3189 ret
= LTTNG_ERR_INVALID
;
3193 /* Use the global session count for the temporary snapshot. */
3194 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3198 if (session
->kernel_session
) {
3199 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3201 if (use_tmp_output
) {
3202 int64_t nb_packets_per_stream
;
3204 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3205 tmp_output
.max_size
);
3206 if (nb_packets_per_stream
< 0) {
3207 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3210 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3211 wait
, nb_packets_per_stream
);
3212 if (ret
!= LTTNG_OK
) {
3215 snapshot_success
= 1;
3217 struct snapshot_output
*sout
;
3218 struct lttng_ht_iter iter
;
3221 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3222 &iter
.iter
, sout
, node
.node
) {
3223 int64_t nb_packets_per_stream
;
3226 * Make a local copy of the output and assign the possible
3227 * temporary value given by the caller.
3229 memset(&tmp_output
, 0, sizeof(tmp_output
));
3230 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3232 if (output
->max_size
!= (uint64_t) -1ULL) {
3233 tmp_output
.max_size
= output
->max_size
;
3236 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3237 tmp_output
.max_size
);
3238 if (nb_packets_per_stream
< 0) {
3239 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3243 /* Use temporary name. */
3244 if (*output
->name
!= '\0') {
3245 strncpy(tmp_output
.name
, output
->name
,
3246 sizeof(tmp_output
.name
));
3249 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3251 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3252 session
, wait
, nb_packets_per_stream
);
3253 if (ret
!= LTTNG_OK
) {
3257 snapshot_success
= 1;
3263 if (session
->ust_session
) {
3264 struct ltt_ust_session
*usess
= session
->ust_session
;
3266 if (use_tmp_output
) {
3267 int64_t nb_packets_per_stream
;
3269 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3270 tmp_output
.max_size
);
3271 if (nb_packets_per_stream
< 0) {
3272 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3275 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3276 wait
, nb_packets_per_stream
);
3277 if (ret
!= LTTNG_OK
) {
3280 snapshot_success
= 1;
3282 struct snapshot_output
*sout
;
3283 struct lttng_ht_iter iter
;
3286 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3287 &iter
.iter
, sout
, node
.node
) {
3288 int64_t nb_packets_per_stream
;
3291 * Make a local copy of the output and assign the possible
3292 * temporary value given by the caller.
3294 memset(&tmp_output
, 0, sizeof(tmp_output
));
3295 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3297 if (output
->max_size
!= (uint64_t) -1ULL) {
3298 tmp_output
.max_size
= output
->max_size
;
3301 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3302 tmp_output
.max_size
);
3303 if (nb_packets_per_stream
< 0) {
3304 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3309 /* Use temporary name. */
3310 if (*output
->name
!= '\0') {
3311 strncpy(tmp_output
.name
, output
->name
,
3312 sizeof(tmp_output
.name
));
3315 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3317 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3318 wait
, nb_packets_per_stream
);
3319 if (ret
!= LTTNG_OK
) {
3323 snapshot_success
= 1;
3329 if (snapshot_success
) {
3330 session
->snapshot
.nb_snapshot
++;
3332 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3340 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3342 int cmd_set_session_shm_path(struct ltt_session
*session
,
3343 const char *shm_path
)
3349 * Can only set shm path before session is started.
3351 if (session
->has_been_started
) {
3352 return LTTNG_ERR_SESSION_STARTED
;
3355 strncpy(session
->shm_path
, shm_path
,
3356 sizeof(session
->shm_path
));
3357 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3363 * Init command subsystem.
3368 * Set network sequence index to 1 for streams to match a relayd
3369 * socket on the consumer side.
3371 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3372 relayd_net_seq_idx
= 1;
3373 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3375 DBG("Command subsystem initialized");