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_PYTHON
:
97 memcpy(dst
, src
, sizeof(struct lttng_domain
));
100 memset(dst
, 0, sizeof(struct lttng_domain
));
107 * Send lttcomm_session_msg to the session daemon.
109 * On success, returns the number of bytes sent (>=0)
110 * On error, returns -1
112 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
117 ret
= -LTTNG_ERR_NO_SESSIOND
;
121 DBG("LSM cmd type: '%s' (%d)",
122 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) lsm
->cmd_type
),
125 ret
= lttcomm_send_creds_unix_sock(
126 sessiond_socket
, lsm
, sizeof(struct lttcomm_session_msg
));
128 ret
= -LTTNG_ERR_FATAL
;
136 * Send var len data to the session daemon.
138 * On success, returns the number of bytes sent (>=0)
139 * On error, returns -1
141 static int send_session_varlen(const void *data
, size_t len
)
146 ret
= -LTTNG_ERR_NO_SESSIOND
;
155 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
157 ret
= -LTTNG_ERR_FATAL
;
165 * Send file descriptors to the session daemon.
167 * On success, returns the number of bytes sent (>=0)
168 * On error, returns -1
170 static int send_session_fds(const int *fds
, size_t nb_fd
)
175 ret
= -LTTNG_ERR_NO_SESSIOND
;
179 if (!fds
|| !nb_fd
) {
184 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
186 ret
= -LTTNG_ERR_FATAL
;
194 * Receive data from the sessiond socket.
196 * On success, returns the number of bytes received (>=0)
197 * On error, returns a negative lttng_error_code.
199 static int recv_data_sessiond(void *buf
, size_t len
)
203 LTTNG_ASSERT(len
> 0);
206 ret
= -LTTNG_ERR_NO_SESSIOND
;
210 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
212 ret
= -LTTNG_ERR_FATAL
;
213 } else if (ret
== 0) {
214 ret
= -LTTNG_ERR_NO_SESSIOND
;
222 * Receive a payload from the session daemon by appending to an existing
224 * On success, returns the number of bytes received (>=0)
225 * On error, returns a negative lttng_error_code.
227 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
230 const size_t original_payload_size
= payload
->buffer
.size
;
232 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
, payload
->buffer
.size
+ len
);
234 ret
= -LTTNG_ERR_NOMEM
;
238 ret
= recv_data_sessiond(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()
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, nullptr);
263 if (grp_list_size
< 0) {
268 /* Alloc group list of the right size */
269 grp_list
= calloc
<gid_t
>(grp_list_size
);
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(uint64_t num_bytes_requested_per_cpu
)
297 enum lttng_error_code ret_code
;
299 uint64_t best_mem_info
;
300 uint64_t num_bytes_requested_total
;
303 * Get the number of CPU currently online to compute the amount of
304 * memory needed to create a buffer for every CPU.
306 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
308 ret_code
= LTTNG_ERR_FATAL
;
312 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
314 ret_code
= LTTNG_ERR_OVERFLOW
;
318 num_bytes_requested_total
= num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
321 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
322 * reliable estimate we can get but it is only exposed by the kernel
323 * since 3.14. (See Linux kernel commit:
324 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
326 ret
= utils_get_memory_available(&best_mem_info
);
332 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
333 * is a sanity check for obvious user error.
335 ret
= utils_get_memory_total(&best_mem_info
);
340 /* No valid source of information. */
341 ret_code
= LTTNG_ERR_NOMEM
;
345 if (best_mem_info
>= num_bytes_requested_total
) {
348 ret_code
= LTTNG_ERR_NOMEM
;
355 * Try connect to session daemon with sock_path.
357 * Return 0 on success, else -1
359 static int try_connect_sessiond(const char *sock_path
)
363 /* If socket exist, we check if the daemon listens for connect. */
364 ret
= access(sock_path
, F_OK
);
370 ret
= lttcomm_connect_unix_sock(sock_path
);
376 ret
= lttcomm_close_unix_sock(ret
);
378 PERROR("lttcomm_close_unix_sock");
388 * Set sessiond socket path by putting it in the global sessiond_sock_path
391 * Returns 0 on success, negative value on failure (the sessiond socket path
392 * is somehow too long or ENOMEM).
394 static int set_session_daemon_path()
396 int in_tgroup
= 0; /* In tracing group. */
402 /* Are we in the tracing group ? */
403 in_tgroup
= lttng_check_tracing_group();
406 if ((uid
== 0) || in_tgroup
== 1) {
407 const int ret
= lttng_strncpy(sessiond_sock_path
,
408 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
409 sizeof(sessiond_sock_path
));
421 ret
= try_connect_sessiond(sessiond_sock_path
);
425 /* Global session daemon not available... */
427 /* ...or not in tracing group (and not root), default */
430 * With GNU C < 2.1, snprintf returns -1 if the target buffer
432 * With GNU C >= 2.1, snprintf returns the required size
433 * (excluding closing null)
435 ret
= snprintf(sessiond_sock_path
,
436 sizeof(sessiond_sock_path
),
437 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
438 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()
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()
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()
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
, size_t *user_len
)
506 ret
= -LTTNG_ERR_INVALID
;
510 buf
= zmalloc
<char>(len
);
516 ret
= recv_data_sessiond(buf
, len
);
522 ret
= -LTTNG_ERR_INVALID
;
526 /* Move ownership of command header buffer to user. */
531 /* No command header. */
547 * Ask the session daemon a specific command and put the data into buf.
548 * Takes extra var. len. data and file descriptors as input to send to the
551 * Return size of data (only payload, not header) or a negative error code.
553 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
558 void **user_payload_buf
,
559 void **user_cmd_header_buf
,
560 size_t *user_cmd_header_len
)
564 struct lttcomm_lttng_msg llm
;
566 ret
= connect_sessiond();
568 ret
= -LTTNG_ERR_NO_SESSIOND
;
571 sessiond_socket
= ret
;
575 ret
= send_session_msg(lsm
);
577 /* Ret value is a valid lttng error code. */
580 /* Send var len data */
581 ret
= send_session_varlen(vardata
, vardata_len
);
583 /* Ret value is a valid lttng error code. */
588 ret
= send_session_fds(fds
, nb_fd
);
590 /* Ret value is a valid lttng error code. */
594 /* Get header from data transmission */
595 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
597 /* Ret value is a valid lttng error code. */
601 /* Check error code if OK */
602 if (llm
.ret_code
!= LTTNG_OK
) {
607 /* Get command header from data transmission */
608 ret
= recv_sessiond_optional_data(
609 llm
.cmd_header_size
, user_cmd_header_buf
, user_cmd_header_len
);
614 /* Get payload from data transmission */
615 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
, &payload_len
);
623 disconnect_sessiond();
627 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
, struct lttng_payload
*reply
)
630 struct lttcomm_lttng_msg llm
;
631 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
633 LTTNG_ASSERT(reply
->buffer
.size
== 0);
634 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
636 ret
= connect_sessiond();
638 ret
= -LTTNG_ERR_NO_SESSIOND
;
641 sessiond_socket
= ret
;
645 /* Send command to session daemon */
646 ret
= lttcomm_send_creds_unix_sock(
647 sessiond_socket
, message
->buffer
.data
, message
->buffer
.size
);
649 ret
= -LTTNG_ERR_FATAL
;
654 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
, message
);
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(sessiond_socket
, llm
.fd_count
, reply
);
703 /* Don't return the llm header to the caller. */
704 memmove(reply
->buffer
.data
,
705 reply
->buffer
.data
+ sizeof(llm
),
706 reply
->buffer
.size
- sizeof(llm
));
707 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
709 /* Can't happen as size is reduced. */
713 ret
= reply
->buffer
.size
;
716 disconnect_sessiond();
721 * Create lttng handle and return pointer.
723 * The returned pointer will be NULL in case of malloc() error.
725 struct lttng_handle
*lttng_create_handle(const char *session_name
, struct lttng_domain
*domain
)
728 struct lttng_handle
*handle
= nullptr;
730 handle
= zmalloc
<lttng_handle
>();
731 if (handle
== nullptr) {
732 PERROR("malloc handle");
736 /* Copy session name */
737 ret
= lttng_strncpy(handle
->session_name
, session_name
?: "", sizeof(handle
->session_name
));
742 /* Copy lttng domain or leave initialized to 0. */
744 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
755 * Destroy handle by free(3) the pointer.
757 void lttng_destroy_handle(struct lttng_handle
*handle
)
763 * Register an outside consumer.
765 * Returns size of returned session payload data or a negative error code.
767 int lttng_register_consumer(struct lttng_handle
*handle
, const char *socket_path
)
770 struct lttcomm_session_msg lsm
;
772 if (handle
== nullptr || socket_path
== nullptr) {
773 ret
= -LTTNG_ERR_INVALID
;
777 memset(&lsm
, 0, sizeof(lsm
));
778 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
;
779 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
781 ret
= -LTTNG_ERR_INVALID
;
785 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
787 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
789 ret
= -LTTNG_ERR_INVALID
;
793 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
799 * Start tracing for all traces of the session.
801 * Returns size of returned session payload data or a negative error code.
803 int lttng_start_tracing(const char *session_name
)
806 struct lttcomm_session_msg lsm
;
808 if (session_name
== nullptr) {
809 ret
= -LTTNG_ERR_INVALID
;
813 memset(&lsm
, 0, sizeof(lsm
));
814 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_START_TRACE
;
816 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
818 ret
= -LTTNG_ERR_INVALID
;
822 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
828 * Stop tracing for all traces of the session.
830 static int _lttng_stop_tracing(const char *session_name
, int wait
)
833 struct lttcomm_session_msg lsm
;
835 if (session_name
== nullptr) {
836 ret
= -LTTNG_ERR_INVALID
;
840 memset(&lsm
, 0, sizeof(lsm
));
841 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
;
843 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
845 ret
= -LTTNG_ERR_INVALID
;
849 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
850 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
858 /* Check for data availability */
860 data_ret
= lttng_data_pending(session_name
);
862 /* Return the data available call error. */
868 * Data sleep time before retrying (in usec). Don't sleep if the
869 * call returned value indicates availability.
872 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
874 } while (data_ret
!= 0);
882 * Stop tracing and wait for data availability.
884 int lttng_stop_tracing(const char *session_name
)
886 return _lttng_stop_tracing(session_name
, 1);
890 * Stop tracing but _don't_ wait for data availability.
892 int lttng_stop_tracing_no_wait(const char *session_name
)
894 return _lttng_stop_tracing(session_name
, 0);
898 * Add context to a channel.
900 * If the given channel is NULL, add the contexts to all channels.
901 * The event_name param is ignored.
903 * Returns the size of the returned payload data or a negative error code.
905 int lttng_add_context(struct lttng_handle
*handle
,
906 struct lttng_event_context
*ctx
,
907 const char *event_name
__attribute__((unused
)),
908 const char *channel_name
)
911 struct lttcomm_session_msg lsm
= {
912 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
,
918 struct lttng_payload payload
;
920 lttng_payload_init(&payload
);
922 /* Safety check. Both are mandatory. */
923 if (handle
== nullptr || ctx
== nullptr) {
924 ret
= -LTTNG_ERR_INVALID
;
928 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
930 ret
= -LTTNG_ERR_NOMEM
;
934 /* If no channel name, send empty string. */
936 lsm
.u
.context
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.context
.channel_name
));
938 ret
= -LTTNG_ERR_INVALID
;
942 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
943 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
945 ret
= -LTTNG_ERR_INVALID
;
949 ret
= lttng_event_context_serialize(ctx
, &payload
);
951 ret
= -LTTNG_ERR_INVALID
;
955 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
957 /* Update message header. */
958 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
961 struct lttng_payload reply
;
962 struct lttng_payload_view payload_view
=
963 lttng_payload_view_from_payload(&payload
, 0, -1);
965 lttng_payload_init(&reply
);
966 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
967 lttng_payload_reset(&reply
);
974 lttng_payload_reset(&payload
);
979 * Enable event(s) for a channel.
981 * If no event name is specified, all events are enabled.
982 * If no channel name is specified, the default 'channel0' is used.
984 * Returns size of returned session payload data or a negative error code.
986 int lttng_enable_event(struct lttng_handle
*handle
,
987 struct lttng_event
*ev
,
988 const char *channel_name
)
990 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
, nullptr, 0, nullptr);
994 * Create or enable an event with a filter expression.
996 * Return negative error value on error.
997 * Return size of returned session payload data if OK.
999 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1000 struct lttng_event
*event
,
1001 const char *channel_name
,
1002 const char *filter_expression
)
1004 return lttng_enable_event_with_exclusions(
1005 handle
, event
, channel_name
, filter_expression
, 0, nullptr);
1009 * Depending on the event, return a newly allocated agent filter expression or
1010 * NULL if not applicable.
1012 * An event with NO loglevel and the name is * will return NULL.
1014 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1017 char *agent_filter
= nullptr;
1021 /* Don't add filter for the '*' event. */
1022 if (strcmp(ev
->name
, "*") != 0) {
1025 &agent_filter
, "(%s) && (logger_name == \"%s\")", filter
, ev
->name
);
1027 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1035 /* Add loglevel filtering if any for the JUL domain. */
1036 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1039 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1045 if (filter
|| agent_filter
) {
1048 err
= asprintf(&new_filter
,
1049 "(%s) && (int_loglevel %s %d)",
1050 agent_filter
? agent_filter
: filter
,
1056 agent_filter
= new_filter
;
1058 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
, ev
->loglevel
);
1066 return agent_filter
;
1073 * Enable event(s) for a channel, possibly with exclusions and a filter.
1074 * If no event name is specified, all events are enabled.
1075 * If no channel name is specified, the default name is used.
1076 * If filter expression is not NULL, the filter is set for the event.
1077 * If exclusion count is not zero, the exclusions are set for the event.
1078 * Returns size of returned session payload data or a negative error code.
1080 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1081 struct lttng_event
*ev
,
1082 const char *channel_name
,
1083 const char *original_filter_expression
,
1084 int exclusion_count
,
1085 char **exclusion_list
)
1087 struct lttcomm_session_msg lsm
= {
1088 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
,
1094 struct lttng_payload payload
;
1096 unsigned int free_filter_expression
= 0;
1097 struct filter_parser_ctx
*ctx
= nullptr;
1098 size_t bytecode_len
= 0;
1101 * We have either a filter or some exclusions, so we need to set up
1102 * a variable-length payload from where to send the data.
1104 lttng_payload_init(&payload
);
1107 * Cast as non-const since we may replace the filter expression
1108 * by a dynamically allocated string. Otherwise, the original
1109 * string is not modified.
1111 char *filter_expression
= (char *) original_filter_expression
;
1113 if (handle
== nullptr || ev
== nullptr) {
1114 ret
= -LTTNG_ERR_INVALID
;
1119 * Empty filter string will always be rejected by the parser
1120 * anyway, so treat this corner-case early to eliminate
1121 * lttng_fmemopen error for 0-byte allocation.
1123 if (filter_expression
&& filter_expression
[0] == '\0') {
1124 ret
= -LTTNG_ERR_INVALID
;
1128 if (ev
->name
[0] == '\0') {
1129 /* Enable all events. */
1130 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1131 LTTNG_ASSERT(ret
== 0);
1134 /* Parse filter expression. */
1135 if (filter_expression
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1136 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1137 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1138 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1139 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1140 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1143 /* Setup agent filter if needed. */
1144 agent_filter
= set_agent_filter(filter_expression
, ev
);
1145 if (!agent_filter
) {
1146 if (!filter_expression
) {
1148 * No JUL and no filter, just skip
1155 * With an agent filter, the original filter has
1156 * been added to it thus replace the filter
1159 filter_expression
= agent_filter
;
1160 free_filter_expression
= 1;
1164 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1165 ret
= -LTTNG_ERR_FILTER_INVAL
;
1169 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1174 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1175 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1176 ret
= -LTTNG_ERR_FILTER_INVAL
;
1182 ret
= lttng_event_serialize(ev
,
1187 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1190 ret
= -LTTNG_ERR_INVALID
;
1194 /* If no channel name, send empty string. */
1195 ret
= lttng_strncpy(
1196 lsm
.u
.enable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.enable
.channel_name
));
1198 ret
= -LTTNG_ERR_INVALID
;
1203 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1206 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1208 ret
= -LTTNG_ERR_INVALID
;
1212 /* Length of the serialized event. */
1213 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1216 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1217 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1224 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1225 if (fd_count
== 1) {
1226 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1232 fd_to_send
= fd_handle_get_fd(h
);
1236 lsm
.fd_count
= fd_count
;
1238 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1239 fd_count
? &fd_to_send
: nullptr,
1241 view
.buffer
.size
? view
.buffer
.data
:
1250 if (filter_expression
&& ctx
) {
1251 filter_bytecode_free(ctx
);
1252 filter_ir_free(ctx
);
1253 filter_parser_ctx_free(ctx
);
1255 if (free_filter_expression
) {
1257 * The filter expression has been replaced and must be freed as
1258 * it is not the original filter expression received as a
1261 free(filter_expression
);
1264 * Return directly to the caller and don't ask the sessiond since
1265 * something went wrong in the parsing of data above.
1267 lttng_payload_reset(&payload
);
1271 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1272 struct lttng_event
*ev
,
1273 const char *channel_name
,
1274 const char *original_filter_expression
)
1276 struct lttcomm_session_msg lsm
= {
1277 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
,
1283 struct lttng_payload payload
;
1285 unsigned int free_filter_expression
= 0;
1286 struct filter_parser_ctx
*ctx
= nullptr;
1287 size_t bytecode_len
= 0;
1290 * We have either a filter or some exclusions, so we need to set up
1291 * a variable-length payload from where to send the data.
1293 lttng_payload_init(&payload
);
1296 * Cast as non-const since we may replace the filter expression
1297 * by a dynamically allocated string. Otherwise, the original
1298 * string is not modified.
1300 char *filter_expression
= (char *) original_filter_expression
;
1302 if (handle
== nullptr || ev
== nullptr) {
1303 ret
= -LTTNG_ERR_INVALID
;
1308 * Empty filter string will always be rejected by the parser
1309 * anyway, so treat this corner-case early to eliminate
1310 * lttng_fmemopen error for 0-byte allocation.
1312 if (filter_expression
&& filter_expression
[0] == '\0') {
1313 ret
= -LTTNG_ERR_INVALID
;
1317 /* Parse filter expression. */
1318 if (filter_expression
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1319 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1320 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1321 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1322 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1323 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1326 /* Setup agent filter if needed. */
1327 agent_filter
= set_agent_filter(filter_expression
, ev
);
1328 if (!agent_filter
) {
1329 if (!filter_expression
) {
1331 * No JUL and no filter, just skip
1338 * With an agent filter, the original filter has
1339 * been added to it thus replace the filter
1342 filter_expression
= agent_filter
;
1343 free_filter_expression
= 1;
1347 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1348 ret
= -LTTNG_ERR_FILTER_INVAL
;
1352 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1357 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1358 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1359 ret
= -LTTNG_ERR_FILTER_INVAL
;
1365 ret
= lttng_event_serialize(ev
,
1370 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1373 ret
= -LTTNG_ERR_INVALID
;
1377 /* If no channel name, send empty string. */
1378 ret
= lttng_strncpy(
1379 lsm
.u
.disable
.channel_name
, channel_name
?: "", 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
, sizeof(lsm
.session
.name
));
1391 ret
= -LTTNG_ERR_INVALID
;
1395 /* Length of the serialized event. */
1396 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1399 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1400 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1407 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1408 if (fd_count
== 1) {
1409 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1415 fd_to_send
= fd_handle_get_fd(h
);
1419 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1420 fd_count
? &fd_to_send
: nullptr,
1422 view
.buffer
.size
? view
.buffer
.data
:
1431 if (filter_expression
&& ctx
) {
1432 filter_bytecode_free(ctx
);
1433 filter_ir_free(ctx
);
1434 filter_parser_ctx_free(ctx
);
1436 if (free_filter_expression
) {
1438 * The filter expression has been replaced and must be freed as
1439 * it is not the original filter expression received as a
1442 free(filter_expression
);
1445 * Return directly to the caller and don't ask the sessiond since
1446 * something went wrong in the parsing of data above.
1448 lttng_payload_reset(&payload
);
1453 * Disable event(s) of a channel and domain.
1454 * If no event name is specified, all events are disabled.
1455 * If no channel name is specified, the default 'channel0' is used.
1456 * Returns size of returned session payload data or a negative error code.
1458 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
, const char *channel_name
)
1461 struct lttng_event ev
;
1463 memset(&ev
, 0, sizeof(ev
));
1465 ev
.type
= LTTNG_EVENT_ALL
;
1466 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1468 ret
= -LTTNG_ERR_INVALID
;
1472 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, nullptr);
1477 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1479 struct lttng_channel
*channel
= nullptr;
1485 /* Validate domain. */
1486 switch (domain
->type
) {
1487 case LTTNG_DOMAIN_UST
:
1488 switch (domain
->buf_type
) {
1489 case LTTNG_BUFFER_PER_UID
:
1490 case LTTNG_BUFFER_PER_PID
:
1496 case LTTNG_DOMAIN_KERNEL
:
1497 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1505 channel
= lttng_channel_create_internal();
1510 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1515 void lttng_channel_destroy(struct lttng_channel
*channel
)
1521 if (channel
->attr
.extended
.ptr
) {
1522 free(channel
->attr
.extended
.ptr
);
1528 * Enable channel per domain
1529 * Returns size of returned session payload data or a negative error code.
1531 int lttng_enable_channel(struct lttng_handle
*handle
, 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
= nullptr;
1540 lttng_dynamic_buffer_init(&buffer
);
1542 /* NULL arguments are forbidden. No default values. */
1543 if (handle
== nullptr || in_chan
== nullptr) {
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
> UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1554 ret
= -LTTNG_ERR_OVERFLOW
;
1558 total_buffer_size_needed_per_cpu
= in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1559 ret_code
= check_enough_available_memory(total_buffer_size_needed_per_cpu
);
1560 if (ret_code
!= LTTNG_OK
) {
1565 /* Copy the channel for easier manipulation. */
1566 channel
= lttng_channel_copy(in_chan
);
1568 ret
= -LTTNG_ERR_NOMEM
;
1572 /* Populate the channel extended attribute if necessary. */
1573 if (!channel
->attr
.extended
.ptr
) {
1574 struct lttng_channel_extended
*extended
= zmalloc
<lttng_channel_extended
>();
1577 ret
= -LTTNG_ERR_NOMEM
;
1581 lttng_channel_set_default_extended_attr(&handle
->domain
, extended
);
1582 channel
->attr
.extended
.ptr
= extended
;
1585 /* Prepare the payload */
1586 memset(&lsm
, 0, sizeof(lsm
));
1588 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
;
1589 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1591 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1593 ret
= -LTTNG_ERR_INVALID
;
1597 ret
= lttng_channel_serialize(channel
, &buffer
);
1599 ret
= -LTTNG_ERR_FATAL
;
1603 lsm
.u
.channel
.length
= buffer
.size
;
1605 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
, buffer
.size
, nullptr);
1607 lttng_channel_destroy(channel
);
1608 lttng_dynamic_buffer_reset(&buffer
);
1613 * All tracing will be stopped for registered events of the channel.
1614 * Returns size of returned session payload data or a negative error code.
1616 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1619 struct lttcomm_session_msg lsm
;
1621 /* Safety check. Both are mandatory. */
1622 if (handle
== nullptr || name
== nullptr) {
1623 return -LTTNG_ERR_INVALID
;
1626 memset(&lsm
, 0, sizeof(lsm
));
1628 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
;
1630 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
, sizeof(lsm
.u
.disable
.channel_name
));
1632 ret
= -LTTNG_ERR_INVALID
;
1636 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1638 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1640 ret
= -LTTNG_ERR_INVALID
;
1644 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
1650 * Lists all available tracepoints of domain.
1651 * Sets the contents of the events array.
1652 * Returns the number of lttng_event entries in events;
1653 * on error, returns a negative value.
1655 int lttng_list_tracepoints(struct lttng_handle
*handle
, struct lttng_event
**events
)
1657 enum lttng_error_code ret_code
;
1658 int ret
, total_payload_received
;
1659 char *reception_buffer
= nullptr;
1660 struct lttcomm_session_msg lsm
= {
1661 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
,
1667 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1668 size_t cmd_header_len
;
1669 unsigned int nb_events
= 0;
1671 if (handle
== nullptr) {
1672 ret
= -LTTNG_ERR_INVALID
;
1676 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1678 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1683 (void **) &reception_buffer
,
1684 (void **) &cmd_header
,
1690 total_payload_received
= ret
;
1693 ret
= -LTTNG_ERR_UNK
;
1697 if (cmd_header
->count
> INT_MAX
) {
1698 ret
= -LTTNG_ERR_OVERFLOW
;
1702 nb_events
= (unsigned int) cmd_header
->count
;
1705 struct lttng_buffer_view events_view
=
1706 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1707 struct lttng_payload_view events_payload_view
=
1708 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1710 ret_code
= lttng_events_create_and_flatten_from_payload(
1711 &events_payload_view
, nb_events
, events
);
1712 if (ret_code
!= LTTNG_OK
) {
1718 ret
= (int) nb_events
;
1722 free(reception_buffer
);
1727 * Lists all available tracepoint fields of domain.
1728 * Sets the contents of the event field array.
1729 * Returns the number of lttng_event_field entries in events;
1730 * on error, returns a negative value.
1732 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
, struct lttng_event_field
**fields
)
1734 enum lttng_error_code ret_code
;
1736 struct lttcomm_session_msg lsm
;
1737 const struct lttcomm_list_command_header
*cmd_header
= nullptr;
1738 unsigned int nb_event_fields
= 0;
1739 struct lttng_payload reply
;
1741 lttng_payload_init(&reply
);
1743 if (handle
== nullptr) {
1744 ret
= -LTTNG_ERR_INVALID
;
1748 memset(&lsm
, 0, sizeof(lsm
));
1749 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
;
1750 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1753 lttng_payload_view message_view
=
1754 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
1756 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1763 const lttng_buffer_view cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
1764 &reply
.buffer
, 0, sizeof(*cmd_header
));
1766 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1767 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1771 cmd_header
= (struct lttcomm_list_command_header
*) cmd_header_view
.data
;
1774 if (cmd_header
->count
> INT_MAX
) {
1775 ret
= -LTTNG_ERR_OVERFLOW
;
1779 nb_event_fields
= cmd_header
->count
;
1782 lttng_payload_view reply_view
=
1783 lttng_payload_view_from_payload(&reply
, sizeof(*cmd_header
), -1);
1785 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1786 &reply_view
, nb_event_fields
, fields
);
1787 if (ret_code
!= LTTNG_OK
) {
1793 ret
= nb_event_fields
;
1796 lttng_payload_reset(&reply
);
1801 * Lists all available kernel system calls. Allocates and sets the contents of
1804 * Returns the number of lttng_event entries in events; on error, returns a
1807 int lttng_list_syscalls(struct lttng_event
**events
)
1809 enum lttng_error_code ret_code
;
1810 int ret
, total_payload_received
;
1811 char *reception_buffer
= nullptr;
1812 struct lttcomm_session_msg lsm
= {};
1813 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1814 size_t cmd_header_len
;
1815 uint32_t nb_events
= 0;
1818 ret
= -LTTNG_ERR_INVALID
;
1822 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
;
1823 /* Force kernel domain for system calls. */
1824 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1826 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1831 (void **) &reception_buffer
,
1832 (void **) &cmd_header
,
1837 total_payload_received
= ret
;
1840 ret
= -LTTNG_ERR_UNK
;
1844 if (cmd_header
->count
> INT_MAX
) {
1845 ret
= -LTTNG_ERR_OVERFLOW
;
1849 nb_events
= (unsigned int) cmd_header
->count
;
1852 const struct lttng_buffer_view events_view
=
1853 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1854 struct lttng_payload_view events_payload_view
=
1855 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1857 ret_code
= lttng_events_create_and_flatten_from_payload(
1858 &events_payload_view
, nb_events
, events
);
1859 if (ret_code
!= LTTNG_OK
) {
1865 ret
= (int) nb_events
;
1868 free(reception_buffer
);
1874 * Returns a human readable string describing
1875 * the error code (positive or negative value).
1877 const char *lttng_strerror(int code
)
1883 return error_get_str(code
);
1886 enum lttng_error_code
lttng_create_session_ext(struct lttng_session_descriptor
*session_descriptor
)
1888 enum lttng_error_code ret_code
;
1889 struct lttcomm_session_msg lsm
= {
1890 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
,
1896 void *reply
= nullptr;
1897 struct lttng_buffer_view reply_view
;
1899 bool sessiond_must_generate_ouput
;
1900 struct lttng_dynamic_buffer payload
;
1902 size_t descriptor_size
;
1903 struct lttng_session_descriptor
*descriptor_reply
= nullptr;
1905 lttng_dynamic_buffer_init(&payload
);
1906 if (!session_descriptor
) {
1907 ret_code
= LTTNG_ERR_INVALID
;
1911 sessiond_must_generate_ouput
=
1912 !lttng_session_descriptor_is_output_destination_initialized(session_descriptor
);
1913 if (sessiond_must_generate_ouput
) {
1914 const char *home_dir
= utils_get_home_dir();
1915 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1917 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1918 ret_code
= LTTNG_ERR_FATAL
;
1922 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1923 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
, home_dir_len
);
1925 ret_code
= LTTNG_ERR_NOMEM
;
1930 descriptor_size
= payload
.size
;
1931 ret
= lttng_session_descriptor_serialize(session_descriptor
, &payload
);
1933 ret_code
= LTTNG_ERR_INVALID
;
1936 descriptor_size
= payload
.size
- descriptor_size
;
1937 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1939 /* Command returns a session descriptor on success. */
1940 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1941 &lsm
, payload
.data
, payload
.size
, &reply
);
1942 if (reply_ret
< 0) {
1943 ret_code
= (lttng_error_code
) -reply_ret
;
1945 } else if (reply_ret
== 0) {
1946 /* Socket unexpectedly closed by the session daemon. */
1947 ret_code
= LTTNG_ERR_FATAL
;
1951 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1952 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
, &descriptor_reply
);
1954 ret_code
= LTTNG_ERR_FATAL
;
1957 ret_code
= LTTNG_OK
;
1958 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1961 lttng_dynamic_buffer_reset(&payload
);
1962 lttng_session_descriptor_destroy(descriptor_reply
);
1967 * Create a new session using name and url for destination.
1969 * Return 0 on success else a negative LTTng error code.
1971 int lttng_create_session(const char *name
, const char *url
)
1975 struct lttng_uri
*uris
= nullptr;
1976 struct lttng_session_descriptor
*descriptor
= nullptr;
1977 enum lttng_error_code ret_code
;
1980 ret
= -LTTNG_ERR_INVALID
;
1984 size
= uri_parse_str_urls(url
, nullptr, &uris
);
1986 ret
= -LTTNG_ERR_INVALID
;
1991 descriptor
= lttng_session_descriptor_create(name
);
1994 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1995 ret
= -LTTNG_ERR_INVALID
;
1998 descriptor
= lttng_session_descriptor_local_create(name
, uris
[0].dst
.path
);
2001 descriptor
= lttng_session_descriptor_network_create(name
, url
, nullptr);
2004 ret
= -LTTNG_ERR_INVALID
;
2008 ret
= -LTTNG_ERR_INVALID
;
2011 ret_code
= lttng_create_session_ext(descriptor
);
2012 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2014 lttng_session_descriptor_destroy(descriptor
);
2020 * Create a session exclusively used for snapshot.
2022 * Return 0 on success else a negative LTTng error code.
2024 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2027 enum lttng_error_code ret_code
;
2029 struct lttng_uri
*uris
= nullptr;
2030 struct lttng_session_descriptor
*descriptor
= nullptr;
2033 ret
= -LTTNG_ERR_INVALID
;
2037 size
= uri_parse_str_urls(snapshot_url
, nullptr, &uris
);
2039 ret
= -LTTNG_ERR_INVALID
;
2043 * If the user does not specify a custom subdir, use the session name.
2045 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2046 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2048 PERROR("Failed to set session name as network destination sub-directory");
2049 ret
= -LTTNG_ERR_FATAL
;
2051 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2052 /* Truncated output. */
2053 ret
= -LTTNG_ERR_INVALID
;
2060 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2063 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2064 ret
= -LTTNG_ERR_INVALID
;
2067 descriptor
= lttng_session_descriptor_snapshot_local_create(name
, uris
[0].dst
.path
);
2070 descriptor
= lttng_session_descriptor_snapshot_network_create(
2071 name
, snapshot_url
, nullptr);
2074 ret
= -LTTNG_ERR_INVALID
;
2078 ret
= -LTTNG_ERR_INVALID
;
2081 ret_code
= lttng_create_session_ext(descriptor
);
2082 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2084 lttng_session_descriptor_destroy(descriptor
);
2090 * Create a session exclusively used for live.
2092 * Return 0 on success else a negative LTTng error code.
2094 int lttng_create_session_live(const char *name
, const char *url
, unsigned int timer_interval
)
2097 enum lttng_error_code ret_code
;
2098 struct lttng_session_descriptor
*descriptor
= nullptr;
2101 ret
= -LTTNG_ERR_INVALID
;
2106 descriptor
= lttng_session_descriptor_live_network_create(
2107 name
, url
, nullptr, timer_interval
);
2109 descriptor
= lttng_session_descriptor_live_create(name
, timer_interval
);
2112 ret
= -LTTNG_ERR_INVALID
;
2115 ret_code
= lttng_create_session_ext(descriptor
);
2116 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2118 lttng_session_descriptor_destroy(descriptor
);
2123 * Stop the session and wait for the data before destroying it
2125 * Return 0 on success else a negative LTTng error code.
2127 int lttng_destroy_session(const char *session_name
)
2130 enum lttng_error_code ret_code
;
2131 enum lttng_destruction_handle_status status
;
2132 struct lttng_destruction_handle
*handle
= nullptr;
2135 * Stop the tracing and wait for the data to be
2138 ret
= _lttng_stop_tracing(session_name
, 1);
2139 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2143 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2144 if (ret_code
!= LTTNG_OK
) {
2145 ret
= (int) -ret_code
;
2148 LTTNG_ASSERT(handle
);
2150 /* Block until the completion of the destruction of the session. */
2151 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2152 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2153 ret
= -LTTNG_ERR_UNK
;
2157 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2158 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2159 ret
= -LTTNG_ERR_UNK
;
2162 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2164 lttng_destruction_handle_destroy(handle
);
2169 * Destroy the session without waiting for the data.
2171 int lttng_destroy_session_no_wait(const char *session_name
)
2173 enum lttng_error_code ret_code
;
2175 ret_code
= lttng_destroy_session_ext(session_name
, nullptr);
2176 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2180 * Ask the session daemon for all available sessions.
2181 * Sets the contents of the sessions array.
2182 * Returns the number of lttng_session entries in sessions;
2183 * on error, returns a negative value.
2185 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2188 struct lttcomm_session_msg lsm
;
2189 const size_t session_size
=
2190 sizeof(struct lttng_session
) + sizeof(struct lttng_session_extended
);
2191 size_t session_count
, i
;
2192 struct lttng_session_extended
*sessions_extended_begin
;
2193 struct lttng_session
*sessions
= nullptr;
2195 memset(&lsm
, 0, sizeof(lsm
));
2196 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
;
2198 * Initialize out_sessions to NULL so it is initialized when
2199 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2200 * be subsequently freed.
2202 *out_sessions
= nullptr;
2203 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &sessions
);
2208 ret
= -LTTNG_ERR_FATAL
;
2212 if (ret
% session_size
) {
2213 ret
= -LTTNG_ERR_UNK
;
2217 session_count
= (size_t) ret
/ session_size
;
2218 sessions_extended_begin
= (struct lttng_session_extended
*) (&sessions
[session_count
]);
2220 /* Set extended session info pointers. */
2221 for (i
= 0; i
< session_count
; i
++) {
2222 struct lttng_session
*session
= &sessions
[i
];
2223 struct lttng_session_extended
*extended
= &(sessions_extended_begin
[i
]);
2225 session
->extended
.ptr
= extended
;
2228 ret
= (int) session_count
;
2229 *out_sessions
= sessions
;
2234 enum lttng_error_code
lttng_session_get_creation_time(const struct lttng_session
*session
,
2235 uint64_t *creation_time
)
2237 enum lttng_error_code ret
= LTTNG_OK
;
2238 struct lttng_session_extended
*extended
;
2240 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2241 ret
= LTTNG_ERR_INVALID
;
2245 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2246 if (!extended
->creation_time
.is_set
) {
2247 /* Not created on the session daemon yet. */
2248 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2251 *creation_time
= extended
->creation_time
.value
;
2256 int lttng_set_session_shm_path(const char *session_name
, const char *shm_path
)
2259 struct lttcomm_session_msg lsm
;
2261 if (session_name
== nullptr) {
2262 return -LTTNG_ERR_INVALID
;
2265 memset(&lsm
, 0, sizeof(lsm
));
2266 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
;
2268 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2270 ret
= -LTTNG_ERR_INVALID
;
2274 ret
= lttng_strncpy(
2275 lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "", sizeof(lsm
.u
.set_shm_path
.shm_path
));
2277 ret
= -LTTNG_ERR_INVALID
;
2281 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2287 * Ask the session daemon for all available domains of a session.
2288 * Sets the contents of the domains array.
2289 * Returns the number of lttng_domain entries in domains;
2290 * on error, returns a negative value.
2292 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
2295 struct lttcomm_session_msg lsm
;
2297 if (session_name
== nullptr) {
2298 ret
= -LTTNG_ERR_INVALID
;
2302 memset(&lsm
, 0, sizeof(lsm
));
2303 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
;
2305 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2307 ret
= -LTTNG_ERR_INVALID
;
2311 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) domains
);
2316 return ret
/ sizeof(struct lttng_domain
);
2322 * Ask the session daemon for all available channels of a session.
2323 * Sets the contents of the channels array.
2324 * Returns the number of lttng_channel entries in channels;
2325 * on error, returns a negative value.
2327 int lttng_list_channels(struct lttng_handle
*handle
, struct lttng_channel
**channels
)
2329 int ret
, total_payload_received
;
2330 struct lttcomm_session_msg lsm
;
2331 char *reception_buffer
= nullptr;
2332 size_t cmd_header_len
= 0;
2333 struct lttcomm_list_command_header
*cmd_header
= nullptr;
2334 struct lttng_dynamic_buffer tmp_buffer
;
2336 lttng_dynamic_buffer_init(&tmp_buffer
);
2338 if (handle
== nullptr) {
2339 ret
= -LTTNG_ERR_INVALID
;
2343 memset(&lsm
, 0, sizeof(lsm
));
2344 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
;
2345 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2347 ret
= -LTTNG_ERR_INVALID
;
2351 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2353 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
2358 (void **) &reception_buffer
,
2359 (void **) &cmd_header
,
2365 total_payload_received
= ret
;
2367 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2368 ret
= -LTTNG_ERR_FATAL
;
2373 ret
= LTTNG_ERR_UNK
;
2377 if (cmd_header
->count
> INT_MAX
) {
2378 ret
= -LTTNG_ERR_OVERFLOW
;
2383 enum lttng_error_code ret_code
;
2384 const struct lttng_buffer_view events_view
=
2385 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
2387 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2388 &events_view
, cmd_header
->count
, channels
);
2389 if (ret_code
!= LTTNG_OK
) {
2395 ret
= (int) cmd_header
->count
;
2398 free(reception_buffer
);
2403 * Ask the session daemon for all available events of a session channel.
2404 * Sets the contents of the events array.
2405 * Returns the number of lttng_event entries in events;
2406 * on error, returns a negative value.
2408 int lttng_list_events(struct lttng_handle
*handle
,
2409 const char *channel_name
,
2410 struct lttng_event
**events
)
2413 struct lttcomm_session_msg lsm
= {};
2414 struct lttng_payload reply
;
2415 struct lttng_payload_view lsm_view
=
2416 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
2417 unsigned int nb_events
= 0;
2419 lttng_payload_init(&reply
);
2421 /* Safety check. An handle and channel name are mandatory. */
2422 if (handle
== nullptr || channel_name
== nullptr) {
2423 ret
= -LTTNG_ERR_INVALID
;
2427 /* Initialize command parameters. */
2428 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
;
2429 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2431 ret
= -LTTNG_ERR_INVALID
;
2435 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
, sizeof(lsm
.u
.list
.channel_name
));
2437 ret
= -LTTNG_ERR_INVALID
;
2441 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2443 /* Execute command against the session daemon. */
2444 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2450 const struct lttcomm_list_command_header
*cmd_reply_header
= nullptr;
2451 const lttng_payload_view cmd_reply_header_view
=
2452 lttng_payload_view_from_payload(&reply
, 0, sizeof(*cmd_reply_header
));
2454 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2455 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2459 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2460 cmd_reply_header_view
.buffer
.data
;
2461 if (cmd_reply_header
->count
> INT_MAX
) {
2462 ret
= -LTTNG_ERR_OVERFLOW
;
2466 nb_events
= (unsigned int) cmd_reply_header
->count
;
2470 enum lttng_error_code ret_code
;
2471 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2472 &reply
, sizeof(struct lttcomm_list_command_header
), -1);
2474 ret_code
= lttng_events_create_and_flatten_from_payload(
2475 &cmd_reply_payload
, nb_events
, events
);
2476 if (ret_code
!= LTTNG_OK
) {
2477 ret
= -((int) ret_code
);
2482 ret
= (int) nb_events
;
2484 lttng_payload_reset(&reply
);
2489 * Sets the tracing_group variable with name.
2490 * This function allocates memory pointed to by tracing_group.
2491 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2493 int lttng_set_tracing_group(const char *name
)
2498 if (name
== nullptr) {
2499 ret
= -LTTNG_ERR_INVALID
;
2503 new_group
= strdup(name
);
2505 ret
= -LTTNG_ERR_FATAL
;
2509 free(tracing_group
);
2510 tracing_group
= new_group
;
2511 new_group
= nullptr;
2517 int lttng_calibrate(struct lttng_handle
*handle
__attribute__((unused
)),
2518 struct lttng_calibrate
*calibrate
__attribute__((unused
)))
2521 * This command was removed in LTTng 2.9.
2523 return -LTTNG_ERR_UND
;
2527 * Set default channel attributes.
2528 * If either or both of the arguments are null, attr content is zeroe'd.
2530 void lttng_channel_set_default_attr(struct lttng_domain
*domain
, struct lttng_channel_attr
*attr
)
2532 struct lttng_channel_extended
*extended
;
2535 if (attr
== nullptr || domain
== nullptr) {
2539 /* Save the pointer for later use */
2540 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2541 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2543 /* Same for all domains. */
2544 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2545 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2546 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2548 switch (domain
->type
) {
2549 case LTTNG_DOMAIN_KERNEL
:
2550 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2551 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2552 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2553 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2554 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2556 case LTTNG_DOMAIN_UST
:
2557 switch (domain
->buf_type
) {
2558 case LTTNG_BUFFER_PER_UID
:
2559 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2560 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2561 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2562 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2563 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2565 case LTTNG_BUFFER_PER_PID
:
2567 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2568 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2569 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2570 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2571 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2575 /* Default behavior: leave set to 0. */
2580 lttng_channel_set_default_extended_attr(domain
, extended
);
2583 /* Reassign the extended pointer. */
2584 attr
->extended
.ptr
= extended
;
2587 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2588 uint64_t *discarded_events
)
2591 struct lttng_channel_extended
*chan_ext
;
2593 if (!channel
|| !discarded_events
) {
2594 ret
= -LTTNG_ERR_INVALID
;
2598 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2601 * This can happen since the lttng_channel structure is
2602 * used for other tasks where this pointer is never set.
2604 *discarded_events
= 0;
2608 *discarded_events
= chan_ext
->discarded_events
;
2613 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
, uint64_t *lost_packets
)
2616 struct lttng_channel_extended
*chan_ext
;
2618 if (!channel
|| !lost_packets
) {
2619 ret
= -LTTNG_ERR_INVALID
;
2623 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2626 * This can happen since the lttng_channel structure is
2627 * used for other tasks where this pointer is never set.
2633 *lost_packets
= chan_ext
->lost_packets
;
2638 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2639 uint64_t *monitor_timer_interval
)
2643 if (!chan
|| !monitor_timer_interval
) {
2644 ret
= -LTTNG_ERR_INVALID
;
2648 if (!chan
->attr
.extended
.ptr
) {
2649 ret
= -LTTNG_ERR_INVALID
;
2653 *monitor_timer_interval
=
2654 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2659 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2660 uint64_t monitor_timer_interval
)
2664 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2665 ret
= -LTTNG_ERR_INVALID
;
2669 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2670 monitor_timer_interval
;
2675 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
, int64_t *blocking_timeout
)
2679 if (!chan
|| !blocking_timeout
) {
2680 ret
= -LTTNG_ERR_INVALID
;
2684 if (!chan
->attr
.extended
.ptr
) {
2685 ret
= -LTTNG_ERR_INVALID
;
2690 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
2695 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
, int64_t blocking_timeout
)
2698 int64_t msec_timeout
;
2700 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2701 ret
= -LTTNG_ERR_INVALID
;
2705 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2706 ret
= -LTTNG_ERR_INVALID
;
2711 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2712 * us to accept a narrower range of values (msecs expressed as a signed
2715 msec_timeout
= blocking_timeout
/ 1000;
2716 if (msec_timeout
!= (int32_t) msec_timeout
) {
2717 ret
= -LTTNG_ERR_INVALID
;
2721 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
=
2728 * Check if session daemon is alive.
2730 * Return 1 if alive or 0 if not.
2731 * On error returns a negative value.
2733 int lttng_session_daemon_alive(void)
2737 ret
= set_session_daemon_path();
2743 if (*sessiond_sock_path
== '\0') {
2745 * No socket path set. Weird error which means the constructor
2751 ret
= try_connect_sessiond(sessiond_sock_path
);
2762 * Set URL for a consumer for a session and domain.
2764 * Return 0 on success, else a negative value.
2766 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2767 const char *control_url
,
2768 const char *data_url
)
2772 struct lttcomm_session_msg lsm
;
2773 struct lttng_uri
*uris
= nullptr;
2775 if (handle
== nullptr || (control_url
== nullptr && data_url
== nullptr)) {
2776 ret
= -LTTNG_ERR_INVALID
;
2780 memset(&lsm
, 0, sizeof(lsm
));
2782 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
;
2784 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2786 ret
= -LTTNG_ERR_INVALID
;
2790 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2792 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2794 ret
= -LTTNG_ERR_INVALID
;
2798 lsm
.u
.uri
.size
= size
;
2800 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
2801 &lsm
, uris
, sizeof(struct lttng_uri
) * size
, nullptr);
2811 extern "C" LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2812 int lttng_enable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2820 extern "C" LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2821 int lttng_disable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2829 extern "C" LTTNG_EXPORT
int
2830 _lttng_create_session_ext(const char *name
, const char *url
, const char *datetime
);
2831 int _lttng_create_session_ext(const char *name
__attribute__((unused
)),
2832 const char *url
__attribute__((unused
)),
2833 const char *datetime
__attribute__((unused
)))
2839 * For a given session name, this call checks if the data is ready to be read
2840 * or is still being extracted by the consumer(s) hence not ready to be used by
2843 int lttng_data_pending(const char *session_name
)
2846 struct lttcomm_session_msg lsm
;
2847 uint8_t *pending
= nullptr;
2849 if (session_name
== nullptr) {
2850 return -LTTNG_ERR_INVALID
;
2853 memset(&lsm
, 0, sizeof(lsm
));
2854 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
;
2856 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2858 ret
= -LTTNG_ERR_INVALID
;
2862 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2865 } else if (ret
!= 1) {
2866 /* Unexpected payload size */
2867 ret
= -LTTNG_ERR_INVALID
;
2869 } else if (!pending
) {
2870 /* Internal error. */
2871 ret
= -LTTNG_ERR_UNK
;
2875 ret
= (int) *pending
;
2882 * Get the status of the kernel tracer
2884 * Sets the value of the argument
2886 enum lttng_error_code
lttng_get_kernel_tracer_status(enum lttng_kernel_tracer_status
*out_status
)
2888 if (out_status
== nullptr) {
2889 return LTTNG_ERR_INVALID
;
2892 struct lttcomm_session_msg lsm
= {};
2893 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
;
2895 uint32_t *raw_tracer_status
= nullptr;
2896 const auto ask_ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &raw_tracer_status
);
2898 const auto tracer_status
=
2899 lttng::make_unique_wrapper
<uint32_t, lttng::free
>(raw_tracer_status
);
2902 return LTTNG_ERR_INVALID
;
2905 *out_status
= (enum lttng_kernel_tracer_status
) * tracer_status
;
2910 * Regenerate the metadata for a session.
2911 * Return 0 on success, a negative error code on error.
2913 int lttng_regenerate_metadata(const char *session_name
)
2916 struct lttcomm_session_msg lsm
;
2918 if (!session_name
) {
2919 ret
= -LTTNG_ERR_INVALID
;
2923 memset(&lsm
, 0, sizeof(lsm
));
2924 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
;
2926 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2928 ret
= -LTTNG_ERR_INVALID
;
2932 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2943 * Deprecated, replaced by lttng_regenerate_metadata.
2945 int lttng_metadata_regenerate(const char *session_name
)
2947 return lttng_regenerate_metadata(session_name
);
2951 * Regenerate the statedump of a session.
2952 * Return 0 on success, a negative error code on error.
2954 int lttng_regenerate_statedump(const char *session_name
)
2957 struct lttcomm_session_msg lsm
;
2959 if (!session_name
) {
2960 ret
= -LTTNG_ERR_INVALID
;
2964 memset(&lsm
, 0, sizeof(lsm
));
2965 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
;
2967 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2969 ret
= -LTTNG_ERR_INVALID
;
2973 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2984 _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
, bool generate_name
)
2987 struct lttcomm_session_msg lsm
= {
2988 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
,
2994 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
2995 struct lttcomm_session_msg
*message_lsm
;
2996 struct lttng_payload message
;
2997 struct lttng_payload reply
;
2998 struct lttng_trigger
*reply_trigger
= nullptr;
2999 enum lttng_domain_type domain_type
;
3000 const struct lttng_credentials user_creds
= {
3001 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3002 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3004 const char *unused_trigger_name
= nullptr;
3005 enum lttng_trigger_status trigger_status
;
3007 lttng_payload_init(&message
);
3008 lttng_payload_init(&reply
);
3011 ret
= -LTTNG_ERR_INVALID
;
3015 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3016 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3017 /* Re-using already registered trigger. */
3018 ret
= -LTTNG_ERR_INVALID
;
3023 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3024 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3025 ret
= -LTTNG_ERR_NOMEM
;
3030 if (!trigger
->creds
.uid
.is_set
) {
3031 /* Use the client's credentials as the trigger credentials. */
3032 lttng_trigger_set_credentials(trigger
, &user_creds
);
3035 * Validate that either the current trigger credentials and the
3036 * client credentials are identical or that the current user is
3037 * root. The root user can register, unregister triggers for
3038 * himself and other users.
3040 * This check is also present on the sessiond side, using the
3041 * credentials passed on the socket. These check are all
3044 const struct lttng_credentials
*trigger_creds
=
3045 lttng_trigger_get_credentials(trigger
);
3047 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3048 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3049 ret
= -LTTNG_ERR_EPERM
;
3050 goto end_unset_name
;
3055 if (!lttng_trigger_validate(trigger
)) {
3056 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3057 goto end_unset_name
;
3060 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(trigger
);
3062 lsm
.domain
.type
= domain_type
;
3064 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3066 ret
= -LTTNG_ERR_NOMEM
;
3067 goto end_unset_name
;
3070 ret
= lttng_trigger_serialize(trigger
, &message
);
3072 ret
= -LTTNG_ERR_UNK
;
3073 goto end_unset_name
;
3077 * This is needed to populate the trigger object size for the command
3080 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3082 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3085 struct lttng_payload_view message_view
=
3086 lttng_payload_view_from_payload(&message
, 0, -1);
3088 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3089 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3091 goto end_unset_name
;
3096 struct lttng_payload_view reply_view
=
3097 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3099 ret
= lttng_trigger_create_from_payload(&reply_view
, &reply_trigger
);
3101 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3102 goto end_unset_name
;
3106 if (name
|| generate_name
) {
3107 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3109 ret
= -LTTNG_ERR_NOMEM
;
3118 trigger_status
= lttng_trigger_set_name(trigger
, nullptr);
3119 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3120 ret
= -LTTNG_ERR_UNK
;
3123 lttng_payload_reset(&message
);
3124 lttng_payload_reset(&reply
);
3125 lttng_trigger_destroy(reply_trigger
);
3129 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3131 /* Register an anonymous trigger. */
3132 return _lttng_register_trigger(trigger
, nullptr, false);
3135 enum lttng_error_code
lttng_register_trigger_with_name(struct lttng_trigger
*trigger
,
3138 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3140 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3143 enum lttng_error_code
lttng_register_trigger_with_automatic_name(struct lttng_trigger
*trigger
)
3145 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3147 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3150 enum lttng_error_code
lttng_error_query_execute(const struct lttng_error_query
*query
,
3151 const struct lttng_endpoint
*endpoint
,
3152 struct lttng_error_query_results
**results
)
3155 enum lttng_error_code ret_code
;
3156 struct lttcomm_session_msg lsm
= {
3157 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
,
3163 struct lttng_payload message
;
3164 struct lttng_payload reply
;
3165 struct lttcomm_session_msg
*message_lsm
;
3167 lttng_payload_init(&message
);
3168 lttng_payload_init(&reply
);
3170 if (!query
|| !results
) {
3171 ret_code
= LTTNG_ERR_INVALID
;
3175 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3176 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3180 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3182 ret_code
= LTTNG_ERR_NOMEM
;
3186 ret
= lttng_error_query_serialize(query
, &message
);
3188 ret_code
= LTTNG_ERR_UNK
;
3192 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3193 message_lsm
->u
.error_query
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3196 struct lttng_payload_view message_view
=
3197 lttng_payload_view_from_payload(&message
, 0, -1);
3199 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3200 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3202 ret_code
= (lttng_error_code
) -ret
;
3208 ssize_t reply_create_ret
;
3209 struct lttng_payload_view reply_view
=
3210 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3213 lttng_error_query_results_create_from_payload(&reply_view
, results
);
3214 if (reply_create_ret
< 0) {
3215 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3220 ret_code
= LTTNG_OK
;
3222 lttng_payload_reset(&message
);
3223 lttng_payload_reset(&reply
);
3227 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3230 struct lttcomm_session_msg lsm
;
3231 struct lttcomm_session_msg
*message_lsm
;
3232 struct lttng_payload message
;
3233 struct lttng_payload reply
;
3234 struct lttng_trigger
*copy
= nullptr;
3235 const struct lttng_credentials user_creds
= {
3236 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3237 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3240 lttng_payload_init(&message
);
3241 lttng_payload_init(&reply
);
3244 ret
= -LTTNG_ERR_INVALID
;
3248 copy
= lttng_trigger_copy(trigger
);
3250 ret
= -LTTNG_ERR_UNK
;
3254 if (!copy
->creds
.uid
.is_set
) {
3255 /* Use the client credentials as the trigger credentials */
3256 lttng_trigger_set_credentials(copy
, &user_creds
);
3259 * Validate that either the current trigger credentials and the
3260 * client credentials are identical or that the current user is
3261 * root. The root user can register, unregister triggers for
3262 * himself and other users.
3264 * This check is also present on the sessiond side, using the
3265 * credentials passed on the socket. These check are all
3268 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(copy
);
3269 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3270 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3271 ret
= -LTTNG_ERR_EPERM
;
3277 if (!lttng_trigger_validate(copy
)) {
3278 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3282 memset(&lsm
, 0, sizeof(lsm
));
3283 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
;
3285 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3287 ret
= -LTTNG_ERR_NOMEM
;
3291 ret
= lttng_trigger_serialize(copy
, &message
);
3293 ret
= -LTTNG_ERR_UNK
;
3298 * This is needed to populate the trigger object size for the command
3299 * header and number of fds sent.
3301 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3303 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3306 struct lttng_payload_view message_view
=
3307 lttng_payload_view_from_payload(&message
, 0, -1);
3310 * Update the message header with the number of fd that will be
3313 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3315 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3323 lttng_trigger_destroy(copy
);
3324 lttng_payload_reset(&message
);
3325 lttng_payload_reset(&reply
);
3330 * Ask the session daemon for all registered triggers for the current user.
3332 * Allocates and return an lttng_triggers set.
3333 * On error, returns a suitable lttng_error_code.
3335 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3338 enum lttng_error_code ret_code
= LTTNG_OK
;
3339 struct lttcomm_session_msg lsm
= {
3340 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
,
3346 struct lttng_triggers
*local_triggers
= nullptr;
3347 struct lttng_payload reply
;
3348 struct lttng_payload_view lsm_view
=
3349 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
3351 lttng_payload_init(&reply
);
3353 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3355 ret_code
= (enum lttng_error_code
) - ret
;
3360 struct lttng_payload_view reply_view
=
3361 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3363 ret
= lttng_triggers_create_from_payload(&reply_view
, &local_triggers
);
3365 ret_code
= LTTNG_ERR_FATAL
;
3370 *triggers
= local_triggers
;
3371 local_triggers
= nullptr;
3373 lttng_payload_reset(&reply
);
3374 lttng_triggers_destroy(local_triggers
);
3381 static void __attribute__((constructor
)) init()
3383 /* Set default session group */
3384 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3390 static void __attribute__((destructor
)) lttng_ctl_exit()
3392 free(tracing_group
);