4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 EfficiOS Inc.
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
21 #include <common/bytecode/bytecode.h>
22 #include <common/align.h>
23 #include <common/common.h>
24 #include <common/compat/errno.h>
25 #include <common/compat/string.h>
26 #include <common/defaults.h>
27 #include <common/dynamic-array.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/payload-view.h>
30 #include <common/payload.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/tracker.h>
33 #include <common/unix.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/channel-internal.h>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-internal.h>
41 #include <lttng/health-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/trigger/trigger-internal.h>
47 #include <lttng/userspace-probe-internal.h>
49 #include "lttng-ctl-helper.h"
50 #include <common/filter/filter-ast.h>
51 #include <common/filter/filter-parser.hpp>
52 #include <common/filter/memstream.h>
54 #define COPY_DOMAIN_PACKED(dst, src) \
56 struct lttng_domain _tmp_domain; \
58 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
62 /* Socket to session daemon for communication */
63 static int sessiond_socket
= -1;
64 static char sessiond_sock_path
[PATH_MAX
];
67 static char *tracing_group
;
73 * Those two variables are used by error.h to silent or control the verbosity of
74 * error message. They are global to the library so application linking with it
75 * are able to compile correctly and also control verbosity of the library.
77 LTTNG_EXPORT
int lttng_opt_quiet
;
78 LTTNG_EXPORT
int lttng_opt_verbose
;
79 LTTNG_EXPORT
int lttng_opt_mi
;
82 * Copy domain to lttcomm_session_msg domain.
84 * If domain is unknown, default domain will be the kernel.
86 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
87 struct lttng_domain
*src
)
91 case LTTNG_DOMAIN_KERNEL
:
92 case LTTNG_DOMAIN_UST
:
93 case LTTNG_DOMAIN_JUL
:
94 case LTTNG_DOMAIN_LOG4J
:
95 case LTTNG_DOMAIN_PYTHON
:
96 memcpy(dst
, src
, sizeof(struct lttng_domain
));
99 memset(dst
, 0, sizeof(struct lttng_domain
));
106 * Send lttcomm_session_msg to the session daemon.
108 * On success, returns the number of bytes sent (>=0)
109 * On error, returns -1
111 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
116 ret
= -LTTNG_ERR_NO_SESSIOND
;
120 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str((lttcomm_sessiond_command
) lsm
->cmd_type
),
123 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
124 sizeof(struct lttcomm_session_msg
));
126 ret
= -LTTNG_ERR_FATAL
;
134 * Send var len data to the session daemon.
136 * On success, returns the number of bytes sent (>=0)
137 * On error, returns -1
139 static int send_session_varlen(const void *data
, size_t len
)
144 ret
= -LTTNG_ERR_NO_SESSIOND
;
153 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
155 ret
= -LTTNG_ERR_FATAL
;
163 * Send file descriptors to the session daemon.
165 * On success, returns the number of bytes sent (>=0)
166 * On error, returns -1
168 static int send_session_fds(const int *fds
, size_t nb_fd
)
173 ret
= -LTTNG_ERR_NO_SESSIOND
;
177 if (!fds
|| !nb_fd
) {
182 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
184 ret
= -LTTNG_ERR_FATAL
;
192 * Receive data from the sessiond socket.
194 * On success, returns the number of bytes received (>=0)
195 * On error, returns a negative lttng_error_code.
197 static int recv_data_sessiond(void *buf
, size_t len
)
201 LTTNG_ASSERT(len
> 0);
204 ret
= -LTTNG_ERR_NO_SESSIOND
;
208 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
210 ret
= -LTTNG_ERR_FATAL
;
211 } else if (ret
== 0) {
212 ret
= -LTTNG_ERR_NO_SESSIOND
;
220 * Receive a payload from the session daemon by appending to an existing
222 * On success, returns the number of bytes received (>=0)
223 * On error, returns a negative lttng_error_code.
225 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
228 const size_t original_payload_size
= payload
->buffer
.size
;
230 ret
= lttng_dynamic_buffer_set_size(
231 &payload
->buffer
, payload
->buffer
.size
+ len
);
233 ret
= -LTTNG_ERR_NOMEM
;
237 ret
= recv_data_sessiond(
238 payload
->buffer
.data
+ original_payload_size
, len
);
244 * Check if we are in the specified group.
246 * If yes return 1, else return -1.
248 int lttng_check_tracing_group(void)
250 gid_t
*grp_list
, tracing_gid
;
251 int grp_list_size
, grp_id
, i
;
253 const char *grp_name
= tracing_group
;
255 /* Get GID of group 'tracing' */
256 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
257 /* If grp_tracing is NULL, the group does not exist. */
261 /* Get number of supplementary group IDs */
262 grp_list_size
= getgroups(0, NULL
);
263 if (grp_list_size
< 0) {
268 /* Alloc group list of the right size */
269 grp_list
= (gid_t
*) zmalloc(grp_list_size
* sizeof(gid_t
));
274 grp_id
= getgroups(grp_list_size
, grp_list
);
280 for (i
= 0; i
< grp_list_size
; i
++) {
281 if (grp_list
[i
] == tracing_gid
) {
294 static enum lttng_error_code
check_enough_available_memory(
295 uint64_t num_bytes_requested_per_cpu
)
298 enum lttng_error_code ret_code
;
300 uint64_t best_mem_info
;
301 uint64_t num_bytes_requested_total
;
304 * Get the number of CPU currently online to compute the amount of
305 * memory needed to create a buffer for every CPU.
307 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
309 ret_code
= LTTNG_ERR_FATAL
;
313 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
315 ret_code
= LTTNG_ERR_OVERFLOW
;
319 num_bytes_requested_total
=
320 num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
323 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
324 * reliable estimate we can get but it is only exposed by the kernel
325 * since 3.14. (See Linux kernel commit:
326 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
328 ret
= utils_get_memory_available(&best_mem_info
);
334 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
335 * is a sanity check for obvious user error.
337 ret
= utils_get_memory_total(&best_mem_info
);
342 /* No valid source of information. */
343 ret_code
= LTTNG_ERR_NOMEM
;
347 if (best_mem_info
>= num_bytes_requested_total
) {
350 ret_code
= LTTNG_ERR_NOMEM
;
357 * Try connect to session daemon with sock_path.
359 * Return 0 on success, else -1
361 static int try_connect_sessiond(const char *sock_path
)
365 /* If socket exist, we check if the daemon listens for connect. */
366 ret
= access(sock_path
, F_OK
);
372 ret
= lttcomm_connect_unix_sock(sock_path
);
378 ret
= lttcomm_close_unix_sock(ret
);
380 PERROR("lttcomm_close_unix_sock");
390 * Set sessiond socket path by putting it in the global sessiond_sock_path
393 * Returns 0 on success, negative value on failure (the sessiond socket path
394 * is somehow too long or ENOMEM).
396 static int set_session_daemon_path(void)
398 int in_tgroup
= 0; /* In tracing group. */
404 /* Are we in the tracing group ? */
405 in_tgroup
= lttng_check_tracing_group();
408 if ((uid
== 0) || in_tgroup
== 1) {
409 const int ret
= lttng_strncpy(sessiond_sock_path
,
410 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
411 sizeof(sessiond_sock_path
));
423 ret
= try_connect_sessiond(sessiond_sock_path
);
427 /* Global session daemon not available... */
429 /* ...or not in tracing group (and not root), default */
432 * With GNU C < 2.1, snprintf returns -1 if the target buffer
434 * With GNU C >= 2.1, snprintf returns the required size
435 * (excluding closing null)
437 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
439 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
451 * Connect to the LTTng session daemon.
453 * On success, return the socket's file descriptor. On error, return -1.
455 int connect_sessiond(void)
459 ret
= set_session_daemon_path();
464 /* Connect to the sesssion daemon. */
465 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
476 static void reset_global_sessiond_connection_state(void)
478 sessiond_socket
= -1;
483 * Clean disconnect from the session daemon.
485 * On success, return 0. On error, return -1.
487 static int disconnect_sessiond(void)
492 ret
= lttcomm_close_unix_sock(sessiond_socket
);
493 reset_global_sessiond_connection_state();
499 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
507 ret
= -LTTNG_ERR_INVALID
;
517 ret
= recv_data_sessiond(buf
, len
);
523 ret
= -LTTNG_ERR_INVALID
;
527 /* Move ownership of command header buffer to user. */
532 /* No command header. */
548 * Ask the session daemon a specific command and put the data into buf.
549 * Takes extra var. len. data and file descriptors as input to send to the
552 * Return size of data (only payload, not header) or a negative error code.
554 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
555 const int *fds
, size_t nb_fd
, const void *vardata
,
556 size_t vardata_len
, void **user_payload_buf
,
557 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
561 struct lttcomm_lttng_msg llm
;
563 ret
= connect_sessiond();
565 ret
= -LTTNG_ERR_NO_SESSIOND
;
568 sessiond_socket
= ret
;
572 ret
= send_session_msg(lsm
);
574 /* Ret value is a valid lttng error code. */
577 /* Send var len data */
578 ret
= send_session_varlen(vardata
, vardata_len
);
580 /* Ret value is a valid lttng error code. */
585 ret
= send_session_fds(fds
, nb_fd
);
587 /* Ret value is a valid lttng error code. */
591 /* Get header from data transmission */
592 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
594 /* Ret value is a valid lttng error code. */
598 /* Check error code if OK */
599 if (llm
.ret_code
!= LTTNG_OK
) {
604 /* Get command header from data transmission */
605 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
606 user_cmd_header_buf
, user_cmd_header_len
);
611 /* Get payload from data transmission */
612 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
621 disconnect_sessiond();
625 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
626 struct lttng_payload
*reply
)
629 struct lttcomm_lttng_msg llm
;
630 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
632 LTTNG_ASSERT(reply
->buffer
.size
== 0);
633 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
635 ret
= connect_sessiond();
637 ret
= -LTTNG_ERR_NO_SESSIOND
;
640 sessiond_socket
= ret
;
644 /* Send command to session daemon */
645 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
646 message
->buffer
.size
);
648 ret
= -LTTNG_ERR_FATAL
;
653 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
656 ret
= -LTTNG_ERR_FATAL
;
661 /* Get header from data transmission */
662 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
664 /* Ret value is a valid lttng error code. */
668 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
670 /* Check error code if OK */
671 if (llm
.ret_code
!= LTTNG_OK
) {
672 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
673 /* Invalid error code received. */
674 ret
= -LTTNG_ERR_UNK
;
681 if (llm
.cmd_header_size
> 0) {
682 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
688 /* Get command header from data transmission */
689 if (llm
.data_size
> 0) {
690 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
696 if (llm
.fd_count
> 0) {
697 ret
= lttcomm_recv_payload_fds_unix_sock(
698 sessiond_socket
, llm
.fd_count
, reply
);
704 /* Don't return the llm header to the caller. */
705 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
706 reply
->buffer
.size
- sizeof(llm
));
707 ret
= lttng_dynamic_buffer_set_size(
708 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
710 /* Can't happen as size is reduced. */
714 ret
= reply
->buffer
.size
;
717 disconnect_sessiond();
722 * Create lttng handle and return pointer.
724 * The returned pointer will be NULL in case of malloc() error.
726 struct lttng_handle
*lttng_create_handle(const char *session_name
,
727 struct lttng_domain
*domain
)
730 struct lttng_handle
*handle
= NULL
;
732 handle
= (lttng_handle
*) zmalloc(sizeof(struct lttng_handle
));
733 if (handle
== NULL
) {
734 PERROR("malloc handle");
738 /* Copy session name */
739 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
740 sizeof(handle
->session_name
));
745 /* Copy lttng domain or leave initialized to 0. */
747 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
758 * Destroy handle by free(3) the pointer.
760 void lttng_destroy_handle(struct lttng_handle
*handle
)
766 * Register an outside consumer.
768 * Returns size of returned session payload data or a negative error code.
770 int lttng_register_consumer(struct lttng_handle
*handle
,
771 const char *socket_path
)
774 struct lttcomm_session_msg lsm
;
776 if (handle
== NULL
|| socket_path
== NULL
) {
777 ret
= -LTTNG_ERR_INVALID
;
781 memset(&lsm
, 0, sizeof(lsm
));
782 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
783 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
784 sizeof(lsm
.session
.name
));
786 ret
= -LTTNG_ERR_INVALID
;
790 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
792 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
793 sizeof(lsm
.u
.reg
.path
));
795 ret
= -LTTNG_ERR_INVALID
;
799 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
805 * Start tracing for all traces of the session.
807 * Returns size of returned session payload data or a negative error code.
809 int lttng_start_tracing(const char *session_name
)
812 struct lttcomm_session_msg lsm
;
814 if (session_name
== NULL
) {
815 ret
= -LTTNG_ERR_INVALID
;
819 memset(&lsm
, 0, sizeof(lsm
));
820 lsm
.cmd_type
= LTTNG_START_TRACE
;
822 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
823 sizeof(lsm
.session
.name
));
825 ret
= -LTTNG_ERR_INVALID
;
829 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
835 * Stop tracing for all traces of the session.
837 static int _lttng_stop_tracing(const char *session_name
, int wait
)
840 struct lttcomm_session_msg lsm
;
842 if (session_name
== NULL
) {
843 ret
= -LTTNG_ERR_INVALID
;
847 memset(&lsm
, 0, sizeof(lsm
));
848 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
850 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
851 sizeof(lsm
.session
.name
));
853 ret
= -LTTNG_ERR_INVALID
;
857 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
858 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
866 /* Check for data availability */
868 data_ret
= lttng_data_pending(session_name
);
870 /* Return the data available call error. */
876 * Data sleep time before retrying (in usec). Don't sleep if the
877 * call returned value indicates availability.
880 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
882 } while (data_ret
!= 0);
890 * Stop tracing and wait for data availability.
892 int lttng_stop_tracing(const char *session_name
)
894 return _lttng_stop_tracing(session_name
, 1);
898 * Stop tracing but _don't_ wait for data availability.
900 int lttng_stop_tracing_no_wait(const char *session_name
)
902 return _lttng_stop_tracing(session_name
, 0);
906 * Add context to a channel.
908 * If the given channel is NULL, add the contexts to all channels.
909 * The event_name param is ignored.
911 * Returns the size of the returned payload data or a negative error code.
913 int lttng_add_context(struct lttng_handle
*handle
,
914 struct lttng_event_context
*ctx
, const char *event_name
,
915 const char *channel_name
)
920 struct lttcomm_session_msg lsm
;
922 /* Safety check. Both are mandatory. */
923 if (handle
== NULL
|| ctx
== NULL
) {
924 ret
= -LTTNG_ERR_INVALID
;
928 memset(&lsm
, 0, sizeof(lsm
));
929 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
931 /* If no channel name, send empty string. */
932 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
933 sizeof(lsm
.u
.context
.channel_name
));
935 ret
= -LTTNG_ERR_INVALID
;
939 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
940 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
941 sizeof(lsm
.session
.name
));
943 ret
= -LTTNG_ERR_INVALID
;
947 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
948 size_t provider_len
, ctx_len
;
949 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
950 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
952 if (!provider_name
|| !ctx_name
) {
953 ret
= -LTTNG_ERR_INVALID
;
957 provider_len
= strlen(provider_name
);
958 if (provider_len
== 0) {
959 ret
= -LTTNG_ERR_INVALID
;
962 lsm
.u
.context
.provider_name_len
= provider_len
;
964 ctx_len
= strlen(ctx_name
);
966 ret
= -LTTNG_ERR_INVALID
;
969 lsm
.u
.context
.context_name_len
= ctx_len
;
971 len
= provider_len
+ ctx_len
;
972 buf
= (char *) zmalloc(len
);
974 ret
= -LTTNG_ERR_NOMEM
;
978 memcpy(buf
, provider_name
, provider_len
);
979 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
981 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
983 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
985 * Don't leak application addresses to the sessiond.
986 * This is only necessary when ctx is for an app ctx otherwise
987 * the values inside the union (type & config) are overwritten.
989 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
990 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
993 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
1000 * Enable event(s) for a channel.
1002 * If no event name is specified, all events are enabled.
1003 * If no channel name is specified, the default 'channel0' is used.
1005 * Returns size of returned session payload data or a negative error code.
1007 int lttng_enable_event(struct lttng_handle
*handle
,
1008 struct lttng_event
*ev
, const char *channel_name
)
1010 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1015 * Create or enable an event with a filter expression.
1017 * Return negative error value on error.
1018 * Return size of returned session payload data if OK.
1020 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1021 struct lttng_event
*event
, const char *channel_name
,
1022 const char *filter_expression
)
1024 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1025 filter_expression
, 0, NULL
);
1029 * Depending on the event, return a newly allocated agent filter expression or
1030 * NULL if not applicable.
1032 * An event with NO loglevel and the name is * will return NULL.
1034 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1037 char *agent_filter
= NULL
;
1041 /* Don't add filter for the '*' event. */
1042 if (strcmp(ev
->name
, "*") != 0) {
1044 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1047 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1055 /* Add loglevel filtering if any for the JUL domain. */
1056 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1059 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1065 if (filter
|| agent_filter
) {
1068 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1069 agent_filter
? agent_filter
: filter
, op
,
1074 agent_filter
= new_filter
;
1076 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1085 return agent_filter
;
1092 * Enable event(s) for a channel, possibly with exclusions and a filter.
1093 * If no event name is specified, all events are enabled.
1094 * If no channel name is specified, the default name is used.
1095 * If filter expression is not NULL, the filter is set for the event.
1096 * If exclusion count is not zero, the exclusions are set for the event.
1097 * Returns size of returned session payload data or a negative error code.
1099 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1100 struct lttng_event
*ev
, const char *channel_name
,
1101 const char *original_filter_expression
,
1102 int exclusion_count
, char **exclusion_list
)
1104 struct lttcomm_session_msg lsm
;
1105 struct lttng_payload payload
;
1107 unsigned int free_filter_expression
= 0;
1108 struct filter_parser_ctx
*ctx
= NULL
;
1111 * We have either a filter or some exclusions, so we need to set up
1112 * a variable-length payload from where to send the data.
1114 lttng_payload_init(&payload
);
1117 * Cast as non-const since we may replace the filter expression
1118 * by a dynamically allocated string. Otherwise, the original
1119 * string is not modified.
1121 char *filter_expression
= (char *) original_filter_expression
;
1123 if (handle
== NULL
|| ev
== NULL
) {
1124 ret
= -LTTNG_ERR_INVALID
;
1129 * Empty filter string will always be rejected by the parser
1130 * anyway, so treat this corner-case early to eliminate
1131 * lttng_fmemopen error for 0-byte allocation.
1133 if (filter_expression
&& filter_expression
[0] == '\0') {
1134 ret
= -LTTNG_ERR_INVALID
;
1138 memset(&lsm
, 0, sizeof(lsm
));
1140 /* If no channel name, send empty string. */
1141 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1142 sizeof(lsm
.u
.enable
.channel_name
));
1144 ret
= -LTTNG_ERR_INVALID
;
1148 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1149 if (ev
->name
[0] == '\0') {
1150 /* Enable all events. */
1151 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1152 LTTNG_ASSERT(ret
== 0);
1155 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1156 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1158 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1159 sizeof(lsm
.session
.name
));
1161 ret
= -LTTNG_ERR_INVALID
;
1165 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1166 lsm
.u
.enable
.bytecode_len
= 0;
1168 /* Parse filter expression. */
1169 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1170 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1171 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1172 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1173 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1174 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1177 /* Setup JUL filter if needed. */
1178 agent_filter
= set_agent_filter(filter_expression
, ev
);
1179 if (!agent_filter
) {
1180 if (!filter_expression
) {
1182 * No JUL and no filter, just skip
1189 * With an agent filter, the original filter has
1190 * been added to it thus replace the filter
1193 filter_expression
= agent_filter
;
1194 free_filter_expression
= 1;
1198 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1203 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1204 + bytecode_get_len(&ctx
->bytecode
->b
);
1205 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1208 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1209 lsm
.u
.enable
.bytecode_len
+
1210 lsm
.u
.enable
.expression_len
+
1211 LTTNG_SYMBOL_NAME_LEN
*
1214 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1218 /* Put exclusion names first in the data. */
1219 for (i
= 0; i
< exclusion_count
; i
++) {
1220 size_t exclusion_len
;
1222 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1223 LTTNG_SYMBOL_NAME_LEN
);
1224 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1225 /* Exclusion is not NULL-terminated. */
1226 ret
= -LTTNG_ERR_INVALID
;
1230 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1231 exclusion_list
[i
], exclusion_len
);
1237 * Padding the rest of the entry with zeros. Every exclusion
1238 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1240 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1241 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1247 /* Add filter expression next. */
1248 if (filter_expression
) {
1249 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1250 filter_expression
, lsm
.u
.enable
.expression_len
);
1255 /* Add filter bytecode next. */
1256 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1257 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1258 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1263 if (ev
->extended
.ptr
) {
1264 struct lttng_event_extended
*ev_ext
=
1265 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1267 if (ev_ext
->probe_location
) {
1269 * lttng_userspace_probe_location_serialize returns the
1270 * number of bytes that was appended to the buffer.
1272 ret
= lttng_userspace_probe_location_serialize(
1273 ev_ext
->probe_location
, &payload
);
1279 * Set the size of the userspace probe location element
1280 * of the buffer so that the receiving side knows where
1283 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1288 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1290 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1297 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1298 if (fd_count
== 1) {
1299 struct fd_handle
*h
=
1300 lttng_payload_view_pop_fd_handle(&view
);
1306 fd_to_send
= fd_handle_get_fd(h
);
1310 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1311 fd_count
? &fd_to_send
: NULL
, fd_count
,
1312 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1313 view
.buffer
.size
, NULL
, NULL
, 0);
1317 if (filter_expression
&& ctx
) {
1318 filter_bytecode_free(ctx
);
1319 filter_ir_free(ctx
);
1320 filter_parser_ctx_free(ctx
);
1323 if (free_filter_expression
) {
1325 * The filter expression has been replaced and must be freed as
1326 * it is not the original filter expression received as a
1329 free(filter_expression
);
1333 * Return directly to the caller and don't ask the sessiond since
1334 * something went wrong in the parsing of data above.
1336 lttng_payload_reset(&payload
);
1340 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1344 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1345 struct lttng_event
*ev
, const char *channel_name
,
1346 const char *original_filter_expression
)
1348 struct lttcomm_session_msg lsm
;
1351 unsigned int free_filter_expression
= 0;
1352 struct filter_parser_ctx
*ctx
= NULL
;
1354 * Cast as non-const since we may replace the filter expression
1355 * by a dynamically allocated string. Otherwise, the original
1356 * string is not modified.
1358 char *filter_expression
= (char *) original_filter_expression
;
1360 if (handle
== NULL
|| ev
== NULL
) {
1361 ret
= -LTTNG_ERR_INVALID
;
1366 * Empty filter string will always be rejected by the parser
1367 * anyway, so treat this corner-case early to eliminate
1368 * lttng_fmemopen error for 0-byte allocation.
1370 if (filter_expression
&& filter_expression
[0] == '\0') {
1371 ret
= -LTTNG_ERR_INVALID
;
1375 memset(&lsm
, 0, sizeof(lsm
));
1377 /* If no channel name, send empty string. */
1378 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1379 sizeof(lsm
.u
.disable
.channel_name
));
1381 ret
= -LTTNG_ERR_INVALID
;
1385 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1387 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1388 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1390 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1391 sizeof(lsm
.session
.name
));
1393 ret
= -LTTNG_ERR_INVALID
;
1397 lsm
.u
.disable
.bytecode_len
= 0;
1400 * For the JUL domain, a filter is enforced except for the
1401 * disable all event. This is done to avoid having the event in
1402 * all sessions thus filtering by logger name.
1404 if (filter_expression
== NULL
&&
1405 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1406 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1407 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1412 * We have a filter, so we need to set up a variable-length
1413 * memory block from where to send the data.
1416 /* Parse filter expression */
1417 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1418 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1419 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1420 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1421 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1422 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1425 /* Setup JUL filter if needed. */
1426 agent_filter
= set_agent_filter(filter_expression
, ev
);
1427 if (!agent_filter
) {
1428 if (!filter_expression
) {
1430 * No JUL and no filter, just skip
1437 * With a JUL filter, the original filter has
1438 * been added to it thus replace the filter
1441 filter_expression
= agent_filter
;
1442 free_filter_expression
= 1;
1446 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1451 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1452 + bytecode_get_len(&ctx
->bytecode
->b
);
1453 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1456 varlen_data
= (char *) zmalloc(lsm
.u
.disable
.bytecode_len
1457 + lsm
.u
.disable
.expression_len
);
1459 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1463 /* Add filter expression. */
1464 if (lsm
.u
.disable
.expression_len
!= 0) {
1467 lsm
.u
.disable
.expression_len
);
1469 /* Add filter bytecode next. */
1470 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1472 + lsm
.u
.disable
.expression_len
,
1474 lsm
.u
.disable
.bytecode_len
);
1477 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1478 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1482 if (filter_expression
&& ctx
) {
1483 filter_bytecode_free(ctx
);
1484 filter_ir_free(ctx
);
1485 filter_parser_ctx_free(ctx
);
1488 if (free_filter_expression
) {
1490 * The filter expression has been replaced and must be freed as
1491 * it is not the original filter expression received as a
1494 free(filter_expression
);
1498 * Return directly to the caller and don't ask the sessiond since
1499 * something went wrong in the parsing of data above.
1504 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1509 * Disable event(s) of a channel and domain.
1510 * If no event name is specified, all events are disabled.
1511 * If no channel name is specified, the default 'channel0' is used.
1512 * Returns size of returned session payload data or a negative error code.
1514 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1515 const char *channel_name
)
1518 struct lttng_event ev
;
1520 memset(&ev
, 0, sizeof(ev
));
1522 ev
.type
= LTTNG_EVENT_ALL
;
1523 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1525 ret
= -LTTNG_ERR_INVALID
;
1529 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1534 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1536 struct lttng_channel
*channel
= NULL
;
1537 struct lttng_channel_extended
*extended
= NULL
;
1543 /* Validate domain. */
1544 switch (domain
->type
) {
1545 case LTTNG_DOMAIN_UST
:
1546 switch (domain
->buf_type
) {
1547 case LTTNG_BUFFER_PER_UID
:
1548 case LTTNG_BUFFER_PER_PID
:
1554 case LTTNG_DOMAIN_KERNEL
:
1555 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1563 channel
= (lttng_channel
*) zmalloc(sizeof(*channel
));
1568 extended
= (lttng_channel_extended
*) zmalloc(sizeof(*extended
));
1573 channel
->attr
.extended
.ptr
= extended
;
1575 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1583 void lttng_channel_destroy(struct lttng_channel
*channel
)
1589 if (channel
->attr
.extended
.ptr
) {
1590 free(channel
->attr
.extended
.ptr
);
1596 * Enable channel per domain
1597 * Returns size of returned session payload data or a negative error code.
1599 int lttng_enable_channel(struct lttng_handle
*handle
,
1600 struct lttng_channel
*in_chan
)
1602 enum lttng_error_code ret_code
;
1604 struct lttcomm_session_msg lsm
;
1605 uint64_t total_buffer_size_needed_per_cpu
= 0;
1607 /* NULL arguments are forbidden. No default values. */
1608 if (handle
== NULL
|| in_chan
== NULL
) {
1609 return -LTTNG_ERR_INVALID
;
1612 memset(&lsm
, 0, sizeof(lsm
));
1613 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1614 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1616 if (!in_chan
->attr
.extended
.ptr
) {
1617 struct lttng_channel
*channel
;
1618 struct lttng_channel_extended
*extended
;
1620 channel
= lttng_channel_create(&handle
->domain
);
1622 return -LTTNG_ERR_NOMEM
;
1626 * Create a new channel in order to use default extended
1629 extended
= (struct lttng_channel_extended
*)
1630 channel
->attr
.extended
.ptr
;
1631 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1632 lttng_channel_destroy(channel
);
1634 struct lttng_channel_extended
*extended
;
1636 extended
= (struct lttng_channel_extended
*)
1637 in_chan
->attr
.extended
.ptr
;
1638 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1642 * Verify that the amount of memory required to create the requested
1643 * buffer is available on the system at the moment.
1645 if (lsm
.u
.channel
.chan
.attr
.num_subbuf
>
1646 UINT64_MAX
/ lsm
.u
.channel
.chan
.attr
.subbuf_size
) {
1648 ret
= -LTTNG_ERR_OVERFLOW
;
1652 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1653 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1654 ret_code
= check_enough_available_memory(
1655 total_buffer_size_needed_per_cpu
);
1656 if (ret_code
!= LTTNG_OK
) {
1661 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1662 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1664 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1665 sizeof(lsm
.session
.name
));
1667 ret
= -LTTNG_ERR_INVALID
;
1671 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1677 * All tracing will be stopped for registered events of the channel.
1678 * Returns size of returned session payload data or a negative error code.
1680 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1683 struct lttcomm_session_msg lsm
;
1685 /* Safety check. Both are mandatory. */
1686 if (handle
== NULL
|| name
== NULL
) {
1687 return -LTTNG_ERR_INVALID
;
1690 memset(&lsm
, 0, sizeof(lsm
));
1692 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1694 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1695 sizeof(lsm
.u
.disable
.channel_name
));
1697 ret
= -LTTNG_ERR_INVALID
;
1701 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1703 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1704 sizeof(lsm
.session
.name
));
1706 ret
= -LTTNG_ERR_INVALID
;
1710 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1716 * Lists all available tracepoints of domain.
1717 * Sets the contents of the events array.
1718 * Returns the number of lttng_event entries in events;
1719 * on error, returns a negative value.
1721 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1722 struct lttng_event
**events
)
1725 struct lttcomm_session_msg lsm
;
1727 if (handle
== NULL
) {
1728 return -LTTNG_ERR_INVALID
;
1731 memset(&lsm
, 0, sizeof(lsm
));
1732 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1733 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1735 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1740 return ret
/ sizeof(struct lttng_event
);
1744 * Lists all available tracepoint fields of domain.
1745 * Sets the contents of the event field array.
1746 * Returns the number of lttng_event_field entries in events;
1747 * on error, returns a negative value.
1749 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1750 struct lttng_event_field
**fields
)
1753 struct lttcomm_session_msg lsm
;
1755 if (handle
== NULL
) {
1756 return -LTTNG_ERR_INVALID
;
1759 memset(&lsm
, 0, sizeof(lsm
));
1760 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1761 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1763 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1768 return ret
/ sizeof(struct lttng_event_field
);
1772 * Lists all available kernel system calls. Allocates and sets the contents of
1775 * Returns the number of lttng_event entries in events; on error, returns a
1778 int lttng_list_syscalls(struct lttng_event
**events
)
1781 struct lttcomm_session_msg lsm
;
1784 return -LTTNG_ERR_INVALID
;
1787 memset(&lsm
, 0, sizeof(lsm
));
1788 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1789 /* Force kernel domain for system calls. */
1790 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1792 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1797 return ret
/ sizeof(struct lttng_event
);
1801 * Returns a human readable string describing
1802 * the error code (positive or negative value).
1804 const char *lttng_strerror(int code
)
1810 return error_get_str(code
);
1813 enum lttng_error_code
lttng_create_session_ext(
1814 struct lttng_session_descriptor
*session_descriptor
)
1816 enum lttng_error_code ret_code
;
1817 struct lttcomm_session_msg lsm
= {
1818 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1821 struct lttng_buffer_view reply_view
;
1823 bool sessiond_must_generate_ouput
;
1824 struct lttng_dynamic_buffer payload
;
1826 size_t descriptor_size
;
1827 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1829 lttng_dynamic_buffer_init(&payload
);
1830 if (!session_descriptor
) {
1831 ret_code
= LTTNG_ERR_INVALID
;
1835 sessiond_must_generate_ouput
=
1836 !lttng_session_descriptor_is_output_destination_initialized(
1837 session_descriptor
);
1838 if (sessiond_must_generate_ouput
) {
1839 const char *home_dir
= utils_get_home_dir();
1840 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1842 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1843 ret_code
= LTTNG_ERR_FATAL
;
1847 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1848 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1851 ret_code
= LTTNG_ERR_NOMEM
;
1856 descriptor_size
= payload
.size
;
1857 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1860 ret_code
= LTTNG_ERR_INVALID
;
1863 descriptor_size
= payload
.size
- descriptor_size
;
1864 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1866 /* Command returns a session descriptor on success. */
1867 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1868 payload
.size
, &reply
);
1869 if (reply_ret
< 0) {
1870 ret_code
= (lttng_error_code
) -reply_ret
;
1872 } else if (reply_ret
== 0) {
1873 /* Socket unexpectedly closed by the session daemon. */
1874 ret_code
= LTTNG_ERR_FATAL
;
1878 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1879 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1882 ret_code
= LTTNG_ERR_FATAL
;
1885 ret_code
= LTTNG_OK
;
1886 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1889 lttng_dynamic_buffer_reset(&payload
);
1890 lttng_session_descriptor_destroy(descriptor_reply
);
1895 * Create a new session using name and url for destination.
1897 * Return 0 on success else a negative LTTng error code.
1899 int lttng_create_session(const char *name
, const char *url
)
1903 struct lttng_uri
*uris
= NULL
;
1904 struct lttng_session_descriptor
*descriptor
= NULL
;
1905 enum lttng_error_code ret_code
;
1908 ret
= -LTTNG_ERR_INVALID
;
1912 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1914 ret
= -LTTNG_ERR_INVALID
;
1919 descriptor
= lttng_session_descriptor_create(name
);
1922 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1923 ret
= -LTTNG_ERR_INVALID
;
1926 descriptor
= lttng_session_descriptor_local_create(name
,
1930 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1934 ret
= -LTTNG_ERR_INVALID
;
1938 ret
= -LTTNG_ERR_INVALID
;
1941 ret_code
= lttng_create_session_ext(descriptor
);
1942 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1944 lttng_session_descriptor_destroy(descriptor
);
1950 * Create a session exclusively used for snapshot.
1952 * Return 0 on success else a negative LTTng error code.
1954 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1957 enum lttng_error_code ret_code
;
1959 struct lttng_uri
*uris
= NULL
;
1960 struct lttng_session_descriptor
*descriptor
= NULL
;
1963 ret
= -LTTNG_ERR_INVALID
;
1967 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1969 ret
= -LTTNG_ERR_INVALID
;
1973 * If the user does not specify a custom subdir, use the session name.
1975 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1976 strlen(uris
[0].subdir
) == 0) {
1977 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1980 PERROR("Failed to set session name as network destination sub-directory");
1981 ret
= -LTTNG_ERR_FATAL
;
1983 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1984 /* Truncated output. */
1985 ret
= -LTTNG_ERR_INVALID
;
1992 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1995 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1996 ret
= -LTTNG_ERR_INVALID
;
1999 descriptor
= lttng_session_descriptor_snapshot_local_create(
2004 descriptor
= lttng_session_descriptor_snapshot_network_create(
2010 ret
= -LTTNG_ERR_INVALID
;
2014 ret
= -LTTNG_ERR_INVALID
;
2017 ret_code
= lttng_create_session_ext(descriptor
);
2018 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2020 lttng_session_descriptor_destroy(descriptor
);
2026 * Create a session exclusively used for live.
2028 * Return 0 on success else a negative LTTng error code.
2030 int lttng_create_session_live(const char *name
, const char *url
,
2031 unsigned int timer_interval
)
2034 enum lttng_error_code ret_code
;
2035 struct lttng_session_descriptor
*descriptor
= NULL
;
2038 ret
= -LTTNG_ERR_INVALID
;
2043 descriptor
= lttng_session_descriptor_live_network_create(
2044 name
, url
, NULL
, timer_interval
);
2046 descriptor
= lttng_session_descriptor_live_create(
2047 name
, timer_interval
);
2050 ret
= -LTTNG_ERR_INVALID
;
2053 ret_code
= lttng_create_session_ext(descriptor
);
2054 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2056 lttng_session_descriptor_destroy(descriptor
);
2061 * Stop the session and wait for the data before destroying it
2063 * Return 0 on success else a negative LTTng error code.
2065 int lttng_destroy_session(const char *session_name
)
2068 enum lttng_error_code ret_code
;
2069 enum lttng_destruction_handle_status status
;
2070 struct lttng_destruction_handle
*handle
= NULL
;
2073 * Stop the tracing and wait for the data to be
2076 ret
= _lttng_stop_tracing(session_name
, 1);
2077 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2081 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2082 if (ret_code
!= LTTNG_OK
) {
2083 ret
= (int) -ret_code
;
2086 LTTNG_ASSERT(handle
);
2088 /* Block until the completion of the destruction of the session. */
2089 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2090 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2091 ret
= -LTTNG_ERR_UNK
;
2095 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2096 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2097 ret
= -LTTNG_ERR_UNK
;
2100 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2102 lttng_destruction_handle_destroy(handle
);
2107 * Destroy the session without waiting for the data.
2109 int lttng_destroy_session_no_wait(const char *session_name
)
2111 enum lttng_error_code ret_code
;
2113 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2114 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2118 * Ask the session daemon for all available sessions.
2119 * Sets the contents of the sessions array.
2120 * Returns the number of lttng_session entries in sessions;
2121 * on error, returns a negative value.
2123 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2126 struct lttcomm_session_msg lsm
;
2127 const size_t session_size
= sizeof(struct lttng_session
) +
2128 sizeof(struct lttng_session_extended
);
2129 size_t session_count
, i
;
2130 struct lttng_session_extended
*sessions_extended_begin
;
2131 struct lttng_session
*sessions
= NULL
;
2133 memset(&lsm
, 0, sizeof(lsm
));
2134 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2136 * Initialize out_sessions to NULL so it is initialized when
2137 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2138 * be subsequently freed.
2140 *out_sessions
= NULL
;
2141 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2146 ret
= -LTTNG_ERR_FATAL
;
2150 if (ret
% session_size
) {
2151 ret
= -LTTNG_ERR_UNK
;
2155 session_count
= (size_t) ret
/ session_size
;
2156 sessions_extended_begin
= (struct lttng_session_extended
*)
2157 (&sessions
[session_count
]);
2159 /* Set extended session info pointers. */
2160 for (i
= 0; i
< session_count
; i
++) {
2161 struct lttng_session
*session
= &sessions
[i
];
2162 struct lttng_session_extended
*extended
=
2163 &(sessions_extended_begin
[i
]);
2165 session
->extended
.ptr
= extended
;
2168 ret
= (int) session_count
;
2169 *out_sessions
= sessions
;
2174 enum lttng_error_code
lttng_session_get_creation_time(
2175 const struct lttng_session
*session
, uint64_t *creation_time
)
2177 enum lttng_error_code ret
= LTTNG_OK
;
2178 struct lttng_session_extended
*extended
;
2180 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2181 ret
= LTTNG_ERR_INVALID
;
2185 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2186 if (!extended
->creation_time
.is_set
) {
2187 /* Not created on the session daemon yet. */
2188 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2191 *creation_time
= extended
->creation_time
.value
;
2196 int lttng_set_session_shm_path(const char *session_name
,
2197 const char *shm_path
)
2200 struct lttcomm_session_msg lsm
;
2202 if (session_name
== NULL
) {
2203 return -LTTNG_ERR_INVALID
;
2206 memset(&lsm
, 0, sizeof(lsm
));
2207 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2209 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2210 sizeof(lsm
.session
.name
));
2212 ret
= -LTTNG_ERR_INVALID
;
2216 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2217 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2219 ret
= -LTTNG_ERR_INVALID
;
2223 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2229 * Ask the session daemon for all available domains of a session.
2230 * Sets the contents of the domains array.
2231 * Returns the number of lttng_domain entries in domains;
2232 * on error, returns a negative value.
2234 int lttng_list_domains(const char *session_name
,
2235 struct lttng_domain
**domains
)
2238 struct lttcomm_session_msg lsm
;
2240 if (session_name
== NULL
) {
2241 ret
= -LTTNG_ERR_INVALID
;
2245 memset(&lsm
, 0, sizeof(lsm
));
2246 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2248 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2249 sizeof(lsm
.session
.name
));
2251 ret
= -LTTNG_ERR_INVALID
;
2255 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2260 return ret
/ sizeof(struct lttng_domain
);
2266 * Ask the session daemon for all available channels of a session.
2267 * Sets the contents of the channels array.
2268 * Returns the number of lttng_channel entries in channels;
2269 * on error, returns a negative value.
2271 int lttng_list_channels(struct lttng_handle
*handle
,
2272 struct lttng_channel
**channels
)
2275 size_t channel_count
, i
;
2276 const size_t channel_size
= sizeof(struct lttng_channel
) +
2277 sizeof(struct lttng_channel_extended
);
2278 struct lttcomm_session_msg lsm
;
2281 if (handle
== NULL
) {
2282 ret
= -LTTNG_ERR_INVALID
;
2286 memset(&lsm
, 0, sizeof(lsm
));
2287 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2288 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2289 sizeof(lsm
.session
.name
));
2291 ret
= -LTTNG_ERR_INVALID
;
2295 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2297 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2302 if (ret
% channel_size
) {
2303 ret
= -LTTNG_ERR_UNK
;
2308 channel_count
= (size_t) ret
/ channel_size
;
2310 /* Set extended info pointers */
2311 extended_at
= ((char *) *channels
) +
2312 channel_count
* sizeof(struct lttng_channel
);
2313 for (i
= 0; i
< channel_count
; i
++) {
2314 struct lttng_channel
*chan
= &(*channels
)[i
];
2316 chan
->attr
.extended
.ptr
= extended_at
;
2317 extended_at
+= sizeof(struct lttng_channel_extended
);
2320 ret
= (int) channel_count
;
2326 * Ask the session daemon for all available events of a session channel.
2327 * Sets the contents of the events array.
2328 * Returns the number of lttng_event entries in events;
2329 * on error, returns a negative value.
2331 int lttng_list_events(struct lttng_handle
*handle
,
2332 const char *channel_name
, struct lttng_event
**events
)
2335 struct lttcomm_session_msg lsm
= {};
2336 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2337 uint32_t nb_events
, i
;
2338 const char *comm_ext_at
;
2339 struct lttng_dynamic_buffer listing
;
2341 struct lttng_payload payload
;
2342 struct lttng_payload payload_copy
;
2343 struct lttng_payload_view lsm_view
=
2344 lttng_payload_view_init_from_buffer(
2345 (const char *) &lsm
, 0, sizeof(lsm
));
2346 struct lttng_buffer_view cmd_header_view
;
2347 struct lttng_buffer_view cmd_payload_view
;
2348 struct lttng_buffer_view flat_events_view
;
2349 struct lttng_buffer_view ext_view
;
2351 /* Safety check. An handle and channel name are mandatory */
2352 if (handle
== NULL
|| channel_name
== NULL
) {
2353 ret
= -LTTNG_ERR_INVALID
;
2357 lttng_payload_init(&payload
);
2358 lttng_payload_init(&payload_copy
);
2360 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2361 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2362 sizeof(lsm
.session
.name
));
2364 ret
= -LTTNG_ERR_INVALID
;
2368 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2369 sizeof(lsm
.u
.list
.channel_name
));
2371 ret
= -LTTNG_ERR_INVALID
;
2375 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2377 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2383 * A copy of the payload is performed since it will be
2384 * consumed twice. Consuming the same payload twice is invalid since
2385 * it will cause any received file descriptor to become "shared"
2386 * between different instances of the resulting objects.
2388 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2390 ret
= -LTTNG_ERR_NOMEM
;
2394 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2395 &payload
.buffer
, 0, sizeof(*cmd_header
));
2396 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2397 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2401 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2403 /* Set number of events and free command header */
2404 nb_events
= cmd_header
->nb_events
;
2405 if (nb_events
> INT_MAX
) {
2406 ret
= -LTTNG_ERR_OVERFLOW
;
2410 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2411 &payload
.buffer
, sizeof(*cmd_header
), -1);
2414 * The buffer that is returned must contain a "flat" version of
2415 * the events that are returned. In other words, all pointers
2416 * within an lttng_event must point to a location within the returned
2417 * buffer so that the user may free everything by simply calling free()
2418 * on the returned buffer. This is needed in order to maintain API
2421 * A first pass is performed to compute the size of the buffer that
2422 * must be allocated. A second pass is then performed to setup
2423 * the returned events so that their members always point within the
2426 * The layout of the returned buffer is as follows:
2427 * - struct lttng_event[nb_events],
2428 * - nb_events times the following:
2429 * - struct lttng_event_extended,
2430 * - flattened version of userspace_probe_location
2431 * - filter_expression
2433 * - padding to align to 64-bits
2435 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2436 nb_events
* sizeof(struct lttng_event
), -1);
2437 comm_ext_at
= ext_view
.data
;
2438 storage_req
= nb_events
* sizeof(struct lttng_event
);
2440 struct lttng_payload_view payload_view
=
2441 lttng_payload_view_from_payload(&payload
, 0, -1);
2443 for (i
= 0; i
< nb_events
; i
++) {
2444 const struct lttcomm_event_extended_header
*ext_comm
=
2445 (struct lttcomm_event_extended_header
*)
2447 int probe_storage_req
= 0;
2449 comm_ext_at
+= sizeof(*ext_comm
);
2450 comm_ext_at
+= ext_comm
->filter_len
;
2451 comm_ext_at
+= ext_comm
->nb_exclusions
*
2452 LTTNG_SYMBOL_NAME_LEN
;
2454 if (ext_comm
->userspace_probe_location_len
) {
2455 struct lttng_userspace_probe_location
2456 *probe_location
= NULL
;
2457 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2459 (const char *) comm_ext_at
-
2460 payload_view
.buffer
.data
,
2461 ext_comm
->userspace_probe_location_len
);
2463 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2464 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2469 * Create a temporary userspace probe location
2470 * to determine the size needed by a "flattened"
2471 * version of that same probe location.
2473 ret
= lttng_userspace_probe_location_create_from_payload(
2474 &probe_location_view
,
2477 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2481 ret
= lttng_userspace_probe_location_flatten(
2482 probe_location
, NULL
);
2483 lttng_userspace_probe_location_destroy(
2486 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2490 probe_storage_req
= ret
;
2491 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2494 storage_req
+= sizeof(struct lttng_event_extended
);
2495 storage_req
+= ext_comm
->filter_len
;
2496 storage_req
+= ext_comm
->nb_exclusions
*
2497 LTTNG_SYMBOL_NAME_LEN
;
2498 /* Padding to ensure the flat probe is aligned. */
2499 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
2500 storage_req
+= probe_storage_req
;
2504 lttng_dynamic_buffer_init(&listing
);
2506 * We must ensure that "listing" is never resized so as to preserve
2507 * the validity of the flattened objects.
2509 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2511 ret
= -LTTNG_ERR_NOMEM
;
2515 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2516 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2517 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2518 nb_events
* sizeof(struct lttng_event
));
2519 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2521 ret
= -LTTNG_ERR_NOMEM
;
2522 goto free_dynamic_buffer
;
2525 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2526 nb_events
* sizeof(struct lttng_event
), -1);
2527 comm_ext_at
= ext_view
.data
;
2530 struct lttng_payload_view payload_copy_view
=
2531 lttng_payload_view_from_payload(
2532 &payload_copy
, 0, -1);
2534 for (i
= 0; i
< nb_events
; i
++) {
2535 struct lttng_event
*event
= (typeof(event
))(
2537 (sizeof(struct lttng_event
) * i
));
2538 const struct lttcomm_event_extended_header
*ext_comm
=
2539 (typeof(ext_comm
)) comm_ext_at
;
2540 struct lttng_event_extended
*event_extended
=
2541 (typeof(event_extended
))(listing
.data
+
2544 /* Insert struct lttng_event_extended. */
2545 ret
= lttng_dynamic_buffer_set_size(&listing
,
2546 listing
.size
+ sizeof(*event_extended
));
2548 ret
= -LTTNG_ERR_NOMEM
;
2549 goto free_dynamic_buffer
;
2551 event
->extended
.ptr
= event_extended
;
2553 comm_ext_at
+= sizeof(*ext_comm
);
2555 /* Insert filter expression. */
2556 if (ext_comm
->filter_len
) {
2557 event_extended
->filter_expression
=
2558 listing
.data
+ listing
.size
;
2559 ret
= lttng_dynamic_buffer_append(&listing
,
2561 ext_comm
->filter_len
);
2563 ret
= -LTTNG_ERR_NOMEM
;
2564 goto free_dynamic_buffer
;
2566 comm_ext_at
+= ext_comm
->filter_len
;
2569 /* Insert exclusions. */
2570 if (ext_comm
->nb_exclusions
) {
2571 event_extended
->exclusions
.count
=
2572 ext_comm
->nb_exclusions
;
2573 event_extended
->exclusions
.strings
=
2574 listing
.data
+ listing
.size
;
2576 ret
= lttng_dynamic_buffer_append(&listing
,
2578 ext_comm
->nb_exclusions
*
2579 LTTNG_SYMBOL_NAME_LEN
);
2581 ret
= -LTTNG_ERR_NOMEM
;
2582 goto free_dynamic_buffer
;
2584 comm_ext_at
+= ext_comm
->nb_exclusions
*
2585 LTTNG_SYMBOL_NAME_LEN
;
2588 /* Insert padding to align to 64-bits. */
2589 ret
= lttng_dynamic_buffer_set_size(&listing
,
2590 lttng_align_ceil(listing
.size
,
2593 ret
= -LTTNG_ERR_NOMEM
;
2594 goto free_dynamic_buffer
;
2597 /* Insert flattened userspace probe location. */
2598 if (ext_comm
->userspace_probe_location_len
) {
2599 struct lttng_userspace_probe_location
2600 *probe_location
= NULL
;
2601 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2603 (const char *) comm_ext_at
-
2604 payload_copy_view
.buffer
.data
,
2605 ext_comm
->userspace_probe_location_len
);
2607 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2608 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2609 goto free_dynamic_buffer
;
2612 ret
= lttng_userspace_probe_location_create_from_payload(
2613 &probe_location_view
,
2616 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2617 goto free_dynamic_buffer
;
2620 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2623 ret
= lttng_userspace_probe_location_flatten(
2624 probe_location
, &listing
);
2625 lttng_userspace_probe_location_destroy(
2628 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2629 goto free_dynamic_buffer
;
2632 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2637 /* Don't reset listing buffer as we return its content. */
2638 *events
= (struct lttng_event
*) listing
.data
;
2639 lttng_dynamic_buffer_init(&listing
);
2640 ret
= (int) nb_events
;
2641 free_dynamic_buffer
:
2642 lttng_dynamic_buffer_reset(&listing
);
2644 lttng_payload_reset(&payload
);
2645 lttng_payload_reset(&payload_copy
);
2650 * Sets the tracing_group variable with name.
2651 * This function allocates memory pointed to by tracing_group.
2652 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2654 int lttng_set_tracing_group(const char *name
)
2660 ret
= -LTTNG_ERR_INVALID
;
2664 new_group
= strdup(name
);
2666 ret
= -LTTNG_ERR_FATAL
;
2670 free(tracing_group
);
2671 tracing_group
= new_group
;
2678 int lttng_calibrate(struct lttng_handle
*handle
,
2679 struct lttng_calibrate
*calibrate
)
2682 * This command was removed in LTTng 2.9.
2684 return -LTTNG_ERR_UND
;
2688 * Set default channel attributes.
2689 * If either or both of the arguments are null, attr content is zeroe'd.
2691 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2692 struct lttng_channel_attr
*attr
)
2694 struct lttng_channel_extended
*extended
;
2697 if (attr
== NULL
|| domain
== NULL
) {
2701 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2702 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2704 /* Same for all domains. */
2705 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2706 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2707 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2709 switch (domain
->type
) {
2710 case LTTNG_DOMAIN_KERNEL
:
2711 attr
->switch_timer_interval
=
2712 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2713 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2714 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2715 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2716 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2718 extended
->monitor_timer_interval
=
2719 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2720 extended
->blocking_timeout
=
2721 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2724 case LTTNG_DOMAIN_UST
:
2725 switch (domain
->buf_type
) {
2726 case LTTNG_BUFFER_PER_UID
:
2727 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2728 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2729 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2730 attr
->switch_timer_interval
=
2731 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2732 attr
->read_timer_interval
=
2733 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2735 extended
->monitor_timer_interval
=
2736 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2737 extended
->blocking_timeout
=
2738 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2741 case LTTNG_BUFFER_PER_PID
:
2743 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2744 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2745 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2746 attr
->switch_timer_interval
=
2747 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2748 attr
->read_timer_interval
=
2749 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2751 extended
->monitor_timer_interval
=
2752 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2753 extended
->blocking_timeout
=
2754 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2759 /* Default behavior: leave set to 0. */
2763 attr
->extended
.ptr
= extended
;
2766 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2767 uint64_t *discarded_events
)
2770 struct lttng_channel_extended
*chan_ext
;
2772 if (!channel
|| !discarded_events
) {
2773 ret
= -LTTNG_ERR_INVALID
;
2777 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2780 * This can happen since the lttng_channel structure is
2781 * used for other tasks where this pointer is never set.
2783 *discarded_events
= 0;
2787 *discarded_events
= chan_ext
->discarded_events
;
2792 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2793 uint64_t *lost_packets
)
2796 struct lttng_channel_extended
*chan_ext
;
2798 if (!channel
|| !lost_packets
) {
2799 ret
= -LTTNG_ERR_INVALID
;
2803 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2806 * This can happen since the lttng_channel structure is
2807 * used for other tasks where this pointer is never set.
2813 *lost_packets
= chan_ext
->lost_packets
;
2818 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2819 uint64_t *monitor_timer_interval
)
2823 if (!chan
|| !monitor_timer_interval
) {
2824 ret
= -LTTNG_ERR_INVALID
;
2828 if (!chan
->attr
.extended
.ptr
) {
2829 ret
= -LTTNG_ERR_INVALID
;
2833 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2834 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2839 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2840 uint64_t monitor_timer_interval
)
2844 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2845 ret
= -LTTNG_ERR_INVALID
;
2849 ((struct lttng_channel_extended
*)
2850 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2851 monitor_timer_interval
;
2856 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2857 int64_t *blocking_timeout
)
2861 if (!chan
|| !blocking_timeout
) {
2862 ret
= -LTTNG_ERR_INVALID
;
2866 if (!chan
->attr
.extended
.ptr
) {
2867 ret
= -LTTNG_ERR_INVALID
;
2871 *blocking_timeout
= ((struct lttng_channel_extended
*)
2872 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2877 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2878 int64_t blocking_timeout
)
2881 int64_t msec_timeout
;
2883 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2884 ret
= -LTTNG_ERR_INVALID
;
2888 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2889 ret
= -LTTNG_ERR_INVALID
;
2894 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2895 * us to accept a narrower range of values (msecs expressed as a signed
2898 msec_timeout
= blocking_timeout
/ 1000;
2899 if (msec_timeout
!= (int32_t) msec_timeout
) {
2900 ret
= -LTTNG_ERR_INVALID
;
2904 ((struct lttng_channel_extended
*)
2905 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2912 * Check if session daemon is alive.
2914 * Return 1 if alive or 0 if not.
2915 * On error returns a negative value.
2917 int lttng_session_daemon_alive(void)
2921 ret
= set_session_daemon_path();
2927 if (*sessiond_sock_path
== '\0') {
2929 * No socket path set. Weird error which means the constructor
2935 ret
= try_connect_sessiond(sessiond_sock_path
);
2946 * Set URL for a consumer for a session and domain.
2948 * Return 0 on success, else a negative value.
2950 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2951 const char *control_url
, const char *data_url
)
2955 struct lttcomm_session_msg lsm
;
2956 struct lttng_uri
*uris
= NULL
;
2958 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2959 ret
= -LTTNG_ERR_INVALID
;
2963 memset(&lsm
, 0, sizeof(lsm
));
2965 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2967 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2968 sizeof(lsm
.session
.name
));
2970 ret
= -LTTNG_ERR_INVALID
;
2974 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2976 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2978 ret
= -LTTNG_ERR_INVALID
;
2982 lsm
.u
.uri
.size
= size
;
2984 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2985 sizeof(struct lttng_uri
) * size
, NULL
);
2996 LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2997 int lttng_enable_consumer(struct lttng_handle
*handle
)
3006 LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
3007 int lttng_disable_consumer(struct lttng_handle
*handle
)
3016 LTTNG_EXPORT
int _lttng_create_session_ext(const char *name
, const char *url
,
3017 const char *datetime
);
3018 int _lttng_create_session_ext(const char *name
, const char *url
,
3019 const char *datetime
)
3025 * For a given session name, this call checks if the data is ready to be read
3026 * or is still being extracted by the consumer(s) hence not ready to be used by
3029 int lttng_data_pending(const char *session_name
)
3032 struct lttcomm_session_msg lsm
;
3033 uint8_t *pending
= NULL
;
3035 if (session_name
== NULL
) {
3036 return -LTTNG_ERR_INVALID
;
3039 memset(&lsm
, 0, sizeof(lsm
));
3040 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
3042 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3043 sizeof(lsm
.session
.name
));
3045 ret
= -LTTNG_ERR_INVALID
;
3049 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
3052 } else if (ret
!= 1) {
3053 /* Unexpected payload size */
3054 ret
= -LTTNG_ERR_INVALID
;
3056 } else if (!pending
) {
3057 /* Internal error. */
3058 ret
= -LTTNG_ERR_UNK
;
3062 ret
= (int) *pending
;
3069 * Regenerate the metadata for a session.
3070 * Return 0 on success, a negative error code on error.
3072 int lttng_regenerate_metadata(const char *session_name
)
3075 struct lttcomm_session_msg lsm
;
3077 if (!session_name
) {
3078 ret
= -LTTNG_ERR_INVALID
;
3082 memset(&lsm
, 0, sizeof(lsm
));
3083 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3085 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3086 sizeof(lsm
.session
.name
));
3088 ret
= -LTTNG_ERR_INVALID
;
3092 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3103 * Deprecated, replaced by lttng_regenerate_metadata.
3105 int lttng_metadata_regenerate(const char *session_name
)
3107 return lttng_regenerate_metadata(session_name
);
3111 * Regenerate the statedump of a session.
3112 * Return 0 on success, a negative error code on error.
3114 int lttng_regenerate_statedump(const char *session_name
)
3117 struct lttcomm_session_msg lsm
;
3119 if (!session_name
) {
3120 ret
= -LTTNG_ERR_INVALID
;
3124 memset(&lsm
, 0, sizeof(lsm
));
3125 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3127 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3128 sizeof(lsm
.session
.name
));
3130 ret
= -LTTNG_ERR_INVALID
;
3134 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3145 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3149 struct lttcomm_session_msg lsm
= {
3150 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3152 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
3153 struct lttcomm_session_msg
*message_lsm
;
3154 struct lttng_payload message
;
3155 struct lttng_payload reply
;
3156 struct lttng_trigger
*reply_trigger
= NULL
;
3157 enum lttng_domain_type domain_type
;
3158 const struct lttng_credentials user_creds
= {
3159 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3160 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3162 const char *unused_trigger_name
= NULL
;
3163 enum lttng_trigger_status trigger_status
;
3165 lttng_payload_init(&message
);
3166 lttng_payload_init(&reply
);
3169 ret
= -LTTNG_ERR_INVALID
;
3173 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3174 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3175 /* Re-using already registered trigger. */
3176 ret
= -LTTNG_ERR_INVALID
;
3181 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3182 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3183 ret
= -LTTNG_ERR_NOMEM
;
3188 if (!trigger
->creds
.uid
.is_set
) {
3189 /* Use the client's credentials as the trigger credentials. */
3190 lttng_trigger_set_credentials(trigger
, &user_creds
);
3193 * Validate that either the current trigger credentials and the
3194 * client credentials are identical or that the current user is
3195 * root. The root user can register, unregister triggers for
3196 * himself and other users.
3198 * This check is also present on the sessiond side, using the
3199 * credentials passed on the socket. These check are all
3202 const struct lttng_credentials
*trigger_creds
=
3203 lttng_trigger_get_credentials(trigger
);
3205 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3206 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3207 ret
= -LTTNG_ERR_EPERM
;
3208 goto end_unset_name
;
3213 if (!lttng_trigger_validate(trigger
)) {
3214 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3215 goto end_unset_name
;
3218 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3221 lsm
.domain
.type
= domain_type
;
3223 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3225 ret
= -LTTNG_ERR_NOMEM
;
3226 goto end_unset_name
;
3229 ret
= lttng_trigger_serialize(trigger
, &message
);
3231 ret
= -LTTNG_ERR_UNK
;
3232 goto end_unset_name
;
3236 * This is needed to populate the trigger object size for the command
3239 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3241 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3244 struct lttng_payload_view message_view
=
3245 lttng_payload_view_from_payload(
3248 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3250 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3252 goto end_unset_name
;
3257 struct lttng_payload_view reply_view
=
3258 lttng_payload_view_from_payload(
3259 &reply
, 0, reply
.buffer
.size
);
3261 ret
= lttng_trigger_create_from_payload(
3262 &reply_view
, &reply_trigger
);
3264 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3265 goto end_unset_name
;
3269 if (name
|| generate_name
) {
3270 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3272 ret
= -LTTNG_ERR_NOMEM
;
3281 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3282 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3283 ret
= -LTTNG_ERR_UNK
;
3286 lttng_payload_reset(&message
);
3287 lttng_payload_reset(&reply
);
3288 lttng_trigger_destroy(reply_trigger
);
3292 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3294 /* Register an anonymous trigger. */
3295 return _lttng_register_trigger(trigger
, NULL
, false);
3298 enum lttng_error_code
lttng_register_trigger_with_name(
3299 struct lttng_trigger
*trigger
, const char *name
)
3301 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3303 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3306 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3307 struct lttng_trigger
*trigger
)
3309 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3311 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3314 enum lttng_error_code
lttng_error_query_execute(
3315 const struct lttng_error_query
*query
,
3316 const struct lttng_endpoint
*endpoint
,
3317 struct lttng_error_query_results
**results
)
3320 enum lttng_error_code ret_code
;
3321 struct lttcomm_session_msg lsm
= {
3322 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3324 struct lttng_payload message
;
3325 struct lttng_payload reply
;
3326 struct lttcomm_session_msg
*message_lsm
;
3328 lttng_payload_init(&message
);
3329 lttng_payload_init(&reply
);
3331 if (!query
|| !results
) {
3332 ret_code
= LTTNG_ERR_INVALID
;
3336 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3337 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3341 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3343 ret_code
= LTTNG_ERR_NOMEM
;
3347 ret
= lttng_error_query_serialize(query
, &message
);
3349 ret_code
= LTTNG_ERR_UNK
;
3353 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3354 message_lsm
->u
.error_query
.length
=
3355 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3358 struct lttng_payload_view message_view
=
3359 lttng_payload_view_from_payload(
3362 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3364 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3366 ret_code
=(lttng_error_code
) -ret
;
3372 ssize_t reply_create_ret
;
3373 struct lttng_payload_view reply_view
=
3374 lttng_payload_view_from_payload(
3375 &reply
, 0, reply
.buffer
.size
);
3377 reply_create_ret
= lttng_error_query_results_create_from_payload(
3378 &reply_view
, results
);
3379 if (reply_create_ret
< 0) {
3380 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3385 ret_code
= LTTNG_OK
;
3387 lttng_payload_reset(&message
);
3388 lttng_payload_reset(&reply
);
3392 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3395 struct lttcomm_session_msg lsm
;
3396 struct lttcomm_session_msg
*message_lsm
;
3397 struct lttng_payload message
;
3398 struct lttng_payload reply
;
3399 struct lttng_trigger
*copy
= NULL
;
3400 const struct lttng_credentials user_creds
= {
3401 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3402 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3405 lttng_payload_init(&message
);
3406 lttng_payload_init(&reply
);
3409 ret
= -LTTNG_ERR_INVALID
;
3413 copy
= lttng_trigger_copy(trigger
);
3415 ret
= -LTTNG_ERR_UNK
;
3419 if (!copy
->creds
.uid
.is_set
) {
3420 /* Use the client credentials as the trigger credentials */
3421 lttng_trigger_set_credentials(copy
, &user_creds
);
3424 * Validate that either the current trigger credentials and the
3425 * client credentials are identical or that the current user is
3426 * root. The root user can register, unregister triggers for
3427 * himself and other users.
3429 * This check is also present on the sessiond side, using the
3430 * credentials passed on the socket. These check are all
3433 const struct lttng_credentials
*trigger_creds
=
3434 lttng_trigger_get_credentials(copy
);
3435 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3436 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3437 ret
= -LTTNG_ERR_EPERM
;
3443 if (!lttng_trigger_validate(copy
)) {
3444 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3448 memset(&lsm
, 0, sizeof(lsm
));
3449 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3451 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3453 ret
= -LTTNG_ERR_NOMEM
;
3457 ret
= lttng_trigger_serialize(copy
, &message
);
3459 ret
= -LTTNG_ERR_UNK
;
3464 * This is needed to populate the trigger object size for the command
3465 * header and number of fds sent.
3467 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3469 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3472 struct lttng_payload_view message_view
=
3473 lttng_payload_view_from_payload(
3477 * Update the message header with the number of fd that will be
3480 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3483 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3491 lttng_trigger_destroy(copy
);
3492 lttng_payload_reset(&message
);
3493 lttng_payload_reset(&reply
);
3498 * Ask the session daemon for all registered triggers for the current user.
3500 * Allocates and return an lttng_triggers set.
3501 * On error, returns a suitable lttng_error_code.
3503 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3506 enum lttng_error_code ret_code
= LTTNG_OK
;
3507 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3508 struct lttng_triggers
*local_triggers
= NULL
;
3509 struct lttng_payload reply
;
3510 struct lttng_payload_view lsm_view
=
3511 lttng_payload_view_init_from_buffer(
3512 (const char *) &lsm
, 0, sizeof(lsm
));
3514 lttng_payload_init(&reply
);
3516 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3518 ret_code
= (enum lttng_error_code
) -ret
;
3523 struct lttng_payload_view reply_view
=
3524 lttng_payload_view_from_payload(
3525 &reply
, 0, reply
.buffer
.size
);
3527 ret
= lttng_triggers_create_from_payload(
3528 &reply_view
, &local_triggers
);
3530 ret_code
= LTTNG_ERR_FATAL
;
3535 *triggers
= local_triggers
;
3536 local_triggers
= NULL
;
3538 lttng_payload_reset(&reply
);
3539 lttng_triggers_destroy(local_triggers
);
3546 static void __attribute__((constructor
)) init(void)
3548 /* Set default session group */
3549 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3555 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3557 free(tracing_group
);