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.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
34 #include "health-sessiond.h"
36 #include "kernel-consumer.h"
37 #include "lttng-sessiond.h"
44 * Used to keep a unique index for each relayd socket created where this value
45 * is associated with streams on the consumer so it can match the right relayd
46 * to send to. It must be accessed with the relayd_net_seq_idx_lock
49 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 static uint64_t relayd_net_seq_idx
;
53 * Both functions below are special case for the Kernel domain when
54 * enabling/disabling all events.
57 int enable_kevent_all(struct ltt_session
*session
,
58 struct lttng_domain
*domain
, char *channel_name
,
59 struct lttng_event
*event
,
60 char *filter_expression
,
61 struct lttng_filter_bytecode
*filter
, int wpipe
);
63 int disable_kevent_all(struct ltt_session
*session
, int domain
,
65 struct lttng_event
*event
);
68 * Create a session path used by list_lttng_sessions for the case that the
69 * session consumer is on the network.
71 static int build_network_session_path(char *dst
, size_t size
,
72 struct ltt_session
*session
)
74 int ret
, kdata_port
, udata_port
;
75 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
76 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
81 memset(tmp_urls
, 0, sizeof(tmp_urls
));
82 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
84 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
86 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
87 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
88 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
91 if (session
->ust_session
&& session
->ust_session
->consumer
) {
92 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
93 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
96 if (uuri
== NULL
&& kuri
== NULL
) {
97 uri
= &session
->consumer
->dst
.net
.control
;
98 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
99 } else if (kuri
&& uuri
) {
100 ret
= uri_compare(kuri
, uuri
);
104 /* Build uuri URL string */
105 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
112 } else if (kuri
&& uuri
== NULL
) {
114 } else if (uuri
&& kuri
== NULL
) {
118 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
124 * Do we have a UST url set. If yes, this means we have both kernel and UST
127 if (*tmp_uurl
!= '\0') {
128 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
129 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
132 if (kuri
|| (!kuri
&& !uuri
)) {
135 /* No kernel URI, use the UST port. */
138 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
146 * Fill lttng_channel array of all channels.
148 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
149 struct lttng_channel
*channels
)
152 struct ltt_kernel_channel
*kchan
;
154 DBG("Listing channels for session %s", session
->name
);
157 case LTTNG_DOMAIN_KERNEL
:
158 /* Kernel channels */
159 if (session
->kernel_session
!= NULL
) {
160 cds_list_for_each_entry(kchan
,
161 &session
->kernel_session
->channel_list
.head
, list
) {
162 /* Copy lttng_channel struct to array */
163 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
164 channels
[i
].enabled
= kchan
->enabled
;
169 case LTTNG_DOMAIN_UST
:
171 struct lttng_ht_iter iter
;
172 struct ltt_ust_channel
*uchan
;
175 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
176 &iter
.iter
, uchan
, node
.node
) {
177 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
178 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
179 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
180 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
181 channels
[i
].attr
.switch_timer_interval
=
182 uchan
->attr
.switch_timer_interval
;
183 channels
[i
].attr
.read_timer_interval
=
184 uchan
->attr
.read_timer_interval
;
185 channels
[i
].enabled
= uchan
->enabled
;
186 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
187 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
188 switch (uchan
->attr
.output
) {
191 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
205 * Create a list of agent domain events.
207 * Return number of events in list on success or else a negative value.
209 static int list_lttng_agent_events(struct agent
*agt
,
210 struct lttng_event
**events
)
213 unsigned int nb_event
= 0;
214 struct agent_event
*event
;
215 struct lttng_event
*tmp_events
;
216 struct lttng_ht_iter iter
;
221 DBG3("Listing agent events");
223 nb_event
= lttng_ht_get_count(agt
->events
);
229 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
231 PERROR("zmalloc agent events session");
232 ret
= -LTTNG_ERR_FATAL
;
237 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
238 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
239 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
240 tmp_events
[i
].enabled
= event
->enabled
;
241 tmp_events
[i
].loglevel
= event
->loglevel
;
242 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
247 *events
= tmp_events
;
251 assert(nb_event
== i
);
256 * Create a list of ust global domain events.
258 static int list_lttng_ust_global_events(char *channel_name
,
259 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
262 unsigned int nb_event
= 0;
263 struct lttng_ht_iter iter
;
264 struct lttng_ht_node_str
*node
;
265 struct ltt_ust_channel
*uchan
;
266 struct ltt_ust_event
*uevent
;
267 struct lttng_event
*tmp
;
269 DBG("Listing UST global events for channel %s", channel_name
);
273 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
274 node
= lttng_ht_iter_get_node_str(&iter
);
276 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
280 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
282 nb_event
+= lttng_ht_get_count(uchan
->events
);
289 DBG3("Listing UST global %d events", nb_event
);
291 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
293 ret
= LTTNG_ERR_FATAL
;
297 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
298 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
299 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
300 tmp
[i
].enabled
= uevent
->enabled
;
302 switch (uevent
->attr
.instrumentation
) {
303 case LTTNG_UST_TRACEPOINT
:
304 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
306 case LTTNG_UST_PROBE
:
307 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
309 case LTTNG_UST_FUNCTION
:
310 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
314 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
315 switch (uevent
->attr
.loglevel_type
) {
316 case LTTNG_UST_LOGLEVEL_ALL
:
317 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
319 case LTTNG_UST_LOGLEVEL_RANGE
:
320 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
322 case LTTNG_UST_LOGLEVEL_SINGLE
:
323 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
326 if (uevent
->filter
) {
329 if (uevent
->exclusion
) {
330 tmp
[i
].exclusion
= 1;
344 * Fill lttng_event array of all kernel events in the channel.
346 static int list_lttng_kernel_events(char *channel_name
,
347 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
350 unsigned int nb_event
;
351 struct ltt_kernel_event
*event
;
352 struct ltt_kernel_channel
*kchan
;
354 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
356 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
360 nb_event
= kchan
->event_count
;
362 DBG("Listing events for channel %s", kchan
->channel
->name
);
369 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
370 if (*events
== NULL
) {
371 ret
= LTTNG_ERR_FATAL
;
375 /* Kernel channels */
376 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
377 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
378 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
379 (*events
)[i
].enabled
= event
->enabled
;
381 switch (event
->event
->instrumentation
) {
382 case LTTNG_KERNEL_TRACEPOINT
:
383 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
385 case LTTNG_KERNEL_KRETPROBE
:
386 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
387 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
388 sizeof(struct lttng_kernel_kprobe
));
390 case LTTNG_KERNEL_KPROBE
:
391 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
392 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
393 sizeof(struct lttng_kernel_kprobe
));
395 case LTTNG_KERNEL_FUNCTION
:
396 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
397 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
398 sizeof(struct lttng_kernel_function
));
400 case LTTNG_KERNEL_NOOP
:
401 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
403 case LTTNG_KERNEL_SYSCALL
:
404 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
406 case LTTNG_KERNEL_ALL
:
417 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
429 /* Negate the error code to differentiate the size from an error */
434 * Add URI so the consumer output object. Set the correct path depending on the
435 * domain adding the default trace directory.
437 static int add_uri_to_consumer(struct consumer_output
*consumer
,
438 struct lttng_uri
*uri
, int domain
, const char *session_name
)
441 const char *default_trace_dir
;
445 if (consumer
== NULL
) {
446 DBG("No consumer detected. Don't add URI. Stopping.");
447 ret
= LTTNG_ERR_NO_CONSUMER
;
452 case LTTNG_DOMAIN_KERNEL
:
453 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
455 case LTTNG_DOMAIN_UST
:
456 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
460 * This case is possible is we try to add the URI to the global tracing
461 * session consumer object which in this case there is no subdir.
463 default_trace_dir
= "";
466 switch (uri
->dtype
) {
469 DBG2("Setting network URI to consumer");
471 if (consumer
->type
== CONSUMER_DST_NET
) {
472 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
473 consumer
->dst
.net
.control_isset
) ||
474 (uri
->stype
== LTTNG_STREAM_DATA
&&
475 consumer
->dst
.net
.data_isset
)) {
476 ret
= LTTNG_ERR_URL_EXIST
;
480 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
483 consumer
->type
= CONSUMER_DST_NET
;
485 /* Set URI into consumer output object */
486 ret
= consumer_set_network_uri(consumer
, uri
);
490 } else if (ret
== 1) {
492 * URI was the same in the consumer so we do not append the subdir
493 * again so to not duplicate output dir.
499 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
500 ret
= consumer_set_subdir(consumer
, session_name
);
502 ret
= LTTNG_ERR_FATAL
;
507 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
508 /* On a new subdir, reappend the default trace dir. */
509 strncat(consumer
->subdir
, default_trace_dir
,
510 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
511 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
516 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
517 memset(consumer
->dst
.trace_path
, 0,
518 sizeof(consumer
->dst
.trace_path
));
519 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
520 sizeof(consumer
->dst
.trace_path
));
521 /* Append default trace dir */
522 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
523 sizeof(consumer
->dst
.trace_path
) -
524 strlen(consumer
->dst
.trace_path
) - 1);
525 /* Flag consumer as local. */
526 consumer
->type
= CONSUMER_DST_LOCAL
;
537 * Init tracing by creating trace directory and sending fds kernel consumer.
539 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
542 struct lttng_ht_iter iter
;
543 struct consumer_socket
*socket
;
549 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
550 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
552 pthread_mutex_lock(socket
->lock
);
553 ret
= kernel_consumer_send_session(socket
, session
);
554 pthread_mutex_unlock(socket
->lock
);
556 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
568 * Create a socket to the relayd using the URI.
570 * On success, the relayd_sock pointer is set to the created socket.
571 * Else, it's stays untouched and a lttcomm error code is returned.
573 static int create_connect_relayd(struct lttng_uri
*uri
,
574 struct lttcomm_relayd_sock
**relayd_sock
)
577 struct lttcomm_relayd_sock
*rsock
;
579 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
580 RELAYD_VERSION_COMM_MINOR
);
582 ret
= LTTNG_ERR_FATAL
;
587 * Connect to relayd so we can proceed with a session creation. This call
588 * can possibly block for an arbitrary amount of time to set the health
589 * state to be in poll execution.
592 ret
= relayd_connect(rsock
);
595 ERR("Unable to reach lttng-relayd");
596 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
600 /* Create socket for control stream. */
601 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
602 DBG3("Creating relayd stream socket from URI");
604 /* Check relayd version */
605 ret
= relayd_version_check(rsock
);
607 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
610 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
611 DBG3("Creating relayd data socket from URI");
613 /* Command is not valid */
614 ERR("Relayd invalid stream type: %d", uri
->stype
);
615 ret
= LTTNG_ERR_INVALID
;
619 *relayd_sock
= rsock
;
624 /* The returned value is not useful since we are on an error path. */
625 (void) relayd_close(rsock
);
633 * Connect to the relayd using URI and send the socket to the right consumer.
635 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
636 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
637 struct consumer_socket
*consumer_sock
,
638 char *session_name
, char *hostname
, int session_live_timer
)
641 struct lttcomm_relayd_sock
*rsock
= NULL
;
643 /* Connect to relayd and make version check if uri is the control. */
644 ret
= create_connect_relayd(relayd_uri
, &rsock
);
645 if (ret
!= LTTNG_OK
) {
650 /* Set the network sequence index if not set. */
651 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
652 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
654 * Increment net_seq_idx because we are about to transfer the
655 * new relayd socket to the consumer.
656 * Assign unique key so the consumer can match streams.
658 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
659 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
662 /* Send relayd socket to consumer. */
663 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
664 relayd_uri
->stype
, session_id
,
665 session_name
, hostname
, session_live_timer
);
667 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
671 /* Flag that the corresponding socket was sent. */
672 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
673 consumer_sock
->control_sock_sent
= 1;
674 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
675 consumer_sock
->data_sock_sent
= 1;
681 * Close socket which was dup on the consumer side. The session daemon does
682 * NOT keep track of the relayd socket(s) once transfer to the consumer.
686 (void) relayd_close(rsock
);
690 if (ret
!= LTTNG_OK
) {
692 * The consumer output for this session should not be used anymore
693 * since the relayd connection failed thus making any tracing or/and
694 * streaming not usable.
696 consumer
->enabled
= 0;
702 * Send both relayd sockets to a specific consumer and domain. This is a
703 * helper function to facilitate sending the information to the consumer for a
706 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
707 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
708 char *session_name
, char *hostname
, int session_live_timer
)
715 /* Sending control relayd socket. */
716 if (!sock
->control_sock_sent
) {
717 ret
= send_consumer_relayd_socket(domain
, session_id
,
718 &consumer
->dst
.net
.control
, consumer
, sock
,
719 session_name
, hostname
, session_live_timer
);
720 if (ret
!= LTTNG_OK
) {
725 /* Sending data relayd socket. */
726 if (!sock
->data_sock_sent
) {
727 ret
= send_consumer_relayd_socket(domain
, session_id
,
728 &consumer
->dst
.net
.data
, consumer
, sock
,
729 session_name
, hostname
, session_live_timer
);
730 if (ret
!= LTTNG_OK
) {
740 * Setup relayd connections for a tracing session. First creates the socket to
741 * the relayd and send them to the right domain consumer. Consumer type MUST be
744 int cmd_setup_relayd(struct ltt_session
*session
)
747 struct ltt_ust_session
*usess
;
748 struct ltt_kernel_session
*ksess
;
749 struct consumer_socket
*socket
;
750 struct lttng_ht_iter iter
;
754 usess
= session
->ust_session
;
755 ksess
= session
->kernel_session
;
757 DBG("Setting relayd for session %s", session
->name
);
761 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
762 && usess
->consumer
->enabled
) {
763 /* For each consumer socket, send relayd sockets */
764 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
766 pthread_mutex_lock(socket
->lock
);
767 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
768 usess
->consumer
, socket
,
769 session
->name
, session
->hostname
,
770 session
->live_timer
);
771 pthread_mutex_unlock(socket
->lock
);
772 if (ret
!= LTTNG_OK
) {
775 /* Session is now ready for network streaming. */
776 session
->net_handle
= 1;
780 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
781 && ksess
->consumer
->enabled
) {
782 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
784 pthread_mutex_lock(socket
->lock
);
785 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
786 ksess
->consumer
, socket
,
787 session
->name
, session
->hostname
,
788 session
->live_timer
);
789 pthread_mutex_unlock(socket
->lock
);
790 if (ret
!= LTTNG_OK
) {
793 /* Session is now ready for network streaming. */
794 session
->net_handle
= 1;
804 * Start a kernel session by opening all necessary streams.
806 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
809 struct ltt_kernel_channel
*kchan
;
811 /* Open kernel metadata */
812 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
813 ret
= kernel_open_metadata(ksess
);
815 ret
= LTTNG_ERR_KERN_META_FAIL
;
820 /* Open kernel metadata stream */
821 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
822 ret
= kernel_open_metadata_stream(ksess
);
824 ERR("Kernel create metadata stream failed");
825 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
830 /* For each channel */
831 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
832 if (kchan
->stream_count
== 0) {
833 ret
= kernel_open_channel_stream(kchan
);
835 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
838 /* Update the stream global counter */
839 ksess
->stream_count_global
+= ret
;
843 /* Setup kernel consumer socket and send fds to it */
844 ret
= init_kernel_tracing(ksess
);
846 ret
= LTTNG_ERR_KERN_START_FAIL
;
850 /* This start the kernel tracing */
851 ret
= kernel_start_session(ksess
);
853 ret
= LTTNG_ERR_KERN_START_FAIL
;
857 /* Quiescent wait after starting trace */
858 kernel_wait_quiescent(kernel_tracer_fd
);
869 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
871 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
875 struct ltt_ust_session
*usess
;
877 usess
= session
->ust_session
;
882 case LTTNG_DOMAIN_KERNEL
:
884 ret
= channel_kernel_disable(session
->kernel_session
,
886 if (ret
!= LTTNG_OK
) {
890 kernel_wait_quiescent(kernel_tracer_fd
);
893 case LTTNG_DOMAIN_UST
:
895 struct ltt_ust_channel
*uchan
;
896 struct lttng_ht
*chan_ht
;
898 chan_ht
= usess
->domain_global
.channels
;
900 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
902 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
906 ret
= channel_ust_disable(usess
, uchan
);
907 if (ret
!= LTTNG_OK
) {
913 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
914 case LTTNG_DOMAIN_UST_EXEC_NAME
:
915 case LTTNG_DOMAIN_UST_PID
:
918 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
930 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
932 * The wpipe arguments is used as a notifier for the kernel thread.
934 int cmd_enable_channel(struct ltt_session
*session
,
935 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
938 struct ltt_ust_session
*usess
= session
->ust_session
;
939 struct lttng_ht
*chan_ht
;
945 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
950 * Don't try to enable a channel if the session has been started at
951 * some point in time before. The tracer does not allow it.
953 if (session
->has_been_started
) {
954 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
959 * If the session is a live session, remove the switch timer, the
960 * live timer does the same thing but sends also synchronisation
961 * beacons for inactive streams.
963 if (session
->live_timer
> 0) {
964 attr
->attr
.live_timer_interval
= session
->live_timer
;
965 attr
->attr
.switch_timer_interval
= 0;
969 * The ringbuffer (both in user space and kernel) behave badly in overwrite
970 * mode and with less than 2 subbuf so block it right away and send back an
971 * invalid attribute error.
973 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
974 ret
= LTTNG_ERR_INVALID
;
978 switch (domain
->type
) {
979 case LTTNG_DOMAIN_KERNEL
:
981 struct ltt_kernel_channel
*kchan
;
983 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
984 session
->kernel_session
);
986 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
987 if (attr
->name
[0] != '\0') {
988 session
->kernel_session
->has_non_default_channel
= 1;
991 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
994 if (ret
!= LTTNG_OK
) {
998 kernel_wait_quiescent(kernel_tracer_fd
);
1001 case LTTNG_DOMAIN_UST
:
1003 struct ltt_ust_channel
*uchan
;
1005 chan_ht
= usess
->domain_global
.channels
;
1007 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1008 if (uchan
== NULL
) {
1009 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1010 if (attr
->name
[0] != '\0') {
1011 usess
->has_non_default_channel
= 1;
1014 ret
= channel_ust_enable(usess
, uchan
);
1019 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1030 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1032 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1034 struct lttng_event
*event
)
1039 DBG("Disable event command for event \'%s\'", event
->name
);
1041 event_name
= event
->name
;
1043 if (event
->loglevel_type
|| event
->loglevel
|| event
->enabled
1044 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1045 return LTTNG_ERR_UNK
;
1047 /* Special handling for kernel domain all events. */
1048 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1049 return disable_kevent_all(session
, domain
, channel_name
, event
);
1055 case LTTNG_DOMAIN_KERNEL
:
1057 struct ltt_kernel_channel
*kchan
;
1058 struct ltt_kernel_session
*ksess
;
1060 ksess
= session
->kernel_session
;
1063 * If a non-default channel has been created in the
1064 * session, explicitely require that -c chan_name needs
1067 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1068 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1072 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1073 if (kchan
== NULL
) {
1074 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1078 switch (event
->type
) {
1079 case LTTNG_EVENT_ALL
:
1080 case LTTNG_EVENT_TRACEPOINT
:
1081 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1082 if (ret
!= LTTNG_OK
) {
1086 case LTTNG_EVENT_SYSCALL
:
1087 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1088 if (ret
!= LTTNG_OK
) {
1093 ret
= LTTNG_ERR_UNK
;
1097 kernel_wait_quiescent(kernel_tracer_fd
);
1100 case LTTNG_DOMAIN_UST
:
1102 struct ltt_ust_channel
*uchan
;
1103 struct ltt_ust_session
*usess
;
1105 usess
= session
->ust_session
;
1108 * If a non-default channel has been created in the
1109 * session, explicitely require that -c chan_name needs
1112 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1113 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1117 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1119 if (uchan
== NULL
) {
1120 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1124 switch (event
->type
) {
1125 case LTTNG_EVENT_ALL
:
1126 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1127 if (ret
!= LTTNG_OK
) {
1132 ret
= LTTNG_ERR_UNK
;
1136 DBG3("Disable UST event %s in channel %s completed", event_name
,
1140 case LTTNG_DOMAIN_LOG4J
:
1141 case LTTNG_DOMAIN_JUL
:
1142 case LTTNG_DOMAIN_PYTHON
:
1145 struct ltt_ust_session
*usess
= session
->ust_session
;
1149 switch (event
->type
) {
1150 case LTTNG_EVENT_ALL
:
1153 ret
= LTTNG_ERR_UNK
;
1157 agt
= trace_ust_find_agent(usess
, domain
);
1159 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1162 /* The wild card * means that everything should be disabled. */
1163 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1164 ret
= event_agent_disable_all(usess
, agt
);
1166 ret
= event_agent_disable(usess
, agt
, event_name
);
1168 if (ret
!= LTTNG_OK
) {
1175 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1176 case LTTNG_DOMAIN_UST_PID
:
1177 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1180 ret
= LTTNG_ERR_UND
;
1192 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1195 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1197 struct lttng_event
*event
)
1204 case LTTNG_DOMAIN_KERNEL
:
1206 struct ltt_kernel_session
*ksess
;
1207 struct ltt_kernel_channel
*kchan
;
1209 ksess
= session
->kernel_session
;
1212 * If a non-default channel has been created in the
1213 * session, explicitely require that -c chan_name needs
1216 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1217 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1221 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1222 if (kchan
== NULL
) {
1223 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1227 switch (event
->type
) {
1228 case LTTNG_EVENT_ALL
:
1229 ret
= event_kernel_disable_all(kchan
);
1230 if (ret
!= LTTNG_OK
) {
1234 case LTTNG_EVENT_SYSCALL
:
1235 ret
= event_kernel_disable_syscall(kchan
, "");
1236 if (ret
!= LTTNG_OK
) {
1241 ret
= LTTNG_ERR_UNK
;
1245 kernel_wait_quiescent(kernel_tracer_fd
);
1249 ret
= LTTNG_ERR_UND
;
1261 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1263 int cmd_add_context(struct ltt_session
*session
, int domain
,
1264 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1266 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1269 case LTTNG_DOMAIN_KERNEL
:
1270 assert(session
->kernel_session
);
1272 if (session
->kernel_session
->channel_count
== 0) {
1273 /* Create default channel */
1274 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1275 if (ret
!= LTTNG_OK
) {
1278 chan_kern_created
= 1;
1280 /* Add kernel context to kernel tracer */
1281 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1282 if (ret
!= LTTNG_OK
) {
1286 case LTTNG_DOMAIN_UST
:
1288 struct ltt_ust_session
*usess
= session
->ust_session
;
1289 unsigned int chan_count
;
1293 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1294 if (chan_count
== 0) {
1295 struct lttng_channel
*attr
;
1296 /* Create default channel */
1297 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1299 ret
= LTTNG_ERR_FATAL
;
1303 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1304 if (ret
!= LTTNG_OK
) {
1309 chan_ust_created
= 1;
1312 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1313 if (ret
!= LTTNG_OK
) {
1319 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1320 case LTTNG_DOMAIN_UST_PID
:
1321 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1324 ret
= LTTNG_ERR_UND
;
1331 if (chan_kern_created
) {
1332 struct ltt_kernel_channel
*kchan
=
1333 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1334 session
->kernel_session
);
1335 /* Created previously, this should NOT fail. */
1337 kernel_destroy_channel(kchan
);
1340 if (chan_ust_created
) {
1341 struct ltt_ust_channel
*uchan
=
1342 trace_ust_find_channel_by_name(
1343 session
->ust_session
->domain_global
.channels
,
1344 DEFAULT_CHANNEL_NAME
);
1345 /* Created previously, this should NOT fail. */
1347 /* Remove from the channel list of the session. */
1348 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1350 trace_ust_destroy_channel(uchan
);
1355 static int validate_event_name(const char *name
)
1358 const char *c
= name
;
1359 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1362 * Make sure that unescaped wildcards are only used as the last
1363 * character of the event name.
1365 while (c
< event_name_end
) {
1373 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1374 /* Wildcard is not the last character */
1375 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1388 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1389 * We own filter, exclusion, and filter_expression.
1391 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1392 char *channel_name
, struct lttng_event
*event
,
1393 char *filter_expression
,
1394 struct lttng_filter_bytecode
*filter
,
1395 struct lttng_event_exclusion
*exclusion
,
1398 int ret
, channel_created
= 0;
1399 struct lttng_channel
*attr
;
1403 assert(channel_name
);
1405 DBG("Enable event command for event \'%s\'", event
->name
);
1407 /* Special handling for kernel domain all events. */
1408 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1409 return enable_kevent_all(session
, domain
, channel_name
, event
,
1410 filter_expression
, filter
, wpipe
);
1413 ret
= validate_event_name(event
->name
);
1420 switch (domain
->type
) {
1421 case LTTNG_DOMAIN_KERNEL
:
1423 struct ltt_kernel_channel
*kchan
;
1426 * If a non-default channel has been created in the
1427 * session, explicitely require that -c chan_name needs
1430 if (session
->kernel_session
->has_non_default_channel
1431 && channel_name
[0] == '\0') {
1432 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1436 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1437 session
->kernel_session
);
1438 if (kchan
== NULL
) {
1439 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1440 LTTNG_BUFFER_GLOBAL
);
1442 ret
= LTTNG_ERR_FATAL
;
1445 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1447 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1448 if (ret
!= LTTNG_OK
) {
1454 channel_created
= 1;
1457 /* Get the newly created kernel channel pointer */
1458 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1459 session
->kernel_session
);
1460 if (kchan
== NULL
) {
1461 /* This sould not happen... */
1462 ret
= LTTNG_ERR_FATAL
;
1466 switch (event
->type
) {
1467 case LTTNG_EVENT_ALL
:
1468 case LTTNG_EVENT_PROBE
:
1469 case LTTNG_EVENT_FUNCTION
:
1470 case LTTNG_EVENT_FUNCTION_ENTRY
:
1471 case LTTNG_EVENT_TRACEPOINT
:
1472 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1473 if (ret
!= LTTNG_OK
) {
1474 if (channel_created
) {
1475 /* Let's not leak a useless channel. */
1476 kernel_destroy_channel(kchan
);
1481 case LTTNG_EVENT_SYSCALL
:
1482 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1483 if (ret
!= LTTNG_OK
) {
1488 ret
= LTTNG_ERR_UNK
;
1492 kernel_wait_quiescent(kernel_tracer_fd
);
1495 case LTTNG_DOMAIN_UST
:
1497 struct ltt_ust_channel
*uchan
;
1498 struct ltt_ust_session
*usess
= session
->ust_session
;
1503 * If a non-default channel has been created in the
1504 * session, explicitely require that -c chan_name needs
1507 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1508 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1512 /* Get channel from global UST domain */
1513 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1515 if (uchan
== NULL
) {
1516 /* Create default channel */
1517 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1518 usess
->buffer_type
);
1520 ret
= LTTNG_ERR_FATAL
;
1523 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1525 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1526 if (ret
!= LTTNG_OK
) {
1532 /* Get the newly created channel reference back */
1533 uchan
= trace_ust_find_channel_by_name(
1534 usess
->domain_global
.channels
, channel_name
);
1538 /* At this point, the session and channel exist on the tracer */
1539 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1540 filter_expression
, filter
, exclusion
);
1541 /* We have passed ownership */
1542 filter_expression
= NULL
;
1545 if (ret
!= LTTNG_OK
) {
1550 case LTTNG_DOMAIN_LOG4J
:
1551 case LTTNG_DOMAIN_JUL
:
1552 case LTTNG_DOMAIN_PYTHON
:
1554 const char *default_event_name
, *default_chan_name
;
1556 struct lttng_event uevent
;
1557 struct lttng_domain tmp_dom
;
1558 struct ltt_ust_session
*usess
= session
->ust_session
;
1562 agt
= trace_ust_find_agent(usess
, domain
->type
);
1564 agt
= agent_create(domain
->type
);
1566 ret
= -LTTNG_ERR_NOMEM
;
1569 agent_add(agt
, usess
->agents
);
1572 /* Create the default tracepoint. */
1573 memset(&uevent
, 0, sizeof(uevent
));
1574 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1575 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1576 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1577 if (!default_event_name
) {
1578 ret
= -LTTNG_ERR_FATAL
;
1581 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1582 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1585 * The domain type is changed because we are about to enable the
1586 * default channel and event for the JUL domain that are hardcoded.
1587 * This happens in the UST domain.
1589 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1590 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1592 switch (domain
->type
) {
1593 case LTTNG_DOMAIN_LOG4J
:
1594 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1596 case LTTNG_DOMAIN_JUL
:
1597 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1599 case LTTNG_DOMAIN_PYTHON
:
1600 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1603 /* The switch/case we are in should avoid this else big problem */
1608 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1611 filter_copy
= zmalloc(
1612 sizeof(struct lttng_filter_bytecode
)
1618 memcpy(filter_copy
, filter
,
1619 sizeof(struct lttng_filter_bytecode
)
1623 ret
= cmd_enable_event(session
, &tmp_dom
,
1624 (char *) default_chan_name
,
1625 &uevent
, filter_expression
, filter_copy
,
1627 /* We have passed ownership */
1628 filter_expression
= NULL
;
1631 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1635 /* The wild card * means that everything should be enabled. */
1636 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1637 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1640 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1643 if (ret
!= LTTNG_OK
) {
1650 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1651 case LTTNG_DOMAIN_UST_PID
:
1652 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1655 ret
= LTTNG_ERR_UND
;
1662 free(filter_expression
);
1670 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1673 int enable_kevent_all(struct ltt_session
*session
,
1674 struct lttng_domain
*domain
, char *channel_name
,
1675 struct lttng_event
*event
,
1676 char *filter_expression
,
1677 struct lttng_filter_bytecode
*filter
, int wpipe
)
1680 struct lttng_channel
*attr
;
1683 assert(channel_name
);
1687 switch (domain
->type
) {
1688 case LTTNG_DOMAIN_KERNEL
:
1690 struct ltt_kernel_channel
*kchan
;
1692 assert(session
->kernel_session
);
1695 * If a non-default channel has been created in the
1696 * session, explicitely require that -c chan_name needs
1699 if (session
->kernel_session
->has_non_default_channel
1700 && channel_name
[0] == '\0') {
1701 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1705 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1706 session
->kernel_session
);
1707 if (kchan
== NULL
) {
1708 /* Create default channel */
1709 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1710 LTTNG_BUFFER_GLOBAL
);
1712 ret
= LTTNG_ERR_FATAL
;
1715 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1717 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1718 if (ret
!= LTTNG_OK
) {
1724 /* Get the newly created kernel channel pointer */
1725 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1726 session
->kernel_session
);
1730 switch (event
->type
) {
1731 case LTTNG_EVENT_SYSCALL
:
1732 ret
= event_kernel_enable_syscall(kchan
, "");
1733 if (ret
!= LTTNG_OK
) {
1737 case LTTNG_EVENT_TRACEPOINT
:
1739 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1740 * events already registered to the channel.
1742 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1744 case LTTNG_EVENT_ALL
:
1745 /* Enable syscalls and tracepoints */
1746 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1749 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1753 /* Manage return value */
1754 if (ret
!= LTTNG_OK
) {
1756 * On error, cmd_enable_channel call will take care of destroying
1757 * the created channel if it was needed.
1762 kernel_wait_quiescent(kernel_tracer_fd
);
1766 ret
= LTTNG_ERR_UND
;
1779 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1781 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1784 ssize_t nb_events
= 0;
1787 case LTTNG_DOMAIN_KERNEL
:
1788 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1789 if (nb_events
< 0) {
1790 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1794 case LTTNG_DOMAIN_UST
:
1795 nb_events
= ust_app_list_events(events
);
1796 if (nb_events
< 0) {
1797 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1801 case LTTNG_DOMAIN_LOG4J
:
1802 case LTTNG_DOMAIN_JUL
:
1803 case LTTNG_DOMAIN_PYTHON
:
1804 nb_events
= agent_list_events(events
, domain
);
1805 if (nb_events
< 0) {
1806 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1811 ret
= LTTNG_ERR_UND
;
1818 /* Return negative value to differentiate return code */
1823 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1825 ssize_t
cmd_list_tracepoint_fields(int domain
,
1826 struct lttng_event_field
**fields
)
1829 ssize_t nb_fields
= 0;
1832 case LTTNG_DOMAIN_UST
:
1833 nb_fields
= ust_app_list_event_fields(fields
);
1834 if (nb_fields
< 0) {
1835 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1839 case LTTNG_DOMAIN_KERNEL
:
1840 default: /* fall-through */
1841 ret
= LTTNG_ERR_UND
;
1848 /* Return negative value to differentiate return code */
1852 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1854 return syscall_table_list(events
);
1858 * Command LTTNG_START_TRACE processed by the client thread.
1860 int cmd_start_trace(struct ltt_session
*session
)
1863 unsigned long nb_chan
= 0;
1864 struct ltt_kernel_session
*ksession
;
1865 struct ltt_ust_session
*usess
;
1869 /* Ease our life a bit ;) */
1870 ksession
= session
->kernel_session
;
1871 usess
= session
->ust_session
;
1873 /* Is the session already started? */
1874 if (session
->active
) {
1875 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1880 * Starting a session without channel is useless since after that it's not
1881 * possible to enable channel thus inform the client.
1883 if (usess
&& usess
->domain_global
.channels
) {
1885 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1889 nb_chan
+= ksession
->channel_count
;
1892 ret
= LTTNG_ERR_NO_CHANNEL
;
1896 /* Kernel tracing */
1897 if (ksession
!= NULL
) {
1898 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1899 if (ret
!= LTTNG_OK
) {
1904 /* Flag session that trace should start automatically */
1907 * Even though the start trace might fail, flag this session active so
1908 * other application coming in are started by default.
1912 ret
= ust_app_start_trace_all(usess
);
1914 ret
= LTTNG_ERR_UST_START_FAIL
;
1919 /* Flag this after a successful start. */
1920 session
->has_been_started
= 1;
1921 session
->active
= 1;
1930 * Command LTTNG_STOP_TRACE processed by the client thread.
1932 int cmd_stop_trace(struct ltt_session
*session
)
1935 struct ltt_kernel_channel
*kchan
;
1936 struct ltt_kernel_session
*ksession
;
1937 struct ltt_ust_session
*usess
;
1942 ksession
= session
->kernel_session
;
1943 usess
= session
->ust_session
;
1945 /* Session is not active. Skip everythong and inform the client. */
1946 if (!session
->active
) {
1947 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1952 if (ksession
&& ksession
->active
) {
1953 DBG("Stop kernel tracing");
1955 /* Flush metadata if exist */
1956 if (ksession
->metadata_stream_fd
>= 0) {
1957 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1959 ERR("Kernel metadata flush failed");
1963 /* Flush all buffers before stopping */
1964 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1965 ret
= kernel_flush_buffer(kchan
);
1967 ERR("Kernel flush buffer error");
1971 ret
= kernel_stop_session(ksession
);
1973 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1977 kernel_wait_quiescent(kernel_tracer_fd
);
1979 ksession
->active
= 0;
1982 if (usess
&& usess
->active
) {
1984 * Even though the stop trace might fail, flag this session inactive so
1985 * other application coming in are not started by default.
1989 ret
= ust_app_stop_trace_all(usess
);
1991 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1996 /* Flag inactive after a successful stop. */
1997 session
->active
= 0;
2005 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2007 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
2008 size_t nb_uri
, struct lttng_uri
*uris
)
2011 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2012 struct ltt_ust_session
*usess
= session
->ust_session
;
2013 struct consumer_output
*consumer
= NULL
;
2019 /* Can't set consumer URI if the session is active. */
2020 if (session
->active
) {
2021 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2026 * This case switch makes sure the domain session has a temporary consumer
2027 * so the URL can be set.
2031 /* Code flow error. A session MUST always have a consumer object */
2032 assert(session
->consumer
);
2034 * The URL will be added to the tracing session consumer instead of a
2035 * specific domain consumer.
2037 consumer
= session
->consumer
;
2039 case LTTNG_DOMAIN_KERNEL
:
2040 /* Code flow error if we don't have a kernel session here. */
2042 assert(ksess
->consumer
);
2043 consumer
= ksess
->consumer
;
2045 case LTTNG_DOMAIN_UST
:
2046 /* Code flow error if we don't have a kernel session here. */
2048 assert(usess
->consumer
);
2049 consumer
= usess
->consumer
;
2053 for (i
= 0; i
< nb_uri
; i
++) {
2054 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
2055 if (ret
!= LTTNG_OK
) {
2061 * Make sure to set the session in output mode after we set URI since a
2062 * session can be created without URL (thus flagged in no output mode).
2064 session
->output_traces
= 1;
2066 ksess
->output_traces
= 1;
2068 usess
->output_traces
= 1;
2079 * Command LTTNG_CREATE_SESSION processed by the client thread.
2081 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2082 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2085 struct ltt_session
*session
;
2091 * Verify if the session already exist
2093 * XXX: There is no need for the session lock list here since the caller
2094 * (process_client_msg) is holding it. We might want to change that so a
2095 * single command does not lock the entire session list.
2097 session
= session_find_by_name(name
);
2098 if (session
!= NULL
) {
2099 ret
= LTTNG_ERR_EXIST_SESS
;
2103 /* Create tracing session in the registry */
2104 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2105 LTTNG_SOCK_GET_GID_CRED(creds
));
2106 if (ret
!= LTTNG_OK
) {
2111 * Get the newly created session pointer back
2113 * XXX: There is no need for the session lock list here since the caller
2114 * (process_client_msg) is holding it. We might want to change that so a
2115 * single command does not lock the entire session list.
2117 session
= session_find_by_name(name
);
2120 session
->live_timer
= live_timer
;
2121 /* Create default consumer output for the session not yet created. */
2122 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2123 if (session
->consumer
== NULL
) {
2124 ret
= LTTNG_ERR_FATAL
;
2125 goto consumer_error
;
2129 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2130 if (ret
!= LTTNG_OK
) {
2131 goto consumer_error
;
2133 session
->output_traces
= 1;
2135 session
->output_traces
= 0;
2136 DBG2("Session %s created with no output", session
->name
);
2139 session
->consumer
->enabled
= 1;
2144 session_destroy(session
);
2151 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2153 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2154 size_t nb_uri
, lttng_sock_cred
*creds
)
2157 struct ltt_session
*session
;
2158 struct snapshot_output
*new_output
= NULL
;
2164 * Create session in no output mode with URIs set to NULL. The uris we've
2165 * received are for a default snapshot output if one.
2167 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2168 if (ret
!= LTTNG_OK
) {
2172 /* Get the newly created session pointer back. This should NEVER fail. */
2173 session
= session_find_by_name(name
);
2176 /* Flag session for snapshot mode. */
2177 session
->snapshot_mode
= 1;
2179 /* Skip snapshot output creation if no URI is given. */
2184 new_output
= snapshot_output_alloc();
2186 ret
= LTTNG_ERR_NOMEM
;
2187 goto error_snapshot_alloc
;
2190 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2191 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2193 if (ret
== -ENOMEM
) {
2194 ret
= LTTNG_ERR_NOMEM
;
2196 ret
= LTTNG_ERR_INVALID
;
2198 goto error_snapshot
;
2202 snapshot_add_output(&session
->snapshot
, new_output
);
2209 snapshot_output_destroy(new_output
);
2210 error_snapshot_alloc
:
2211 session_destroy(session
);
2217 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2219 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2222 struct ltt_ust_session
*usess
;
2223 struct ltt_kernel_session
*ksess
;
2228 usess
= session
->ust_session
;
2229 ksess
= session
->kernel_session
;
2231 /* Clean kernel session teardown */
2232 kernel_destroy_session(ksess
);
2234 /* UST session teardown */
2236 /* Close any relayd session */
2237 consumer_output_send_destroy_relayd(usess
->consumer
);
2239 /* Destroy every UST application related to this session. */
2240 ret
= ust_app_destroy_trace_all(usess
);
2242 ERR("Error in ust_app_destroy_trace_all");
2245 /* Clean up the rest. */
2246 trace_ust_destroy_session(usess
);
2250 * Must notify the kernel thread here to update it's poll set in order to
2251 * remove the channel(s)' fd just destroyed.
2253 ret
= notify_thread_pipe(wpipe
);
2255 PERROR("write kernel poll pipe");
2258 ret
= session_destroy(session
);
2264 * Command LTTNG_CALIBRATE processed by the client thread.
2266 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2271 case LTTNG_DOMAIN_KERNEL
:
2273 struct lttng_kernel_calibrate kcalibrate
;
2275 switch (calibrate
->type
) {
2276 case LTTNG_CALIBRATE_FUNCTION
:
2278 /* Default and only possible calibrate option. */
2279 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2283 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2285 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2290 case LTTNG_DOMAIN_UST
:
2292 struct lttng_ust_calibrate ucalibrate
;
2294 switch (calibrate
->type
) {
2295 case LTTNG_CALIBRATE_FUNCTION
:
2297 /* Default and only possible calibrate option. */
2298 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2302 ret
= ust_app_calibrate_glb(&ucalibrate
);
2304 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2310 ret
= LTTNG_ERR_UND
;
2321 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2323 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2324 const char *sock_path
, struct consumer_data
*cdata
)
2327 struct consumer_socket
*socket
= NULL
;
2334 case LTTNG_DOMAIN_KERNEL
:
2336 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2340 /* Can't register a consumer if there is already one */
2341 if (ksess
->consumer_fds_sent
!= 0) {
2342 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2346 sock
= lttcomm_connect_unix_sock(sock_path
);
2348 ret
= LTTNG_ERR_CONNECT_FAIL
;
2351 cdata
->cmd_sock
= sock
;
2353 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2354 if (socket
== NULL
) {
2357 PERROR("close register consumer");
2359 cdata
->cmd_sock
= -1;
2360 ret
= LTTNG_ERR_FATAL
;
2364 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2365 if (socket
->lock
== NULL
) {
2366 PERROR("zmalloc pthread mutex");
2367 ret
= LTTNG_ERR_FATAL
;
2370 pthread_mutex_init(socket
->lock
, NULL
);
2371 socket
->registered
= 1;
2374 consumer_add_socket(socket
, ksess
->consumer
);
2377 pthread_mutex_lock(&cdata
->pid_mutex
);
2379 pthread_mutex_unlock(&cdata
->pid_mutex
);
2384 /* TODO: Userspace tracing */
2385 ret
= LTTNG_ERR_UND
;
2393 consumer_destroy_socket(socket
);
2399 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2401 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2402 struct lttng_domain
**domains
)
2407 struct lttng_ht_iter iter
;
2409 if (session
->kernel_session
!= NULL
) {
2410 DBG3("Listing domains found kernel domain");
2414 if (session
->ust_session
!= NULL
) {
2415 DBG3("Listing domains found UST global domain");
2419 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2421 if (agt
->being_used
) {
2432 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2433 if (*domains
== NULL
) {
2434 ret
= LTTNG_ERR_FATAL
;
2438 if (session
->kernel_session
!= NULL
) {
2439 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2443 if (session
->ust_session
!= NULL
) {
2444 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2445 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2449 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2451 if (agt
->being_used
) {
2452 (*domains
)[index
].type
= agt
->domain
;
2453 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2463 /* Return negative value to differentiate return code */
2469 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2471 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2472 struct lttng_channel
**channels
)
2475 ssize_t nb_chan
= 0;
2478 case LTTNG_DOMAIN_KERNEL
:
2479 if (session
->kernel_session
!= NULL
) {
2480 nb_chan
= session
->kernel_session
->channel_count
;
2482 DBG3("Number of kernel channels %zd", nb_chan
);
2484 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2487 case LTTNG_DOMAIN_UST
:
2488 if (session
->ust_session
!= NULL
) {
2490 nb_chan
= lttng_ht_get_count(
2491 session
->ust_session
->domain_global
.channels
);
2494 DBG3("Number of UST global channels %zd", nb_chan
);
2496 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2501 ret
= LTTNG_ERR_UND
;
2506 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2507 if (*channels
== NULL
) {
2508 ret
= LTTNG_ERR_FATAL
;
2512 list_lttng_channels(domain
, session
, *channels
);
2518 /* Return negative value to differentiate return code */
2523 * Command LTTNG_LIST_EVENTS processed by the client thread.
2525 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2526 char *channel_name
, struct lttng_event
**events
)
2529 ssize_t nb_event
= 0;
2532 case LTTNG_DOMAIN_KERNEL
:
2533 if (session
->kernel_session
!= NULL
) {
2534 nb_event
= list_lttng_kernel_events(channel_name
,
2535 session
->kernel_session
, events
);
2538 case LTTNG_DOMAIN_UST
:
2540 if (session
->ust_session
!= NULL
) {
2541 nb_event
= list_lttng_ust_global_events(channel_name
,
2542 &session
->ust_session
->domain_global
, events
);
2546 case LTTNG_DOMAIN_LOG4J
:
2547 case LTTNG_DOMAIN_JUL
:
2548 case LTTNG_DOMAIN_PYTHON
:
2549 if (session
->ust_session
) {
2550 struct lttng_ht_iter iter
;
2553 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2554 &iter
.iter
, agt
, node
.node
) {
2555 nb_event
= list_lttng_agent_events(agt
, events
);
2560 ret
= LTTNG_ERR_UND
;
2567 /* Return negative value to differentiate return code */
2572 * Using the session list, filled a lttng_session array to send back to the
2573 * client for session listing.
2575 * The session list lock MUST be acquired before calling this function. Use
2576 * session_lock_list() and session_unlock_list().
2578 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2583 struct ltt_session
*session
;
2584 struct ltt_session_list
*list
= session_get_list();
2586 DBG("Getting all available session for UID %d GID %d",
2589 * Iterate over session list and append data after the control struct in
2592 cds_list_for_each_entry(session
, &list
->head
, list
) {
2594 * Only list the sessions the user can control.
2596 if (!session_access_ok(session
, uid
, gid
)) {
2600 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2601 struct ltt_ust_session
*usess
= session
->ust_session
;
2603 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2604 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2605 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2606 ret
= build_network_session_path(sessions
[i
].path
,
2607 sizeof(sessions
[i
].path
), session
);
2609 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2610 session
->consumer
->dst
.trace_path
);
2613 PERROR("snprintf session path");
2617 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2618 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2619 sessions
[i
].enabled
= session
->active
;
2620 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2621 sessions
[i
].live_timer_interval
= session
->live_timer
;
2627 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2628 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2630 int cmd_data_pending(struct ltt_session
*session
)
2633 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2634 struct ltt_ust_session
*usess
= session
->ust_session
;
2638 /* Session MUST be stopped to ask for data availability. */
2639 if (session
->active
) {
2640 ret
= LTTNG_ERR_SESSION_STARTED
;
2644 * If stopped, just make sure we've started before else the above call
2645 * will always send that there is data pending.
2647 * The consumer assumes that when the data pending command is received,
2648 * the trace has been started before or else no output data is written
2649 * by the streams which is a condition for data pending. So, this is
2650 * *VERY* important that we don't ask the consumer before a start
2653 if (!session
->has_been_started
) {
2659 if (ksess
&& ksess
->consumer
) {
2660 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2662 /* Data is still being extracted for the kernel. */
2667 if (usess
&& usess
->consumer
) {
2668 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2670 /* Data is still being extracted for the kernel. */
2675 /* Data is ready to be read by a viewer */
2683 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2685 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2687 int cmd_snapshot_add_output(struct ltt_session
*session
,
2688 struct lttng_snapshot_output
*output
, uint32_t *id
)
2691 struct snapshot_output
*new_output
;
2696 DBG("Cmd snapshot add output for session %s", session
->name
);
2699 * Permission denied to create an output if the session is not
2700 * set in no output mode.
2702 if (session
->output_traces
) {
2703 ret
= LTTNG_ERR_EPERM
;
2707 /* Only one output is allowed until we have the "tee" feature. */
2708 if (session
->snapshot
.nb_output
== 1) {
2709 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2713 new_output
= snapshot_output_alloc();
2715 ret
= LTTNG_ERR_NOMEM
;
2719 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2720 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2721 &session
->snapshot
);
2723 if (ret
== -ENOMEM
) {
2724 ret
= LTTNG_ERR_NOMEM
;
2726 ret
= LTTNG_ERR_INVALID
;
2732 snapshot_add_output(&session
->snapshot
, new_output
);
2734 *id
= new_output
->id
;
2741 snapshot_output_destroy(new_output
);
2747 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2749 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2751 int cmd_snapshot_del_output(struct ltt_session
*session
,
2752 struct lttng_snapshot_output
*output
)
2755 struct snapshot_output
*sout
= NULL
;
2763 * Permission denied to create an output if the session is not
2764 * set in no output mode.
2766 if (session
->output_traces
) {
2767 ret
= LTTNG_ERR_EPERM
;
2772 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2774 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2775 } else if (*output
->name
!= '\0') {
2776 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2778 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2781 ret
= LTTNG_ERR_INVALID
;
2785 snapshot_delete_output(&session
->snapshot
, sout
);
2786 snapshot_output_destroy(sout
);
2795 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2797 * If no output is available, outputs is untouched and 0 is returned.
2799 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2801 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2802 struct lttng_snapshot_output
**outputs
)
2805 struct lttng_snapshot_output
*list
= NULL
;
2806 struct lttng_ht_iter iter
;
2807 struct snapshot_output
*output
;
2812 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2815 * Permission denied to create an output if the session is not
2816 * set in no output mode.
2818 if (session
->output_traces
) {
2819 ret
= -LTTNG_ERR_EPERM
;
2823 if (session
->snapshot
.nb_output
== 0) {
2828 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2830 ret
= -LTTNG_ERR_NOMEM
;
2834 /* Copy list from session to the new list object. */
2836 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2837 output
, node
.node
) {
2838 assert(output
->consumer
);
2839 list
[idx
].id
= output
->id
;
2840 list
[idx
].max_size
= output
->max_size
;
2841 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2842 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2843 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2844 sizeof(list
[idx
].ctrl_url
));
2847 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2848 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2850 ret
= -LTTNG_ERR_NOMEM
;
2855 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2856 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2858 ret
= -LTTNG_ERR_NOMEM
;
2867 ret
= session
->snapshot
.nb_output
;
2875 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2876 * snapshot output is *not* set with a remote destination.
2878 * Return 0 on success or a LTTNG_ERR code.
2880 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2881 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2884 struct lttng_ht_iter iter
;
2885 struct consumer_socket
*socket
;
2888 assert(snap_output
);
2891 DBG2("Set relayd object from snapshot output");
2893 /* Ignore if snapshot consumer output is not network. */
2894 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2899 * For each consumer socket, create and send the relayd object of the
2903 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2904 socket
, node
.node
) {
2905 ret
= send_consumer_relayd_sockets(0, session
->id
,
2906 snap_output
->consumer
, socket
,
2907 session
->name
, session
->hostname
,
2908 session
->live_timer
);
2909 if (ret
!= LTTNG_OK
) {
2921 * Record a kernel snapshot.
2923 * Return LTTNG_OK on success or a LTTNG_ERR code.
2925 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2926 struct snapshot_output
*output
, struct ltt_session
*session
,
2927 int wait
, uint64_t max_stream_size
)
2935 /* Get the datetime for the snapshot output directory. */
2936 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2937 sizeof(output
->datetime
));
2939 ret
= LTTNG_ERR_INVALID
;
2944 * Copy kernel session sockets so we can communicate with the right
2945 * consumer for the snapshot record command.
2947 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2949 ret
= LTTNG_ERR_NOMEM
;
2953 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2954 if (ret
!= LTTNG_OK
) {
2955 goto error_snapshot
;
2958 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2959 if (ret
!= LTTNG_OK
) {
2960 goto error_snapshot
;
2966 /* Clean up copied sockets so this output can use some other later on. */
2967 consumer_destroy_output_sockets(output
->consumer
);
2973 * Record a UST snapshot.
2975 * Return 0 on success or a LTTNG_ERR error code.
2977 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2978 struct snapshot_output
*output
, struct ltt_session
*session
,
2979 int wait
, uint64_t max_stream_size
)
2987 /* Get the datetime for the snapshot output directory. */
2988 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2989 sizeof(output
->datetime
));
2991 ret
= LTTNG_ERR_INVALID
;
2996 * Copy UST session sockets so we can communicate with the right
2997 * consumer for the snapshot record command.
2999 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3001 ret
= LTTNG_ERR_NOMEM
;
3005 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3006 if (ret
!= LTTNG_OK
) {
3007 goto error_snapshot
;
3010 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
3014 ret
= LTTNG_ERR_INVALID
;
3017 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3020 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3023 goto error_snapshot
;
3029 /* Clean up copied sockets so this output can use some other later on. */
3030 consumer_destroy_output_sockets(output
->consumer
);
3036 * Return the biggest subbuffer size of all channels in the given session.
3038 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
3040 uint64_t max_size
= 0;
3044 if (session
->kernel_session
) {
3045 struct ltt_kernel_channel
*chan
;
3046 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3049 * For each channel, add to the max size the size of each subbuffer
3050 * multiplied by their sized.
3052 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3053 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
3054 max_size
= chan
->channel
->attr
.subbuf_size
;
3059 if (session
->ust_session
) {
3060 struct lttng_ht_iter iter
;
3061 struct ltt_ust_channel
*uchan
;
3062 struct ltt_ust_session
*usess
= session
->ust_session
;
3065 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
3067 if (uchan
->attr
.subbuf_size
> max_size
) {
3068 max_size
= uchan
->attr
.subbuf_size
;
3078 * Returns the total number of streams for a session or a negative value
3081 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3083 unsigned int total_streams
= 0;
3085 if (session
->kernel_session
) {
3086 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3088 total_streams
+= ksess
->stream_count_global
;
3091 if (session
->ust_session
) {
3092 struct ltt_ust_session
*usess
= session
->ust_session
;
3094 total_streams
+= ust_app_get_nb_stream(usess
);
3097 return total_streams
;
3101 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3103 * The wait parameter is ignored so this call always wait for the snapshot to
3104 * complete before returning.
3106 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3108 int cmd_snapshot_record(struct ltt_session
*session
,
3109 struct lttng_snapshot_output
*output
, int wait
)
3112 unsigned int use_tmp_output
= 0;
3113 struct snapshot_output tmp_output
;
3114 unsigned int nb_streams
, snapshot_success
= 0;
3115 uint64_t session_max_size
= 0, max_stream_size
= 0;
3120 DBG("Cmd snapshot record for session %s", session
->name
);
3123 * Permission denied to create an output if the session is not
3124 * set in no output mode.
3126 if (session
->output_traces
) {
3127 ret
= LTTNG_ERR_EPERM
;
3131 /* The session needs to be started at least once. */
3132 if (!session
->has_been_started
) {
3133 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3137 /* Use temporary output for the session. */
3138 if (*output
->ctrl_url
!= '\0') {
3139 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3140 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3143 if (ret
== -ENOMEM
) {
3144 ret
= LTTNG_ERR_NOMEM
;
3146 ret
= LTTNG_ERR_INVALID
;
3150 /* Use the global session count for the temporary snapshot. */
3151 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3156 * Get the session maximum size for a snapshot meaning it will compute the
3157 * size of all streams from all domain.
3159 max_stream_size
= get_session_max_subbuf_size(session
);
3161 nb_streams
= get_session_nb_streams(session
);
3164 * The maximum size of the snapshot is the number of streams multiplied
3165 * by the biggest subbuf size of all channels in a session which is the
3166 * maximum stream size available for each stream. The session max size
3167 * is now checked against the snapshot max size value given by the user
3168 * and if lower, an error is returned.
3170 session_max_size
= max_stream_size
* nb_streams
;
3173 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3174 session_max_size
, max_stream_size
);
3177 * If we use a temporary output, check right away if the max size fits else
3178 * for each output the max size will be checked.
3180 if (use_tmp_output
&&
3181 (tmp_output
.max_size
!= 0 &&
3182 tmp_output
.max_size
< session_max_size
)) {
3183 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3187 if (session
->kernel_session
) {
3188 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3190 if (use_tmp_output
) {
3191 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3192 wait
, max_stream_size
);
3193 if (ret
!= LTTNG_OK
) {
3196 snapshot_success
= 1;
3198 struct snapshot_output
*sout
;
3199 struct lttng_ht_iter iter
;
3202 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3203 &iter
.iter
, sout
, node
.node
) {
3205 * Make a local copy of the output and assign the possible
3206 * temporary value given by the caller.
3208 memset(&tmp_output
, 0, sizeof(tmp_output
));
3209 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3211 /* Use temporary max size. */
3212 if (output
->max_size
!= (uint64_t) -1ULL) {
3213 tmp_output
.max_size
= output
->max_size
;
3216 if (tmp_output
.max_size
!= 0 &&
3217 tmp_output
.max_size
< session_max_size
) {
3219 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3223 /* Use temporary name. */
3224 if (*output
->name
!= '\0') {
3225 strncpy(tmp_output
.name
, output
->name
,
3226 sizeof(tmp_output
.name
));
3229 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3231 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3232 session
, wait
, max_stream_size
);
3233 if (ret
!= LTTNG_OK
) {
3237 snapshot_success
= 1;
3243 if (session
->ust_session
) {
3244 struct ltt_ust_session
*usess
= session
->ust_session
;
3246 if (use_tmp_output
) {
3247 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3248 wait
, max_stream_size
);
3249 if (ret
!= LTTNG_OK
) {
3252 snapshot_success
= 1;
3254 struct snapshot_output
*sout
;
3255 struct lttng_ht_iter iter
;
3258 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3259 &iter
.iter
, sout
, node
.node
) {
3261 * Make a local copy of the output and assign the possible
3262 * temporary value given by the caller.
3264 memset(&tmp_output
, 0, sizeof(tmp_output
));
3265 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3267 /* Use temporary max size. */
3268 if (output
->max_size
!= (uint64_t) -1ULL) {
3269 tmp_output
.max_size
= output
->max_size
;
3272 if (tmp_output
.max_size
!= 0 &&
3273 tmp_output
.max_size
< session_max_size
) {
3275 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3279 /* Use temporary name. */
3280 if (*output
->name
!= '\0') {
3281 strncpy(tmp_output
.name
, output
->name
,
3282 sizeof(tmp_output
.name
));
3285 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3287 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3288 wait
, max_stream_size
);
3289 if (ret
!= LTTNG_OK
) {
3293 snapshot_success
= 1;
3299 if (snapshot_success
) {
3300 session
->snapshot
.nb_snapshot
++;
3302 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3310 * Init command subsystem.
3315 * Set network sequence index to 1 for streams to match a relayd
3316 * socket on the consumer side.
3318 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3319 relayd_net_seq_idx
= 1;
3320 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3322 DBG("Command subsystem initialized");