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/payload-view.hpp>
28 #include <common/payload.hpp>
29 #include <common/sessiond-comm/sessiond-comm.hpp>
30 #include <common/tracker.hpp>
31 #include <common/unix.hpp>
32 #include <common/uri.hpp>
33 #include <common/utils.hpp>
35 #include <lttng/channel-internal.hpp>
36 #include <lttng/destruction-handle.h>
37 #include <lttng/endpoint.h>
38 #include <lttng/error-query-internal.hpp>
39 #include <lttng/event-internal.hpp>
40 #include <lttng/health-internal.hpp>
41 #include <lttng/lttng-error.h>
42 #include <lttng/lttng.h>
43 #include <lttng/session-descriptor-internal.hpp>
44 #include <lttng/session-internal.hpp>
45 #include <lttng/trigger/trigger-internal.hpp>
46 #include <lttng/userspace-probe-internal.hpp>
55 #define COPY_DOMAIN_PACKED(dst, src) \
57 struct lttng_domain _tmp_domain; \
59 lttng_ctl_copy_lttng_domain(&_tmp_domain, &(src)); \
60 (dst) = _tmp_domain; \
63 /* Socket to session daemon for communication */
64 static int sessiond_socket
= -1;
65 static char sessiond_sock_path
[PATH_MAX
];
68 static char *tracing_group
;
74 * Those two variables are used by error.h to silent or control the verbosity of
75 * error message. They are global to the library so application linking with it
76 * are able to compile correctly and also control verbosity of the library.
78 LTTNG_EXPORT
int lttng_opt_quiet
;
79 LTTNG_EXPORT
int lttng_opt_verbose
;
80 LTTNG_EXPORT
int lttng_opt_mi
;
83 * Copy domain to lttcomm_session_msg domain.
85 * If domain is unknown, default domain will be the kernel.
87 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
91 case LTTNG_DOMAIN_KERNEL
:
92 case LTTNG_DOMAIN_UST
:
93 case LTTNG_DOMAIN_JUL
:
94 case LTTNG_DOMAIN_LOG4J
:
95 case LTTNG_DOMAIN_PYTHON
:
96 memcpy(dst
, src
, sizeof(struct lttng_domain
));
99 memset(dst
, 0, sizeof(struct lttng_domain
));
106 * Send lttcomm_session_msg to the session daemon.
108 * On success, returns the number of bytes sent (>=0)
109 * On error, returns -1
111 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
116 ret
= -LTTNG_ERR_NO_SESSIOND
;
120 DBG("LSM cmd type: '%s' (%d)",
121 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) lsm
->cmd_type
),
124 ret
= lttcomm_send_creds_unix_sock(
125 sessiond_socket
, lsm
, sizeof(struct lttcomm_session_msg
));
127 ret
= -LTTNG_ERR_FATAL
;
135 * Send var len data to the session daemon.
137 * On success, returns the number of bytes sent (>=0)
138 * On error, returns -1
140 static int send_session_varlen(const void *data
, size_t len
)
145 ret
= -LTTNG_ERR_NO_SESSIOND
;
154 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
156 ret
= -LTTNG_ERR_FATAL
;
164 * Send file descriptors to the session daemon.
166 * On success, returns the number of bytes sent (>=0)
167 * On error, returns -1
169 static int send_session_fds(const int *fds
, size_t nb_fd
)
174 ret
= -LTTNG_ERR_NO_SESSIOND
;
178 if (!fds
|| !nb_fd
) {
183 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
185 ret
= -LTTNG_ERR_FATAL
;
193 * Receive data from the sessiond socket.
195 * On success, returns the number of bytes received (>=0)
196 * On error, returns a negative lttng_error_code.
198 static int recv_data_sessiond(void *buf
, size_t len
)
202 LTTNG_ASSERT(len
> 0);
205 ret
= -LTTNG_ERR_NO_SESSIOND
;
209 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
211 ret
= -LTTNG_ERR_FATAL
;
212 } else if (ret
== 0) {
213 ret
= -LTTNG_ERR_NO_SESSIOND
;
221 * Receive a payload from the session daemon by appending to an existing
223 * On success, returns the number of bytes received (>=0)
224 * On error, returns a negative lttng_error_code.
226 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
229 const size_t original_payload_size
= payload
->buffer
.size
;
231 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
, payload
->buffer
.size
+ len
);
233 ret
= -LTTNG_ERR_NOMEM
;
237 ret
= recv_data_sessiond(payload
->buffer
.data
+ original_payload_size
, len
);
243 * Check if we are in the specified group.
245 * If yes return 1, else return -1.
247 int lttng_check_tracing_group()
249 gid_t
*grp_list
, tracing_gid
;
250 int grp_list_size
, grp_id
, i
;
252 const char *grp_name
= tracing_group
;
254 /* Get GID of group 'tracing' */
255 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
256 /* If grp_tracing is NULL, the group does not exist. */
260 /* Get number of supplementary group IDs */
261 grp_list_size
= getgroups(0, nullptr);
262 if (grp_list_size
< 0) {
267 /* Alloc group list of the right size */
268 grp_list
= calloc
<gid_t
>(grp_list_size
);
273 grp_id
= getgroups(grp_list_size
, grp_list
);
279 for (i
= 0; i
< grp_list_size
; i
++) {
280 if (grp_list
[i
] == tracing_gid
) {
293 static enum lttng_error_code
check_enough_available_memory(uint64_t num_bytes_requested_per_cpu
)
296 enum lttng_error_code ret_code
;
298 uint64_t best_mem_info
;
299 uint64_t num_bytes_requested_total
;
302 * Get the number of CPU currently online to compute the amount of
303 * memory needed to create a buffer for every CPU.
305 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
307 ret_code
= LTTNG_ERR_FATAL
;
311 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
313 ret_code
= LTTNG_ERR_OVERFLOW
;
317 num_bytes_requested_total
= num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
320 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
321 * reliable estimate we can get but it is only exposed by the kernel
322 * since 3.14. (See Linux kernel commit:
323 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
325 ret
= utils_get_memory_available(&best_mem_info
);
331 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
332 * is a sanity check for obvious user error.
334 ret
= utils_get_memory_total(&best_mem_info
);
339 /* No valid source of information. */
340 ret_code
= LTTNG_ERR_NOMEM
;
344 if (best_mem_info
>= num_bytes_requested_total
) {
347 ret_code
= LTTNG_ERR_NOMEM
;
354 * Try connect to session daemon with sock_path.
356 * Return 0 on success, else -1
358 static int try_connect_sessiond(const char *sock_path
)
362 /* If socket exist, we check if the daemon listens for connect. */
363 ret
= access(sock_path
, F_OK
);
369 ret
= lttcomm_connect_unix_sock(sock_path
);
375 ret
= lttcomm_close_unix_sock(ret
);
377 PERROR("lttcomm_close_unix_sock");
387 * Set sessiond socket path by putting it in the global sessiond_sock_path
390 * Returns 0 on success, negative value on failure (the sessiond socket path
391 * is somehow too long or ENOMEM).
393 static int set_session_daemon_path()
395 int in_tgroup
= 0; /* In tracing group. */
401 /* Are we in the tracing group ? */
402 in_tgroup
= lttng_check_tracing_group();
405 if ((uid
== 0) || in_tgroup
== 1) {
406 const int ret
= lttng_strncpy(sessiond_sock_path
,
407 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
408 sizeof(sessiond_sock_path
));
420 ret
= try_connect_sessiond(sessiond_sock_path
);
424 /* Global session daemon not available... */
426 /* ...or not in tracing group (and not root), default */
429 * With GNU C < 2.1, snprintf returns -1 if the target buffer
431 * With GNU C >= 2.1, snprintf returns the required size
432 * (excluding closing null)
434 ret
= snprintf(sessiond_sock_path
,
435 sizeof(sessiond_sock_path
),
436 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
437 utils_get_home_dir());
438 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
450 * Connect to the LTTng session daemon.
452 * On success, return the socket's file descriptor. On error, return -1.
454 int connect_sessiond()
458 ret
= set_session_daemon_path();
463 /* Connect to the sesssion daemon. */
464 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
475 static void reset_global_sessiond_connection_state()
477 sessiond_socket
= -1;
482 * Clean disconnect from the session daemon.
484 * On success, return 0. On error, return -1.
486 static int disconnect_sessiond()
491 ret
= lttcomm_close_unix_sock(sessiond_socket
);
492 reset_global_sessiond_connection_state();
498 static int recv_sessiond_optional_data(size_t len
, void **user_buf
, size_t *user_len
)
505 ret
= -LTTNG_ERR_INVALID
;
509 buf
= zmalloc
<char>(len
);
515 ret
= recv_data_sessiond(buf
, len
);
521 ret
= -LTTNG_ERR_INVALID
;
525 /* Move ownership of command header buffer to user. */
530 /* No command header. */
546 * Ask the session daemon a specific command and put the data into buf.
547 * Takes extra var. len. data and file descriptors as input to send to the
550 * Return size of data (only payload, not header) or a negative error code.
552 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
557 void **user_payload_buf
,
558 void **user_cmd_header_buf
,
559 size_t *user_cmd_header_len
)
563 struct lttcomm_lttng_msg llm
;
565 ret
= connect_sessiond();
567 ret
= -LTTNG_ERR_NO_SESSIOND
;
570 sessiond_socket
= ret
;
574 ret
= send_session_msg(lsm
);
576 /* Ret value is a valid lttng error code. */
579 /* Send var len data */
580 ret
= send_session_varlen(vardata
, vardata_len
);
582 /* Ret value is a valid lttng error code. */
587 ret
= send_session_fds(fds
, nb_fd
);
589 /* Ret value is a valid lttng error code. */
593 /* Get header from data transmission */
594 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
596 /* Ret value is a valid lttng error code. */
600 /* Check error code if OK */
601 if (llm
.ret_code
!= LTTNG_OK
) {
606 /* Get command header from data transmission */
607 ret
= recv_sessiond_optional_data(
608 llm
.cmd_header_size
, user_cmd_header_buf
, user_cmd_header_len
);
613 /* Get payload from data transmission */
614 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
, &payload_len
);
622 disconnect_sessiond();
626 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
, struct lttng_payload
*reply
)
629 struct lttcomm_lttng_msg llm
;
630 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
632 LTTNG_ASSERT(reply
->buffer
.size
== 0);
633 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
635 ret
= connect_sessiond();
637 ret
= -LTTNG_ERR_NO_SESSIOND
;
640 sessiond_socket
= ret
;
644 /* Send command to session daemon */
645 ret
= lttcomm_send_creds_unix_sock(
646 sessiond_socket
, message
->buffer
.data
, message
->buffer
.size
);
648 ret
= -LTTNG_ERR_FATAL
;
653 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
, message
);
655 ret
= -LTTNG_ERR_FATAL
;
660 /* Get header from data transmission */
661 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
663 /* Ret value is a valid lttng error code. */
667 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
669 /* Check error code if OK */
670 if (llm
.ret_code
!= LTTNG_OK
) {
671 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
672 /* Invalid error code received. */
673 ret
= -LTTNG_ERR_UNK
;
680 if (llm
.cmd_header_size
> 0) {
681 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
687 /* Get command header from data transmission */
688 if (llm
.data_size
> 0) {
689 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
695 if (llm
.fd_count
> 0) {
696 ret
= lttcomm_recv_payload_fds_unix_sock(sessiond_socket
, llm
.fd_count
, reply
);
702 /* Don't return the llm header to the caller. */
703 memmove(reply
->buffer
.data
,
704 reply
->buffer
.data
+ sizeof(llm
),
705 reply
->buffer
.size
- sizeof(llm
));
706 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
708 /* Can't happen as size is reduced. */
712 ret
= reply
->buffer
.size
;
715 disconnect_sessiond();
720 * Create lttng handle and return pointer.
722 * The returned pointer will be NULL in case of malloc() error.
724 struct lttng_handle
*lttng_create_handle(const char *session_name
, struct lttng_domain
*domain
)
727 struct lttng_handle
*handle
= nullptr;
729 handle
= zmalloc
<lttng_handle
>();
730 if (handle
== nullptr) {
731 PERROR("malloc handle");
735 /* Copy session name */
736 ret
= lttng_strncpy(handle
->session_name
, session_name
?: "", sizeof(handle
->session_name
));
741 /* Copy lttng domain or leave initialized to 0. */
743 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
754 * Destroy handle by free(3) the pointer.
756 void lttng_destroy_handle(struct lttng_handle
*handle
)
762 * Register an outside consumer.
764 * Returns size of returned session payload data or a negative error code.
766 int lttng_register_consumer(struct lttng_handle
*handle
, const char *socket_path
)
769 struct lttcomm_session_msg lsm
;
771 if (handle
== nullptr || socket_path
== nullptr) {
772 ret
= -LTTNG_ERR_INVALID
;
776 memset(&lsm
, 0, sizeof(lsm
));
777 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
;
778 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
780 ret
= -LTTNG_ERR_INVALID
;
784 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
786 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
788 ret
= -LTTNG_ERR_INVALID
;
792 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
798 * Start tracing for all traces of the session.
800 * Returns size of returned session payload data or a negative error code.
802 int lttng_start_tracing(const char *session_name
)
805 struct lttcomm_session_msg lsm
;
807 if (session_name
== nullptr) {
808 ret
= -LTTNG_ERR_INVALID
;
812 memset(&lsm
, 0, sizeof(lsm
));
813 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_START_TRACE
;
815 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
817 ret
= -LTTNG_ERR_INVALID
;
821 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
827 * Stop tracing for all traces of the session.
829 static int _lttng_stop_tracing(const char *session_name
, int wait
)
832 struct lttcomm_session_msg lsm
;
834 if (session_name
== nullptr) {
835 ret
= -LTTNG_ERR_INVALID
;
839 memset(&lsm
, 0, sizeof(lsm
));
840 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
;
842 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
844 ret
= -LTTNG_ERR_INVALID
;
848 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
849 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
857 /* Check for data availability */
859 data_ret
= lttng_data_pending(session_name
);
861 /* Return the data available call error. */
867 * Data sleep time before retrying (in usec). Don't sleep if the
868 * call returned value indicates availability.
871 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
873 } while (data_ret
!= 0);
881 * Stop tracing and wait for data availability.
883 int lttng_stop_tracing(const char *session_name
)
885 return _lttng_stop_tracing(session_name
, 1);
889 * Stop tracing but _don't_ wait for data availability.
891 int lttng_stop_tracing_no_wait(const char *session_name
)
893 return _lttng_stop_tracing(session_name
, 0);
897 * Add context to a channel.
899 * If the given channel is NULL, add the contexts to all channels.
900 * The event_name param is ignored.
902 * Returns the size of the returned payload data or a negative error code.
904 int lttng_add_context(struct lttng_handle
*handle
,
905 struct lttng_event_context
*ctx
,
906 const char *event_name
__attribute__((unused
)),
907 const char *channel_name
)
910 struct lttcomm_session_msg lsm
= {
911 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
,
917 struct lttng_payload payload
;
919 lttng_payload_init(&payload
);
921 /* Safety check. Both are mandatory. */
922 if (handle
== nullptr || ctx
== nullptr) {
923 ret
= -LTTNG_ERR_INVALID
;
927 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
929 ret
= -LTTNG_ERR_NOMEM
;
933 /* If no channel name, send empty string. */
935 lsm
.u
.context
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.context
.channel_name
));
937 ret
= -LTTNG_ERR_INVALID
;
941 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
942 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
944 ret
= -LTTNG_ERR_INVALID
;
948 ret
= lttng_event_context_serialize(ctx
, &payload
);
950 ret
= -LTTNG_ERR_INVALID
;
954 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
956 /* Update message header. */
957 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
960 struct lttng_payload reply
;
961 struct lttng_payload_view payload_view
=
962 lttng_payload_view_from_payload(&payload
, 0, -1);
964 lttng_payload_init(&reply
);
965 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
966 lttng_payload_reset(&reply
);
973 lttng_payload_reset(&payload
);
978 * Enable event(s) for a channel.
980 * If no event name is specified, all events are enabled.
981 * If no channel name is specified, the default 'channel0' is used.
983 * Returns size of returned session payload data or a negative error code.
985 int lttng_enable_event(struct lttng_handle
*handle
,
986 struct lttng_event
*ev
,
987 const char *channel_name
)
989 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
, nullptr, 0, nullptr);
993 * Create or enable an event with a filter expression.
995 * Return negative error value on error.
996 * Return size of returned session payload data if OK.
998 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
999 struct lttng_event
*event
,
1000 const char *channel_name
,
1001 const char *filter_expression
)
1003 return lttng_enable_event_with_exclusions(
1004 handle
, event
, channel_name
, filter_expression
, 0, nullptr);
1008 * Depending on the event, return a newly allocated agent filter expression or
1009 * NULL if not applicable.
1011 * An event with NO loglevel and the name is * will return NULL.
1013 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1016 char *agent_filter
= nullptr;
1020 /* Don't add filter for the '*' event. */
1021 if (strcmp(ev
->name
, "*") != 0) {
1024 &agent_filter
, "(%s) && (logger_name == \"%s\")", filter
, ev
->name
);
1026 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1034 /* Add loglevel filtering if any for the JUL domain. */
1035 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1038 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1044 if (filter
|| agent_filter
) {
1047 err
= asprintf(&new_filter
,
1048 "(%s) && (int_loglevel %s %d)",
1049 agent_filter
? agent_filter
: filter
,
1055 agent_filter
= new_filter
;
1057 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
, ev
->loglevel
);
1065 return agent_filter
;
1072 * Enable event(s) for a channel, possibly with exclusions and a filter.
1073 * If no event name is specified, all events are enabled.
1074 * If no channel name is specified, the default name is used.
1075 * If filter expression is not NULL, the filter is set for the event.
1076 * If exclusion count is not zero, the exclusions are set for the event.
1077 * Returns size of returned session payload data or a negative error code.
1079 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1080 struct lttng_event
*ev
,
1081 const char *channel_name
,
1082 const char *original_filter_expression
,
1083 int exclusion_count
,
1084 char **exclusion_list
)
1086 struct lttcomm_session_msg lsm
= {
1087 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
,
1093 struct lttng_payload payload
;
1095 unsigned int free_filter_expression
= 0;
1096 struct filter_parser_ctx
*ctx
= nullptr;
1097 size_t bytecode_len
= 0;
1100 * We have either a filter or some exclusions, so we need to set up
1101 * a variable-length payload from where to send the data.
1103 lttng_payload_init(&payload
);
1106 * Cast as non-const since we may replace the filter expression
1107 * by a dynamically allocated string. Otherwise, the original
1108 * string is not modified.
1110 char *filter_expression
= (char *) original_filter_expression
;
1112 if (handle
== nullptr || ev
== nullptr) {
1113 ret
= -LTTNG_ERR_INVALID
;
1118 * Empty filter string will always be rejected by the parser
1119 * anyway, so treat this corner-case early to eliminate
1120 * lttng_fmemopen error for 0-byte allocation.
1122 if (filter_expression
&& filter_expression
[0] == '\0') {
1123 ret
= -LTTNG_ERR_INVALID
;
1127 if (ev
->name
[0] == '\0') {
1128 /* Enable all events. */
1129 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1130 LTTNG_ASSERT(ret
== 0);
1133 /* Parse filter expression. */
1134 if (filter_expression
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1135 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1136 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1137 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1138 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1139 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1142 /* Setup agent filter if needed. */
1143 agent_filter
= set_agent_filter(filter_expression
, ev
);
1144 if (!agent_filter
) {
1145 if (!filter_expression
) {
1147 * No JUL and no filter, just skip
1154 * With an agent filter, the original filter has
1155 * been added to it thus replace the filter
1158 filter_expression
= agent_filter
;
1159 free_filter_expression
= 1;
1163 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1164 ret
= -LTTNG_ERR_FILTER_INVAL
;
1168 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1173 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1174 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1175 ret
= -LTTNG_ERR_FILTER_INVAL
;
1181 ret
= lttng_event_serialize(ev
,
1186 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1189 ret
= -LTTNG_ERR_INVALID
;
1193 /* If no channel name, send empty string. */
1194 ret
= lttng_strncpy(
1195 lsm
.u
.enable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.enable
.channel_name
));
1197 ret
= -LTTNG_ERR_INVALID
;
1202 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1205 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1207 ret
= -LTTNG_ERR_INVALID
;
1211 /* Length of the serialized event. */
1212 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1215 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1216 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1223 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1224 if (fd_count
== 1) {
1225 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1231 fd_to_send
= fd_handle_get_fd(h
);
1235 lsm
.fd_count
= fd_count
;
1237 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1238 fd_count
? &fd_to_send
: nullptr,
1240 view
.buffer
.size
? view
.buffer
.data
:
1249 if (filter_expression
&& ctx
) {
1250 filter_bytecode_free(ctx
);
1251 filter_ir_free(ctx
);
1252 filter_parser_ctx_free(ctx
);
1254 if (free_filter_expression
) {
1256 * The filter expression has been replaced and must be freed as
1257 * it is not the original filter expression received as a
1260 free(filter_expression
);
1263 * Return directly to the caller and don't ask the sessiond since
1264 * something went wrong in the parsing of data above.
1266 lttng_payload_reset(&payload
);
1270 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1271 struct lttng_event
*ev
,
1272 const char *channel_name
,
1273 const char *original_filter_expression
)
1275 struct lttcomm_session_msg lsm
= {
1276 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
,
1282 struct lttng_payload payload
;
1284 unsigned int free_filter_expression
= 0;
1285 struct filter_parser_ctx
*ctx
= nullptr;
1286 size_t bytecode_len
= 0;
1289 * We have either a filter or some exclusions, so we need to set up
1290 * a variable-length payload from where to send the data.
1292 lttng_payload_init(&payload
);
1295 * Cast as non-const since we may replace the filter expression
1296 * by a dynamically allocated string. Otherwise, the original
1297 * string is not modified.
1299 char *filter_expression
= (char *) original_filter_expression
;
1301 if (handle
== nullptr || ev
== nullptr) {
1302 ret
= -LTTNG_ERR_INVALID
;
1307 * Empty filter string will always be rejected by the parser
1308 * anyway, so treat this corner-case early to eliminate
1309 * lttng_fmemopen error for 0-byte allocation.
1311 if (filter_expression
&& filter_expression
[0] == '\0') {
1312 ret
= -LTTNG_ERR_INVALID
;
1316 /* Parse filter expression. */
1317 if (filter_expression
!= nullptr || handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1318 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1319 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1320 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1321 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1322 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1325 /* Setup agent filter if needed. */
1326 agent_filter
= set_agent_filter(filter_expression
, ev
);
1327 if (!agent_filter
) {
1328 if (!filter_expression
) {
1330 * No JUL and no filter, just skip
1337 * With an agent filter, the original filter has
1338 * been added to it thus replace the filter
1341 filter_expression
= agent_filter
;
1342 free_filter_expression
= 1;
1346 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1347 ret
= -LTTNG_ERR_FILTER_INVAL
;
1351 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1356 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1357 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1358 ret
= -LTTNG_ERR_FILTER_INVAL
;
1364 ret
= lttng_event_serialize(ev
,
1369 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: nullptr,
1372 ret
= -LTTNG_ERR_INVALID
;
1376 /* If no channel name, send empty string. */
1377 ret
= lttng_strncpy(
1378 lsm
.u
.disable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.disable
.channel_name
));
1380 ret
= -LTTNG_ERR_INVALID
;
1385 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1388 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1390 ret
= -LTTNG_ERR_INVALID
;
1394 /* Length of the serialized event. */
1395 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1398 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1399 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1406 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1407 if (fd_count
== 1) {
1408 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1414 fd_to_send
= fd_handle_get_fd(h
);
1418 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1419 fd_count
? &fd_to_send
: nullptr,
1421 view
.buffer
.size
? view
.buffer
.data
:
1430 if (filter_expression
&& ctx
) {
1431 filter_bytecode_free(ctx
);
1432 filter_ir_free(ctx
);
1433 filter_parser_ctx_free(ctx
);
1435 if (free_filter_expression
) {
1437 * The filter expression has been replaced and must be freed as
1438 * it is not the original filter expression received as a
1441 free(filter_expression
);
1444 * Return directly to the caller and don't ask the sessiond since
1445 * something went wrong in the parsing of data above.
1447 lttng_payload_reset(&payload
);
1452 * Disable event(s) of a channel and domain.
1453 * If no event name is specified, all events are disabled.
1454 * If no channel name is specified, the default 'channel0' is used.
1455 * Returns size of returned session payload data or a negative error code.
1457 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
, const char *channel_name
)
1460 struct lttng_event ev
;
1462 memset(&ev
, 0, sizeof(ev
));
1464 ev
.type
= LTTNG_EVENT_ALL
;
1465 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1467 ret
= -LTTNG_ERR_INVALID
;
1471 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, nullptr);
1476 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1478 struct lttng_channel
*channel
= nullptr;
1484 /* Validate domain. */
1485 switch (domain
->type
) {
1486 case LTTNG_DOMAIN_UST
:
1487 switch (domain
->buf_type
) {
1488 case LTTNG_BUFFER_PER_UID
:
1489 case LTTNG_BUFFER_PER_PID
:
1495 case LTTNG_DOMAIN_KERNEL
:
1496 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1504 channel
= lttng_channel_create_internal();
1509 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1514 void lttng_channel_destroy(struct lttng_channel
*channel
)
1520 if (channel
->attr
.extended
.ptr
) {
1521 free(channel
->attr
.extended
.ptr
);
1527 * Enable channel per domain
1528 * Returns size of returned session payload data or a negative error code.
1530 int lttng_enable_channel(struct lttng_handle
*handle
, struct lttng_channel
*in_chan
)
1532 enum lttng_error_code ret_code
;
1534 struct lttng_dynamic_buffer buffer
;
1535 struct lttcomm_session_msg lsm
;
1536 uint64_t total_buffer_size_needed_per_cpu
= 0;
1537 struct lttng_channel
*channel
= nullptr;
1539 lttng_dynamic_buffer_init(&buffer
);
1541 /* NULL arguments are forbidden. No default values. */
1542 if (handle
== nullptr || in_chan
== nullptr) {
1543 ret
= -LTTNG_ERR_INVALID
;
1548 * Verify that the amount of memory required to create the requested
1549 * buffer is available on the system at the moment.
1551 if (in_chan
->attr
.num_subbuf
> UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1553 ret
= -LTTNG_ERR_OVERFLOW
;
1557 total_buffer_size_needed_per_cpu
= in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1558 ret_code
= check_enough_available_memory(total_buffer_size_needed_per_cpu
);
1559 if (ret_code
!= LTTNG_OK
) {
1564 /* Copy the channel for easier manipulation. */
1565 channel
= lttng_channel_copy(in_chan
);
1567 ret
= -LTTNG_ERR_NOMEM
;
1571 /* Populate the channel extended attribute if necessary. */
1572 if (!channel
->attr
.extended
.ptr
) {
1573 struct lttng_channel_extended
*extended
= zmalloc
<lttng_channel_extended
>();
1576 ret
= -LTTNG_ERR_NOMEM
;
1580 lttng_channel_set_default_extended_attr(&handle
->domain
, extended
);
1581 channel
->attr
.extended
.ptr
= extended
;
1584 /* Prepare the payload */
1585 memset(&lsm
, 0, sizeof(lsm
));
1587 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
;
1588 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1590 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1592 ret
= -LTTNG_ERR_INVALID
;
1596 ret
= lttng_channel_serialize(channel
, &buffer
);
1598 ret
= -LTTNG_ERR_FATAL
;
1602 lsm
.u
.channel
.length
= buffer
.size
;
1604 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
, buffer
.size
, nullptr);
1606 lttng_channel_destroy(channel
);
1607 lttng_dynamic_buffer_reset(&buffer
);
1612 * All tracing will be stopped for registered events of the channel.
1613 * Returns size of returned session payload data or a negative error code.
1615 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1618 struct lttcomm_session_msg lsm
;
1620 /* Safety check. Both are mandatory. */
1621 if (handle
== nullptr || name
== nullptr) {
1622 return -LTTNG_ERR_INVALID
;
1625 memset(&lsm
, 0, sizeof(lsm
));
1627 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
;
1629 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
, sizeof(lsm
.u
.disable
.channel_name
));
1631 ret
= -LTTNG_ERR_INVALID
;
1635 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1637 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1639 ret
= -LTTNG_ERR_INVALID
;
1643 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
1649 * Lists all available tracepoints of domain.
1650 * Sets the contents of the events array.
1651 * Returns the number of lttng_event entries in events;
1652 * on error, returns a negative value.
1654 int lttng_list_tracepoints(struct lttng_handle
*handle
, struct lttng_event
**events
)
1656 enum lttng_error_code ret_code
;
1657 int ret
, total_payload_received
;
1658 char *reception_buffer
= nullptr;
1659 struct lttcomm_session_msg lsm
= {
1660 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
,
1666 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1667 size_t cmd_header_len
;
1668 unsigned int nb_events
= 0;
1670 if (handle
== nullptr) {
1671 ret
= -LTTNG_ERR_INVALID
;
1675 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1677 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1682 (void **) &reception_buffer
,
1683 (void **) &cmd_header
,
1689 total_payload_received
= ret
;
1692 ret
= -LTTNG_ERR_UNK
;
1696 if (cmd_header
->count
> INT_MAX
) {
1697 ret
= -LTTNG_ERR_OVERFLOW
;
1701 nb_events
= (unsigned int) cmd_header
->count
;
1704 struct lttng_buffer_view events_view
=
1705 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1706 struct lttng_payload_view events_payload_view
=
1707 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1709 ret_code
= lttng_events_create_and_flatten_from_payload(
1710 &events_payload_view
, nb_events
, events
);
1711 if (ret_code
!= LTTNG_OK
) {
1717 ret
= (int) nb_events
;
1721 free(reception_buffer
);
1726 * Lists all available tracepoint fields of domain.
1727 * Sets the contents of the event field array.
1728 * Returns the number of lttng_event_field entries in events;
1729 * on error, returns a negative value.
1731 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
, struct lttng_event_field
**fields
)
1733 enum lttng_error_code ret_code
;
1735 struct lttcomm_session_msg lsm
;
1736 const struct lttcomm_list_command_header
*cmd_header
= nullptr;
1737 unsigned int nb_event_fields
= 0;
1738 struct lttng_payload reply
;
1740 lttng_payload_init(&reply
);
1742 if (handle
== nullptr) {
1743 ret
= -LTTNG_ERR_INVALID
;
1747 memset(&lsm
, 0, sizeof(lsm
));
1748 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
;
1749 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1752 lttng_payload_view message_view
=
1753 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
1755 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1762 const lttng_buffer_view cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
1763 &reply
.buffer
, 0, sizeof(*cmd_header
));
1765 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1766 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1770 cmd_header
= (struct lttcomm_list_command_header
*) cmd_header_view
.data
;
1773 if (cmd_header
->count
> INT_MAX
) {
1774 ret
= -LTTNG_ERR_OVERFLOW
;
1778 nb_event_fields
= cmd_header
->count
;
1781 lttng_payload_view reply_view
=
1782 lttng_payload_view_from_payload(&reply
, sizeof(*cmd_header
), -1);
1784 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1785 &reply_view
, nb_event_fields
, fields
);
1786 if (ret_code
!= LTTNG_OK
) {
1792 ret
= nb_event_fields
;
1795 lttng_payload_reset(&reply
);
1800 * Lists all available kernel system calls. Allocates and sets the contents of
1803 * Returns the number of lttng_event entries in events; on error, returns a
1806 int lttng_list_syscalls(struct lttng_event
**events
)
1808 enum lttng_error_code ret_code
;
1809 int ret
, total_payload_received
;
1810 char *reception_buffer
= nullptr;
1811 struct lttcomm_session_msg lsm
= {};
1812 struct lttcomm_list_command_header
*cmd_header
= nullptr;
1813 size_t cmd_header_len
;
1814 uint32_t nb_events
= 0;
1817 ret
= -LTTNG_ERR_INVALID
;
1821 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
;
1822 /* Force kernel domain for system calls. */
1823 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1825 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1830 (void **) &reception_buffer
,
1831 (void **) &cmd_header
,
1836 total_payload_received
= ret
;
1839 ret
= -LTTNG_ERR_UNK
;
1843 if (cmd_header
->count
> INT_MAX
) {
1844 ret
= -LTTNG_ERR_OVERFLOW
;
1848 nb_events
= (unsigned int) cmd_header
->count
;
1851 const struct lttng_buffer_view events_view
=
1852 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1853 struct lttng_payload_view events_payload_view
=
1854 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1856 ret_code
= lttng_events_create_and_flatten_from_payload(
1857 &events_payload_view
, nb_events
, events
);
1858 if (ret_code
!= LTTNG_OK
) {
1864 ret
= (int) nb_events
;
1867 free(reception_buffer
);
1873 * Returns a human readable string describing
1874 * the error code (positive or negative value).
1876 const char *lttng_strerror(int code
)
1882 return error_get_str(code
);
1885 enum lttng_error_code
lttng_create_session_ext(struct lttng_session_descriptor
*session_descriptor
)
1887 enum lttng_error_code ret_code
;
1888 struct lttcomm_session_msg lsm
= {
1889 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
,
1895 void *reply
= nullptr;
1896 struct lttng_buffer_view reply_view
;
1898 bool sessiond_must_generate_ouput
;
1899 struct lttng_dynamic_buffer payload
;
1901 size_t descriptor_size
;
1902 struct lttng_session_descriptor
*descriptor_reply
= nullptr;
1904 lttng_dynamic_buffer_init(&payload
);
1905 if (!session_descriptor
) {
1906 ret_code
= LTTNG_ERR_INVALID
;
1910 sessiond_must_generate_ouput
=
1911 !lttng_session_descriptor_is_output_destination_initialized(session_descriptor
);
1912 if (sessiond_must_generate_ouput
) {
1913 const char *home_dir
= utils_get_home_dir();
1914 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1916 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1917 ret_code
= LTTNG_ERR_FATAL
;
1921 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1922 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
, home_dir_len
);
1924 ret_code
= LTTNG_ERR_NOMEM
;
1929 descriptor_size
= payload
.size
;
1930 ret
= lttng_session_descriptor_serialize(session_descriptor
, &payload
);
1932 ret_code
= LTTNG_ERR_INVALID
;
1935 descriptor_size
= payload
.size
- descriptor_size
;
1936 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1938 /* Command returns a session descriptor on success. */
1939 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1940 &lsm
, payload
.data
, payload
.size
, &reply
);
1941 if (reply_ret
< 0) {
1942 ret_code
= (lttng_error_code
) -reply_ret
;
1944 } else if (reply_ret
== 0) {
1945 /* Socket unexpectedly closed by the session daemon. */
1946 ret_code
= LTTNG_ERR_FATAL
;
1950 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1951 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
, &descriptor_reply
);
1953 ret_code
= LTTNG_ERR_FATAL
;
1956 ret_code
= LTTNG_OK
;
1957 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1960 lttng_dynamic_buffer_reset(&payload
);
1961 lttng_session_descriptor_destroy(descriptor_reply
);
1966 * Create a new session using name and url for destination.
1968 * Return 0 on success else a negative LTTng error code.
1970 int lttng_create_session(const char *name
, const char *url
)
1974 struct lttng_uri
*uris
= nullptr;
1975 struct lttng_session_descriptor
*descriptor
= nullptr;
1976 enum lttng_error_code ret_code
;
1979 ret
= -LTTNG_ERR_INVALID
;
1983 size
= uri_parse_str_urls(url
, nullptr, &uris
);
1985 ret
= -LTTNG_ERR_INVALID
;
1990 descriptor
= lttng_session_descriptor_create(name
);
1993 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1994 ret
= -LTTNG_ERR_INVALID
;
1997 descriptor
= lttng_session_descriptor_local_create(name
, uris
[0].dst
.path
);
2000 descriptor
= lttng_session_descriptor_network_create(name
, url
, nullptr);
2003 ret
= -LTTNG_ERR_INVALID
;
2007 ret
= -LTTNG_ERR_INVALID
;
2010 ret_code
= lttng_create_session_ext(descriptor
);
2011 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2013 lttng_session_descriptor_destroy(descriptor
);
2019 * Create a session exclusively used for snapshot.
2021 * Return 0 on success else a negative LTTng error code.
2023 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2026 enum lttng_error_code ret_code
;
2028 struct lttng_uri
*uris
= nullptr;
2029 struct lttng_session_descriptor
*descriptor
= nullptr;
2032 ret
= -LTTNG_ERR_INVALID
;
2036 size
= uri_parse_str_urls(snapshot_url
, nullptr, &uris
);
2038 ret
= -LTTNG_ERR_INVALID
;
2042 * If the user does not specify a custom subdir, use the session name.
2044 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2045 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2047 PERROR("Failed to set session name as network destination sub-directory");
2048 ret
= -LTTNG_ERR_FATAL
;
2050 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2051 /* Truncated output. */
2052 ret
= -LTTNG_ERR_INVALID
;
2059 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2062 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2063 ret
= -LTTNG_ERR_INVALID
;
2066 descriptor
= lttng_session_descriptor_snapshot_local_create(name
, uris
[0].dst
.path
);
2069 descriptor
= lttng_session_descriptor_snapshot_network_create(
2070 name
, snapshot_url
, nullptr);
2073 ret
= -LTTNG_ERR_INVALID
;
2077 ret
= -LTTNG_ERR_INVALID
;
2080 ret_code
= lttng_create_session_ext(descriptor
);
2081 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2083 lttng_session_descriptor_destroy(descriptor
);
2089 * Create a session exclusively used for live.
2091 * Return 0 on success else a negative LTTng error code.
2093 int lttng_create_session_live(const char *name
, const char *url
, unsigned int timer_interval
)
2096 enum lttng_error_code ret_code
;
2097 struct lttng_session_descriptor
*descriptor
= nullptr;
2100 ret
= -LTTNG_ERR_INVALID
;
2105 descriptor
= lttng_session_descriptor_live_network_create(
2106 name
, url
, nullptr, timer_interval
);
2108 descriptor
= lttng_session_descriptor_live_create(name
, timer_interval
);
2111 ret
= -LTTNG_ERR_INVALID
;
2114 ret_code
= lttng_create_session_ext(descriptor
);
2115 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2117 lttng_session_descriptor_destroy(descriptor
);
2122 * Stop the session and wait for the data before destroying it
2124 * Return 0 on success else a negative LTTng error code.
2126 int lttng_destroy_session(const char *session_name
)
2129 enum lttng_error_code ret_code
;
2130 enum lttng_destruction_handle_status status
;
2131 struct lttng_destruction_handle
*handle
= nullptr;
2134 * Stop the tracing and wait for the data to be
2137 ret
= _lttng_stop_tracing(session_name
, 1);
2138 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2142 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2143 if (ret_code
!= LTTNG_OK
) {
2144 ret
= (int) -ret_code
;
2147 LTTNG_ASSERT(handle
);
2149 /* Block until the completion of the destruction of the session. */
2150 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2151 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2152 ret
= -LTTNG_ERR_UNK
;
2156 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2157 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2158 ret
= -LTTNG_ERR_UNK
;
2161 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2163 lttng_destruction_handle_destroy(handle
);
2168 * Destroy the session without waiting for the data.
2170 int lttng_destroy_session_no_wait(const char *session_name
)
2172 enum lttng_error_code ret_code
;
2174 ret_code
= lttng_destroy_session_ext(session_name
, nullptr);
2175 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2179 * Ask the session daemon for all available sessions.
2180 * Sets the contents of the sessions array.
2181 * Returns the number of lttng_session entries in sessions;
2182 * on error, returns a negative value.
2184 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2187 struct lttcomm_session_msg lsm
;
2188 const size_t session_size
=
2189 sizeof(struct lttng_session
) + sizeof(struct lttng_session_extended
);
2190 size_t session_count
, i
;
2191 struct lttng_session_extended
*sessions_extended_begin
;
2192 struct lttng_session
*sessions
= nullptr;
2194 memset(&lsm
, 0, sizeof(lsm
));
2195 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
;
2197 * Initialize out_sessions to NULL so it is initialized when
2198 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2199 * be subsequently freed.
2201 *out_sessions
= nullptr;
2202 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &sessions
);
2207 ret
= -LTTNG_ERR_FATAL
;
2211 if (ret
% session_size
) {
2212 ret
= -LTTNG_ERR_UNK
;
2216 session_count
= (size_t) ret
/ session_size
;
2217 sessions_extended_begin
= (struct lttng_session_extended
*) (&sessions
[session_count
]);
2219 /* Set extended session info pointers. */
2220 for (i
= 0; i
< session_count
; i
++) {
2221 struct lttng_session
*session
= &sessions
[i
];
2222 struct lttng_session_extended
*extended
= &(sessions_extended_begin
[i
]);
2224 session
->extended
.ptr
= extended
;
2227 ret
= (int) session_count
;
2228 *out_sessions
= sessions
;
2233 enum lttng_error_code
lttng_session_get_creation_time(const struct lttng_session
*session
,
2234 uint64_t *creation_time
)
2236 enum lttng_error_code ret
= LTTNG_OK
;
2237 struct lttng_session_extended
*extended
;
2239 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2240 ret
= LTTNG_ERR_INVALID
;
2244 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2245 if (!extended
->creation_time
.is_set
) {
2246 /* Not created on the session daemon yet. */
2247 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2250 *creation_time
= extended
->creation_time
.value
;
2255 int lttng_set_session_shm_path(const char *session_name
, const char *shm_path
)
2258 struct lttcomm_session_msg lsm
;
2260 if (session_name
== nullptr) {
2261 return -LTTNG_ERR_INVALID
;
2264 memset(&lsm
, 0, sizeof(lsm
));
2265 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
;
2267 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2269 ret
= -LTTNG_ERR_INVALID
;
2273 ret
= lttng_strncpy(
2274 lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "", sizeof(lsm
.u
.set_shm_path
.shm_path
));
2276 ret
= -LTTNG_ERR_INVALID
;
2280 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2286 * Ask the session daemon for all available domains of a session.
2287 * Sets the contents of the domains array.
2288 * Returns the number of lttng_domain entries in domains;
2289 * on error, returns a negative value.
2291 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
2294 struct lttcomm_session_msg lsm
;
2296 if (session_name
== nullptr) {
2297 ret
= -LTTNG_ERR_INVALID
;
2301 memset(&lsm
, 0, sizeof(lsm
));
2302 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
;
2304 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2306 ret
= -LTTNG_ERR_INVALID
;
2310 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) domains
);
2315 return ret
/ sizeof(struct lttng_domain
);
2321 * Ask the session daemon for all available channels of a session.
2322 * Sets the contents of the channels array.
2323 * Returns the number of lttng_channel entries in channels;
2324 * on error, returns a negative value.
2326 int lttng_list_channels(struct lttng_handle
*handle
, struct lttng_channel
**channels
)
2328 int ret
, total_payload_received
;
2329 struct lttcomm_session_msg lsm
;
2330 char *reception_buffer
= nullptr;
2331 size_t cmd_header_len
= 0;
2332 struct lttcomm_list_command_header
*cmd_header
= nullptr;
2333 struct lttng_dynamic_buffer tmp_buffer
;
2335 lttng_dynamic_buffer_init(&tmp_buffer
);
2337 if (handle
== nullptr) {
2338 ret
= -LTTNG_ERR_INVALID
;
2342 memset(&lsm
, 0, sizeof(lsm
));
2343 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
;
2344 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2346 ret
= -LTTNG_ERR_INVALID
;
2350 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2352 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
2357 (void **) &reception_buffer
,
2358 (void **) &cmd_header
,
2364 total_payload_received
= ret
;
2366 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2367 ret
= -LTTNG_ERR_FATAL
;
2372 ret
= LTTNG_ERR_UNK
;
2376 if (cmd_header
->count
> INT_MAX
) {
2377 ret
= -LTTNG_ERR_OVERFLOW
;
2382 enum lttng_error_code ret_code
;
2383 const struct lttng_buffer_view events_view
=
2384 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
2386 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2387 &events_view
, cmd_header
->count
, channels
);
2388 if (ret_code
!= LTTNG_OK
) {
2394 ret
= (int) cmd_header
->count
;
2397 free(reception_buffer
);
2402 * Ask the session daemon for all available events of a session channel.
2403 * Sets the contents of the events array.
2404 * Returns the number of lttng_event entries in events;
2405 * on error, returns a negative value.
2407 int lttng_list_events(struct lttng_handle
*handle
,
2408 const char *channel_name
,
2409 struct lttng_event
**events
)
2412 struct lttcomm_session_msg lsm
= {};
2413 struct lttng_payload reply
;
2414 struct lttng_payload_view lsm_view
=
2415 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
2416 unsigned int nb_events
= 0;
2418 lttng_payload_init(&reply
);
2420 /* Safety check. An handle and channel name are mandatory. */
2421 if (handle
== nullptr || channel_name
== nullptr) {
2422 ret
= -LTTNG_ERR_INVALID
;
2426 /* Initialize command parameters. */
2427 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
;
2428 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2430 ret
= -LTTNG_ERR_INVALID
;
2434 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
, sizeof(lsm
.u
.list
.channel_name
));
2436 ret
= -LTTNG_ERR_INVALID
;
2440 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2442 /* Execute command against the session daemon. */
2443 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2449 const struct lttcomm_list_command_header
*cmd_reply_header
= nullptr;
2450 const lttng_payload_view cmd_reply_header_view
=
2451 lttng_payload_view_from_payload(&reply
, 0, sizeof(*cmd_reply_header
));
2453 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2454 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2458 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2459 cmd_reply_header_view
.buffer
.data
;
2460 if (cmd_reply_header
->count
> INT_MAX
) {
2461 ret
= -LTTNG_ERR_OVERFLOW
;
2465 nb_events
= (unsigned int) cmd_reply_header
->count
;
2469 enum lttng_error_code ret_code
;
2470 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2471 &reply
, sizeof(struct lttcomm_list_command_header
), -1);
2473 ret_code
= lttng_events_create_and_flatten_from_payload(
2474 &cmd_reply_payload
, nb_events
, events
);
2475 if (ret_code
!= LTTNG_OK
) {
2476 ret
= -((int) ret_code
);
2481 ret
= (int) nb_events
;
2483 lttng_payload_reset(&reply
);
2488 * Sets the tracing_group variable with name.
2489 * This function allocates memory pointed to by tracing_group.
2490 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2492 int lttng_set_tracing_group(const char *name
)
2497 if (name
== nullptr) {
2498 ret
= -LTTNG_ERR_INVALID
;
2502 new_group
= strdup(name
);
2504 ret
= -LTTNG_ERR_FATAL
;
2508 free(tracing_group
);
2509 tracing_group
= new_group
;
2510 new_group
= nullptr;
2516 int lttng_calibrate(struct lttng_handle
*handle
__attribute__((unused
)),
2517 struct lttng_calibrate
*calibrate
__attribute__((unused
)))
2520 * This command was removed in LTTng 2.9.
2522 return -LTTNG_ERR_UND
;
2526 * Set default channel attributes.
2527 * If either or both of the arguments are null, attr content is zeroe'd.
2529 void lttng_channel_set_default_attr(struct lttng_domain
*domain
, struct lttng_channel_attr
*attr
)
2531 struct lttng_channel_extended
*extended
;
2534 if (attr
== nullptr || domain
== nullptr) {
2538 /* Save the pointer for later use */
2539 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2540 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2542 /* Same for all domains. */
2543 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2544 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2545 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2547 switch (domain
->type
) {
2548 case LTTNG_DOMAIN_KERNEL
:
2549 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2550 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2551 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2552 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2553 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2555 case LTTNG_DOMAIN_UST
:
2556 switch (domain
->buf_type
) {
2557 case LTTNG_BUFFER_PER_UID
:
2558 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2559 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2560 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2561 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2562 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2564 case LTTNG_BUFFER_PER_PID
:
2566 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2567 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2568 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2569 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2570 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2574 /* Default behavior: leave set to 0. */
2579 lttng_channel_set_default_extended_attr(domain
, extended
);
2582 /* Reassign the extended pointer. */
2583 attr
->extended
.ptr
= extended
;
2586 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2587 uint64_t *discarded_events
)
2590 struct lttng_channel_extended
*chan_ext
;
2592 if (!channel
|| !discarded_events
) {
2593 ret
= -LTTNG_ERR_INVALID
;
2597 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2600 * This can happen since the lttng_channel structure is
2601 * used for other tasks where this pointer is never set.
2603 *discarded_events
= 0;
2607 *discarded_events
= chan_ext
->discarded_events
;
2612 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
, uint64_t *lost_packets
)
2615 struct lttng_channel_extended
*chan_ext
;
2617 if (!channel
|| !lost_packets
) {
2618 ret
= -LTTNG_ERR_INVALID
;
2622 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2625 * This can happen since the lttng_channel structure is
2626 * used for other tasks where this pointer is never set.
2632 *lost_packets
= chan_ext
->lost_packets
;
2637 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2638 uint64_t *monitor_timer_interval
)
2642 if (!chan
|| !monitor_timer_interval
) {
2643 ret
= -LTTNG_ERR_INVALID
;
2647 if (!chan
->attr
.extended
.ptr
) {
2648 ret
= -LTTNG_ERR_INVALID
;
2652 *monitor_timer_interval
=
2653 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2658 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2659 uint64_t monitor_timer_interval
)
2663 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2664 ret
= -LTTNG_ERR_INVALID
;
2668 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2669 monitor_timer_interval
;
2674 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
, int64_t *blocking_timeout
)
2678 if (!chan
|| !blocking_timeout
) {
2679 ret
= -LTTNG_ERR_INVALID
;
2683 if (!chan
->attr
.extended
.ptr
) {
2684 ret
= -LTTNG_ERR_INVALID
;
2689 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
2694 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
, int64_t blocking_timeout
)
2697 int64_t msec_timeout
;
2699 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2700 ret
= -LTTNG_ERR_INVALID
;
2704 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2705 ret
= -LTTNG_ERR_INVALID
;
2710 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2711 * us to accept a narrower range of values (msecs expressed as a signed
2714 msec_timeout
= blocking_timeout
/ 1000;
2715 if (msec_timeout
!= (int32_t) msec_timeout
) {
2716 ret
= -LTTNG_ERR_INVALID
;
2720 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
=
2727 * Check if session daemon is alive.
2729 * Return 1 if alive or 0 if not.
2730 * On error returns a negative value.
2732 int lttng_session_daemon_alive(void)
2736 ret
= set_session_daemon_path();
2742 if (*sessiond_sock_path
== '\0') {
2744 * No socket path set. Weird error which means the constructor
2750 ret
= try_connect_sessiond(sessiond_sock_path
);
2761 * Set URL for a consumer for a session and domain.
2763 * Return 0 on success, else a negative value.
2765 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2766 const char *control_url
,
2767 const char *data_url
)
2771 struct lttcomm_session_msg lsm
;
2772 struct lttng_uri
*uris
= nullptr;
2774 if (handle
== nullptr || (control_url
== nullptr && data_url
== nullptr)) {
2775 ret
= -LTTNG_ERR_INVALID
;
2779 memset(&lsm
, 0, sizeof(lsm
));
2781 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
;
2783 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2785 ret
= -LTTNG_ERR_INVALID
;
2789 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2791 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2793 ret
= -LTTNG_ERR_INVALID
;
2797 lsm
.u
.uri
.size
= size
;
2799 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
2800 &lsm
, uris
, sizeof(struct lttng_uri
) * size
, nullptr);
2810 extern "C" LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2811 int lttng_enable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2819 extern "C" LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2820 int lttng_disable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2828 extern "C" LTTNG_EXPORT
int
2829 _lttng_create_session_ext(const char *name
, const char *url
, const char *datetime
);
2830 int _lttng_create_session_ext(const char *name
__attribute__((unused
)),
2831 const char *url
__attribute__((unused
)),
2832 const char *datetime
__attribute__((unused
)))
2838 * For a given session name, this call checks if the data is ready to be read
2839 * or is still being extracted by the consumer(s) hence not ready to be used by
2842 int lttng_data_pending(const char *session_name
)
2845 struct lttcomm_session_msg lsm
;
2846 uint8_t *pending
= nullptr;
2848 if (session_name
== nullptr) {
2849 return -LTTNG_ERR_INVALID
;
2852 memset(&lsm
, 0, sizeof(lsm
));
2853 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
;
2855 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2857 ret
= -LTTNG_ERR_INVALID
;
2861 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2864 } else if (ret
!= 1) {
2865 /* Unexpected payload size */
2866 ret
= -LTTNG_ERR_INVALID
;
2868 } else if (!pending
) {
2869 /* Internal error. */
2870 ret
= -LTTNG_ERR_UNK
;
2874 ret
= (int) *pending
;
2881 * Regenerate the metadata for a session.
2882 * Return 0 on success, a negative error code on error.
2884 int lttng_regenerate_metadata(const char *session_name
)
2887 struct lttcomm_session_msg lsm
;
2889 if (!session_name
) {
2890 ret
= -LTTNG_ERR_INVALID
;
2894 memset(&lsm
, 0, sizeof(lsm
));
2895 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
;
2897 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2899 ret
= -LTTNG_ERR_INVALID
;
2903 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2914 * Deprecated, replaced by lttng_regenerate_metadata.
2916 int lttng_metadata_regenerate(const char *session_name
)
2918 return lttng_regenerate_metadata(session_name
);
2922 * Regenerate the statedump of a session.
2923 * Return 0 on success, a negative error code on error.
2925 int lttng_regenerate_statedump(const char *session_name
)
2928 struct lttcomm_session_msg lsm
;
2930 if (!session_name
) {
2931 ret
= -LTTNG_ERR_INVALID
;
2935 memset(&lsm
, 0, sizeof(lsm
));
2936 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
;
2938 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2940 ret
= -LTTNG_ERR_INVALID
;
2944 ret
= lttng_ctl_ask_sessiond(&lsm
, nullptr);
2955 _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
, bool generate_name
)
2958 struct lttcomm_session_msg lsm
= {
2959 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
,
2965 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
2966 struct lttcomm_session_msg
*message_lsm
;
2967 struct lttng_payload message
;
2968 struct lttng_payload reply
;
2969 struct lttng_trigger
*reply_trigger
= nullptr;
2970 enum lttng_domain_type domain_type
;
2971 const struct lttng_credentials user_creds
= {
2972 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2973 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2975 const char *unused_trigger_name
= nullptr;
2976 enum lttng_trigger_status trigger_status
;
2978 lttng_payload_init(&message
);
2979 lttng_payload_init(&reply
);
2982 ret
= -LTTNG_ERR_INVALID
;
2986 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2987 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2988 /* Re-using already registered trigger. */
2989 ret
= -LTTNG_ERR_INVALID
;
2994 trigger_status
= lttng_trigger_set_name(trigger
, name
);
2995 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2996 ret
= -LTTNG_ERR_NOMEM
;
3001 if (!trigger
->creds
.uid
.is_set
) {
3002 /* Use the client's credentials as the trigger credentials. */
3003 lttng_trigger_set_credentials(trigger
, &user_creds
);
3006 * Validate that either the current trigger credentials and the
3007 * client credentials are identical or that the current user is
3008 * root. The root user can register, unregister triggers for
3009 * himself and other users.
3011 * This check is also present on the sessiond side, using the
3012 * credentials passed on the socket. These check are all
3015 const struct lttng_credentials
*trigger_creds
=
3016 lttng_trigger_get_credentials(trigger
);
3018 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3019 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3020 ret
= -LTTNG_ERR_EPERM
;
3021 goto end_unset_name
;
3026 if (!lttng_trigger_validate(trigger
)) {
3027 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3028 goto end_unset_name
;
3031 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(trigger
);
3033 lsm
.domain
.type
= domain_type
;
3035 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3037 ret
= -LTTNG_ERR_NOMEM
;
3038 goto end_unset_name
;
3041 ret
= lttng_trigger_serialize(trigger
, &message
);
3043 ret
= -LTTNG_ERR_UNK
;
3044 goto end_unset_name
;
3048 * This is needed to populate the trigger object size for the command
3051 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3053 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3056 struct lttng_payload_view message_view
=
3057 lttng_payload_view_from_payload(&message
, 0, -1);
3059 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3060 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3062 goto end_unset_name
;
3067 struct lttng_payload_view reply_view
=
3068 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3070 ret
= lttng_trigger_create_from_payload(&reply_view
, &reply_trigger
);
3072 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3073 goto end_unset_name
;
3077 if (name
|| generate_name
) {
3078 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3080 ret
= -LTTNG_ERR_NOMEM
;
3089 trigger_status
= lttng_trigger_set_name(trigger
, nullptr);
3090 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3091 ret
= -LTTNG_ERR_UNK
;
3094 lttng_payload_reset(&message
);
3095 lttng_payload_reset(&reply
);
3096 lttng_trigger_destroy(reply_trigger
);
3100 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3102 /* Register an anonymous trigger. */
3103 return _lttng_register_trigger(trigger
, nullptr, false);
3106 enum lttng_error_code
lttng_register_trigger_with_name(struct lttng_trigger
*trigger
,
3109 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3111 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3114 enum lttng_error_code
lttng_register_trigger_with_automatic_name(struct lttng_trigger
*trigger
)
3116 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3118 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3121 enum lttng_error_code
lttng_error_query_execute(const struct lttng_error_query
*query
,
3122 const struct lttng_endpoint
*endpoint
,
3123 struct lttng_error_query_results
**results
)
3126 enum lttng_error_code ret_code
;
3127 struct lttcomm_session_msg lsm
= {
3128 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
,
3134 struct lttng_payload message
;
3135 struct lttng_payload reply
;
3136 struct lttcomm_session_msg
*message_lsm
;
3138 lttng_payload_init(&message
);
3139 lttng_payload_init(&reply
);
3141 if (!query
|| !results
) {
3142 ret_code
= LTTNG_ERR_INVALID
;
3146 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3147 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3151 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3153 ret_code
= LTTNG_ERR_NOMEM
;
3157 ret
= lttng_error_query_serialize(query
, &message
);
3159 ret_code
= LTTNG_ERR_UNK
;
3163 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3164 message_lsm
->u
.error_query
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3167 struct lttng_payload_view message_view
=
3168 lttng_payload_view_from_payload(&message
, 0, -1);
3170 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3171 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3173 ret_code
= (lttng_error_code
) -ret
;
3179 ssize_t reply_create_ret
;
3180 struct lttng_payload_view reply_view
=
3181 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3184 lttng_error_query_results_create_from_payload(&reply_view
, results
);
3185 if (reply_create_ret
< 0) {
3186 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3191 ret_code
= LTTNG_OK
;
3193 lttng_payload_reset(&message
);
3194 lttng_payload_reset(&reply
);
3198 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3201 struct lttcomm_session_msg lsm
;
3202 struct lttcomm_session_msg
*message_lsm
;
3203 struct lttng_payload message
;
3204 struct lttng_payload reply
;
3205 struct lttng_trigger
*copy
= nullptr;
3206 const struct lttng_credentials user_creds
= {
3207 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3208 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3211 lttng_payload_init(&message
);
3212 lttng_payload_init(&reply
);
3215 ret
= -LTTNG_ERR_INVALID
;
3219 copy
= lttng_trigger_copy(trigger
);
3221 ret
= -LTTNG_ERR_UNK
;
3225 if (!copy
->creds
.uid
.is_set
) {
3226 /* Use the client credentials as the trigger credentials */
3227 lttng_trigger_set_credentials(copy
, &user_creds
);
3230 * Validate that either the current trigger credentials and the
3231 * client credentials are identical or that the current user is
3232 * root. The root user can register, unregister triggers for
3233 * himself and other users.
3235 * This check is also present on the sessiond side, using the
3236 * credentials passed on the socket. These check are all
3239 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(copy
);
3240 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3241 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3242 ret
= -LTTNG_ERR_EPERM
;
3248 if (!lttng_trigger_validate(copy
)) {
3249 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3253 memset(&lsm
, 0, sizeof(lsm
));
3254 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
;
3256 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3258 ret
= -LTTNG_ERR_NOMEM
;
3262 ret
= lttng_trigger_serialize(copy
, &message
);
3264 ret
= -LTTNG_ERR_UNK
;
3269 * This is needed to populate the trigger object size for the command
3270 * header and number of fds sent.
3272 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3274 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3277 struct lttng_payload_view message_view
=
3278 lttng_payload_view_from_payload(&message
, 0, -1);
3281 * Update the message header with the number of fd that will be
3284 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3286 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3294 lttng_trigger_destroy(copy
);
3295 lttng_payload_reset(&message
);
3296 lttng_payload_reset(&reply
);
3301 * Ask the session daemon for all registered triggers for the current user.
3303 * Allocates and return an lttng_triggers set.
3304 * On error, returns a suitable lttng_error_code.
3306 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3309 enum lttng_error_code ret_code
= LTTNG_OK
;
3310 struct lttcomm_session_msg lsm
= {
3311 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
,
3317 struct lttng_triggers
*local_triggers
= nullptr;
3318 struct lttng_payload reply
;
3319 struct lttng_payload_view lsm_view
=
3320 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
3322 lttng_payload_init(&reply
);
3324 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3326 ret_code
= (enum lttng_error_code
) - ret
;
3331 struct lttng_payload_view reply_view
=
3332 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3334 ret
= lttng_triggers_create_from_payload(&reply_view
, &local_triggers
);
3336 ret_code
= LTTNG_ERR_FATAL
;
3341 *triggers
= local_triggers
;
3342 local_triggers
= nullptr;
3344 lttng_payload_reset(&reply
);
3345 lttng_triggers_destroy(local_triggers
);
3352 static void __attribute__((constructor
)) init()
3354 /* Set default session group */
3355 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3361 static void __attribute__((destructor
)) lttng_ctl_exit()
3363 free(tracing_group
);