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
14 #include "lttng-ctl-helper.hpp"
16 #include <common/align.hpp>
17 #include <common/bytecode/bytecode.hpp>
18 #include <common/common.hpp>
19 #include <common/compat/errno.hpp>
20 #include <common/compat/string.hpp>
21 #include <common/defaults.hpp>
22 #include <common/dynamic-array.hpp>
23 #include <common/dynamic-buffer.hpp>
24 #include <common/filter/filter-ast.hpp>
25 #include <common/filter/filter-parser.hpp>
26 #include <common/filter/memstream.hpp>
27 #include <common/make-unique-wrapper.hpp>
28 #include <common/payload-view.hpp>
29 #include <common/payload.hpp>
30 #include <common/sessiond-comm/sessiond-comm.hpp>
31 #include <common/tracker.hpp>
32 #include <common/unix.hpp>
33 #include <common/uri.hpp>
34 #include <common/utils.hpp>
36 #include <lttng/channel-internal.hpp>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.hpp>
40 #include <lttng/event-internal.hpp>
41 #include <lttng/health-internal.hpp>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.hpp>
45 #include <lttng/session-internal.hpp>
46 #include <lttng/trigger/trigger-internal.hpp>
47 #include <lttng/userspace-probe-internal.hpp>
56 #define COPY_DOMAIN_PACKED(dst, src) \
58 struct lttng_domain _tmp_domain; \
60 lttng_ctl_copy_lttng_domain(&_tmp_domain, &(src)); \
61 (dst) = _tmp_domain; \
64 /* Socket to session daemon for communication */
65 static int sessiond_socket
= -1;
66 static char sessiond_sock_path
[PATH_MAX
];
69 static char *tracing_group
;
75 * Those two variables are used by error.h to silent or control the verbosity of
76 * error message. They are global to the library so application linking with it
77 * are able to compile correctly and also control verbosity of the library.
79 LTTNG_EXPORT
int lttng_opt_quiet
;
80 LTTNG_EXPORT
int lttng_opt_verbose
;
81 LTTNG_EXPORT
int lttng_opt_mi
;
84 * Copy domain to lttcomm_session_msg domain.
86 * If domain is unknown, default domain will be the kernel.
88 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
92 case LTTNG_DOMAIN_KERNEL
:
93 case LTTNG_DOMAIN_UST
:
94 case LTTNG_DOMAIN_JUL
:
95 case LTTNG_DOMAIN_LOG4J
:
96 case LTTNG_DOMAIN_LOG4J2
:
97 case LTTNG_DOMAIN_PYTHON
:
98 memcpy(dst
, src
, sizeof(struct lttng_domain
));
101 memset(dst
, 0, sizeof(struct lttng_domain
));
108 * Send lttcomm_session_msg to the session daemon.
110 * On success, returns the number of bytes sent (>=0)
111 * On error, returns -1
113 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
118 ret
= -LTTNG_ERR_NO_SESSIOND
;
122 DBG("LSM cmd type: '%s' (%d)",
123 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) lsm
->cmd_type
),
126 ret
= lttcomm_send_creds_unix_sock(
127 sessiond_socket
, lsm
, sizeof(struct lttcomm_session_msg
));
129 ret
= -LTTNG_ERR_FATAL
;
137 * Send var len data to the session daemon.
139 * On success, returns the number of bytes sent (>=0)
140 * On error, returns -1
142 static int send_session_varlen(const void *data
, size_t len
)
147 ret
= -LTTNG_ERR_NO_SESSIOND
;
156 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
158 ret
= -LTTNG_ERR_FATAL
;
166 * Send file descriptors to the session daemon.
168 * On success, returns the number of bytes sent (>=0)
169 * On error, returns -1
171 static int send_session_fds(const int *fds
, size_t nb_fd
)
176 ret
= -LTTNG_ERR_NO_SESSIOND
;
180 if (!fds
|| !nb_fd
) {
185 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
187 ret
= -LTTNG_ERR_FATAL
;
195 * Receive data from the sessiond socket.
197 * On success, returns the number of bytes received (>=0)
198 * On error, returns a negative lttng_error_code.
200 static int recv_data_sessiond(void *buf
, size_t len
)
204 LTTNG_ASSERT(len
> 0);
207 ret
= -LTTNG_ERR_NO_SESSIOND
;
211 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
213 ret
= -LTTNG_ERR_FATAL
;
214 } else if (ret
== 0) {
215 ret
= -LTTNG_ERR_NO_SESSIOND
;
223 * Receive a payload from the session daemon by appending to an existing
225 * On success, returns the number of bytes received (>=0)
226 * On error, returns a negative lttng_error_code.
228 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
231 const size_t original_payload_size
= payload
->buffer
.size
;
233 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
, payload
->buffer
.size
+ len
);
235 ret
= -LTTNG_ERR_NOMEM
;
239 ret
= recv_data_sessiond(payload
->buffer
.data
+ original_payload_size
, len
);
245 * Check if we are in the specified group.
247 * If yes return 1, else return -1.
249 int lttng_check_tracing_group()
251 gid_t
*grp_list
, tracing_gid
;
252 int grp_list_size
, grp_id
, i
;
254 const char *grp_name
= tracing_group
;
256 /* Get GID of group 'tracing' */
257 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
258 /* If grp_tracing is NULL, the group does not exist. */
262 /* Get number of supplementary group IDs */
263 grp_list_size
= getgroups(0, nullptr);
264 if (grp_list_size
< 0) {
269 /* Alloc group list of the right size */
270 grp_list
= calloc
<gid_t
>(grp_list_size
);
275 grp_id
= getgroups(grp_list_size
, grp_list
);
281 for (i
= 0; i
< grp_list_size
; i
++) {
282 if (grp_list
[i
] == tracing_gid
) {
295 static enum lttng_error_code
check_enough_available_memory(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
= num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
322 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
323 * reliable estimate we can get but it is only exposed by the kernel
324 * since 3.14. (See Linux kernel commit:
325 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
327 ret
= utils_get_memory_available(&best_mem_info
);
333 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
334 * is a sanity check for obvious user error.
336 ret
= utils_get_memory_total(&best_mem_info
);
341 /* No valid source of information. */
342 ret_code
= LTTNG_ERR_NOMEM
;
346 if (best_mem_info
>= num_bytes_requested_total
) {
349 ret_code
= LTTNG_ERR_NOMEM
;
356 * Try connect to session daemon with sock_path.
358 * Return 0 on success, else -1
360 static int try_connect_sessiond(const char *sock_path
)
364 /* If socket exist, we check if the daemon listens for connect. */
365 ret
= access(sock_path
, F_OK
);
371 ret
= lttcomm_connect_unix_sock(sock_path
);
377 ret
= lttcomm_close_unix_sock(ret
);
379 PERROR("lttcomm_close_unix_sock");
389 * Set sessiond socket path by putting it in the global sessiond_sock_path
392 * Returns 0 on success, negative value on failure (the sessiond socket path
393 * is somehow too long or ENOMEM).
395 static int set_session_daemon_path()
397 int in_tgroup
= 0; /* In tracing group. */
403 /* Are we in the tracing group ? */
404 in_tgroup
= lttng_check_tracing_group();
407 if ((uid
== 0) || in_tgroup
== 1) {
408 const int ret
= lttng_strncpy(sessiond_sock_path
,
409 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
410 sizeof(sessiond_sock_path
));
422 ret
= try_connect_sessiond(sessiond_sock_path
);
426 /* Global session daemon not available... */
428 /* ...or not in tracing group (and not root), default */
431 * With GNU C < 2.1, snprintf returns -1 if the target buffer
433 * With GNU C >= 2.1, snprintf returns the required size
434 * (excluding closing null)
436 ret
= snprintf(sessiond_sock_path
,
437 sizeof(sessiond_sock_path
),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
439 utils_get_home_dir());
440 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
452 * Connect to the LTTng session daemon.
454 * On success, return the socket's file descriptor. On error, return -1.
456 int connect_sessiond()
460 ret
= set_session_daemon_path();
465 /* Connect to the sesssion daemon. */
466 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
477 static void reset_global_sessiond_connection_state()
479 sessiond_socket
= -1;
484 * Clean disconnect from the session daemon.
486 * On success, return 0. On error, return -1.
488 static int disconnect_sessiond()
493 ret
= lttcomm_close_unix_sock(sessiond_socket
);
494 reset_global_sessiond_connection_state();
500 static int recv_sessiond_optional_data(size_t len
, void **user_buf
, size_t *user_len
)
507 ret
= -LTTNG_ERR_INVALID
;
511 buf
= zmalloc
<char>(len
);
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
,
559 void **user_payload_buf
,
560 void **user_cmd_header_buf
,
561 size_t *user_cmd_header_len
)
565 struct lttcomm_lttng_msg llm
;
567 ret
= connect_sessiond();
569 ret
= -LTTNG_ERR_NO_SESSIOND
;
572 sessiond_socket
= ret
;
576 ret
= send_session_msg(lsm
);
578 /* Ret value is a valid lttng error code. */
581 /* Send var len data */
582 ret
= send_session_varlen(vardata
, vardata_len
);
584 /* Ret value is a valid lttng error code. */
589 ret
= send_session_fds(fds
, nb_fd
);
591 /* Ret value is a valid lttng error code. */
595 /* Get header from data transmission */
596 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
598 /* Ret value is a valid lttng error code. */
602 /* Check error code if OK */
603 if (llm
.ret_code
!= LTTNG_OK
) {
608 /* Get command header from data transmission */
609 ret
= recv_sessiond_optional_data(
610 llm
.cmd_header_size
, user_cmd_header_buf
, user_cmd_header_len
);
615 /* Get payload from data transmission */
616 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
, &payload_len
);
624 disconnect_sessiond();
628 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
, struct lttng_payload
*reply
)
631 struct lttcomm_lttng_msg llm
;
632 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
634 LTTNG_ASSERT(reply
->buffer
.size
== 0);
635 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
637 ret
= connect_sessiond();
639 ret
= -LTTNG_ERR_NO_SESSIOND
;
642 sessiond_socket
= ret
;
646 /* Send command to session daemon */
647 ret
= lttcomm_send_creds_unix_sock(
648 sessiond_socket
, message
->buffer
.data
, message
->buffer
.size
);
650 ret
= -LTTNG_ERR_FATAL
;
655 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
, message
);
657 ret
= -LTTNG_ERR_FATAL
;
662 /* Get header from data transmission */
663 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
665 /* Ret value is a valid lttng error code. */
669 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
671 /* Check error code if OK */
672 if (llm
.ret_code
!= LTTNG_OK
) {
673 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
674 /* Invalid error code received. */
675 ret
= -LTTNG_ERR_UNK
;
682 if (llm
.cmd_header_size
> 0) {
683 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
689 /* Get command header from data transmission */
690 if (llm
.data_size
> 0) {
691 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
697 if (llm
.fd_count
> 0) {
698 ret
= lttcomm_recv_payload_fds_unix_sock(sessiond_socket
, llm
.fd_count
, reply
);
704 /* Don't return the llm header to the caller. */
705 memmove(reply
->buffer
.data
,
706 reply
->buffer
.data
+ sizeof(llm
),
707 reply
->buffer
.size
- sizeof(llm
));
708 ret
= lttng_dynamic_buffer_set_size(&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
, struct lttng_domain
*domain
)
729 struct lttng_handle
*handle
= nullptr;
731 handle
= zmalloc
<lttng_handle
>();
732 if (handle
== nullptr) {
733 PERROR("malloc handle");
737 /* Copy session name */
738 ret
= lttng_strncpy(handle
->session_name
, session_name
?: "", sizeof(handle
->session_name
));
743 /* Copy lttng domain or leave initialized to 0. */
745 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
756 * Destroy handle by free(3) the pointer.
758 void lttng_destroy_handle(struct lttng_handle
*handle
)
764 * Register an outside consumer.
766 * Returns size of returned session payload data or a negative error code.
768 int lttng_register_consumer(struct lttng_handle
*handle
, const char *socket_path
)
771 struct lttcomm_session_msg lsm
;
773 if (handle
== nullptr || socket_path
== nullptr) {
774 ret
= -LTTNG_ERR_INVALID
;
778 memset(&lsm
, 0, sizeof(lsm
));
779 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
;
780 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
782 ret
= -LTTNG_ERR_INVALID
;
786 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
788 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
790 ret
= -LTTNG_ERR_INVALID
;
794 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
800 * Start tracing for all traces of the session.
802 * Returns size of returned session payload data or a negative error code.
804 int lttng_start_tracing(const char *session_name
)
807 struct lttcomm_session_msg lsm
;
809 if (session_name
== nullptr) {
810 ret
= -LTTNG_ERR_INVALID
;
814 memset(&lsm
, 0, sizeof(lsm
));
815 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_START_TRACE
;
817 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
819 ret
= -LTTNG_ERR_INVALID
;
823 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
829 * Stop tracing for all traces of the session.
831 static int _lttng_stop_tracing(const char *session_name
, int wait
)
834 struct lttcomm_session_msg lsm
;
836 if (session_name
== nullptr) {
837 ret
= -LTTNG_ERR_INVALID
;
841 memset(&lsm
, 0, sizeof(lsm
));
842 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
;
844 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
846 ret
= -LTTNG_ERR_INVALID
;
850 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
851 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
859 /* Check for data availability */
861 data_ret
= lttng_data_pending(session_name
);
863 /* Return the data available call error. */
869 * Data sleep time before retrying (in usec). Don't sleep if the
870 * call returned value indicates availability.
873 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
875 } while (data_ret
!= 0);
883 * Stop tracing and wait for data availability.
885 int lttng_stop_tracing(const char *session_name
)
887 return _lttng_stop_tracing(session_name
, 1);
891 * Stop tracing but _don't_ wait for data availability.
893 int lttng_stop_tracing_no_wait(const char *session_name
)
895 return _lttng_stop_tracing(session_name
, 0);
899 * Add context to a channel.
901 * If the given channel is NULL, add the contexts to all channels.
902 * The event_name param is ignored.
904 * Returns the size of the returned payload data or a negative error code.
906 int lttng_add_context(struct lttng_handle
*handle
,
907 struct lttng_event_context
*ctx
,
908 const char *event_name
__attribute__((unused
)),
909 const char *channel_name
)
912 struct lttcomm_session_msg lsm
= {
913 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
,
919 struct lttng_payload payload
;
921 lttng_payload_init(&payload
);
923 /* Safety check. Both are mandatory. */
924 if (handle
== nullptr || ctx
== nullptr) {
925 ret
= -LTTNG_ERR_INVALID
;
929 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
931 ret
= -LTTNG_ERR_NOMEM
;
935 /* If no channel name, send empty string. */
937 lsm
.u
.context
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.context
.channel_name
));
939 ret
= -LTTNG_ERR_INVALID
;
943 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
944 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
946 ret
= -LTTNG_ERR_INVALID
;
950 ret
= lttng_event_context_serialize(ctx
, &payload
);
952 ret
= -LTTNG_ERR_INVALID
;
956 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
958 /* Update message header. */
959 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
962 struct lttng_payload reply
;
963 struct lttng_payload_view payload_view
=
964 lttng_payload_view_from_payload(&payload
, 0, -1);
966 lttng_payload_init(&reply
);
967 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
968 lttng_payload_reset(&reply
);
975 lttng_payload_reset(&payload
);
980 * Enable event(s) for a channel.
982 * If no event name is specified, all events are enabled.
983 * If no channel name is specified, the default 'channel0' is used.
985 * Returns size of returned session payload data or a negative error code.
987 int lttng_enable_event(struct lttng_handle
*handle
,
988 struct lttng_event
*ev
,
989 const char *channel_name
)
991 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
, nullptr, 0, nullptr);
995 * Create or enable an event with a filter expression.
997 * Return negative error value on error.
998 * Return size of returned session payload data if OK.
1000 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1001 struct lttng_event
*event
,
1002 const char *channel_name
,
1003 const char *filter_expression
)
1005 return lttng_enable_event_with_exclusions(
1006 handle
, event
, channel_name
, filter_expression
, 0, nullptr);
1010 * Depending on the event, return a newly allocated agent filter expression or
1011 * NULL if not applicable.
1013 * An event with NO loglevel and the name is * will return NULL.
1016 set_agent_filter(const char *filter
, struct lttng_event
*ev
, struct lttng_domain
*domain
)
1019 char *agent_filter
= nullptr;
1022 LTTNG_ASSERT(domain
);
1024 /* Don't add filter for the '*' event. */
1025 if (strcmp(ev
->name
, "*") != 0) {
1028 &agent_filter
, "(%s) && (logger_name == \"%s\")", filter
, ev
->name
);
1030 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1038 /* Add loglevel filtering if any for the agent domains. */
1039 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1042 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1044 * Log4j2 is the only agent domain for which more severe
1045 * logging levels have a lower numerical value.
1047 if (domain
->type
== LTTNG_DOMAIN_LOG4J2
) {
1056 if (filter
|| agent_filter
) {
1059 err
= asprintf(&new_filter
,
1060 "(%s) && (int_loglevel %s %d)",
1061 agent_filter
? agent_filter
: filter
,
1067 agent_filter
= new_filter
;
1069 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
, ev
->loglevel
);
1077 return agent_filter
;
1084 * Enable event(s) for a channel, possibly with exclusions and a filter.
1085 * If no event name is specified, all events are enabled.
1086 * If no channel name is specified, the default name is used.
1087 * If filter expression is not NULL, the filter is set for the event.
1088 * If exclusion count is not zero, the exclusions are set for the event.
1089 * Returns size of returned session payload data or a negative error code.
1091 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1092 struct lttng_event
*ev
,
1093 const char *channel_name
,
1094 const char *original_filter_expression
,
1095 int exclusion_count
,
1096 char **exclusion_list
)
1098 struct lttcomm_session_msg lsm
= {
1099 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
,
1105 struct lttng_payload payload
;
1107 unsigned int free_filter_expression
= 0;
1108 struct filter_parser_ctx
*ctx
= nullptr;
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
== nullptr || ev
== nullptr) {
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
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1147 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1148 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J2
||
1149 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1150 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1151 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1152 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J2
||
1153 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1156 /* Setup agent filter if needed. */
1157 agent_filter
= set_agent_filter(filter_expression
, ev
, &handle
->domain
);
1158 if (!agent_filter
) {
1159 if (!filter_expression
) {
1161 * No agent and no filter, just skip
1168 * With an agent filter, the original filter has
1169 * been added to it thus replace the filter
1172 filter_expression
= agent_filter
;
1173 free_filter_expression
= 1;
1177 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1178 ret
= -LTTNG_ERR_FILTER_INVAL
;
1182 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1187 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1188 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1189 ret
= -LTTNG_ERR_FILTER_INVAL
;
1195 ret
= lttng_event_serialize(ev
,
1200 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1203 ret
= -LTTNG_ERR_INVALID
;
1207 /* If no channel name, send empty string. */
1208 ret
= lttng_strncpy(
1209 lsm
.u
.enable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.enable
.channel_name
));
1211 ret
= -LTTNG_ERR_INVALID
;
1216 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1219 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1221 ret
= -LTTNG_ERR_INVALID
;
1225 /* Length of the serialized event. */
1226 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1229 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1230 const int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1237 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1238 if (fd_count
== 1) {
1239 struct fd_handle
*h
= 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
: nullptr,
1254 view
.buffer
.size
? view
.buffer
.data
:
1263 if (filter_expression
&& ctx
) {
1264 filter_bytecode_free(ctx
);
1265 filter_ir_free(ctx
);
1266 filter_parser_ctx_free(ctx
);
1268 if (free_filter_expression
) {
1270 * The filter expression has been replaced and must be freed as
1271 * it is not the original filter expression received as a
1274 free(filter_expression
);
1277 * Return directly to the caller and don't ask the sessiond since
1278 * something went wrong in the parsing of data above.
1280 lttng_payload_reset(&payload
);
1284 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1285 struct lttng_event
*ev
,
1286 const char *channel_name
,
1287 const char *original_filter_expression
)
1289 struct lttcomm_session_msg lsm
= {
1290 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
,
1296 struct lttng_payload payload
;
1298 unsigned int free_filter_expression
= 0;
1299 struct filter_parser_ctx
*ctx
= nullptr;
1300 size_t bytecode_len
= 0;
1303 * We have either a filter or some exclusions, so we need to set up
1304 * a variable-length payload from where to send the data.
1306 lttng_payload_init(&payload
);
1309 * Cast as non-const since we may replace the filter expression
1310 * by a dynamically allocated string. Otherwise, the original
1311 * string is not modified.
1313 char *filter_expression
= (char *) original_filter_expression
;
1315 if (handle
== nullptr || ev
== nullptr) {
1316 ret
= -LTTNG_ERR_INVALID
;
1321 * Empty filter string will always be rejected by the parser
1322 * anyway, so treat this corner-case early to eliminate
1323 * lttng_fmemopen error for 0-byte allocation.
1325 if (filter_expression
&& filter_expression
[0] == '\0') {
1326 ret
= -LTTNG_ERR_INVALID
;
1330 /* Parse filter expression. */
1331 if (filter_expression
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1332 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1333 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J2
||
1334 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1335 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1336 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1337 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J2
||
1338 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1341 /* Setup agent filter if needed. */
1342 agent_filter
= set_agent_filter(filter_expression
, ev
, &handle
->domain
);
1343 if (!agent_filter
) {
1344 if (!filter_expression
) {
1346 * No JUL and no filter, just skip
1353 * With an agent filter, the original filter has
1354 * been added to it thus replace the filter
1357 filter_expression
= agent_filter
;
1358 free_filter_expression
= 1;
1362 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1363 ret
= -LTTNG_ERR_FILTER_INVAL
;
1367 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1372 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1373 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1374 ret
= -LTTNG_ERR_FILTER_INVAL
;
1380 ret
= lttng_event_serialize(ev
,
1385 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1388 ret
= -LTTNG_ERR_INVALID
;
1392 /* If no channel name, send empty string. */
1393 ret
= lttng_strncpy(
1394 lsm
.u
.disable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.disable
.channel_name
));
1396 ret
= -LTTNG_ERR_INVALID
;
1401 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1404 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1406 ret
= -LTTNG_ERR_INVALID
;
1410 /* Length of the serialized event. */
1411 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1414 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1415 const int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1422 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1423 if (fd_count
== 1) {
1424 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1430 fd_to_send
= fd_handle_get_fd(h
);
1434 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1435 fd_count
? &fd_to_send
: nullptr,
1437 view
.buffer
.size
? view
.buffer
.data
:
1446 if (filter_expression
&& ctx
) {
1447 filter_bytecode_free(ctx
);
1448 filter_ir_free(ctx
);
1449 filter_parser_ctx_free(ctx
);
1451 if (free_filter_expression
) {
1453 * The filter expression has been replaced and must be freed as
1454 * it is not the original filter expression received as a
1457 free(filter_expression
);
1460 * Return directly to the caller and don't ask the sessiond since
1461 * something went wrong in the parsing of data above.
1463 lttng_payload_reset(&payload
);
1468 * Disable event(s) of a channel and domain.
1469 * If no event name is specified, all events are disabled.
1470 * If no channel name is specified, the default 'channel0' is used.
1471 * Returns size of returned session payload data or a negative error code.
1473 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
, const char *channel_name
)
1476 struct lttng_event ev
;
1478 memset(&ev
, 0, sizeof(ev
));
1480 ev
.type
= LTTNG_EVENT_ALL
;
1481 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1483 ret
= -LTTNG_ERR_INVALID
;
1487 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, nullptr);
1492 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1494 struct lttng_channel
*channel
= nullptr;
1500 /* Validate domain. */
1501 switch (domain
->type
) {
1502 case LTTNG_DOMAIN_UST
:
1503 switch (domain
->buf_type
) {
1504 case LTTNG_BUFFER_PER_UID
:
1505 case LTTNG_BUFFER_PER_PID
:
1511 case LTTNG_DOMAIN_KERNEL
:
1512 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1520 channel
= lttng_channel_create_internal();
1525 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1530 void lttng_channel_destroy(struct lttng_channel
*channel
)
1536 if (channel
->attr
.extended
.ptr
) {
1537 free(channel
->attr
.extended
.ptr
);
1543 * Enable channel per domain
1544 * Returns size of returned session payload data or a negative error code.
1546 int lttng_enable_channel(struct lttng_handle
*handle
, struct lttng_channel
*in_chan
)
1548 enum lttng_error_code ret_code
;
1550 struct lttng_dynamic_buffer buffer
;
1551 struct lttcomm_session_msg lsm
;
1552 uint64_t total_buffer_size_needed_per_cpu
= 0;
1553 struct lttng_channel
*channel
= nullptr;
1555 lttng_dynamic_buffer_init(&buffer
);
1557 /* NULL arguments are forbidden. No default values. */
1558 if (handle
== nullptr || in_chan
== nullptr) {
1559 ret
= -LTTNG_ERR_INVALID
;
1564 * Verify that the amount of memory required to create the requested
1565 * buffer is available on the system at the moment.
1567 if (in_chan
->attr
.num_subbuf
> UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1569 ret
= -LTTNG_ERR_OVERFLOW
;
1573 total_buffer_size_needed_per_cpu
= in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1574 ret_code
= check_enough_available_memory(total_buffer_size_needed_per_cpu
);
1575 if (ret_code
!= LTTNG_OK
) {
1580 /* Copy the channel for easier manipulation. */
1581 channel
= lttng_channel_copy(in_chan
);
1583 ret
= -LTTNG_ERR_NOMEM
;
1587 /* Populate the channel extended attribute if necessary. */
1588 if (!channel
->attr
.extended
.ptr
) {
1589 struct lttng_channel_extended
*extended
= zmalloc
<lttng_channel_extended
>();
1592 ret
= -LTTNG_ERR_NOMEM
;
1596 lttng_channel_set_default_extended_attr(&handle
->domain
, extended
);
1597 channel
->attr
.extended
.ptr
= extended
;
1600 /* Prepare the payload */
1601 memset(&lsm
, 0, sizeof(lsm
));
1603 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
;
1604 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1606 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1608 ret
= -LTTNG_ERR_INVALID
;
1612 ret
= lttng_channel_serialize(channel
, &buffer
);
1614 ret
= -LTTNG_ERR_FATAL
;
1618 lsm
.u
.channel
.length
= buffer
.size
;
1620 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
, buffer
.size
, nullptr);
1622 lttng_channel_destroy(channel
);
1623 lttng_dynamic_buffer_reset(&buffer
);
1628 * All tracing will be stopped for registered events of the channel.
1629 * Returns size of returned session payload data or a negative error code.
1631 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1634 struct lttcomm_session_msg lsm
;
1636 /* Safety check. Both are mandatory. */
1637 if (handle
== nullptr || name
== nullptr) {
1638 return -LTTNG_ERR_INVALID
;
1641 memset(&lsm
, 0, sizeof(lsm
));
1643 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
;
1645 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
, sizeof(lsm
.u
.disable
.channel_name
));
1647 ret
= -LTTNG_ERR_INVALID
;
1651 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1653 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1655 ret
= -LTTNG_ERR_INVALID
;
1659 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
1665 * Lists all available tracepoints of domain.
1666 * Sets the contents of the events array.
1667 * Returns the number of lttng_event entries in events;
1668 * on error, returns a negative value.
1670 int lttng_list_tracepoints(struct lttng_handle
*handle
, struct lttng_event
**events
)
1672 enum lttng_error_code ret_code
;
1673 int ret
, total_payload_received
;
1674 char *reception_buffer
= nullptr;
1675 struct lttcomm_session_msg lsm
= {
1676 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
,
1682 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1683 size_t cmd_header_len
;
1684 unsigned int nb_events
= 0;
1686 if (handle
== nullptr) {
1687 ret
= -LTTNG_ERR_INVALID
;
1691 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1693 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1698 (void **) &reception_buffer
,
1699 (void **) &cmd_header
,
1705 total_payload_received
= ret
;
1708 ret
= -LTTNG_ERR_UNK
;
1712 if (cmd_header
->count
> INT_MAX
) {
1713 ret
= -LTTNG_ERR_OVERFLOW
;
1717 nb_events
= (unsigned int) cmd_header
->count
;
1720 const lttng_buffer_view events_view
=
1721 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1722 struct lttng_payload_view events_payload_view
=
1723 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1725 ret_code
= lttng_events_create_and_flatten_from_payload(
1726 &events_payload_view
, nb_events
, events
);
1727 if (ret_code
!= LTTNG_OK
) {
1733 ret
= (int) nb_events
;
1737 free(reception_buffer
);
1742 * Lists all available tracepoint fields of domain.
1743 * Sets the contents of the event field array.
1744 * Returns the number of lttng_event_field entries in events;
1745 * on error, returns a negative value.
1747 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
, struct lttng_event_field
**fields
)
1749 enum lttng_error_code ret_code
;
1751 struct lttcomm_session_msg lsm
;
1752 const struct lttcomm_list_command_header
*cmd_header
= nullptr;
1753 unsigned int nb_event_fields
= 0;
1754 struct lttng_payload reply
;
1756 lttng_payload_init(&reply
);
1758 if (handle
== nullptr) {
1759 ret
= -LTTNG_ERR_INVALID
;
1763 memset(&lsm
, 0, sizeof(lsm
));
1764 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
;
1765 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1768 lttng_payload_view message_view
=
1769 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
1771 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1778 const lttng_buffer_view cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
1779 &reply
.buffer
, 0, sizeof(*cmd_header
));
1781 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1782 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1786 cmd_header
= (struct lttcomm_list_command_header
*) cmd_header_view
.data
;
1789 if (cmd_header
->count
> INT_MAX
) {
1790 ret
= -LTTNG_ERR_OVERFLOW
;
1794 nb_event_fields
= cmd_header
->count
;
1797 lttng_payload_view reply_view
=
1798 lttng_payload_view_from_payload(&reply
, sizeof(*cmd_header
), -1);
1800 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1801 &reply_view
, nb_event_fields
, fields
);
1802 if (ret_code
!= LTTNG_OK
) {
1808 ret
= nb_event_fields
;
1811 lttng_payload_reset(&reply
);
1816 * Lists all available kernel system calls. Allocates and sets the contents of
1819 * Returns the number of lttng_event entries in events; on error, returns a
1822 int lttng_list_syscalls(struct lttng_event
**events
)
1824 enum lttng_error_code ret_code
;
1825 int ret
, total_payload_received
;
1826 char *reception_buffer
= nullptr;
1827 struct lttcomm_session_msg lsm
= {};
1828 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1829 size_t cmd_header_len
;
1830 uint32_t nb_events
= 0;
1833 ret
= -LTTNG_ERR_INVALID
;
1837 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
;
1838 /* Force kernel domain for system calls. */
1839 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1841 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1846 (void **) &reception_buffer
,
1847 (void **) &cmd_header
,
1852 total_payload_received
= ret
;
1855 ret
= -LTTNG_ERR_UNK
;
1859 if (cmd_header
->count
> INT_MAX
) {
1860 ret
= -LTTNG_ERR_OVERFLOW
;
1864 nb_events
= (unsigned int) cmd_header
->count
;
1867 const struct lttng_buffer_view events_view
=
1868 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1869 struct lttng_payload_view events_payload_view
=
1870 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1872 ret_code
= lttng_events_create_and_flatten_from_payload(
1873 &events_payload_view
, nb_events
, events
);
1874 if (ret_code
!= LTTNG_OK
) {
1880 ret
= (int) nb_events
;
1883 free(reception_buffer
);
1889 * Returns a human readable string describing
1890 * the error code (positive or negative value).
1892 const char *lttng_strerror(int code
)
1898 return error_get_str(code
);
1901 enum lttng_error_code
lttng_create_session_ext(struct lttng_session_descriptor
*session_descriptor
)
1903 enum lttng_error_code ret_code
;
1904 struct lttcomm_session_msg lsm
= {
1905 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
,
1911 void *reply
= nullptr;
1912 struct lttng_buffer_view reply_view
;
1914 bool sessiond_must_generate_ouput
;
1915 struct lttng_dynamic_buffer payload
;
1917 size_t descriptor_size
;
1918 struct lttng_session_descriptor
*descriptor_reply
= nullptr;
1920 lttng_dynamic_buffer_init(&payload
);
1921 if (!session_descriptor
) {
1922 ret_code
= LTTNG_ERR_INVALID
;
1926 sessiond_must_generate_ouput
=
1927 !lttng_session_descriptor_is_output_destination_initialized(session_descriptor
);
1928 if (sessiond_must_generate_ouput
) {
1929 const char *home_dir
= utils_get_home_dir();
1930 const size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1932 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1933 ret_code
= LTTNG_ERR_FATAL
;
1937 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1938 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
, home_dir_len
);
1940 ret_code
= LTTNG_ERR_NOMEM
;
1945 descriptor_size
= payload
.size
;
1946 ret
= lttng_session_descriptor_serialize(session_descriptor
, &payload
);
1948 ret_code
= LTTNG_ERR_INVALID
;
1951 descriptor_size
= payload
.size
- descriptor_size
;
1952 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1954 /* Command returns a session descriptor on success. */
1955 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1956 &lsm
, payload
.data
, payload
.size
, &reply
);
1957 if (reply_ret
< 0) {
1958 ret_code
= (lttng_error_code
) -reply_ret
;
1960 } else if (reply_ret
== 0) {
1961 /* Socket unexpectedly closed by the session daemon. */
1962 ret_code
= LTTNG_ERR_FATAL
;
1966 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1967 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
, &descriptor_reply
);
1969 ret_code
= LTTNG_ERR_FATAL
;
1972 ret_code
= LTTNG_OK
;
1973 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1976 lttng_dynamic_buffer_reset(&payload
);
1977 lttng_session_descriptor_destroy(descriptor_reply
);
1982 * Create a new session using name and url for destination.
1984 * Return 0 on success else a negative LTTng error code.
1986 int lttng_create_session(const char *name
, const char *url
)
1990 struct lttng_uri
*uris
= nullptr;
1991 struct lttng_session_descriptor
*descriptor
= nullptr;
1992 enum lttng_error_code ret_code
;
1995 ret
= -LTTNG_ERR_INVALID
;
1999 size
= uri_parse_str_urls(url
, nullptr, &uris
);
2001 ret
= -LTTNG_ERR_INVALID
;
2006 descriptor
= lttng_session_descriptor_create(name
);
2009 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2010 ret
= -LTTNG_ERR_INVALID
;
2013 descriptor
= lttng_session_descriptor_local_create(name
, uris
[0].dst
.path
);
2016 descriptor
= lttng_session_descriptor_network_create(name
, url
, nullptr);
2019 ret
= -LTTNG_ERR_INVALID
;
2023 ret
= -LTTNG_ERR_INVALID
;
2026 ret_code
= lttng_create_session_ext(descriptor
);
2027 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2029 lttng_session_descriptor_destroy(descriptor
);
2035 * Create a session exclusively used for snapshot.
2037 * Return 0 on success else a negative LTTng error code.
2039 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2042 enum lttng_error_code ret_code
;
2044 struct lttng_uri
*uris
= nullptr;
2045 struct lttng_session_descriptor
*descriptor
= nullptr;
2048 ret
= -LTTNG_ERR_INVALID
;
2052 size
= uri_parse_str_urls(snapshot_url
, nullptr, &uris
);
2054 ret
= -LTTNG_ERR_INVALID
;
2058 * If the user does not specify a custom subdir, use the session name.
2060 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2061 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2063 PERROR("Failed to set session name as network destination sub-directory");
2064 ret
= -LTTNG_ERR_FATAL
;
2066 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2067 /* Truncated output. */
2068 ret
= -LTTNG_ERR_INVALID
;
2075 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2078 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2079 ret
= -LTTNG_ERR_INVALID
;
2082 descriptor
= lttng_session_descriptor_snapshot_local_create(name
, uris
[0].dst
.path
);
2085 descriptor
= lttng_session_descriptor_snapshot_network_create(
2086 name
, snapshot_url
, nullptr);
2089 ret
= -LTTNG_ERR_INVALID
;
2093 ret
= -LTTNG_ERR_INVALID
;
2096 ret_code
= lttng_create_session_ext(descriptor
);
2097 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2099 lttng_session_descriptor_destroy(descriptor
);
2105 * Create a session exclusively used for live.
2107 * Return 0 on success else a negative LTTng error code.
2109 int lttng_create_session_live(const char *name
, const char *url
, unsigned int timer_interval
)
2112 enum lttng_error_code ret_code
;
2113 struct lttng_session_descriptor
*descriptor
= nullptr;
2116 ret
= -LTTNG_ERR_INVALID
;
2121 descriptor
= lttng_session_descriptor_live_network_create(
2122 name
, url
, nullptr, timer_interval
);
2124 descriptor
= lttng_session_descriptor_live_create(name
, timer_interval
);
2127 ret
= -LTTNG_ERR_INVALID
;
2130 ret_code
= lttng_create_session_ext(descriptor
);
2131 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2133 lttng_session_descriptor_destroy(descriptor
);
2138 * Stop the session and wait for the data before destroying it
2140 * Return 0 on success else a negative LTTng error code.
2142 int lttng_destroy_session(const char *session_name
)
2145 enum lttng_error_code ret_code
;
2146 enum lttng_destruction_handle_status status
;
2147 struct lttng_destruction_handle
*handle
= nullptr;
2150 * Stop the tracing and wait for the data to be
2153 ret
= _lttng_stop_tracing(session_name
, 1);
2154 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2158 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2159 if (ret_code
!= LTTNG_OK
) {
2160 ret
= (int) -ret_code
;
2163 LTTNG_ASSERT(handle
);
2165 /* Block until the completion of the destruction of the session. */
2166 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2167 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2168 ret
= -LTTNG_ERR_UNK
;
2172 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2173 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2174 ret
= -LTTNG_ERR_UNK
;
2177 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2179 lttng_destruction_handle_destroy(handle
);
2184 * Destroy the session without waiting for the data.
2186 int lttng_destroy_session_no_wait(const char *session_name
)
2188 enum lttng_error_code ret_code
;
2190 ret_code
= lttng_destroy_session_ext(session_name
, nullptr);
2191 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2195 * Ask the session daemon for all available sessions.
2196 * Sets the contents of the sessions array.
2197 * Returns the number of lttng_session entries in sessions;
2198 * on error, returns a negative value.
2200 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2203 struct lttcomm_session_msg lsm
;
2204 const size_t session_size
=
2205 sizeof(struct lttng_session
) + sizeof(struct lttng_session_extended
);
2206 size_t session_count
, i
;
2207 struct lttng_session_extended
*sessions_extended_begin
;
2208 struct lttng_session
*sessions
= nullptr;
2210 memset(&lsm
, 0, sizeof(lsm
));
2211 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
;
2213 * Initialize out_sessions to NULL so it is initialized when
2214 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2215 * be subsequently freed.
2217 *out_sessions
= nullptr;
2218 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &sessions
);
2223 ret
= -LTTNG_ERR_FATAL
;
2227 if (ret
% session_size
) {
2228 ret
= -LTTNG_ERR_UNK
;
2232 session_count
= (size_t) ret
/ session_size
;
2233 sessions_extended_begin
= (struct lttng_session_extended
*) (&sessions
[session_count
]);
2235 /* Set extended session info pointers. */
2236 for (i
= 0; i
< session_count
; i
++) {
2237 struct lttng_session
*session
= &sessions
[i
];
2238 struct lttng_session_extended
*extended
= &(sessions_extended_begin
[i
]);
2240 session
->extended
.ptr
= extended
;
2243 ret
= (int) session_count
;
2244 *out_sessions
= sessions
;
2249 enum lttng_error_code
lttng_session_get_creation_time(const struct lttng_session
*session
,
2250 uint64_t *creation_time
)
2252 enum lttng_error_code ret
= LTTNG_OK
;
2253 struct lttng_session_extended
*extended
;
2255 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2256 ret
= LTTNG_ERR_INVALID
;
2260 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2261 if (!extended
->creation_time
.is_set
) {
2262 /* Not created on the session daemon yet. */
2263 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2266 *creation_time
= extended
->creation_time
.value
;
2271 int lttng_set_session_shm_path(const char *session_name
, const char *shm_path
)
2274 struct lttcomm_session_msg lsm
;
2276 if (session_name
== nullptr) {
2277 return -LTTNG_ERR_INVALID
;
2280 memset(&lsm
, 0, sizeof(lsm
));
2281 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
;
2283 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2285 ret
= -LTTNG_ERR_INVALID
;
2289 ret
= lttng_strncpy(
2290 lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "", sizeof(lsm
.u
.set_shm_path
.shm_path
));
2292 ret
= -LTTNG_ERR_INVALID
;
2296 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2302 * Ask the session daemon for all available domains of a session.
2303 * Sets the contents of the domains array.
2304 * Returns the number of lttng_domain entries in domains;
2305 * on error, returns a negative value.
2307 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
2310 struct lttcomm_session_msg lsm
;
2312 if (session_name
== nullptr) {
2313 ret
= -LTTNG_ERR_INVALID
;
2317 memset(&lsm
, 0, sizeof(lsm
));
2318 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
;
2320 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2322 ret
= -LTTNG_ERR_INVALID
;
2326 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) domains
);
2331 return ret
/ sizeof(struct lttng_domain
);
2337 * Ask the session daemon for all available channels of a session.
2338 * Sets the contents of the channels array.
2339 * Returns the number of lttng_channel entries in channels;
2340 * on error, returns a negative value.
2342 int lttng_list_channels(struct lttng_handle
*handle
, struct lttng_channel
**channels
)
2344 int ret
, total_payload_received
;
2345 struct lttcomm_session_msg lsm
;
2346 char *reception_buffer
= nullptr;
2347 size_t cmd_header_len
= 0;
2348 struct lttcomm_list_command_header
*cmd_header
= nullptr;
2349 struct lttng_dynamic_buffer tmp_buffer
;
2351 lttng_dynamic_buffer_init(&tmp_buffer
);
2353 if (handle
== nullptr) {
2354 ret
= -LTTNG_ERR_INVALID
;
2358 memset(&lsm
, 0, sizeof(lsm
));
2359 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
;
2360 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2362 ret
= -LTTNG_ERR_INVALID
;
2366 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2368 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
2373 (void **) &reception_buffer
,
2374 (void **) &cmd_header
,
2380 total_payload_received
= ret
;
2382 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2383 ret
= -LTTNG_ERR_FATAL
;
2388 ret
= LTTNG_ERR_UNK
;
2392 if (cmd_header
->count
> INT_MAX
) {
2393 ret
= -LTTNG_ERR_OVERFLOW
;
2398 enum lttng_error_code ret_code
;
2399 const struct lttng_buffer_view events_view
=
2400 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
2402 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2403 &events_view
, cmd_header
->count
, channels
);
2404 if (ret_code
!= LTTNG_OK
) {
2410 ret
= (int) cmd_header
->count
;
2413 free(reception_buffer
);
2418 * Ask the session daemon for all available events of a session channel.
2419 * Sets the contents of the events array.
2420 * Returns the number of lttng_event entries in events;
2421 * on error, returns a negative value.
2423 int lttng_list_events(struct lttng_handle
*handle
,
2424 const char *channel_name
,
2425 struct lttng_event
**events
)
2428 struct lttcomm_session_msg lsm
= {};
2429 struct lttng_payload reply
;
2430 struct lttng_payload_view lsm_view
=
2431 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
2432 unsigned int nb_events
= 0;
2434 lttng_payload_init(&reply
);
2436 /* Safety check. An handle and channel name are mandatory. */
2437 if (handle
== nullptr || channel_name
== nullptr) {
2438 ret
= -LTTNG_ERR_INVALID
;
2442 /* Initialize command parameters. */
2443 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
;
2444 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2446 ret
= -LTTNG_ERR_INVALID
;
2450 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
, sizeof(lsm
.u
.list
.channel_name
));
2452 ret
= -LTTNG_ERR_INVALID
;
2456 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2458 /* Execute command against the session daemon. */
2459 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2465 const struct lttcomm_list_command_header
*cmd_reply_header
= nullptr;
2466 const lttng_payload_view cmd_reply_header_view
=
2467 lttng_payload_view_from_payload(&reply
, 0, sizeof(*cmd_reply_header
));
2469 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2470 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2474 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2475 cmd_reply_header_view
.buffer
.data
;
2476 if (cmd_reply_header
->count
> INT_MAX
) {
2477 ret
= -LTTNG_ERR_OVERFLOW
;
2481 nb_events
= (unsigned int) cmd_reply_header
->count
;
2485 enum lttng_error_code ret_code
;
2486 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2487 &reply
, sizeof(struct lttcomm_list_command_header
), -1);
2489 ret_code
= lttng_events_create_and_flatten_from_payload(
2490 &cmd_reply_payload
, nb_events
, events
);
2491 if (ret_code
!= LTTNG_OK
) {
2492 ret
= -((int) ret_code
);
2497 ret
= (int) nb_events
;
2499 lttng_payload_reset(&reply
);
2504 * Sets the tracing_group variable with name.
2505 * This function allocates memory pointed to by tracing_group.
2506 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2508 int lttng_set_tracing_group(const char *name
)
2513 if (name
== nullptr) {
2514 ret
= -LTTNG_ERR_INVALID
;
2518 new_group
= strdup(name
);
2520 ret
= -LTTNG_ERR_FATAL
;
2524 free(tracing_group
);
2525 tracing_group
= new_group
;
2526 new_group
= nullptr;
2532 int lttng_calibrate(struct lttng_handle
*handle
__attribute__((unused
)),
2533 struct lttng_calibrate
*calibrate
__attribute__((unused
)))
2536 * This command was removed in LTTng 2.9.
2538 return -LTTNG_ERR_UND
;
2542 * Set default channel attributes.
2543 * If either or both of the arguments are null, attr content is zeroe'd.
2545 void lttng_channel_set_default_attr(struct lttng_domain
*domain
, struct lttng_channel_attr
*attr
)
2547 struct lttng_channel_extended
*extended
;
2550 if (attr
== nullptr || domain
== nullptr) {
2554 /* Save the pointer for later use */
2555 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2556 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2558 /* Same for all domains. */
2559 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2560 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2561 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2563 switch (domain
->type
) {
2564 case LTTNG_DOMAIN_KERNEL
:
2565 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2566 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2567 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2568 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2569 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2571 case LTTNG_DOMAIN_UST
:
2572 switch (domain
->buf_type
) {
2573 case LTTNG_BUFFER_PER_UID
:
2574 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2575 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2576 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2577 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2578 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2580 case LTTNG_BUFFER_PER_PID
:
2582 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2583 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2584 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2585 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2586 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2590 /* Default behavior: leave set to 0. */
2595 lttng_channel_set_default_extended_attr(domain
, extended
);
2598 /* Reassign the extended pointer. */
2599 attr
->extended
.ptr
= extended
;
2602 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2603 uint64_t *discarded_events
)
2606 struct lttng_channel_extended
*chan_ext
;
2608 if (!channel
|| !discarded_events
) {
2609 ret
= -LTTNG_ERR_INVALID
;
2613 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2616 * This can happen since the lttng_channel structure is
2617 * used for other tasks where this pointer is never set.
2619 *discarded_events
= 0;
2623 *discarded_events
= chan_ext
->discarded_events
;
2628 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
, uint64_t *lost_packets
)
2631 struct lttng_channel_extended
*chan_ext
;
2633 if (!channel
|| !lost_packets
) {
2634 ret
= -LTTNG_ERR_INVALID
;
2638 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2641 * This can happen since the lttng_channel structure is
2642 * used for other tasks where this pointer is never set.
2648 *lost_packets
= chan_ext
->lost_packets
;
2653 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2654 uint64_t *monitor_timer_interval
)
2658 if (!chan
|| !monitor_timer_interval
) {
2659 ret
= -LTTNG_ERR_INVALID
;
2663 if (!chan
->attr
.extended
.ptr
) {
2664 ret
= -LTTNG_ERR_INVALID
;
2668 *monitor_timer_interval
=
2669 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2674 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2675 uint64_t monitor_timer_interval
)
2679 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2680 ret
= -LTTNG_ERR_INVALID
;
2684 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2685 monitor_timer_interval
;
2690 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
, int64_t *blocking_timeout
)
2694 if (!chan
|| !blocking_timeout
) {
2695 ret
= -LTTNG_ERR_INVALID
;
2699 if (!chan
->attr
.extended
.ptr
) {
2700 ret
= -LTTNG_ERR_INVALID
;
2705 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
2710 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
, int64_t blocking_timeout
)
2713 int64_t msec_timeout
;
2715 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2716 ret
= -LTTNG_ERR_INVALID
;
2720 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2721 ret
= -LTTNG_ERR_INVALID
;
2726 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2727 * us to accept a narrower range of values (msecs expressed as a signed
2730 msec_timeout
= blocking_timeout
/ 1000;
2731 if (msec_timeout
!= (int32_t) msec_timeout
) {
2732 ret
= -LTTNG_ERR_INVALID
;
2736 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
=
2743 * Check if session daemon is alive.
2745 * Return 1 if alive or 0 if not.
2746 * On error returns a negative value.
2748 int lttng_session_daemon_alive(void)
2752 ret
= set_session_daemon_path();
2758 if (*sessiond_sock_path
== '\0') {
2760 * No socket path set. Weird error which means the constructor
2766 ret
= try_connect_sessiond(sessiond_sock_path
);
2777 * Set URL for a consumer for a session and domain.
2779 * Return 0 on success, else a negative value.
2781 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2782 const char *control_url
,
2783 const char *data_url
)
2787 struct lttcomm_session_msg lsm
;
2788 struct lttng_uri
*uris
= nullptr;
2790 if (handle
== nullptr || (control_url
== nullptr && data_url
== nullptr)) {
2791 ret
= -LTTNG_ERR_INVALID
;
2795 memset(&lsm
, 0, sizeof(lsm
));
2797 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
;
2799 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2801 ret
= -LTTNG_ERR_INVALID
;
2805 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2807 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2809 ret
= -LTTNG_ERR_INVALID
;
2813 lsm
.u
.uri
.size
= size
;
2815 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
2816 &lsm
, uris
, sizeof(struct lttng_uri
) * size
, nullptr);
2826 extern "C" LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2827 int lttng_enable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2835 extern "C" LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2836 int lttng_disable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2844 extern "C" LTTNG_EXPORT
int
2845 _lttng_create_session_ext(const char *name
, const char *url
, const char *datetime
);
2846 int _lttng_create_session_ext(const char *name
__attribute__((unused
)),
2847 const char *url
__attribute__((unused
)),
2848 const char *datetime
__attribute__((unused
)))
2854 * For a given session name, this call checks if the data is ready to be read
2855 * or is still being extracted by the consumer(s) hence not ready to be used by
2858 int lttng_data_pending(const char *session_name
)
2861 struct lttcomm_session_msg lsm
;
2862 uint8_t *pending
= nullptr;
2864 if (session_name
== nullptr) {
2865 return -LTTNG_ERR_INVALID
;
2868 memset(&lsm
, 0, sizeof(lsm
));
2869 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
;
2871 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2873 ret
= -LTTNG_ERR_INVALID
;
2877 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2880 } else if (ret
!= 1) {
2881 /* Unexpected payload size */
2882 ret
= -LTTNG_ERR_INVALID
;
2884 } else if (!pending
) {
2885 /* Internal error. */
2886 ret
= -LTTNG_ERR_UNK
;
2890 ret
= (int) *pending
;
2897 * Get the status of the kernel tracer
2899 * Sets the value of the argument
2901 enum lttng_error_code
lttng_get_kernel_tracer_status(enum lttng_kernel_tracer_status
*out_status
)
2903 if (out_status
== nullptr) {
2904 return LTTNG_ERR_INVALID
;
2907 struct lttcomm_session_msg lsm
= {};
2908 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
;
2910 uint32_t *raw_tracer_status
= nullptr;
2911 const auto ask_ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &raw_tracer_status
);
2913 const auto tracer_status
=
2914 lttng::make_unique_wrapper
<uint32_t, lttng::memory::free
>(raw_tracer_status
);
2917 return LTTNG_ERR_INVALID
;
2920 *out_status
= (enum lttng_kernel_tracer_status
) * tracer_status
;
2925 * Regenerate the metadata for a session.
2926 * Return 0 on success, a negative error code on error.
2928 int lttng_regenerate_metadata(const char *session_name
)
2931 struct lttcomm_session_msg lsm
;
2933 if (!session_name
) {
2934 ret
= -LTTNG_ERR_INVALID
;
2938 memset(&lsm
, 0, sizeof(lsm
));
2939 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
;
2941 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2943 ret
= -LTTNG_ERR_INVALID
;
2947 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2958 * Deprecated, replaced by lttng_regenerate_metadata.
2960 int lttng_metadata_regenerate(const char *session_name
)
2962 return lttng_regenerate_metadata(session_name
);
2966 * Regenerate the statedump of a session.
2967 * Return 0 on success, a negative error code on error.
2969 int lttng_regenerate_statedump(const char *session_name
)
2972 struct lttcomm_session_msg lsm
;
2974 if (!session_name
) {
2975 ret
= -LTTNG_ERR_INVALID
;
2979 memset(&lsm
, 0, sizeof(lsm
));
2980 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
;
2982 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2984 ret
= -LTTNG_ERR_INVALID
;
2988 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2999 _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
, bool generate_name
)
3002 struct lttcomm_session_msg lsm
= {
3003 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
,
3009 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
3010 struct lttcomm_session_msg
*message_lsm
;
3011 struct lttng_payload message
;
3012 struct lttng_payload reply
;
3013 struct lttng_trigger
*reply_trigger
= nullptr;
3014 enum lttng_domain_type domain_type
;
3015 const struct lttng_credentials user_creds
= {
3016 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3017 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3019 const char *unused_trigger_name
= nullptr;
3020 enum lttng_trigger_status trigger_status
;
3022 lttng_payload_init(&message
);
3023 lttng_payload_init(&reply
);
3026 ret
= -LTTNG_ERR_INVALID
;
3030 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3031 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3032 /* Re-using already registered trigger. */
3033 ret
= -LTTNG_ERR_INVALID
;
3038 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3039 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3040 ret
= -LTTNG_ERR_NOMEM
;
3045 if (!trigger
->creds
.uid
.is_set
) {
3046 /* Use the client's credentials as the trigger credentials. */
3047 lttng_trigger_set_credentials(trigger
, &user_creds
);
3050 * Validate that either the current trigger credentials and the
3051 * client credentials are identical or that the current user is
3052 * root. The root user can register, unregister triggers for
3053 * himself and other users.
3055 * This check is also present on the sessiond side, using the
3056 * credentials passed on the socket. These check are all
3059 const struct lttng_credentials
*trigger_creds
=
3060 lttng_trigger_get_credentials(trigger
);
3062 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3063 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3064 ret
= -LTTNG_ERR_EPERM
;
3065 goto end_unset_name
;
3070 if (!lttng_trigger_validate(trigger
)) {
3071 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3072 goto end_unset_name
;
3075 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(trigger
);
3077 lsm
.domain
.type
= domain_type
;
3079 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3081 ret
= -LTTNG_ERR_NOMEM
;
3082 goto end_unset_name
;
3085 ret
= lttng_trigger_serialize(trigger
, &message
);
3087 ret
= -LTTNG_ERR_UNK
;
3088 goto end_unset_name
;
3092 * This is needed to populate the trigger object size for the command
3095 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3097 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3100 struct lttng_payload_view message_view
=
3101 lttng_payload_view_from_payload(&message
, 0, -1);
3103 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3104 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3106 goto end_unset_name
;
3111 struct lttng_payload_view reply_view
=
3112 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3114 ret
= lttng_trigger_create_from_payload(&reply_view
, &reply_trigger
);
3116 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3117 goto end_unset_name
;
3121 if (name
|| generate_name
) {
3122 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3124 ret
= -LTTNG_ERR_NOMEM
;
3133 trigger_status
= lttng_trigger_set_name(trigger
, nullptr);
3134 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3135 ret
= -LTTNG_ERR_UNK
;
3138 lttng_payload_reset(&message
);
3139 lttng_payload_reset(&reply
);
3140 lttng_trigger_destroy(reply_trigger
);
3144 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3146 /* Register an anonymous trigger. */
3147 return _lttng_register_trigger(trigger
, nullptr, false);
3150 enum lttng_error_code
lttng_register_trigger_with_name(struct lttng_trigger
*trigger
,
3153 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3155 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3158 enum lttng_error_code
lttng_register_trigger_with_automatic_name(struct lttng_trigger
*trigger
)
3160 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3162 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3165 enum lttng_error_code
lttng_error_query_execute(const struct lttng_error_query
*query
,
3166 const struct lttng_endpoint
*endpoint
,
3167 struct lttng_error_query_results
**results
)
3170 enum lttng_error_code ret_code
;
3171 struct lttcomm_session_msg lsm
= {
3172 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
,
3178 struct lttng_payload message
;
3179 struct lttng_payload reply
;
3180 struct lttcomm_session_msg
*message_lsm
;
3182 lttng_payload_init(&message
);
3183 lttng_payload_init(&reply
);
3185 if (!query
|| !results
) {
3186 ret_code
= LTTNG_ERR_INVALID
;
3190 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3191 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3195 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3197 ret_code
= LTTNG_ERR_NOMEM
;
3201 ret
= lttng_error_query_serialize(query
, &message
);
3203 ret_code
= LTTNG_ERR_UNK
;
3207 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3208 message_lsm
->u
.error_query
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3211 struct lttng_payload_view message_view
=
3212 lttng_payload_view_from_payload(&message
, 0, -1);
3214 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3215 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3217 ret_code
= (lttng_error_code
) -ret
;
3223 ssize_t reply_create_ret
;
3224 struct lttng_payload_view reply_view
=
3225 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3228 lttng_error_query_results_create_from_payload(&reply_view
, results
);
3229 if (reply_create_ret
< 0) {
3230 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3235 ret_code
= LTTNG_OK
;
3237 lttng_payload_reset(&message
);
3238 lttng_payload_reset(&reply
);
3242 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3245 struct lttcomm_session_msg lsm
;
3246 struct lttcomm_session_msg
*message_lsm
;
3247 struct lttng_payload message
;
3248 struct lttng_payload reply
;
3249 struct lttng_trigger
*copy
= nullptr;
3250 const struct lttng_credentials user_creds
= {
3251 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3252 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3255 lttng_payload_init(&message
);
3256 lttng_payload_init(&reply
);
3259 ret
= -LTTNG_ERR_INVALID
;
3263 copy
= lttng_trigger_copy(trigger
);
3265 ret
= -LTTNG_ERR_UNK
;
3269 if (!copy
->creds
.uid
.is_set
) {
3270 /* Use the client credentials as the trigger credentials */
3271 lttng_trigger_set_credentials(copy
, &user_creds
);
3274 * Validate that either the current trigger credentials and the
3275 * client credentials are identical or that the current user is
3276 * root. The root user can register, unregister triggers for
3277 * himself and other users.
3279 * This check is also present on the sessiond side, using the
3280 * credentials passed on the socket. These check are all
3283 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(copy
);
3284 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3285 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3286 ret
= -LTTNG_ERR_EPERM
;
3292 if (!lttng_trigger_validate(copy
)) {
3293 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3297 memset(&lsm
, 0, sizeof(lsm
));
3298 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
;
3300 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3302 ret
= -LTTNG_ERR_NOMEM
;
3306 ret
= lttng_trigger_serialize(copy
, &message
);
3308 ret
= -LTTNG_ERR_UNK
;
3313 * This is needed to populate the trigger object size for the command
3314 * header and number of fds sent.
3316 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3318 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3321 struct lttng_payload_view message_view
=
3322 lttng_payload_view_from_payload(&message
, 0, -1);
3325 * Update the message header with the number of fd that will be
3328 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3330 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3338 lttng_trigger_destroy(copy
);
3339 lttng_payload_reset(&message
);
3340 lttng_payload_reset(&reply
);
3345 * Ask the session daemon for all registered triggers for the current user.
3347 * Allocates and return an lttng_triggers set.
3348 * On error, returns a suitable lttng_error_code.
3350 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3353 enum lttng_error_code ret_code
= LTTNG_OK
;
3354 struct lttcomm_session_msg lsm
= {
3355 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
,
3361 struct lttng_triggers
*local_triggers
= nullptr;
3362 struct lttng_payload reply
;
3363 struct lttng_payload_view lsm_view
=
3364 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
3366 lttng_payload_init(&reply
);
3368 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3370 ret_code
= (enum lttng_error_code
) - ret
;
3375 struct lttng_payload_view reply_view
=
3376 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3378 ret
= lttng_triggers_create_from_payload(&reply_view
, &local_triggers
);
3380 ret_code
= LTTNG_ERR_FATAL
;
3385 *triggers
= local_triggers
;
3386 local_triggers
= nullptr;
3388 lttng_payload_reset(&reply
);
3389 lttng_triggers_destroy(local_triggers
);
3396 static void __attribute__((constructor
)) init()
3398 /* Set default session group */
3399 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3405 static void __attribute__((destructor
)) lttng_ctl_exit()
3407 free(tracing_group
);