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
= { .cmd_type
= LTTNG_ENABLE_EVENT
};
1105 struct lttng_payload payload
;
1107 unsigned int free_filter_expression
= 0;
1108 struct filter_parser_ctx
*ctx
= NULL
;
1109 size_t bytecode_len
= 0;
1112 * We have either a filter or some exclusions, so we need to set up
1113 * a variable-length payload from where to send the data.
1115 lttng_payload_init(&payload
);
1118 * Cast as non-const since we may replace the filter expression
1119 * by a dynamically allocated string. Otherwise, the original
1120 * string is not modified.
1122 char *filter_expression
= (char *) original_filter_expression
;
1124 if (handle
== NULL
|| ev
== NULL
) {
1125 ret
= -LTTNG_ERR_INVALID
;
1130 * Empty filter string will always be rejected by the parser
1131 * anyway, so treat this corner-case early to eliminate
1132 * lttng_fmemopen error for 0-byte allocation.
1134 if (filter_expression
&& filter_expression
[0] == '\0') {
1135 ret
= -LTTNG_ERR_INVALID
;
1139 if (ev
->name
[0] == '\0') {
1140 /* Enable all events. */
1141 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1142 LTTNG_ASSERT(ret
== 0);
1145 /* Parse filter expression. */
1146 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1147 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1148 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1149 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1150 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1151 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1154 /* Setup agent filter if needed. */
1155 agent_filter
= set_agent_filter(filter_expression
, ev
);
1156 if (!agent_filter
) {
1157 if (!filter_expression
) {
1159 * No JUL and no filter, just skip
1166 * With an agent filter, the original filter has
1167 * been added to it thus replace the filter
1170 filter_expression
= agent_filter
;
1171 free_filter_expression
= 1;
1175 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1176 LTTNG_FILTER_MAX_LEN
) {
1177 ret
= -LTTNG_ERR_FILTER_INVAL
;
1181 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1186 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1187 sizeof(ctx
->bytecode
->b
);
1188 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1189 ret
= -LTTNG_ERR_FILTER_INVAL
;
1195 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1196 filter_expression
, bytecode_len
,
1197 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1200 ret
= -LTTNG_ERR_INVALID
;
1204 /* If no channel name, send empty string. */
1205 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1206 sizeof(lsm
.u
.enable
.channel_name
));
1208 ret
= -LTTNG_ERR_INVALID
;
1213 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1216 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1217 sizeof(lsm
.session
.name
));
1219 ret
= -LTTNG_ERR_INVALID
;
1223 /* Length of the serialized event. */
1224 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1227 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1229 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1236 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1237 if (fd_count
== 1) {
1238 struct fd_handle
*h
=
1239 lttng_payload_view_pop_fd_handle(&view
);
1245 fd_to_send
= fd_handle_get_fd(h
);
1249 lsm
.fd_count
= fd_count
;
1251 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1252 fd_count
? &fd_to_send
: NULL
, fd_count
,
1253 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1254 view
.buffer
.size
, NULL
, NULL
, 0);
1258 if (filter_expression
&& ctx
) {
1259 filter_bytecode_free(ctx
);
1260 filter_ir_free(ctx
);
1261 filter_parser_ctx_free(ctx
);
1263 if (free_filter_expression
) {
1265 * The filter expression has been replaced and must be freed as
1266 * it is not the original filter expression received as a
1269 free(filter_expression
);
1272 * Return directly to the caller and don't ask the sessiond since
1273 * something went wrong in the parsing of data above.
1275 lttng_payload_reset(&payload
);
1279 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1280 struct lttng_event
*ev
, const char *channel_name
,
1281 const char *original_filter_expression
)
1283 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_DISABLE_EVENT
};
1284 struct lttng_payload payload
;
1286 unsigned int free_filter_expression
= 0;
1287 struct filter_parser_ctx
*ctx
= NULL
;
1288 size_t bytecode_len
= 0;
1291 * We have either a filter or some exclusions, so we need to set up
1292 * a variable-length payload from where to send the data.
1294 lttng_payload_init(&payload
);
1297 * Cast as non-const since we may replace the filter expression
1298 * by a dynamically allocated string. Otherwise, the original
1299 * string is not modified.
1301 char *filter_expression
= (char *) original_filter_expression
;
1303 if (handle
== NULL
|| ev
== NULL
) {
1304 ret
= -LTTNG_ERR_INVALID
;
1309 * Empty filter string will always be rejected by the parser
1310 * anyway, so treat this corner-case early to eliminate
1311 * lttng_fmemopen error for 0-byte allocation.
1313 if (filter_expression
&& filter_expression
[0] == '\0') {
1314 ret
= -LTTNG_ERR_INVALID
;
1318 /* Parse filter expression. */
1319 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1320 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1321 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1322 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1323 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1324 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1327 /* Setup agent filter if needed. */
1328 agent_filter
= set_agent_filter(filter_expression
, ev
);
1329 if (!agent_filter
) {
1330 if (!filter_expression
) {
1332 * No JUL and no filter, just skip
1339 * With an agent filter, the original filter has
1340 * been added to it thus replace the filter
1343 filter_expression
= agent_filter
;
1344 free_filter_expression
= 1;
1348 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1349 LTTNG_FILTER_MAX_LEN
) {
1350 ret
= -LTTNG_ERR_FILTER_INVAL
;
1354 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1359 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1360 sizeof(ctx
->bytecode
->b
);
1361 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1362 ret
= -LTTNG_ERR_FILTER_INVAL
;
1368 ret
= lttng_event_serialize(ev
, 0, NULL
,
1369 filter_expression
, bytecode_len
,
1370 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1373 ret
= -LTTNG_ERR_INVALID
;
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
;
1386 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1389 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1390 sizeof(lsm
.session
.name
));
1392 ret
= -LTTNG_ERR_INVALID
;
1396 /* Length of the serialized event. */
1397 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1400 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1402 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1409 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1410 if (fd_count
== 1) {
1411 struct fd_handle
*h
=
1412 lttng_payload_view_pop_fd_handle(&view
);
1418 fd_to_send
= fd_handle_get_fd(h
);
1422 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1423 fd_count
? &fd_to_send
: NULL
, fd_count
,
1424 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1425 view
.buffer
.size
, NULL
, NULL
, 0);
1429 if (filter_expression
&& ctx
) {
1430 filter_bytecode_free(ctx
);
1431 filter_ir_free(ctx
);
1432 filter_parser_ctx_free(ctx
);
1434 if (free_filter_expression
) {
1436 * The filter expression has been replaced and must be freed as
1437 * it is not the original filter expression received as a
1440 free(filter_expression
);
1443 * Return directly to the caller and don't ask the sessiond since
1444 * something went wrong in the parsing of data above.
1446 lttng_payload_reset(&payload
);
1451 * Disable event(s) of a channel and domain.
1452 * If no event name is specified, all events are disabled.
1453 * If no channel name is specified, the default 'channel0' is used.
1454 * Returns size of returned session payload data or a negative error code.
1456 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1457 const char *channel_name
)
1460 struct lttng_event ev
;
1462 memset(&ev
, 0, sizeof(ev
));
1464 ev
.type
= LTTNG_EVENT_ALL
;
1465 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1467 ret
= -LTTNG_ERR_INVALID
;
1471 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1476 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1478 struct lttng_channel
*channel
= NULL
;
1484 /* Validate domain. */
1485 switch (domain
->type
) {
1486 case LTTNG_DOMAIN_UST
:
1487 switch (domain
->buf_type
) {
1488 case LTTNG_BUFFER_PER_UID
:
1489 case LTTNG_BUFFER_PER_PID
:
1495 case LTTNG_DOMAIN_KERNEL
:
1496 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1504 channel
= lttng_channel_create_internal();
1509 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1514 void lttng_channel_destroy(struct lttng_channel
*channel
)
1520 if (channel
->attr
.extended
.ptr
) {
1521 free(channel
->attr
.extended
.ptr
);
1527 * Enable channel per domain
1528 * Returns size of returned session payload data or a negative error code.
1530 int lttng_enable_channel(struct lttng_handle
*handle
,
1531 struct lttng_channel
*in_chan
)
1533 enum lttng_error_code ret_code
;
1535 struct lttng_dynamic_buffer buffer
;
1536 struct lttcomm_session_msg lsm
;
1537 uint64_t total_buffer_size_needed_per_cpu
= 0;
1538 struct lttng_channel
*channel
= NULL
;
1540 lttng_dynamic_buffer_init(&buffer
);
1542 /* NULL arguments are forbidden. No default values. */
1543 if (handle
== NULL
|| in_chan
== NULL
) {
1544 ret
= -LTTNG_ERR_INVALID
;
1549 * Verify that the amount of memory required to create the requested
1550 * buffer is available on the system at the moment.
1552 if (in_chan
->attr
.num_subbuf
>
1553 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1555 ret
= -LTTNG_ERR_OVERFLOW
;
1559 total_buffer_size_needed_per_cpu
=
1560 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1561 ret_code
= check_enough_available_memory(
1562 total_buffer_size_needed_per_cpu
);
1563 if (ret_code
!= LTTNG_OK
) {
1568 /* Copy the channel for easier manipulation. */
1569 channel
= lttng_channel_copy(in_chan
);
1571 ret
= -LTTNG_ERR_NOMEM
;
1575 /* Populate the channel extended attribute if necessary. */
1576 if (!channel
->attr
.extended
.ptr
) {
1577 struct lttng_channel_extended
*extended
=
1578 (struct lttng_channel_extended
*) zmalloc(
1582 ret
= -LTTNG_ERR_NOMEM
;
1585 lttng_channel_set_default_extended_attr(
1586 &handle
->domain
, extended
);
1587 channel
->attr
.extended
.ptr
= extended
;
1590 /* Prepare the payload */
1591 memset(&lsm
, 0, sizeof(lsm
));
1593 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1594 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1596 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1597 sizeof(lsm
.session
.name
));
1599 ret
= -LTTNG_ERR_INVALID
;
1603 ret
= lttng_channel_serialize(channel
, &buffer
);
1605 ret
= -LTTNG_ERR_FATAL
;
1609 lsm
.u
.channel
.length
= buffer
.size
;
1611 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1612 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1614 lttng_channel_destroy(channel
);
1615 lttng_dynamic_buffer_reset(&buffer
);
1620 * All tracing will be stopped for registered events of the channel.
1621 * Returns size of returned session payload data or a negative error code.
1623 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1626 struct lttcomm_session_msg lsm
;
1628 /* Safety check. Both are mandatory. */
1629 if (handle
== NULL
|| name
== NULL
) {
1630 return -LTTNG_ERR_INVALID
;
1633 memset(&lsm
, 0, sizeof(lsm
));
1635 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1637 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1638 sizeof(lsm
.u
.disable
.channel_name
));
1640 ret
= -LTTNG_ERR_INVALID
;
1644 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1646 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1647 sizeof(lsm
.session
.name
));
1649 ret
= -LTTNG_ERR_INVALID
;
1653 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1659 * Lists all available tracepoints of domain.
1660 * Sets the contents of the events array.
1661 * Returns the number of lttng_event entries in events;
1662 * on error, returns a negative value.
1664 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1665 struct lttng_event
**events
)
1667 enum lttng_error_code ret_code
;
1668 int ret
, total_payload_received
;
1669 char *reception_buffer
= NULL
;
1670 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRACEPOINTS
};
1671 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1672 size_t cmd_header_len
;
1673 unsigned int nb_events
= 0;
1675 if (handle
== NULL
) {
1676 ret
= -LTTNG_ERR_INVALID
;
1680 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1682 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1683 (void **) &reception_buffer
, (void **) &cmd_header
,
1689 total_payload_received
= ret
;
1692 ret
= -LTTNG_ERR_UNK
;
1696 if (cmd_header
->count
> INT_MAX
) {
1697 ret
= -LTTNG_ERR_OVERFLOW
;
1701 nb_events
= (unsigned int) cmd_header
->count
;
1704 struct lttng_buffer_view events_view
=
1705 lttng_buffer_view_init(reception_buffer
, 0,
1706 total_payload_received
);
1707 struct lttng_payload_view events_payload_view
=
1708 lttng_payload_view_from_buffer_view(
1709 &events_view
, 0, -1);
1711 ret_code
= lttng_events_create_and_flatten_from_payload(
1712 &events_payload_view
, nb_events
, events
);
1713 if (ret_code
!= LTTNG_OK
) {
1719 ret
= (int) nb_events
;
1723 free(reception_buffer
);
1728 * Lists all available tracepoint fields of domain.
1729 * Sets the contents of the event field array.
1730 * Returns the number of lttng_event_field entries in events;
1731 * on error, returns a negative value.
1733 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1734 struct lttng_event_field
**fields
)
1737 struct lttcomm_session_msg lsm
;
1739 if (handle
== NULL
) {
1740 return -LTTNG_ERR_INVALID
;
1743 memset(&lsm
, 0, sizeof(lsm
));
1744 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1745 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1747 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1752 return ret
/ sizeof(struct lttng_event_field
);
1756 * Lists all available kernel system calls. Allocates and sets the contents of
1759 * Returns the number of lttng_event entries in events; on error, returns a
1762 int lttng_list_syscalls(struct lttng_event
**events
)
1764 enum lttng_error_code ret_code
;
1765 int ret
, total_payload_received
;
1766 char *reception_buffer
= NULL
;
1767 struct lttcomm_session_msg lsm
= {};
1768 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1769 size_t cmd_header_len
;
1770 uint32_t nb_events
= 0;
1773 ret
= -LTTNG_ERR_INVALID
;
1777 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1778 /* Force kernel domain for system calls. */
1779 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1781 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1782 (void **) &reception_buffer
, (void **) &cmd_header
,
1787 total_payload_received
= ret
;
1790 ret
= -LTTNG_ERR_UNK
;
1794 if (cmd_header
->count
> INT_MAX
) {
1795 ret
= -LTTNG_ERR_OVERFLOW
;
1799 nb_events
= (unsigned int) cmd_header
->count
;
1802 const struct lttng_buffer_view events_view
=
1803 lttng_buffer_view_init(reception_buffer
, 0,
1804 total_payload_received
);
1805 struct lttng_payload_view events_payload_view
=
1806 lttng_payload_view_from_buffer_view(
1807 &events_view
, 0, -1);
1809 ret_code
= lttng_events_create_and_flatten_from_payload(
1810 &events_payload_view
, nb_events
, events
);
1811 if (ret_code
!= LTTNG_OK
) {
1817 ret
= (int) nb_events
;
1820 free(reception_buffer
);
1826 * Returns a human readable string describing
1827 * the error code (positive or negative value).
1829 const char *lttng_strerror(int code
)
1835 return error_get_str(code
);
1838 enum lttng_error_code
lttng_create_session_ext(
1839 struct lttng_session_descriptor
*session_descriptor
)
1841 enum lttng_error_code ret_code
;
1842 struct lttcomm_session_msg lsm
= {
1843 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1846 struct lttng_buffer_view reply_view
;
1848 bool sessiond_must_generate_ouput
;
1849 struct lttng_dynamic_buffer payload
;
1851 size_t descriptor_size
;
1852 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1854 lttng_dynamic_buffer_init(&payload
);
1855 if (!session_descriptor
) {
1856 ret_code
= LTTNG_ERR_INVALID
;
1860 sessiond_must_generate_ouput
=
1861 !lttng_session_descriptor_is_output_destination_initialized(
1862 session_descriptor
);
1863 if (sessiond_must_generate_ouput
) {
1864 const char *home_dir
= utils_get_home_dir();
1865 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1867 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1868 ret_code
= LTTNG_ERR_FATAL
;
1872 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1873 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1876 ret_code
= LTTNG_ERR_NOMEM
;
1881 descriptor_size
= payload
.size
;
1882 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1885 ret_code
= LTTNG_ERR_INVALID
;
1888 descriptor_size
= payload
.size
- descriptor_size
;
1889 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1891 /* Command returns a session descriptor on success. */
1892 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1893 payload
.size
, &reply
);
1894 if (reply_ret
< 0) {
1895 ret_code
= (lttng_error_code
) -reply_ret
;
1897 } else if (reply_ret
== 0) {
1898 /* Socket unexpectedly closed by the session daemon. */
1899 ret_code
= LTTNG_ERR_FATAL
;
1903 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1904 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1907 ret_code
= LTTNG_ERR_FATAL
;
1910 ret_code
= LTTNG_OK
;
1911 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1914 lttng_dynamic_buffer_reset(&payload
);
1915 lttng_session_descriptor_destroy(descriptor_reply
);
1920 * Create a new session using name and url for destination.
1922 * Return 0 on success else a negative LTTng error code.
1924 int lttng_create_session(const char *name
, const char *url
)
1928 struct lttng_uri
*uris
= NULL
;
1929 struct lttng_session_descriptor
*descriptor
= NULL
;
1930 enum lttng_error_code ret_code
;
1933 ret
= -LTTNG_ERR_INVALID
;
1937 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1939 ret
= -LTTNG_ERR_INVALID
;
1944 descriptor
= lttng_session_descriptor_create(name
);
1947 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1948 ret
= -LTTNG_ERR_INVALID
;
1951 descriptor
= lttng_session_descriptor_local_create(name
,
1955 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1959 ret
= -LTTNG_ERR_INVALID
;
1963 ret
= -LTTNG_ERR_INVALID
;
1966 ret_code
= lttng_create_session_ext(descriptor
);
1967 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1969 lttng_session_descriptor_destroy(descriptor
);
1975 * Create a session exclusively used for snapshot.
1977 * Return 0 on success else a negative LTTng error code.
1979 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1982 enum lttng_error_code ret_code
;
1984 struct lttng_uri
*uris
= NULL
;
1985 struct lttng_session_descriptor
*descriptor
= NULL
;
1988 ret
= -LTTNG_ERR_INVALID
;
1992 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1994 ret
= -LTTNG_ERR_INVALID
;
1998 * If the user does not specify a custom subdir, use the session name.
2000 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
2001 strlen(uris
[0].subdir
) == 0) {
2002 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2005 PERROR("Failed to set session name as network destination sub-directory");
2006 ret
= -LTTNG_ERR_FATAL
;
2008 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2009 /* Truncated output. */
2010 ret
= -LTTNG_ERR_INVALID
;
2017 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2020 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2021 ret
= -LTTNG_ERR_INVALID
;
2024 descriptor
= lttng_session_descriptor_snapshot_local_create(
2029 descriptor
= lttng_session_descriptor_snapshot_network_create(
2035 ret
= -LTTNG_ERR_INVALID
;
2039 ret
= -LTTNG_ERR_INVALID
;
2042 ret_code
= lttng_create_session_ext(descriptor
);
2043 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2045 lttng_session_descriptor_destroy(descriptor
);
2051 * Create a session exclusively used for live.
2053 * Return 0 on success else a negative LTTng error code.
2055 int lttng_create_session_live(const char *name
, const char *url
,
2056 unsigned int timer_interval
)
2059 enum lttng_error_code ret_code
;
2060 struct lttng_session_descriptor
*descriptor
= NULL
;
2063 ret
= -LTTNG_ERR_INVALID
;
2068 descriptor
= lttng_session_descriptor_live_network_create(
2069 name
, url
, NULL
, timer_interval
);
2071 descriptor
= lttng_session_descriptor_live_create(
2072 name
, timer_interval
);
2075 ret
= -LTTNG_ERR_INVALID
;
2078 ret_code
= lttng_create_session_ext(descriptor
);
2079 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2081 lttng_session_descriptor_destroy(descriptor
);
2086 * Stop the session and wait for the data before destroying it
2088 * Return 0 on success else a negative LTTng error code.
2090 int lttng_destroy_session(const char *session_name
)
2093 enum lttng_error_code ret_code
;
2094 enum lttng_destruction_handle_status status
;
2095 struct lttng_destruction_handle
*handle
= NULL
;
2098 * Stop the tracing and wait for the data to be
2101 ret
= _lttng_stop_tracing(session_name
, 1);
2102 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2106 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2107 if (ret_code
!= LTTNG_OK
) {
2108 ret
= (int) -ret_code
;
2111 LTTNG_ASSERT(handle
);
2113 /* Block until the completion of the destruction of the session. */
2114 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2115 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2116 ret
= -LTTNG_ERR_UNK
;
2120 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2121 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2122 ret
= -LTTNG_ERR_UNK
;
2125 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2127 lttng_destruction_handle_destroy(handle
);
2132 * Destroy the session without waiting for the data.
2134 int lttng_destroy_session_no_wait(const char *session_name
)
2136 enum lttng_error_code ret_code
;
2138 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2139 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2143 * Ask the session daemon for all available sessions.
2144 * Sets the contents of the sessions array.
2145 * Returns the number of lttng_session entries in sessions;
2146 * on error, returns a negative value.
2148 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2151 struct lttcomm_session_msg lsm
;
2152 const size_t session_size
= sizeof(struct lttng_session
) +
2153 sizeof(struct lttng_session_extended
);
2154 size_t session_count
, i
;
2155 struct lttng_session_extended
*sessions_extended_begin
;
2156 struct lttng_session
*sessions
= NULL
;
2158 memset(&lsm
, 0, sizeof(lsm
));
2159 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2161 * Initialize out_sessions to NULL so it is initialized when
2162 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2163 * be subsequently freed.
2165 *out_sessions
= NULL
;
2166 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2171 ret
= -LTTNG_ERR_FATAL
;
2175 if (ret
% session_size
) {
2176 ret
= -LTTNG_ERR_UNK
;
2180 session_count
= (size_t) ret
/ session_size
;
2181 sessions_extended_begin
= (struct lttng_session_extended
*)
2182 (&sessions
[session_count
]);
2184 /* Set extended session info pointers. */
2185 for (i
= 0; i
< session_count
; i
++) {
2186 struct lttng_session
*session
= &sessions
[i
];
2187 struct lttng_session_extended
*extended
=
2188 &(sessions_extended_begin
[i
]);
2190 session
->extended
.ptr
= extended
;
2193 ret
= (int) session_count
;
2194 *out_sessions
= sessions
;
2199 enum lttng_error_code
lttng_session_get_creation_time(
2200 const struct lttng_session
*session
, uint64_t *creation_time
)
2202 enum lttng_error_code ret
= LTTNG_OK
;
2203 struct lttng_session_extended
*extended
;
2205 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2206 ret
= LTTNG_ERR_INVALID
;
2210 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2211 if (!extended
->creation_time
.is_set
) {
2212 /* Not created on the session daemon yet. */
2213 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2216 *creation_time
= extended
->creation_time
.value
;
2221 int lttng_set_session_shm_path(const char *session_name
,
2222 const char *shm_path
)
2225 struct lttcomm_session_msg lsm
;
2227 if (session_name
== NULL
) {
2228 return -LTTNG_ERR_INVALID
;
2231 memset(&lsm
, 0, sizeof(lsm
));
2232 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2234 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2235 sizeof(lsm
.session
.name
));
2237 ret
= -LTTNG_ERR_INVALID
;
2241 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2242 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2244 ret
= -LTTNG_ERR_INVALID
;
2248 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2254 * Ask the session daemon for all available domains of a session.
2255 * Sets the contents of the domains array.
2256 * Returns the number of lttng_domain entries in domains;
2257 * on error, returns a negative value.
2259 int lttng_list_domains(const char *session_name
,
2260 struct lttng_domain
**domains
)
2263 struct lttcomm_session_msg lsm
;
2265 if (session_name
== NULL
) {
2266 ret
= -LTTNG_ERR_INVALID
;
2270 memset(&lsm
, 0, sizeof(lsm
));
2271 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2273 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2274 sizeof(lsm
.session
.name
));
2276 ret
= -LTTNG_ERR_INVALID
;
2280 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2285 return ret
/ sizeof(struct lttng_domain
);
2291 * Ask the session daemon for all available channels of a session.
2292 * Sets the contents of the channels array.
2293 * Returns the number of lttng_channel entries in channels;
2294 * on error, returns a negative value.
2296 int lttng_list_channels(struct lttng_handle
*handle
,
2297 struct lttng_channel
**channels
)
2299 int ret
, total_payload_received
;
2300 struct lttcomm_session_msg lsm
;
2301 char *reception_buffer
= NULL
;
2302 size_t cmd_header_len
= 0;
2303 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2304 struct lttng_dynamic_buffer tmp_buffer
;
2306 lttng_dynamic_buffer_init(&tmp_buffer
);
2308 if (handle
== NULL
) {
2309 ret
= -LTTNG_ERR_INVALID
;
2313 memset(&lsm
, 0, sizeof(lsm
));
2314 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2315 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2316 sizeof(lsm
.session
.name
));
2318 ret
= -LTTNG_ERR_INVALID
;
2322 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2324 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2325 (void **) &reception_buffer
, (void **) &cmd_header
,
2331 total_payload_received
= ret
;
2333 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2334 ret
= -LTTNG_ERR_FATAL
;
2339 ret
= LTTNG_ERR_UNK
;
2343 if (cmd_header
->count
> INT_MAX
) {
2344 ret
= -LTTNG_ERR_OVERFLOW
;
2349 enum lttng_error_code ret_code
;
2350 const struct lttng_buffer_view events_view
=
2351 lttng_buffer_view_init(reception_buffer
, 0,
2352 total_payload_received
);
2354 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2355 &events_view
, cmd_header
->count
, channels
);
2356 if (ret_code
!= LTTNG_OK
) {
2362 ret
= (int) cmd_header
->count
;
2365 free(reception_buffer
);
2370 * Ask the session daemon for all available events of a session channel.
2371 * Sets the contents of the events array.
2372 * Returns the number of lttng_event entries in events;
2373 * on error, returns a negative value.
2375 int lttng_list_events(struct lttng_handle
*handle
,
2376 const char *channel_name
, struct lttng_event
**events
)
2379 struct lttcomm_session_msg lsm
= {};
2380 struct lttng_payload reply
;
2381 struct lttng_payload_view lsm_view
=
2382 lttng_payload_view_init_from_buffer(
2383 (const char *) &lsm
, 0, sizeof(lsm
));
2384 unsigned int nb_events
= 0;
2386 /* Safety check. An handle and channel name are mandatory. */
2387 if (handle
== NULL
|| channel_name
== NULL
) {
2388 ret
= -LTTNG_ERR_INVALID
;
2392 lttng_payload_init(&reply
);
2394 /* Initialize command parameters. */
2395 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2396 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2397 sizeof(lsm
.session
.name
));
2399 ret
= -LTTNG_ERR_INVALID
;
2403 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2404 sizeof(lsm
.u
.list
.channel_name
));
2406 ret
= -LTTNG_ERR_INVALID
;
2410 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2412 /* Execute command against the session daemon. */
2413 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2419 const struct lttcomm_list_command_header
*cmd_reply_header
=
2421 const lttng_payload_view cmd_reply_header_view
=
2422 lttng_payload_view_from_payload(&reply
, 0,
2423 sizeof(*cmd_reply_header
));
2425 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2426 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2430 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2431 cmd_reply_header_view
.buffer
2433 if (cmd_reply_header
->count
> INT_MAX
) {
2434 ret
= -LTTNG_ERR_OVERFLOW
;
2438 nb_events
= (unsigned int) cmd_reply_header
->count
;
2442 enum lttng_error_code ret_code
;
2443 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2445 sizeof(struct lttcomm_list_command_header
), -1);
2447 ret_code
= lttng_events_create_and_flatten_from_payload(
2448 &cmd_reply_payload
, nb_events
, events
);
2449 if (ret_code
!= LTTNG_OK
) {
2450 ret
= -((int) ret_code
);
2455 ret
= (int) nb_events
;
2457 lttng_payload_reset(&reply
);
2462 * Sets the tracing_group variable with name.
2463 * This function allocates memory pointed to by tracing_group.
2464 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2466 int lttng_set_tracing_group(const char *name
)
2472 ret
= -LTTNG_ERR_INVALID
;
2476 new_group
= strdup(name
);
2478 ret
= -LTTNG_ERR_FATAL
;
2482 free(tracing_group
);
2483 tracing_group
= new_group
;
2490 int lttng_calibrate(struct lttng_handle
*handle
,
2491 struct lttng_calibrate
*calibrate
)
2494 * This command was removed in LTTng 2.9.
2496 return -LTTNG_ERR_UND
;
2500 * Set default channel attributes.
2501 * If either or both of the arguments are null, attr content is zeroe'd.
2503 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2504 struct lttng_channel_attr
*attr
)
2506 struct lttng_channel_extended
*extended
;
2509 if (attr
== NULL
|| domain
== NULL
) {
2513 /* Save the pointer for later use */
2514 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2515 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2517 /* Same for all domains. */
2518 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2519 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2520 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2522 switch (domain
->type
) {
2523 case LTTNG_DOMAIN_KERNEL
:
2524 attr
->switch_timer_interval
=
2525 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2526 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2527 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2528 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2529 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2531 case LTTNG_DOMAIN_UST
:
2532 switch (domain
->buf_type
) {
2533 case LTTNG_BUFFER_PER_UID
:
2534 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2535 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2536 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2537 attr
->switch_timer_interval
=
2538 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2539 attr
->read_timer_interval
=
2540 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2542 case LTTNG_BUFFER_PER_PID
:
2544 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2545 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2546 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2547 attr
->switch_timer_interval
=
2548 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2549 attr
->read_timer_interval
=
2550 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2554 /* Default behavior: leave set to 0. */
2559 lttng_channel_set_default_extended_attr(domain
, extended
);
2562 /* Reassign the extended pointer. */
2563 attr
->extended
.ptr
= extended
;
2566 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2567 uint64_t *discarded_events
)
2570 struct lttng_channel_extended
*chan_ext
;
2572 if (!channel
|| !discarded_events
) {
2573 ret
= -LTTNG_ERR_INVALID
;
2577 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2580 * This can happen since the lttng_channel structure is
2581 * used for other tasks where this pointer is never set.
2583 *discarded_events
= 0;
2587 *discarded_events
= chan_ext
->discarded_events
;
2592 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2593 uint64_t *lost_packets
)
2596 struct lttng_channel_extended
*chan_ext
;
2598 if (!channel
|| !lost_packets
) {
2599 ret
= -LTTNG_ERR_INVALID
;
2603 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2606 * This can happen since the lttng_channel structure is
2607 * used for other tasks where this pointer is never set.
2613 *lost_packets
= chan_ext
->lost_packets
;
2618 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2619 uint64_t *monitor_timer_interval
)
2623 if (!chan
|| !monitor_timer_interval
) {
2624 ret
= -LTTNG_ERR_INVALID
;
2628 if (!chan
->attr
.extended
.ptr
) {
2629 ret
= -LTTNG_ERR_INVALID
;
2633 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2634 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2639 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2640 uint64_t monitor_timer_interval
)
2644 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2645 ret
= -LTTNG_ERR_INVALID
;
2649 ((struct lttng_channel_extended
*)
2650 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2651 monitor_timer_interval
;
2656 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2657 int64_t *blocking_timeout
)
2661 if (!chan
|| !blocking_timeout
) {
2662 ret
= -LTTNG_ERR_INVALID
;
2666 if (!chan
->attr
.extended
.ptr
) {
2667 ret
= -LTTNG_ERR_INVALID
;
2671 *blocking_timeout
= ((struct lttng_channel_extended
*)
2672 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2677 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2678 int64_t blocking_timeout
)
2681 int64_t msec_timeout
;
2683 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2684 ret
= -LTTNG_ERR_INVALID
;
2688 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2689 ret
= -LTTNG_ERR_INVALID
;
2694 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2695 * us to accept a narrower range of values (msecs expressed as a signed
2698 msec_timeout
= blocking_timeout
/ 1000;
2699 if (msec_timeout
!= (int32_t) msec_timeout
) {
2700 ret
= -LTTNG_ERR_INVALID
;
2704 ((struct lttng_channel_extended
*)
2705 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2712 * Check if session daemon is alive.
2714 * Return 1 if alive or 0 if not.
2715 * On error returns a negative value.
2717 int lttng_session_daemon_alive(void)
2721 ret
= set_session_daemon_path();
2727 if (*sessiond_sock_path
== '\0') {
2729 * No socket path set. Weird error which means the constructor
2735 ret
= try_connect_sessiond(sessiond_sock_path
);
2746 * Set URL for a consumer for a session and domain.
2748 * Return 0 on success, else a negative value.
2750 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2751 const char *control_url
, const char *data_url
)
2755 struct lttcomm_session_msg lsm
;
2756 struct lttng_uri
*uris
= NULL
;
2758 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2759 ret
= -LTTNG_ERR_INVALID
;
2763 memset(&lsm
, 0, sizeof(lsm
));
2765 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2767 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2768 sizeof(lsm
.session
.name
));
2770 ret
= -LTTNG_ERR_INVALID
;
2774 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2776 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2778 ret
= -LTTNG_ERR_INVALID
;
2782 lsm
.u
.uri
.size
= size
;
2784 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2785 sizeof(struct lttng_uri
) * size
, NULL
);
2796 LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2797 int lttng_enable_consumer(struct lttng_handle
*handle
)
2806 LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2807 int lttng_disable_consumer(struct lttng_handle
*handle
)
2816 LTTNG_EXPORT
int _lttng_create_session_ext(const char *name
, const char *url
,
2817 const char *datetime
);
2818 int _lttng_create_session_ext(const char *name
, const char *url
,
2819 const char *datetime
)
2825 * For a given session name, this call checks if the data is ready to be read
2826 * or is still being extracted by the consumer(s) hence not ready to be used by
2829 int lttng_data_pending(const char *session_name
)
2832 struct lttcomm_session_msg lsm
;
2833 uint8_t *pending
= NULL
;
2835 if (session_name
== NULL
) {
2836 return -LTTNG_ERR_INVALID
;
2839 memset(&lsm
, 0, sizeof(lsm
));
2840 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2842 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2843 sizeof(lsm
.session
.name
));
2845 ret
= -LTTNG_ERR_INVALID
;
2849 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2852 } else if (ret
!= 1) {
2853 /* Unexpected payload size */
2854 ret
= -LTTNG_ERR_INVALID
;
2856 } else if (!pending
) {
2857 /* Internal error. */
2858 ret
= -LTTNG_ERR_UNK
;
2862 ret
= (int) *pending
;
2869 * Regenerate the metadata for a session.
2870 * Return 0 on success, a negative error code on error.
2872 int lttng_regenerate_metadata(const char *session_name
)
2875 struct lttcomm_session_msg lsm
;
2877 if (!session_name
) {
2878 ret
= -LTTNG_ERR_INVALID
;
2882 memset(&lsm
, 0, sizeof(lsm
));
2883 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2885 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2886 sizeof(lsm
.session
.name
));
2888 ret
= -LTTNG_ERR_INVALID
;
2892 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2903 * Deprecated, replaced by lttng_regenerate_metadata.
2905 int lttng_metadata_regenerate(const char *session_name
)
2907 return lttng_regenerate_metadata(session_name
);
2911 * Regenerate the statedump of a session.
2912 * Return 0 on success, a negative error code on error.
2914 int lttng_regenerate_statedump(const char *session_name
)
2917 struct lttcomm_session_msg lsm
;
2919 if (!session_name
) {
2920 ret
= -LTTNG_ERR_INVALID
;
2924 memset(&lsm
, 0, sizeof(lsm
));
2925 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2927 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2928 sizeof(lsm
.session
.name
));
2930 ret
= -LTTNG_ERR_INVALID
;
2934 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2945 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
2949 struct lttcomm_session_msg lsm
= {
2950 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2952 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
2953 struct lttcomm_session_msg
*message_lsm
;
2954 struct lttng_payload message
;
2955 struct lttng_payload reply
;
2956 struct lttng_trigger
*reply_trigger
= NULL
;
2957 enum lttng_domain_type domain_type
;
2958 const struct lttng_credentials user_creds
= {
2959 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2960 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2962 const char *unused_trigger_name
= NULL
;
2963 enum lttng_trigger_status trigger_status
;
2965 lttng_payload_init(&message
);
2966 lttng_payload_init(&reply
);
2969 ret
= -LTTNG_ERR_INVALID
;
2973 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2974 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2975 /* Re-using already registered trigger. */
2976 ret
= -LTTNG_ERR_INVALID
;
2981 trigger_status
= lttng_trigger_set_name(trigger
, name
);
2982 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2983 ret
= -LTTNG_ERR_NOMEM
;
2988 if (!trigger
->creds
.uid
.is_set
) {
2989 /* Use the client's credentials as the trigger credentials. */
2990 lttng_trigger_set_credentials(trigger
, &user_creds
);
2993 * Validate that either the current trigger credentials and the
2994 * client credentials are identical or that the current user is
2995 * root. The root user can register, unregister triggers for
2996 * himself and other users.
2998 * This check is also present on the sessiond side, using the
2999 * credentials passed on the socket. These check are all
3002 const struct lttng_credentials
*trigger_creds
=
3003 lttng_trigger_get_credentials(trigger
);
3005 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3006 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3007 ret
= -LTTNG_ERR_EPERM
;
3008 goto end_unset_name
;
3013 if (!lttng_trigger_validate(trigger
)) {
3014 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3015 goto end_unset_name
;
3018 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3021 lsm
.domain
.type
= domain_type
;
3023 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3025 ret
= -LTTNG_ERR_NOMEM
;
3026 goto end_unset_name
;
3029 ret
= lttng_trigger_serialize(trigger
, &message
);
3031 ret
= -LTTNG_ERR_UNK
;
3032 goto end_unset_name
;
3036 * This is needed to populate the trigger object size for the command
3039 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3041 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3044 struct lttng_payload_view message_view
=
3045 lttng_payload_view_from_payload(
3048 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3050 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3052 goto end_unset_name
;
3057 struct lttng_payload_view reply_view
=
3058 lttng_payload_view_from_payload(
3059 &reply
, 0, reply
.buffer
.size
);
3061 ret
= lttng_trigger_create_from_payload(
3062 &reply_view
, &reply_trigger
);
3064 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3065 goto end_unset_name
;
3069 if (name
|| generate_name
) {
3070 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3072 ret
= -LTTNG_ERR_NOMEM
;
3081 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3082 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3083 ret
= -LTTNG_ERR_UNK
;
3086 lttng_payload_reset(&message
);
3087 lttng_payload_reset(&reply
);
3088 lttng_trigger_destroy(reply_trigger
);
3092 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3094 /* Register an anonymous trigger. */
3095 return _lttng_register_trigger(trigger
, NULL
, false);
3098 enum lttng_error_code
lttng_register_trigger_with_name(
3099 struct lttng_trigger
*trigger
, const char *name
)
3101 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3103 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3106 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3107 struct lttng_trigger
*trigger
)
3109 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3111 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3114 enum lttng_error_code
lttng_error_query_execute(
3115 const struct lttng_error_query
*query
,
3116 const struct lttng_endpoint
*endpoint
,
3117 struct lttng_error_query_results
**results
)
3120 enum lttng_error_code ret_code
;
3121 struct lttcomm_session_msg lsm
= {
3122 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3124 struct lttng_payload message
;
3125 struct lttng_payload reply
;
3126 struct lttcomm_session_msg
*message_lsm
;
3128 lttng_payload_init(&message
);
3129 lttng_payload_init(&reply
);
3131 if (!query
|| !results
) {
3132 ret_code
= LTTNG_ERR_INVALID
;
3136 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3137 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3141 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3143 ret_code
= LTTNG_ERR_NOMEM
;
3147 ret
= lttng_error_query_serialize(query
, &message
);
3149 ret_code
= LTTNG_ERR_UNK
;
3153 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3154 message_lsm
->u
.error_query
.length
=
3155 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3158 struct lttng_payload_view message_view
=
3159 lttng_payload_view_from_payload(
3162 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3164 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3166 ret_code
=(lttng_error_code
) -ret
;
3172 ssize_t reply_create_ret
;
3173 struct lttng_payload_view reply_view
=
3174 lttng_payload_view_from_payload(
3175 &reply
, 0, reply
.buffer
.size
);
3177 reply_create_ret
= lttng_error_query_results_create_from_payload(
3178 &reply_view
, results
);
3179 if (reply_create_ret
< 0) {
3180 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3185 ret_code
= LTTNG_OK
;
3187 lttng_payload_reset(&message
);
3188 lttng_payload_reset(&reply
);
3192 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3195 struct lttcomm_session_msg lsm
;
3196 struct lttcomm_session_msg
*message_lsm
;
3197 struct lttng_payload message
;
3198 struct lttng_payload reply
;
3199 struct lttng_trigger
*copy
= NULL
;
3200 const struct lttng_credentials user_creds
= {
3201 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3202 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3205 lttng_payload_init(&message
);
3206 lttng_payload_init(&reply
);
3209 ret
= -LTTNG_ERR_INVALID
;
3213 copy
= lttng_trigger_copy(trigger
);
3215 ret
= -LTTNG_ERR_UNK
;
3219 if (!copy
->creds
.uid
.is_set
) {
3220 /* Use the client credentials as the trigger credentials */
3221 lttng_trigger_set_credentials(copy
, &user_creds
);
3224 * Validate that either the current trigger credentials and the
3225 * client credentials are identical or that the current user is
3226 * root. The root user can register, unregister triggers for
3227 * himself and other users.
3229 * This check is also present on the sessiond side, using the
3230 * credentials passed on the socket. These check are all
3233 const struct lttng_credentials
*trigger_creds
=
3234 lttng_trigger_get_credentials(copy
);
3235 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3236 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3237 ret
= -LTTNG_ERR_EPERM
;
3243 if (!lttng_trigger_validate(copy
)) {
3244 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3248 memset(&lsm
, 0, sizeof(lsm
));
3249 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3251 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3253 ret
= -LTTNG_ERR_NOMEM
;
3257 ret
= lttng_trigger_serialize(copy
, &message
);
3259 ret
= -LTTNG_ERR_UNK
;
3264 * This is needed to populate the trigger object size for the command
3265 * header and number of fds sent.
3267 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3269 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3272 struct lttng_payload_view message_view
=
3273 lttng_payload_view_from_payload(
3277 * Update the message header with the number of fd that will be
3280 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3283 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3291 lttng_trigger_destroy(copy
);
3292 lttng_payload_reset(&message
);
3293 lttng_payload_reset(&reply
);
3298 * Ask the session daemon for all registered triggers for the current user.
3300 * Allocates and return an lttng_triggers set.
3301 * On error, returns a suitable lttng_error_code.
3303 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3306 enum lttng_error_code ret_code
= LTTNG_OK
;
3307 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3308 struct lttng_triggers
*local_triggers
= NULL
;
3309 struct lttng_payload reply
;
3310 struct lttng_payload_view lsm_view
=
3311 lttng_payload_view_init_from_buffer(
3312 (const char *) &lsm
, 0, sizeof(lsm
));
3314 lttng_payload_init(&reply
);
3316 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3318 ret_code
= (enum lttng_error_code
) -ret
;
3323 struct lttng_payload_view reply_view
=
3324 lttng_payload_view_from_payload(
3325 &reply
, 0, reply
.buffer
.size
);
3327 ret
= lttng_triggers_create_from_payload(
3328 &reply_view
, &local_triggers
);
3330 ret_code
= LTTNG_ERR_FATAL
;
3335 *triggers
= local_triggers
;
3336 local_triggers
= NULL
;
3338 lttng_payload_reset(&reply
);
3339 lttng_triggers_destroy(local_triggers
);
3346 static void __attribute__((constructor
)) init(void)
3348 /* Set default session group */
3349 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3355 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3357 free(tracing_group
);