2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * 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>
30 #include <common/compat/string.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
36 #include "health-sessiond.h"
38 #include "kernel-consumer.h"
39 #include "lttng-sessiond.h"
43 #include "buffer-registry.h"
48 * Used to keep a unique index for each relayd socket created where this value
49 * is associated with streams on the consumer so it can match the right relayd
50 * to send to. It must be accessed with the relayd_net_seq_idx_lock
53 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 static uint64_t relayd_net_seq_idx
;
56 static int validate_event_name(const char *);
57 static int validate_ust_event_name(const char *);
58 static int cmd_enable_event_internal(struct ltt_session
*session
,
59 struct lttng_domain
*domain
,
60 char *channel_name
, struct lttng_event
*event
,
61 char *filter_expression
,
62 struct lttng_filter_bytecode
*filter
,
63 struct lttng_event_exclusion
*exclusion
,
67 * Create a session path used by list_lttng_sessions for the case that the
68 * session consumer is on the network.
70 static int build_network_session_path(char *dst
, size_t size
,
71 struct ltt_session
*session
)
73 int ret
, kdata_port
, udata_port
;
74 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
75 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
80 memset(tmp_urls
, 0, sizeof(tmp_urls
));
81 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
83 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
85 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
86 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
87 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
90 if (session
->ust_session
&& session
->ust_session
->consumer
) {
91 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
92 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
95 if (uuri
== NULL
&& kuri
== NULL
) {
96 uri
= &session
->consumer
->dst
.net
.control
;
97 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
98 } else if (kuri
&& uuri
) {
99 ret
= uri_compare(kuri
, uuri
);
103 /* Build uuri URL string */
104 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
111 } else if (kuri
&& uuri
== NULL
) {
113 } else if (uuri
&& kuri
== NULL
) {
117 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
123 * Do we have a UST url set. If yes, this means we have both kernel and UST
126 if (*tmp_uurl
!= '\0') {
127 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
128 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
131 if (kuri
|| (!kuri
&& !uuri
)) {
134 /* No kernel URI, use the UST port. */
137 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
145 * Get run-time attributes if the session has been started (discarded events,
148 static int get_kernel_runtime_stats(struct ltt_session
*session
,
149 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
150 uint64_t *lost_packets
)
154 if (!session
->has_been_started
) {
156 *discarded_events
= 0;
161 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
162 session
->kernel_session
->consumer
,
168 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
169 session
->kernel_session
->consumer
,
180 * Get run-time attributes if the session has been started (discarded events,
183 static int get_ust_runtime_stats(struct ltt_session
*session
,
184 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
185 uint64_t *lost_packets
)
188 struct ltt_ust_session
*usess
;
190 usess
= session
->ust_session
;
191 assert(discarded_events
);
192 assert(lost_packets
);
194 if (!usess
|| !session
->has_been_started
) {
195 *discarded_events
= 0;
201 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
202 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
203 &usess
->buffer_reg_uid_list
,
204 usess
->consumer
, uchan
->id
,
205 uchan
->attr
.overwrite
,
208 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
209 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
210 uchan
, usess
->consumer
,
211 uchan
->attr
.overwrite
,
217 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
218 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
220 ERR("Unsupported buffer type");
231 * Fill lttng_channel array of all channels.
233 static void list_lttng_channels(enum lttng_domain_type domain
,
234 struct ltt_session
*session
, struct lttng_channel
*channels
,
235 struct lttcomm_channel_extended
*chan_exts
)
238 struct ltt_kernel_channel
*kchan
;
240 DBG("Listing channels for session %s", session
->name
);
243 case LTTNG_DOMAIN_KERNEL
:
244 /* Kernel channels */
245 if (session
->kernel_session
!= NULL
) {
246 cds_list_for_each_entry(kchan
,
247 &session
->kernel_session
->channel_list
.head
, list
) {
248 uint64_t discarded_events
, lost_packets
;
250 ret
= get_kernel_runtime_stats(session
, kchan
,
251 &discarded_events
, &lost_packets
);
255 /* Copy lttng_channel struct to array */
256 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
257 channels
[i
].enabled
= kchan
->enabled
;
258 chan_exts
[i
].discarded_events
=
260 chan_exts
[i
].lost_packets
= lost_packets
;
265 case LTTNG_DOMAIN_UST
:
267 struct lttng_ht_iter iter
;
268 struct ltt_ust_channel
*uchan
;
271 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
272 &iter
.iter
, uchan
, node
.node
) {
273 uint64_t discarded_events
, lost_packets
;
275 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
276 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
277 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
278 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
279 channels
[i
].attr
.switch_timer_interval
=
280 uchan
->attr
.switch_timer_interval
;
281 channels
[i
].attr
.read_timer_interval
=
282 uchan
->attr
.read_timer_interval
;
283 channels
[i
].enabled
= uchan
->enabled
;
284 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
285 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
288 * Map enum lttng_ust_output to enum lttng_event_output.
290 switch (uchan
->attr
.output
) {
292 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
296 * LTTNG_UST_MMAP is the only supported UST
303 ret
= get_ust_runtime_stats(session
, uchan
,
304 &discarded_events
, &lost_packets
);
308 chan_exts
[i
].discarded_events
= discarded_events
;
309 chan_exts
[i
].lost_packets
= lost_packets
;
323 static void increment_extended_len(const char *filter_expression
,
324 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
326 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
328 if (filter_expression
) {
329 *extended_len
+= strlen(filter_expression
) + 1;
333 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
337 static void append_extended_info(const char *filter_expression
,
338 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
340 struct lttcomm_event_extended_header extended_header
;
341 size_t filter_len
= 0;
342 size_t nb_exclusions
= 0;
344 if (filter_expression
) {
345 filter_len
= strlen(filter_expression
) + 1;
349 nb_exclusions
= exclusion
->count
;
352 /* Set header fields */
353 extended_header
.filter_len
= filter_len
;
354 extended_header
.nb_exclusions
= nb_exclusions
;
357 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
358 *extended_at
+= sizeof(extended_header
);
360 /* Copy filter string */
361 if (filter_expression
) {
362 memcpy(*extended_at
, filter_expression
, filter_len
);
363 *extended_at
+= filter_len
;
366 /* Copy exclusion names */
368 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
370 memcpy(*extended_at
, &exclusion
->names
, len
);
376 * Create a list of agent domain events.
378 * Return number of events in list on success or else a negative value.
380 static int list_lttng_agent_events(struct agent
*agt
,
381 struct lttng_event
**events
, size_t *total_size
)
384 unsigned int nb_event
= 0;
385 struct agent_event
*event
;
386 struct lttng_event
*tmp_events
;
387 struct lttng_ht_iter iter
;
388 size_t extended_len
= 0;
394 DBG3("Listing agent events");
397 nb_event
= lttng_ht_get_count(agt
->events
);
405 /* Compute required extended infos size */
406 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
409 * This is only valid because the commands which add events are
410 * processed in the same thread as the listing.
413 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
414 increment_extended_len(event
->filter_expression
, NULL
,
419 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
420 tmp_events
= zmalloc(*total_size
);
422 PERROR("zmalloc agent events session");
423 ret
= -LTTNG_ERR_FATAL
;
427 extended_at
= ((uint8_t *) tmp_events
) +
428 nb_event
* sizeof(struct lttng_event
);
431 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
432 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
433 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
434 tmp_events
[i
].enabled
= event
->enabled
;
435 tmp_events
[i
].loglevel
= event
->loglevel_value
;
436 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
439 /* Append extended info */
440 append_extended_info(event
->filter_expression
, NULL
,
445 *events
= tmp_events
;
449 assert(nb_event
== i
);
454 * Create a list of ust global domain events.
456 static int list_lttng_ust_global_events(char *channel_name
,
457 struct ltt_ust_domain_global
*ust_global
,
458 struct lttng_event
**events
, size_t *total_size
)
461 unsigned int nb_event
= 0;
462 struct lttng_ht_iter iter
;
463 struct lttng_ht_node_str
*node
;
464 struct ltt_ust_channel
*uchan
;
465 struct ltt_ust_event
*uevent
;
466 struct lttng_event
*tmp
;
467 size_t extended_len
= 0;
470 DBG("Listing UST global events for channel %s", channel_name
);
474 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
475 node
= lttng_ht_iter_get_node_str(&iter
);
477 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
481 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
483 nb_event
= lttng_ht_get_count(uchan
->events
);
490 DBG3("Listing UST global %d events", nb_event
);
492 /* Compute required extended infos size */
493 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
494 if (uevent
->internal
) {
499 increment_extended_len(uevent
->filter_expression
,
500 uevent
->exclusion
, &extended_len
);
503 /* All events are internal, skip. */
509 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
510 tmp
= zmalloc(*total_size
);
512 ret
= -LTTNG_ERR_FATAL
;
516 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
518 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
519 if (uevent
->internal
) {
520 /* This event should remain hidden from clients */
523 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
524 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
525 tmp
[i
].enabled
= uevent
->enabled
;
527 switch (uevent
->attr
.instrumentation
) {
528 case LTTNG_UST_TRACEPOINT
:
529 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
531 case LTTNG_UST_PROBE
:
532 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
534 case LTTNG_UST_FUNCTION
:
535 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
539 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
540 switch (uevent
->attr
.loglevel_type
) {
541 case LTTNG_UST_LOGLEVEL_ALL
:
542 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
544 case LTTNG_UST_LOGLEVEL_RANGE
:
545 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
547 case LTTNG_UST_LOGLEVEL_SINGLE
:
548 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
551 if (uevent
->filter
) {
554 if (uevent
->exclusion
) {
555 tmp
[i
].exclusion
= 1;
559 /* Append extended info */
560 append_extended_info(uevent
->filter_expression
,
561 uevent
->exclusion
, &extended_at
);
572 * Fill lttng_event array of all kernel events in the channel.
574 static int list_lttng_kernel_events(char *channel_name
,
575 struct ltt_kernel_session
*kernel_session
,
576 struct lttng_event
**events
, size_t *total_size
)
579 unsigned int nb_event
;
580 struct ltt_kernel_event
*event
;
581 struct ltt_kernel_channel
*kchan
;
582 size_t extended_len
= 0;
585 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
587 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
591 nb_event
= kchan
->event_count
;
593 DBG("Listing events for channel %s", kchan
->channel
->name
);
601 /* Compute required extended infos size */
602 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
603 increment_extended_len(event
->filter_expression
, NULL
,
607 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
608 *events
= zmalloc(*total_size
);
609 if (*events
== NULL
) {
610 ret
= LTTNG_ERR_FATAL
;
614 extended_at
= ((void *) *events
) +
615 nb_event
* sizeof(struct lttng_event
);
617 /* Kernel channels */
618 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
619 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
620 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
621 (*events
)[i
].enabled
= event
->enabled
;
622 (*events
)[i
].filter
=
623 (unsigned char) !!event
->filter_expression
;
625 switch (event
->event
->instrumentation
) {
626 case LTTNG_KERNEL_TRACEPOINT
:
627 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
629 case LTTNG_KERNEL_KRETPROBE
:
630 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
631 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
632 sizeof(struct lttng_kernel_kprobe
));
634 case LTTNG_KERNEL_KPROBE
:
635 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
636 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
637 sizeof(struct lttng_kernel_kprobe
));
639 case LTTNG_KERNEL_FUNCTION
:
640 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
641 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
642 sizeof(struct lttng_kernel_function
));
644 case LTTNG_KERNEL_NOOP
:
645 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
647 case LTTNG_KERNEL_SYSCALL
:
648 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
650 case LTTNG_KERNEL_ALL
:
656 /* Append extended info */
657 append_extended_info(event
->filter_expression
, NULL
,
665 /* Negate the error code to differentiate the size from an error */
670 * Add URI so the consumer output object. Set the correct path depending on the
671 * domain adding the default trace directory.
673 static int add_uri_to_consumer(struct consumer_output
*consumer
,
674 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
675 const char *session_name
)
678 const char *default_trace_dir
;
682 if (consumer
== NULL
) {
683 DBG("No consumer detected. Don't add URI. Stopping.");
684 ret
= LTTNG_ERR_NO_CONSUMER
;
689 case LTTNG_DOMAIN_KERNEL
:
690 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
692 case LTTNG_DOMAIN_UST
:
693 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
697 * This case is possible is we try to add the URI to the global tracing
698 * session consumer object which in this case there is no subdir.
700 default_trace_dir
= "";
703 switch (uri
->dtype
) {
706 DBG2("Setting network URI to consumer");
708 if (consumer
->type
== CONSUMER_DST_NET
) {
709 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
710 consumer
->dst
.net
.control_isset
) ||
711 (uri
->stype
== LTTNG_STREAM_DATA
&&
712 consumer
->dst
.net
.data_isset
)) {
713 ret
= LTTNG_ERR_URL_EXIST
;
717 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
720 consumer
->type
= CONSUMER_DST_NET
;
722 /* Set URI into consumer output object */
723 ret
= consumer_set_network_uri(consumer
, uri
);
727 } else if (ret
== 1) {
729 * URI was the same in the consumer so we do not append the subdir
730 * again so to not duplicate output dir.
736 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
737 ret
= consumer_set_subdir(consumer
, session_name
);
739 ret
= LTTNG_ERR_FATAL
;
744 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
745 /* On a new subdir, reappend the default trace dir. */
746 strncat(consumer
->subdir
, default_trace_dir
,
747 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
748 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
753 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
754 memset(consumer
->dst
.trace_path
, 0,
755 sizeof(consumer
->dst
.trace_path
));
756 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
757 sizeof(consumer
->dst
.trace_path
));
758 /* Append default trace dir */
759 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
760 sizeof(consumer
->dst
.trace_path
) -
761 strlen(consumer
->dst
.trace_path
) - 1);
762 /* Flag consumer as local. */
763 consumer
->type
= CONSUMER_DST_LOCAL
;
774 * Init tracing by creating trace directory and sending fds kernel consumer.
776 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
779 struct lttng_ht_iter iter
;
780 struct consumer_socket
*socket
;
786 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
787 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
789 pthread_mutex_lock(socket
->lock
);
790 ret
= kernel_consumer_send_session(socket
, session
);
791 pthread_mutex_unlock(socket
->lock
);
793 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
805 * Create a socket to the relayd using the URI.
807 * On success, the relayd_sock pointer is set to the created socket.
808 * Else, it's stays untouched and a lttcomm error code is returned.
810 static int create_connect_relayd(struct lttng_uri
*uri
,
811 struct lttcomm_relayd_sock
**relayd_sock
,
812 struct consumer_output
*consumer
)
815 struct lttcomm_relayd_sock
*rsock
;
817 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
818 RELAYD_VERSION_COMM_MINOR
);
820 ret
= LTTNG_ERR_FATAL
;
825 * Connect to relayd so we can proceed with a session creation. This call
826 * can possibly block for an arbitrary amount of time to set the health
827 * state to be in poll execution.
830 ret
= relayd_connect(rsock
);
833 ERR("Unable to reach lttng-relayd");
834 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
838 /* Create socket for control stream. */
839 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
840 DBG3("Creating relayd stream socket from URI");
842 /* Check relayd version */
843 ret
= relayd_version_check(rsock
);
845 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
848 consumer
->relay_major_version
= rsock
->major
;
849 consumer
->relay_minor_version
= rsock
->minor
;
850 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
851 DBG3("Creating relayd data socket from URI");
853 /* Command is not valid */
854 ERR("Relayd invalid stream type: %d", uri
->stype
);
855 ret
= LTTNG_ERR_INVALID
;
859 *relayd_sock
= rsock
;
864 /* The returned value is not useful since we are on an error path. */
865 (void) relayd_close(rsock
);
873 * Connect to the relayd using URI and send the socket to the right consumer.
875 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
876 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
877 struct consumer_output
*consumer
,
878 struct consumer_socket
*consumer_sock
,
879 char *session_name
, char *hostname
, int session_live_timer
)
882 struct lttcomm_relayd_sock
*rsock
= NULL
;
884 /* Connect to relayd and make version check if uri is the control. */
885 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
886 if (ret
!= LTTNG_OK
) {
891 /* Set the network sequence index if not set. */
892 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
893 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
895 * Increment net_seq_idx because we are about to transfer the
896 * new relayd socket to the consumer.
897 * Assign unique key so the consumer can match streams.
899 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
900 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
903 /* Send relayd socket to consumer. */
904 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
905 relayd_uri
->stype
, session_id
,
906 session_name
, hostname
, session_live_timer
);
908 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
912 /* Flag that the corresponding socket was sent. */
913 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
914 consumer_sock
->control_sock_sent
= 1;
915 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
916 consumer_sock
->data_sock_sent
= 1;
922 * Close socket which was dup on the consumer side. The session daemon does
923 * NOT keep track of the relayd socket(s) once transfer to the consumer.
927 (void) relayd_close(rsock
);
931 if (ret
!= LTTNG_OK
) {
933 * The consumer output for this session should not be used anymore
934 * since the relayd connection failed thus making any tracing or/and
935 * streaming not usable.
937 consumer
->enabled
= 0;
943 * Send both relayd sockets to a specific consumer and domain. This is a
944 * helper function to facilitate sending the information to the consumer for a
947 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
948 unsigned int session_id
, struct consumer_output
*consumer
,
949 struct consumer_socket
*sock
, char *session_name
,
950 char *hostname
, int session_live_timer
)
957 /* Sending control relayd socket. */
958 if (!sock
->control_sock_sent
) {
959 ret
= send_consumer_relayd_socket(domain
, session_id
,
960 &consumer
->dst
.net
.control
, consumer
, sock
,
961 session_name
, hostname
, session_live_timer
);
962 if (ret
!= LTTNG_OK
) {
967 /* Sending data relayd socket. */
968 if (!sock
->data_sock_sent
) {
969 ret
= send_consumer_relayd_socket(domain
, session_id
,
970 &consumer
->dst
.net
.data
, consumer
, sock
,
971 session_name
, hostname
, session_live_timer
);
972 if (ret
!= LTTNG_OK
) {
982 * Setup relayd connections for a tracing session. First creates the socket to
983 * the relayd and send them to the right domain consumer. Consumer type MUST be
986 int cmd_setup_relayd(struct ltt_session
*session
)
989 struct ltt_ust_session
*usess
;
990 struct ltt_kernel_session
*ksess
;
991 struct consumer_socket
*socket
;
992 struct lttng_ht_iter iter
;
996 usess
= session
->ust_session
;
997 ksess
= session
->kernel_session
;
999 DBG("Setting relayd for session %s", session
->name
);
1003 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1004 && usess
->consumer
->enabled
) {
1005 /* For each consumer socket, send relayd sockets */
1006 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1007 socket
, node
.node
) {
1008 pthread_mutex_lock(socket
->lock
);
1009 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1010 usess
->consumer
, socket
,
1011 session
->name
, session
->hostname
,
1012 session
->live_timer
);
1013 pthread_mutex_unlock(socket
->lock
);
1014 if (ret
!= LTTNG_OK
) {
1017 /* Session is now ready for network streaming. */
1018 session
->net_handle
= 1;
1020 session
->consumer
->relay_major_version
=
1021 usess
->consumer
->relay_major_version
;
1022 session
->consumer
->relay_minor_version
=
1023 usess
->consumer
->relay_minor_version
;
1026 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1027 && ksess
->consumer
->enabled
) {
1028 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1029 socket
, node
.node
) {
1030 pthread_mutex_lock(socket
->lock
);
1031 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1032 ksess
->consumer
, socket
,
1033 session
->name
, session
->hostname
,
1034 session
->live_timer
);
1035 pthread_mutex_unlock(socket
->lock
);
1036 if (ret
!= LTTNG_OK
) {
1039 /* Session is now ready for network streaming. */
1040 session
->net_handle
= 1;
1042 session
->consumer
->relay_major_version
=
1043 ksess
->consumer
->relay_major_version
;
1044 session
->consumer
->relay_minor_version
=
1045 ksess
->consumer
->relay_minor_version
;
1054 * Start a kernel session by opening all necessary streams.
1056 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1059 struct ltt_kernel_channel
*kchan
;
1061 /* Open kernel metadata */
1062 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1063 ret
= kernel_open_metadata(ksess
);
1065 ret
= LTTNG_ERR_KERN_META_FAIL
;
1070 /* Open kernel metadata stream */
1071 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1072 ret
= kernel_open_metadata_stream(ksess
);
1074 ERR("Kernel create metadata stream failed");
1075 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1080 /* For each channel */
1081 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1082 if (kchan
->stream_count
== 0) {
1083 ret
= kernel_open_channel_stream(kchan
);
1085 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1088 /* Update the stream global counter */
1089 ksess
->stream_count_global
+= ret
;
1093 /* Setup kernel consumer socket and send fds to it */
1094 ret
= init_kernel_tracing(ksess
);
1096 ret
= LTTNG_ERR_KERN_START_FAIL
;
1100 /* This start the kernel tracing */
1101 ret
= kernel_start_session(ksess
);
1103 ret
= LTTNG_ERR_KERN_START_FAIL
;
1107 /* Quiescent wait after starting trace */
1108 kernel_wait_quiescent(kernel_tracer_fd
);
1119 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1121 int cmd_disable_channel(struct ltt_session
*session
,
1122 enum lttng_domain_type domain
, char *channel_name
)
1125 struct ltt_ust_session
*usess
;
1127 usess
= session
->ust_session
;
1132 case LTTNG_DOMAIN_KERNEL
:
1134 ret
= channel_kernel_disable(session
->kernel_session
,
1136 if (ret
!= LTTNG_OK
) {
1140 kernel_wait_quiescent(kernel_tracer_fd
);
1143 case LTTNG_DOMAIN_UST
:
1145 struct ltt_ust_channel
*uchan
;
1146 struct lttng_ht
*chan_ht
;
1148 chan_ht
= usess
->domain_global
.channels
;
1150 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1151 if (uchan
== NULL
) {
1152 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1156 ret
= channel_ust_disable(usess
, uchan
);
1157 if (ret
!= LTTNG_OK
) {
1163 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1175 * Command LTTNG_TRACK_PID processed by the client thread.
1177 * Called with session lock held.
1179 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1187 case LTTNG_DOMAIN_KERNEL
:
1189 struct ltt_kernel_session
*ksess
;
1191 ksess
= session
->kernel_session
;
1193 ret
= kernel_track_pid(ksess
, pid
);
1194 if (ret
!= LTTNG_OK
) {
1198 kernel_wait_quiescent(kernel_tracer_fd
);
1201 case LTTNG_DOMAIN_UST
:
1203 struct ltt_ust_session
*usess
;
1205 usess
= session
->ust_session
;
1207 ret
= trace_ust_track_pid(usess
, pid
);
1208 if (ret
!= LTTNG_OK
) {
1214 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1226 * Command LTTNG_UNTRACK_PID processed by the client thread.
1228 * Called with session lock held.
1230 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1238 case LTTNG_DOMAIN_KERNEL
:
1240 struct ltt_kernel_session
*ksess
;
1242 ksess
= session
->kernel_session
;
1244 ret
= kernel_untrack_pid(ksess
, pid
);
1245 if (ret
!= LTTNG_OK
) {
1249 kernel_wait_quiescent(kernel_tracer_fd
);
1252 case LTTNG_DOMAIN_UST
:
1254 struct ltt_ust_session
*usess
;
1256 usess
= session
->ust_session
;
1258 ret
= trace_ust_untrack_pid(usess
, pid
);
1259 if (ret
!= LTTNG_OK
) {
1265 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1277 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1279 * The wpipe arguments is used as a notifier for the kernel thread.
1281 int cmd_enable_channel(struct ltt_session
*session
,
1282 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1285 struct ltt_ust_session
*usess
= session
->ust_session
;
1286 struct lttng_ht
*chan_ht
;
1293 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1295 /* Validate channel name */
1296 if (attr
->name
[0] == '.' ||
1297 memchr(attr
->name
, '/', len
) != NULL
) {
1298 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1302 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1307 * Don't try to enable a channel if the session has been started at
1308 * some point in time before. The tracer does not allow it.
1310 if (session
->has_been_started
) {
1311 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1316 * If the session is a live session, remove the switch timer, the
1317 * live timer does the same thing but sends also synchronisation
1318 * beacons for inactive streams.
1320 if (session
->live_timer
> 0) {
1321 attr
->attr
.live_timer_interval
= session
->live_timer
;
1322 attr
->attr
.switch_timer_interval
= 0;
1326 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1327 * mode and with less than 2 subbuf so block it right away and send back an
1328 * invalid attribute error.
1330 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1331 ret
= LTTNG_ERR_INVALID
;
1335 switch (domain
->type
) {
1336 case LTTNG_DOMAIN_KERNEL
:
1338 struct ltt_kernel_channel
*kchan
;
1340 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1341 session
->kernel_session
);
1342 if (kchan
== NULL
) {
1343 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1344 if (attr
->name
[0] != '\0') {
1345 session
->kernel_session
->has_non_default_channel
= 1;
1348 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1351 if (ret
!= LTTNG_OK
) {
1355 kernel_wait_quiescent(kernel_tracer_fd
);
1358 case LTTNG_DOMAIN_UST
:
1359 case LTTNG_DOMAIN_JUL
:
1360 case LTTNG_DOMAIN_LOG4J
:
1361 case LTTNG_DOMAIN_PYTHON
:
1363 struct ltt_ust_channel
*uchan
;
1368 * Current agent implementation limitations force us to allow
1369 * only one channel at once in "agent" subdomains. Each
1370 * subdomain has a default channel name which must be strictly
1373 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1374 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1375 LTTNG_SYMBOL_NAME_LEN
)) {
1376 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1379 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1380 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1381 LTTNG_SYMBOL_NAME_LEN
)) {
1382 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1385 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1386 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1387 LTTNG_SYMBOL_NAME_LEN
)) {
1388 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1393 chan_ht
= usess
->domain_global
.channels
;
1395 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1396 if (uchan
== NULL
) {
1397 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1398 if (attr
->name
[0] != '\0') {
1399 usess
->has_non_default_channel
= 1;
1402 ret
= channel_ust_enable(usess
, uchan
);
1407 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1418 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1420 int cmd_disable_event(struct ltt_session
*session
,
1421 enum lttng_domain_type domain
, char *channel_name
,
1422 struct lttng_event
*event
)
1427 DBG("Disable event command for event \'%s\'", event
->name
);
1429 event_name
= event
->name
;
1430 if (validate_event_name(event_name
)) {
1431 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1435 /* Error out on unhandled search criteria */
1436 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1437 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1438 ret
= LTTNG_ERR_UNK
;
1445 case LTTNG_DOMAIN_KERNEL
:
1447 struct ltt_kernel_channel
*kchan
;
1448 struct ltt_kernel_session
*ksess
;
1450 ksess
= session
->kernel_session
;
1453 * If a non-default channel has been created in the
1454 * session, explicitely require that -c chan_name needs
1457 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1458 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1462 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1463 if (kchan
== NULL
) {
1464 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1468 switch (event
->type
) {
1469 case LTTNG_EVENT_ALL
:
1470 case LTTNG_EVENT_TRACEPOINT
:
1471 case LTTNG_EVENT_SYSCALL
:
1472 case LTTNG_EVENT_PROBE
:
1473 case LTTNG_EVENT_FUNCTION
:
1474 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1475 if (event_name
[0] == '\0') {
1476 ret
= event_kernel_disable_event(kchan
,
1479 ret
= event_kernel_disable_event(kchan
,
1480 event_name
, event
->type
);
1482 if (ret
!= LTTNG_OK
) {
1487 ret
= LTTNG_ERR_UNK
;
1491 kernel_wait_quiescent(kernel_tracer_fd
);
1494 case LTTNG_DOMAIN_UST
:
1496 struct ltt_ust_channel
*uchan
;
1497 struct ltt_ust_session
*usess
;
1499 usess
= session
->ust_session
;
1501 if (validate_ust_event_name(event_name
)) {
1502 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1507 * If a non-default channel has been created in the
1508 * session, explicitly require that -c chan_name needs
1511 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1512 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1516 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1518 if (uchan
== NULL
) {
1519 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1523 switch (event
->type
) {
1524 case LTTNG_EVENT_ALL
:
1526 * An empty event name means that everything
1527 * should be disabled.
1529 if (event
->name
[0] == '\0') {
1530 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1532 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1535 if (ret
!= LTTNG_OK
) {
1540 ret
= LTTNG_ERR_UNK
;
1544 DBG3("Disable UST event %s in channel %s completed", event_name
,
1548 case LTTNG_DOMAIN_LOG4J
:
1549 case LTTNG_DOMAIN_JUL
:
1550 case LTTNG_DOMAIN_PYTHON
:
1553 struct ltt_ust_session
*usess
= session
->ust_session
;
1557 switch (event
->type
) {
1558 case LTTNG_EVENT_ALL
:
1561 ret
= LTTNG_ERR_UNK
;
1565 agt
= trace_ust_find_agent(usess
, domain
);
1567 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1571 * An empty event name means that everything
1572 * should be disabled.
1574 if (event
->name
[0] == '\0') {
1575 ret
= event_agent_disable_all(usess
, agt
);
1577 ret
= event_agent_disable(usess
, agt
, event_name
);
1579 if (ret
!= LTTNG_OK
) {
1586 ret
= LTTNG_ERR_UND
;
1599 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1601 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1602 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1604 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1605 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1607 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1608 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1609 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1613 case LTTNG_DOMAIN_KERNEL
:
1614 assert(session
->kernel_session
);
1616 if (session
->kernel_session
->channel_count
== 0) {
1617 /* Create default channel */
1618 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1619 if (ret
!= LTTNG_OK
) {
1622 chan_kern_created
= 1;
1624 /* Add kernel context to kernel tracer */
1625 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1626 if (ret
!= LTTNG_OK
) {
1630 case LTTNG_DOMAIN_JUL
:
1631 case LTTNG_DOMAIN_LOG4J
:
1634 * Validate channel name.
1635 * If no channel name is given and the domain is JUL or LOG4J,
1636 * set it to the appropriate domain-specific channel name. If
1637 * a name is provided but does not match the expexted channel
1638 * name, return an error.
1640 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1641 strcmp(channel_name
,
1642 DEFAULT_JUL_CHANNEL_NAME
)) {
1643 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1645 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1646 strcmp(channel_name
,
1647 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1648 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1651 /* break is _not_ missing here. */
1653 case LTTNG_DOMAIN_UST
:
1655 struct ltt_ust_session
*usess
= session
->ust_session
;
1656 unsigned int chan_count
;
1660 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1661 if (chan_count
== 0) {
1662 struct lttng_channel
*attr
;
1663 /* Create default channel */
1664 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1666 ret
= LTTNG_ERR_FATAL
;
1670 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1671 if (ret
!= LTTNG_OK
) {
1676 chan_ust_created
= 1;
1679 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1680 free(app_ctx_provider_name
);
1682 app_ctx_name
= NULL
;
1683 app_ctx_provider_name
= NULL
;
1684 if (ret
!= LTTNG_OK
) {
1690 ret
= LTTNG_ERR_UND
;
1698 if (chan_kern_created
) {
1699 struct ltt_kernel_channel
*kchan
=
1700 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1701 session
->kernel_session
);
1702 /* Created previously, this should NOT fail. */
1704 kernel_destroy_channel(kchan
);
1707 if (chan_ust_created
) {
1708 struct ltt_ust_channel
*uchan
=
1709 trace_ust_find_channel_by_name(
1710 session
->ust_session
->domain_global
.channels
,
1711 DEFAULT_CHANNEL_NAME
);
1712 /* Created previously, this should NOT fail. */
1714 /* Remove from the channel list of the session. */
1715 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1717 trace_ust_destroy_channel(uchan
);
1720 free(app_ctx_provider_name
);
1725 static int validate_event_name(const char *name
)
1728 const char *c
= name
;
1729 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1730 bool null_terminated
= false;
1733 * Make sure that unescaped wildcards are only used as the last
1734 * character of the event name.
1736 while (c
< event_name_end
) {
1739 null_terminated
= true;
1745 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1746 /* Wildcard is not the last character */
1747 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1756 if (!ret
&& !null_terminated
) {
1757 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1762 static inline bool name_starts_with(const char *name
, const char *prefix
)
1764 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1766 return !strncmp(name
, prefix
, max_cmp_len
);
1769 /* Perform userspace-specific event name validation */
1770 static int validate_ust_event_name(const char *name
)
1780 * Check name against all internal UST event component namespaces used
1783 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1784 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1785 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1794 * Internal version of cmd_enable_event() with a supplemental
1795 * "internal_event" flag which is used to enable internal events which should
1796 * be hidden from clients. Such events are used in the agent implementation to
1797 * enable the events through which all "agent" events are funeled.
1799 static int _cmd_enable_event(struct ltt_session
*session
,
1800 struct lttng_domain
*domain
,
1801 char *channel_name
, struct lttng_event
*event
,
1802 char *filter_expression
,
1803 struct lttng_filter_bytecode
*filter
,
1804 struct lttng_event_exclusion
*exclusion
,
1805 int wpipe
, bool internal_event
)
1807 int ret
, channel_created
= 0;
1808 struct lttng_channel
*attr
;
1812 assert(channel_name
);
1814 /* If we have a filter, we must have its filter expression */
1815 assert(!(!!filter_expression
^ !!filter
));
1817 DBG("Enable event command for event \'%s\'", event
->name
);
1821 ret
= validate_event_name(event
->name
);
1826 switch (domain
->type
) {
1827 case LTTNG_DOMAIN_KERNEL
:
1829 struct ltt_kernel_channel
*kchan
;
1832 * If a non-default channel has been created in the
1833 * session, explicitely require that -c chan_name needs
1836 if (session
->kernel_session
->has_non_default_channel
1837 && channel_name
[0] == '\0') {
1838 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1842 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1843 session
->kernel_session
);
1844 if (kchan
== NULL
) {
1845 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1846 LTTNG_BUFFER_GLOBAL
);
1848 ret
= LTTNG_ERR_FATAL
;
1851 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1853 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1854 if (ret
!= LTTNG_OK
) {
1860 channel_created
= 1;
1863 /* Get the newly created kernel channel pointer */
1864 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1865 session
->kernel_session
);
1866 if (kchan
== NULL
) {
1867 /* This sould not happen... */
1868 ret
= LTTNG_ERR_FATAL
;
1872 switch (event
->type
) {
1873 case LTTNG_EVENT_ALL
:
1875 char *filter_expression_a
= NULL
;
1876 struct lttng_filter_bytecode
*filter_a
= NULL
;
1879 * We need to duplicate filter_expression and filter,
1880 * because ownership is passed to first enable
1883 if (filter_expression
) {
1884 filter_expression_a
= strdup(filter_expression
);
1885 if (!filter_expression_a
) {
1886 ret
= LTTNG_ERR_FATAL
;
1891 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1893 free(filter_expression_a
);
1894 ret
= LTTNG_ERR_FATAL
;
1897 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1899 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1900 ret
= event_kernel_enable_event(kchan
, event
,
1901 filter_expression
, filter
);
1902 /* We have passed ownership */
1903 filter_expression
= NULL
;
1905 if (ret
!= LTTNG_OK
) {
1906 if (channel_created
) {
1907 /* Let's not leak a useless channel. */
1908 kernel_destroy_channel(kchan
);
1910 free(filter_expression_a
);
1914 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1915 ret
= event_kernel_enable_event(kchan
, event
,
1916 filter_expression_a
, filter_a
);
1917 /* We have passed ownership */
1918 filter_expression_a
= NULL
;
1920 if (ret
!= LTTNG_OK
) {
1925 case LTTNG_EVENT_PROBE
:
1926 case LTTNG_EVENT_FUNCTION
:
1927 case LTTNG_EVENT_FUNCTION_ENTRY
:
1928 case LTTNG_EVENT_TRACEPOINT
:
1929 ret
= event_kernel_enable_event(kchan
, event
,
1930 filter_expression
, filter
);
1931 /* We have passed ownership */
1932 filter_expression
= NULL
;
1934 if (ret
!= LTTNG_OK
) {
1935 if (channel_created
) {
1936 /* Let's not leak a useless channel. */
1937 kernel_destroy_channel(kchan
);
1942 case LTTNG_EVENT_SYSCALL
:
1943 ret
= event_kernel_enable_event(kchan
, event
,
1944 filter_expression
, filter
);
1945 /* We have passed ownership */
1946 filter_expression
= NULL
;
1948 if (ret
!= LTTNG_OK
) {
1953 ret
= LTTNG_ERR_UNK
;
1957 kernel_wait_quiescent(kernel_tracer_fd
);
1960 case LTTNG_DOMAIN_UST
:
1962 struct ltt_ust_channel
*uchan
;
1963 struct ltt_ust_session
*usess
= session
->ust_session
;
1968 * If a non-default channel has been created in the
1969 * session, explicitely require that -c chan_name needs
1972 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1973 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1977 /* Get channel from global UST domain */
1978 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1980 if (uchan
== NULL
) {
1981 /* Create default channel */
1982 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1983 usess
->buffer_type
);
1985 ret
= LTTNG_ERR_FATAL
;
1988 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1990 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1991 if (ret
!= LTTNG_OK
) {
1997 /* Get the newly created channel reference back */
1998 uchan
= trace_ust_find_channel_by_name(
1999 usess
->domain_global
.channels
, channel_name
);
2003 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2005 * Don't allow users to add UST events to channels which
2006 * are assigned to a userspace subdomain (JUL, Log4J,
2009 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2013 if (!internal_event
) {
2015 * Ensure the event name is not reserved for internal
2018 ret
= validate_ust_event_name(event
->name
);
2020 WARN("Userspace event name %s failed validation.",
2022 event
->name
: "NULL");
2023 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2028 /* At this point, the session and channel exist on the tracer */
2029 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2030 filter_expression
, filter
, exclusion
,
2032 /* We have passed ownership */
2033 filter_expression
= NULL
;
2036 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2037 goto already_enabled
;
2038 } else if (ret
!= LTTNG_OK
) {
2043 case LTTNG_DOMAIN_LOG4J
:
2044 case LTTNG_DOMAIN_JUL
:
2045 case LTTNG_DOMAIN_PYTHON
:
2047 const char *default_event_name
, *default_chan_name
;
2049 struct lttng_event uevent
;
2050 struct lttng_domain tmp_dom
;
2051 struct ltt_ust_session
*usess
= session
->ust_session
;
2055 agt
= trace_ust_find_agent(usess
, domain
->type
);
2057 agt
= agent_create(domain
->type
);
2059 ret
= LTTNG_ERR_NOMEM
;
2062 agent_add(agt
, usess
->agents
);
2065 /* Create the default tracepoint. */
2066 memset(&uevent
, 0, sizeof(uevent
));
2067 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2068 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2069 default_event_name
= event_get_default_agent_ust_name(
2071 if (!default_event_name
) {
2072 ret
= LTTNG_ERR_FATAL
;
2075 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2076 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2079 * The domain type is changed because we are about to enable the
2080 * default channel and event for the JUL domain that are hardcoded.
2081 * This happens in the UST domain.
2083 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2084 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2086 switch (domain
->type
) {
2087 case LTTNG_DOMAIN_LOG4J
:
2088 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2090 case LTTNG_DOMAIN_JUL
:
2091 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2093 case LTTNG_DOMAIN_PYTHON
:
2094 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2097 /* The switch/case we are in makes this impossible */
2102 char *filter_expression_copy
= NULL
;
2103 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2106 const size_t filter_size
= sizeof(
2107 struct lttng_filter_bytecode
)
2110 filter_copy
= zmalloc(filter_size
);
2112 ret
= LTTNG_ERR_NOMEM
;
2115 memcpy(filter_copy
, filter
, filter_size
);
2117 filter_expression_copy
=
2118 strdup(filter_expression
);
2119 if (!filter_expression
) {
2120 ret
= LTTNG_ERR_NOMEM
;
2123 if (!filter_expression_copy
|| !filter_copy
) {
2124 free(filter_expression_copy
);
2130 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2131 (char *) default_chan_name
,
2132 &uevent
, filter_expression_copy
,
2133 filter_copy
, NULL
, wpipe
);
2136 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2137 goto already_enabled
;
2138 } else if (ret
!= LTTNG_OK
) {
2142 /* The wild card * means that everything should be enabled. */
2143 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2144 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2147 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2151 filter_expression
= NULL
;
2152 if (ret
!= LTTNG_OK
) {
2159 ret
= LTTNG_ERR_UND
;
2167 free(filter_expression
);
2175 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2176 * We own filter, exclusion, and filter_expression.
2178 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2179 char *channel_name
, struct lttng_event
*event
,
2180 char *filter_expression
,
2181 struct lttng_filter_bytecode
*filter
,
2182 struct lttng_event_exclusion
*exclusion
,
2185 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2186 filter_expression
, filter
, exclusion
, wpipe
, false);
2190 * Enable an event which is internal to LTTng. An internal should
2191 * never be made visible to clients and are immune to checks such as
2194 static int cmd_enable_event_internal(struct ltt_session
*session
,
2195 struct lttng_domain
*domain
,
2196 char *channel_name
, struct lttng_event
*event
,
2197 char *filter_expression
,
2198 struct lttng_filter_bytecode
*filter
,
2199 struct lttng_event_exclusion
*exclusion
,
2202 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2203 filter_expression
, filter
, exclusion
, wpipe
, true);
2207 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2209 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2210 struct lttng_event
**events
)
2213 ssize_t nb_events
= 0;
2216 case LTTNG_DOMAIN_KERNEL
:
2217 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2218 if (nb_events
< 0) {
2219 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2223 case LTTNG_DOMAIN_UST
:
2224 nb_events
= ust_app_list_events(events
);
2225 if (nb_events
< 0) {
2226 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2230 case LTTNG_DOMAIN_LOG4J
:
2231 case LTTNG_DOMAIN_JUL
:
2232 case LTTNG_DOMAIN_PYTHON
:
2233 nb_events
= agent_list_events(events
, domain
);
2234 if (nb_events
< 0) {
2235 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2240 ret
= LTTNG_ERR_UND
;
2247 /* Return negative value to differentiate return code */
2252 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2254 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2255 struct lttng_event_field
**fields
)
2258 ssize_t nb_fields
= 0;
2261 case LTTNG_DOMAIN_UST
:
2262 nb_fields
= ust_app_list_event_fields(fields
);
2263 if (nb_fields
< 0) {
2264 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2268 case LTTNG_DOMAIN_KERNEL
:
2269 default: /* fall-through */
2270 ret
= LTTNG_ERR_UND
;
2277 /* Return negative value to differentiate return code */
2281 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2283 return syscall_table_list(events
);
2287 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2289 * Called with session lock held.
2291 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2292 enum lttng_domain_type domain
, int32_t **pids
)
2295 ssize_t nr_pids
= 0;
2298 case LTTNG_DOMAIN_KERNEL
:
2300 struct ltt_kernel_session
*ksess
;
2302 ksess
= session
->kernel_session
;
2303 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2305 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2310 case LTTNG_DOMAIN_UST
:
2312 struct ltt_ust_session
*usess
;
2314 usess
= session
->ust_session
;
2315 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2317 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2322 case LTTNG_DOMAIN_LOG4J
:
2323 case LTTNG_DOMAIN_JUL
:
2324 case LTTNG_DOMAIN_PYTHON
:
2326 ret
= LTTNG_ERR_UND
;
2333 /* Return negative value to differentiate return code */
2338 * Command LTTNG_START_TRACE processed by the client thread.
2340 * Called with session mutex held.
2342 int cmd_start_trace(struct ltt_session
*session
)
2345 unsigned long nb_chan
= 0;
2346 struct ltt_kernel_session
*ksession
;
2347 struct ltt_ust_session
*usess
;
2351 /* Ease our life a bit ;) */
2352 ksession
= session
->kernel_session
;
2353 usess
= session
->ust_session
;
2355 /* Is the session already started? */
2356 if (session
->active
) {
2357 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2362 * Starting a session without channel is useless since after that it's not
2363 * possible to enable channel thus inform the client.
2365 if (usess
&& usess
->domain_global
.channels
) {
2366 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2369 nb_chan
+= ksession
->channel_count
;
2372 ret
= LTTNG_ERR_NO_CHANNEL
;
2376 /* Kernel tracing */
2377 if (ksession
!= NULL
) {
2378 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2379 if (ret
!= LTTNG_OK
) {
2384 /* Flag session that trace should start automatically */
2387 * Even though the start trace might fail, flag this session active so
2388 * other application coming in are started by default.
2392 ret
= ust_app_start_trace_all(usess
);
2394 ret
= LTTNG_ERR_UST_START_FAIL
;
2399 /* Flag this after a successful start. */
2400 session
->has_been_started
= 1;
2401 session
->active
= 1;
2410 * Command LTTNG_STOP_TRACE processed by the client thread.
2412 int cmd_stop_trace(struct ltt_session
*session
)
2415 struct ltt_kernel_channel
*kchan
;
2416 struct ltt_kernel_session
*ksession
;
2417 struct ltt_ust_session
*usess
;
2422 ksession
= session
->kernel_session
;
2423 usess
= session
->ust_session
;
2425 /* Session is not active. Skip everythong and inform the client. */
2426 if (!session
->active
) {
2427 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2432 if (ksession
&& ksession
->active
) {
2433 DBG("Stop kernel tracing");
2435 /* Flush metadata if exist */
2436 if (ksession
->metadata_stream_fd
>= 0) {
2437 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2439 ERR("Kernel metadata flush failed");
2443 /* Flush all buffers before stopping */
2444 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2445 ret
= kernel_flush_buffer(kchan
);
2447 ERR("Kernel flush buffer error");
2451 ret
= kernel_stop_session(ksession
);
2453 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2457 kernel_wait_quiescent(kernel_tracer_fd
);
2459 ksession
->active
= 0;
2462 if (usess
&& usess
->active
) {
2464 * Even though the stop trace might fail, flag this session inactive so
2465 * other application coming in are not started by default.
2469 ret
= ust_app_stop_trace_all(usess
);
2471 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2476 /* Flag inactive after a successful stop. */
2477 session
->active
= 0;
2485 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2487 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2488 struct lttng_uri
*uris
)
2491 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2492 struct ltt_ust_session
*usess
= session
->ust_session
;
2498 /* Can't set consumer URI if the session is active. */
2499 if (session
->active
) {
2500 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2504 /* Set the "global" consumer URIs */
2505 for (i
= 0; i
< nb_uri
; i
++) {
2506 ret
= add_uri_to_consumer(session
->consumer
,
2507 &uris
[i
], 0, session
->name
);
2508 if (ret
!= LTTNG_OK
) {
2513 /* Set UST session URIs */
2514 if (session
->ust_session
) {
2515 for (i
= 0; i
< nb_uri
; i
++) {
2516 ret
= add_uri_to_consumer(
2517 session
->ust_session
->consumer
,
2518 &uris
[i
], LTTNG_DOMAIN_UST
,
2520 if (ret
!= LTTNG_OK
) {
2526 /* Set kernel session URIs */
2527 if (session
->kernel_session
) {
2528 for (i
= 0; i
< nb_uri
; i
++) {
2529 ret
= add_uri_to_consumer(
2530 session
->kernel_session
->consumer
,
2531 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2533 if (ret
!= LTTNG_OK
) {
2540 * Make sure to set the session in output mode after we set URI since a
2541 * session can be created without URL (thus flagged in no output mode).
2543 session
->output_traces
= 1;
2545 ksess
->output_traces
= 1;
2549 usess
->output_traces
= 1;
2560 * Command LTTNG_CREATE_SESSION processed by the client thread.
2562 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2563 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2566 struct ltt_session
*session
;
2572 * Verify if the session already exist
2574 * XXX: There is no need for the session lock list here since the caller
2575 * (process_client_msg) is holding it. We might want to change that so a
2576 * single command does not lock the entire session list.
2578 session
= session_find_by_name(name
);
2579 if (session
!= NULL
) {
2580 ret
= LTTNG_ERR_EXIST_SESS
;
2584 /* Create tracing session in the registry */
2585 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2586 LTTNG_SOCK_GET_GID_CRED(creds
));
2587 if (ret
!= LTTNG_OK
) {
2592 * Get the newly created session pointer back
2594 * XXX: There is no need for the session lock list here since the caller
2595 * (process_client_msg) is holding it. We might want to change that so a
2596 * single command does not lock the entire session list.
2598 session
= session_find_by_name(name
);
2601 session
->live_timer
= live_timer
;
2602 /* Create default consumer output for the session not yet created. */
2603 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2604 if (session
->consumer
== NULL
) {
2605 ret
= LTTNG_ERR_FATAL
;
2606 goto consumer_error
;
2610 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2611 if (ret
!= LTTNG_OK
) {
2612 goto consumer_error
;
2614 session
->output_traces
= 1;
2616 session
->output_traces
= 0;
2617 DBG2("Session %s created with no output", session
->name
);
2620 session
->consumer
->enabled
= 1;
2625 session_destroy(session
);
2632 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2634 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2635 size_t nb_uri
, lttng_sock_cred
*creds
)
2638 struct ltt_session
*session
;
2639 struct snapshot_output
*new_output
= NULL
;
2645 * Create session in no output mode with URIs set to NULL. The uris we've
2646 * received are for a default snapshot output if one.
2648 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2649 if (ret
!= LTTNG_OK
) {
2653 /* Get the newly created session pointer back. This should NEVER fail. */
2654 session
= session_find_by_name(name
);
2657 /* Flag session for snapshot mode. */
2658 session
->snapshot_mode
= 1;
2660 /* Skip snapshot output creation if no URI is given. */
2665 new_output
= snapshot_output_alloc();
2667 ret
= LTTNG_ERR_NOMEM
;
2668 goto error_snapshot_alloc
;
2671 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2672 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2674 if (ret
== -ENOMEM
) {
2675 ret
= LTTNG_ERR_NOMEM
;
2677 ret
= LTTNG_ERR_INVALID
;
2679 goto error_snapshot
;
2683 snapshot_add_output(&session
->snapshot
, new_output
);
2690 snapshot_output_destroy(new_output
);
2691 error_snapshot_alloc
:
2692 session_destroy(session
);
2698 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2700 * Called with session lock held.
2702 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2705 struct ltt_ust_session
*usess
;
2706 struct ltt_kernel_session
*ksess
;
2711 usess
= session
->ust_session
;
2712 ksess
= session
->kernel_session
;
2714 /* Clean kernel session teardown */
2715 kernel_destroy_session(ksess
);
2717 /* UST session teardown */
2719 /* Close any relayd session */
2720 consumer_output_send_destroy_relayd(usess
->consumer
);
2722 /* Destroy every UST application related to this session. */
2723 ret
= ust_app_destroy_trace_all(usess
);
2725 ERR("Error in ust_app_destroy_trace_all");
2728 /* Clean up the rest. */
2729 trace_ust_destroy_session(usess
);
2733 * Must notify the kernel thread here to update it's poll set in order to
2734 * remove the channel(s)' fd just destroyed.
2736 ret
= notify_thread_pipe(wpipe
);
2738 PERROR("write kernel poll pipe");
2741 ret
= session_destroy(session
);
2747 * Command LTTNG_CALIBRATE processed by the client thread.
2749 int cmd_calibrate(enum lttng_domain_type domain
,
2750 struct lttng_calibrate
*calibrate
)
2755 case LTTNG_DOMAIN_KERNEL
:
2757 struct lttng_kernel_calibrate kcalibrate
;
2759 switch (calibrate
->type
) {
2760 case LTTNG_CALIBRATE_FUNCTION
:
2762 /* Default and only possible calibrate option. */
2763 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2767 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2769 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2774 case LTTNG_DOMAIN_UST
:
2776 struct lttng_ust_calibrate ucalibrate
;
2778 switch (calibrate
->type
) {
2779 case LTTNG_CALIBRATE_FUNCTION
:
2781 /* Default and only possible calibrate option. */
2782 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2786 ret
= ust_app_calibrate_glb(&ucalibrate
);
2788 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2794 ret
= LTTNG_ERR_UND
;
2805 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2807 int cmd_register_consumer(struct ltt_session
*session
,
2808 enum lttng_domain_type domain
, const char *sock_path
,
2809 struct consumer_data
*cdata
)
2812 struct consumer_socket
*socket
= NULL
;
2819 case LTTNG_DOMAIN_KERNEL
:
2821 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2825 /* Can't register a consumer if there is already one */
2826 if (ksess
->consumer_fds_sent
!= 0) {
2827 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2831 sock
= lttcomm_connect_unix_sock(sock_path
);
2833 ret
= LTTNG_ERR_CONNECT_FAIL
;
2836 cdata
->cmd_sock
= sock
;
2838 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2839 if (socket
== NULL
) {
2842 PERROR("close register consumer");
2844 cdata
->cmd_sock
= -1;
2845 ret
= LTTNG_ERR_FATAL
;
2849 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2850 if (socket
->lock
== NULL
) {
2851 PERROR("zmalloc pthread mutex");
2852 ret
= LTTNG_ERR_FATAL
;
2855 pthread_mutex_init(socket
->lock
, NULL
);
2856 socket
->registered
= 1;
2859 consumer_add_socket(socket
, ksess
->consumer
);
2862 pthread_mutex_lock(&cdata
->pid_mutex
);
2864 pthread_mutex_unlock(&cdata
->pid_mutex
);
2869 /* TODO: Userspace tracing */
2870 ret
= LTTNG_ERR_UND
;
2878 consumer_destroy_socket(socket
);
2884 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2886 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2887 struct lttng_domain
**domains
)
2892 struct lttng_ht_iter iter
;
2894 if (session
->kernel_session
!= NULL
) {
2895 DBG3("Listing domains found kernel domain");
2899 if (session
->ust_session
!= NULL
) {
2900 DBG3("Listing domains found UST global domain");
2904 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2906 if (agt
->being_used
) {
2917 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2918 if (*domains
== NULL
) {
2919 ret
= LTTNG_ERR_FATAL
;
2923 if (session
->kernel_session
!= NULL
) {
2924 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2926 /* Kernel session buffer type is always GLOBAL */
2927 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2932 if (session
->ust_session
!= NULL
) {
2933 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2934 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2938 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2940 if (agt
->being_used
) {
2941 (*domains
)[index
].type
= agt
->domain
;
2942 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2952 /* Return negative value to differentiate return code */
2958 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2960 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2961 struct ltt_session
*session
, struct lttng_channel
**channels
)
2963 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2966 case LTTNG_DOMAIN_KERNEL
:
2967 if (session
->kernel_session
!= NULL
) {
2968 nb_chan
= session
->kernel_session
->channel_count
;
2970 DBG3("Number of kernel channels %zd", nb_chan
);
2972 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2976 case LTTNG_DOMAIN_UST
:
2977 if (session
->ust_session
!= NULL
) {
2979 nb_chan
= lttng_ht_get_count(
2980 session
->ust_session
->domain_global
.channels
);
2983 DBG3("Number of UST global channels %zd", nb_chan
);
2985 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2990 ret
= -LTTNG_ERR_UND
;
2995 const size_t channel_size
= sizeof(struct lttng_channel
) +
2996 sizeof(struct lttcomm_channel_extended
);
2997 struct lttcomm_channel_extended
*channel_exts
;
2999 payload_size
= nb_chan
* channel_size
;
3000 *channels
= zmalloc(payload_size
);
3001 if (*channels
== NULL
) {
3002 ret
= -LTTNG_ERR_FATAL
;
3006 channel_exts
= ((void *) *channels
) +
3007 (nb_chan
* sizeof(struct lttng_channel
));
3008 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3019 * Command LTTNG_LIST_EVENTS processed by the client thread.
3021 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3022 struct ltt_session
*session
, char *channel_name
,
3023 struct lttng_event
**events
, size_t *total_size
)
3026 ssize_t nb_event
= 0;
3029 case LTTNG_DOMAIN_KERNEL
:
3030 if (session
->kernel_session
!= NULL
) {
3031 nb_event
= list_lttng_kernel_events(channel_name
,
3032 session
->kernel_session
, events
,
3036 case LTTNG_DOMAIN_UST
:
3038 if (session
->ust_session
!= NULL
) {
3039 nb_event
= list_lttng_ust_global_events(channel_name
,
3040 &session
->ust_session
->domain_global
, events
,
3045 case LTTNG_DOMAIN_LOG4J
:
3046 case LTTNG_DOMAIN_JUL
:
3047 case LTTNG_DOMAIN_PYTHON
:
3048 if (session
->ust_session
) {
3049 struct lttng_ht_iter iter
;
3053 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3054 &iter
.iter
, agt
, node
.node
) {
3055 if (agt
->domain
== domain
) {
3056 nb_event
= list_lttng_agent_events(
3066 ret
= LTTNG_ERR_UND
;
3073 /* Return negative value to differentiate return code */
3078 * Using the session list, filled a lttng_session array to send back to the
3079 * client for session listing.
3081 * The session list lock MUST be acquired before calling this function. Use
3082 * session_lock_list() and session_unlock_list().
3084 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3089 struct ltt_session
*session
;
3090 struct ltt_session_list
*list
= session_get_list();
3092 DBG("Getting all available session for UID %d GID %d",
3095 * Iterate over session list and append data after the control struct in
3098 cds_list_for_each_entry(session
, &list
->head
, list
) {
3100 * Only list the sessions the user can control.
3102 if (!session_access_ok(session
, uid
, gid
)) {
3106 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3107 struct ltt_ust_session
*usess
= session
->ust_session
;
3109 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3110 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3111 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3112 ret
= build_network_session_path(sessions
[i
].path
,
3113 sizeof(sessions
[i
].path
), session
);
3115 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3116 session
->consumer
->dst
.trace_path
);
3119 PERROR("snprintf session path");
3123 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3124 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3125 sessions
[i
].enabled
= session
->active
;
3126 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3127 sessions
[i
].live_timer_interval
= session
->live_timer
;
3133 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3134 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3136 int cmd_data_pending(struct ltt_session
*session
)
3139 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3140 struct ltt_ust_session
*usess
= session
->ust_session
;
3144 /* Session MUST be stopped to ask for data availability. */
3145 if (session
->active
) {
3146 ret
= LTTNG_ERR_SESSION_STARTED
;
3150 * If stopped, just make sure we've started before else the above call
3151 * will always send that there is data pending.
3153 * The consumer assumes that when the data pending command is received,
3154 * the trace has been started before or else no output data is written
3155 * by the streams which is a condition for data pending. So, this is
3156 * *VERY* important that we don't ask the consumer before a start
3159 if (!session
->has_been_started
) {
3165 if (ksess
&& ksess
->consumer
) {
3166 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3168 /* Data is still being extracted for the kernel. */
3173 if (usess
&& usess
->consumer
) {
3174 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3176 /* Data is still being extracted for the kernel. */
3181 /* Data is ready to be read by a viewer */
3189 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3191 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3193 int cmd_snapshot_add_output(struct ltt_session
*session
,
3194 struct lttng_snapshot_output
*output
, uint32_t *id
)
3197 struct snapshot_output
*new_output
;
3202 DBG("Cmd snapshot add output for session %s", session
->name
);
3205 * Permission denied to create an output if the session is not
3206 * set in no output mode.
3208 if (session
->output_traces
) {
3209 ret
= LTTNG_ERR_EPERM
;
3213 /* Only one output is allowed until we have the "tee" feature. */
3214 if (session
->snapshot
.nb_output
== 1) {
3215 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3219 new_output
= snapshot_output_alloc();
3221 ret
= LTTNG_ERR_NOMEM
;
3225 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3226 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3227 &session
->snapshot
);
3229 if (ret
== -ENOMEM
) {
3230 ret
= LTTNG_ERR_NOMEM
;
3232 ret
= LTTNG_ERR_INVALID
;
3238 snapshot_add_output(&session
->snapshot
, new_output
);
3240 *id
= new_output
->id
;
3247 snapshot_output_destroy(new_output
);
3253 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3255 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3257 int cmd_snapshot_del_output(struct ltt_session
*session
,
3258 struct lttng_snapshot_output
*output
)
3261 struct snapshot_output
*sout
= NULL
;
3269 * Permission denied to create an output if the session is not
3270 * set in no output mode.
3272 if (session
->output_traces
) {
3273 ret
= LTTNG_ERR_EPERM
;
3278 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3280 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3281 } else if (*output
->name
!= '\0') {
3282 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3284 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3287 ret
= LTTNG_ERR_INVALID
;
3291 snapshot_delete_output(&session
->snapshot
, sout
);
3292 snapshot_output_destroy(sout
);
3301 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3303 * If no output is available, outputs is untouched and 0 is returned.
3305 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3307 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3308 struct lttng_snapshot_output
**outputs
)
3311 struct lttng_snapshot_output
*list
= NULL
;
3312 struct lttng_ht_iter iter
;
3313 struct snapshot_output
*output
;
3318 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3321 * Permission denied to create an output if the session is not
3322 * set in no output mode.
3324 if (session
->output_traces
) {
3325 ret
= -LTTNG_ERR_EPERM
;
3329 if (session
->snapshot
.nb_output
== 0) {
3334 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3336 ret
= -LTTNG_ERR_NOMEM
;
3340 /* Copy list from session to the new list object. */
3342 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3343 output
, node
.node
) {
3344 assert(output
->consumer
);
3345 list
[idx
].id
= output
->id
;
3346 list
[idx
].max_size
= output
->max_size
;
3347 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3348 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3349 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3350 sizeof(list
[idx
].ctrl_url
));
3353 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3354 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3356 ret
= -LTTNG_ERR_NOMEM
;
3361 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3362 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3364 ret
= -LTTNG_ERR_NOMEM
;
3373 ret
= session
->snapshot
.nb_output
;
3381 * Check if we can regenerate the metadata for this session.
3382 * Only kernel, UST per-uid and non-live sessions are supported.
3384 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3387 int check_metadata_regenerate_support(struct ltt_session
*session
)
3393 if (session
->live_timer
!= 0) {
3394 ret
= LTTNG_ERR_LIVE_SESSION
;
3397 if (!session
->active
) {
3398 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3401 if (session
->ust_session
) {
3402 switch (session
->ust_session
->buffer_type
) {
3403 case LTTNG_BUFFER_PER_UID
:
3405 case LTTNG_BUFFER_PER_PID
:
3406 ret
= LTTNG_ERR_PER_PID_SESSION
;
3410 ret
= LTTNG_ERR_UNK
;
3414 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3415 session
->consumer
->relay_minor_version
< 8) {
3416 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3426 int ust_metadata_regenerate(struct ltt_ust_session
*usess
)
3429 struct buffer_reg_uid
*uid_reg
= NULL
;
3430 struct buffer_reg_session
*session_reg
= NULL
;
3433 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3434 struct ust_registry_session
*registry
;
3435 struct ust_registry_channel
*chan
;
3436 struct lttng_ht_iter iter_chan
;
3438 session_reg
= uid_reg
->registry
;
3439 registry
= session_reg
->reg
.ust
;
3441 pthread_mutex_lock(®istry
->lock
);
3442 registry
->metadata_len_sent
= 0;
3443 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3444 registry
->metadata_len
= 0;
3445 registry
->metadata_version
++;
3446 ret
= ust_metadata_session_statedump(registry
, NULL
,
3447 registry
->major
, registry
->minor
);
3449 pthread_mutex_unlock(®istry
->lock
);
3450 ERR("Failed to generate session metadata (err = %d)",
3454 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3456 struct ust_registry_event
*event
;
3457 struct lttng_ht_iter iter_event
;
3459 ret
= ust_metadata_channel_statedump(registry
, chan
);
3461 pthread_mutex_unlock(®istry
->lock
);
3462 ERR("Failed to generate channel metadata "
3466 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3468 ret
= ust_metadata_event_statedump(registry
,
3471 pthread_mutex_unlock(®istry
->lock
);
3472 ERR("Failed to generate event metadata "
3478 pthread_mutex_unlock(®istry
->lock
);
3487 * Command LTTNG_METADATA_REGENERATE from the lttng-ctl library.
3489 * Ask the consumer to truncate the existing metadata file(s) and
3490 * then regenerate the metadata. Live and per-pid sessions are not
3491 * supported and return an error.
3493 * Return 0 on success or else a LTTNG_ERR code.
3495 int cmd_metadata_regenerate(struct ltt_session
*session
)
3501 ret
= check_metadata_regenerate_support(session
);
3506 if (session
->kernel_session
) {
3507 ret
= kernctl_session_metadata_regenerate(
3508 session
->kernel_session
->fd
);
3510 ERR("Failed to regenerate the kernel metadata");
3515 if (session
->ust_session
) {
3516 ret
= ust_metadata_regenerate(session
->ust_session
);
3518 ERR("Failed to regenerate the UST metadata");
3522 DBG("Cmd metadata regenerate for session %s", session
->name
);
3531 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3532 * snapshot output is *not* set with a remote destination.
3534 * Return 0 on success or a LTTNG_ERR code.
3536 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3537 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3540 struct lttng_ht_iter iter
;
3541 struct consumer_socket
*socket
;
3544 assert(snap_output
);
3547 DBG2("Set relayd object from snapshot output");
3549 /* Ignore if snapshot consumer output is not network. */
3550 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3555 * For each consumer socket, create and send the relayd object of the
3559 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3560 socket
, node
.node
) {
3561 ret
= send_consumer_relayd_sockets(0, session
->id
,
3562 snap_output
->consumer
, socket
,
3563 session
->name
, session
->hostname
,
3564 session
->live_timer
);
3565 if (ret
!= LTTNG_OK
) {
3577 * Record a kernel snapshot.
3579 * Return LTTNG_OK on success or a LTTNG_ERR code.
3581 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3582 struct snapshot_output
*output
, struct ltt_session
*session
,
3583 int wait
, uint64_t nb_packets_per_stream
)
3591 /* Get the datetime for the snapshot output directory. */
3592 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3593 sizeof(output
->datetime
));
3595 ret
= LTTNG_ERR_INVALID
;
3600 * Copy kernel session sockets so we can communicate with the right
3601 * consumer for the snapshot record command.
3603 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3605 ret
= LTTNG_ERR_NOMEM
;
3609 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3610 if (ret
!= LTTNG_OK
) {
3611 goto error_snapshot
;
3614 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3615 if (ret
!= LTTNG_OK
) {
3616 goto error_snapshot
;
3623 /* Clean up copied sockets so this output can use some other later on. */
3624 consumer_destroy_output_sockets(output
->consumer
);
3631 * Record a UST snapshot.
3633 * Return 0 on success or a LTTNG_ERR error code.
3635 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3636 struct snapshot_output
*output
, struct ltt_session
*session
,
3637 int wait
, uint64_t nb_packets_per_stream
)
3645 /* Get the datetime for the snapshot output directory. */
3646 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3647 sizeof(output
->datetime
));
3649 ret
= LTTNG_ERR_INVALID
;
3654 * Copy UST session sockets so we can communicate with the right
3655 * consumer for the snapshot record command.
3657 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3659 ret
= LTTNG_ERR_NOMEM
;
3663 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3664 if (ret
!= LTTNG_OK
) {
3665 goto error_snapshot
;
3668 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3672 ret
= LTTNG_ERR_INVALID
;
3675 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3678 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3681 goto error_snapshot
;
3687 /* Clean up copied sockets so this output can use some other later on. */
3688 consumer_destroy_output_sockets(output
->consumer
);
3694 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3695 uint64_t cur_nr_packets
)
3697 uint64_t tot_size
= 0;
3699 if (session
->kernel_session
) {
3700 struct ltt_kernel_channel
*chan
;
3701 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3703 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3704 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3706 * Don't take channel into account if we
3707 * already grab all its packets.
3711 tot_size
+= chan
->channel
->attr
.subbuf_size
3712 * chan
->stream_count
;
3716 if (session
->ust_session
) {
3717 struct ltt_ust_session
*usess
= session
->ust_session
;
3719 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3727 * Calculate the number of packets we can grab from each stream that
3728 * fits within the overall snapshot max size.
3730 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3731 * the number of packets per stream.
3733 * TODO: this approach is not perfect: we consider the worse case
3734 * (packet filling the sub-buffers) as an upper bound, but we could do
3735 * better if we do this calculation while we actually grab the packet
3736 * content: we would know how much padding we don't actually store into
3739 * This algorithm is currently bounded by the number of packets per
3742 * Since we call this algorithm before actually grabbing the data, it's
3743 * an approximation: for instance, applications could appear/disappear
3744 * in between this call and actually grabbing data.
3747 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3750 uint64_t cur_nb_packets
= 0;
3753 return 0; /* Infinite */
3756 size_left
= max_size
;
3758 uint64_t one_more_packet_tot_size
;
3760 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3762 if (!one_more_packet_tot_size
) {
3763 /* We are already grabbing all packets. */
3766 size_left
-= one_more_packet_tot_size
;
3767 if (size_left
< 0) {
3772 if (!cur_nb_packets
) {
3773 /* Not enough room to grab one packet of each stream, error. */
3776 return cur_nb_packets
;
3780 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3782 * The wait parameter is ignored so this call always wait for the snapshot to
3783 * complete before returning.
3785 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3787 int cmd_snapshot_record(struct ltt_session
*session
,
3788 struct lttng_snapshot_output
*output
, int wait
)
3791 unsigned int use_tmp_output
= 0;
3792 struct snapshot_output tmp_output
;
3793 unsigned int snapshot_success
= 0;
3798 DBG("Cmd snapshot record for session %s", session
->name
);
3801 * Permission denied to create an output if the session is not
3802 * set in no output mode.
3804 if (session
->output_traces
) {
3805 ret
= LTTNG_ERR_EPERM
;
3809 /* The session needs to be started at least once. */
3810 if (!session
->has_been_started
) {
3811 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3815 /* Use temporary output for the session. */
3816 if (*output
->ctrl_url
!= '\0') {
3817 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3818 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3821 if (ret
== -ENOMEM
) {
3822 ret
= LTTNG_ERR_NOMEM
;
3824 ret
= LTTNG_ERR_INVALID
;
3828 /* Use the global session count for the temporary snapshot. */
3829 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3833 if (session
->kernel_session
) {
3834 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3836 if (use_tmp_output
) {
3837 int64_t nb_packets_per_stream
;
3839 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3840 tmp_output
.max_size
);
3841 if (nb_packets_per_stream
< 0) {
3842 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3845 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3846 wait
, nb_packets_per_stream
);
3847 if (ret
!= LTTNG_OK
) {
3850 snapshot_success
= 1;
3852 struct snapshot_output
*sout
;
3853 struct lttng_ht_iter iter
;
3856 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3857 &iter
.iter
, sout
, node
.node
) {
3858 int64_t nb_packets_per_stream
;
3861 * Make a local copy of the output and assign the possible
3862 * temporary value given by the caller.
3864 memset(&tmp_output
, 0, sizeof(tmp_output
));
3865 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3867 if (output
->max_size
!= (uint64_t) -1ULL) {
3868 tmp_output
.max_size
= output
->max_size
;
3871 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3872 tmp_output
.max_size
);
3873 if (nb_packets_per_stream
< 0) {
3874 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3878 /* Use temporary name. */
3879 if (*output
->name
!= '\0') {
3880 strncpy(tmp_output
.name
, output
->name
,
3881 sizeof(tmp_output
.name
));
3884 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3886 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3887 session
, wait
, nb_packets_per_stream
);
3888 if (ret
!= LTTNG_OK
) {
3892 snapshot_success
= 1;
3898 if (session
->ust_session
) {
3899 struct ltt_ust_session
*usess
= session
->ust_session
;
3901 if (use_tmp_output
) {
3902 int64_t nb_packets_per_stream
;
3904 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3905 tmp_output
.max_size
);
3906 if (nb_packets_per_stream
< 0) {
3907 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3910 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3911 wait
, nb_packets_per_stream
);
3912 if (ret
!= LTTNG_OK
) {
3915 snapshot_success
= 1;
3917 struct snapshot_output
*sout
;
3918 struct lttng_ht_iter iter
;
3921 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3922 &iter
.iter
, sout
, node
.node
) {
3923 int64_t nb_packets_per_stream
;
3926 * Make a local copy of the output and assign the possible
3927 * temporary value given by the caller.
3929 memset(&tmp_output
, 0, sizeof(tmp_output
));
3930 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3932 if (output
->max_size
!= (uint64_t) -1ULL) {
3933 tmp_output
.max_size
= output
->max_size
;
3936 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3937 tmp_output
.max_size
);
3938 if (nb_packets_per_stream
< 0) {
3939 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3944 /* Use temporary name. */
3945 if (*output
->name
!= '\0') {
3946 strncpy(tmp_output
.name
, output
->name
,
3947 sizeof(tmp_output
.name
));
3950 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3952 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3953 wait
, nb_packets_per_stream
);
3954 if (ret
!= LTTNG_OK
) {
3958 snapshot_success
= 1;
3964 if (snapshot_success
) {
3965 session
->snapshot
.nb_snapshot
++;
3967 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3975 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3977 int cmd_set_session_shm_path(struct ltt_session
*session
,
3978 const char *shm_path
)
3984 * Can only set shm path before session is started.
3986 if (session
->has_been_started
) {
3987 return LTTNG_ERR_SESSION_STARTED
;
3990 strncpy(session
->shm_path
, shm_path
,
3991 sizeof(session
->shm_path
));
3992 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3998 * Init command subsystem.
4003 * Set network sequence index to 1 for streams to match a relayd
4004 * socket on the consumer side.
4006 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4007 relayd_net_seq_idx
= 1;
4008 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4010 DBG("Command subsystem initialized");