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); \
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(void)
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, NULL
);
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(void)
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(void)
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(void)
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(void)
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
= NULL
;
729 handle
= zmalloc
<lttng_handle
>();
730 if (handle
== NULL
) {
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
== NULL
|| socket_path
== NULL
) {
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
, NULL
);
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
== NULL
) {
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
, NULL
);
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
== NULL
) {
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
, NULL
);
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
== NULL
|| ctx
== NULL
) {
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
, NULL
, 0, NULL
);
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, NULL
);
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
= NULL
;
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
= NULL
;
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
== NULL
|| ev
== NULL
) {
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
!= NULL
|| 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
: NULL
,
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
: NULL
,
1240 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1248 if (filter_expression
&& ctx
) {
1249 filter_bytecode_free(ctx
);
1250 filter_ir_free(ctx
);
1251 filter_parser_ctx_free(ctx
);
1253 if (free_filter_expression
) {
1255 * The filter expression has been replaced and must be freed as
1256 * it is not the original filter expression received as a
1259 free(filter_expression
);
1262 * Return directly to the caller and don't ask the sessiond since
1263 * something went wrong in the parsing of data above.
1265 lttng_payload_reset(&payload
);
1269 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1270 struct lttng_event
*ev
,
1271 const char *channel_name
,
1272 const char *original_filter_expression
)
1274 struct lttcomm_session_msg lsm
= {
1275 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
,
1281 struct lttng_payload payload
;
1283 unsigned int free_filter_expression
= 0;
1284 struct filter_parser_ctx
*ctx
= NULL
;
1285 size_t bytecode_len
= 0;
1288 * We have either a filter or some exclusions, so we need to set up
1289 * a variable-length payload from where to send the data.
1291 lttng_payload_init(&payload
);
1294 * Cast as non-const since we may replace the filter expression
1295 * by a dynamically allocated string. Otherwise, the original
1296 * string is not modified.
1298 char *filter_expression
= (char *) original_filter_expression
;
1300 if (handle
== NULL
|| ev
== NULL
) {
1301 ret
= -LTTNG_ERR_INVALID
;
1306 * Empty filter string will always be rejected by the parser
1307 * anyway, so treat this corner-case early to eliminate
1308 * lttng_fmemopen error for 0-byte allocation.
1310 if (filter_expression
&& filter_expression
[0] == '\0') {
1311 ret
= -LTTNG_ERR_INVALID
;
1315 /* Parse filter expression. */
1316 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1317 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1318 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1319 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1320 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1321 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1324 /* Setup agent filter if needed. */
1325 agent_filter
= set_agent_filter(filter_expression
, ev
);
1326 if (!agent_filter
) {
1327 if (!filter_expression
) {
1329 * No JUL and no filter, just skip
1336 * With an agent filter, the original filter has
1337 * been added to it thus replace the filter
1340 filter_expression
= agent_filter
;
1341 free_filter_expression
= 1;
1345 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) == LTTNG_FILTER_MAX_LEN
) {
1346 ret
= -LTTNG_ERR_FILTER_INVAL
;
1350 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1355 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) + sizeof(ctx
->bytecode
->b
);
1356 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1357 ret
= -LTTNG_ERR_FILTER_INVAL
;
1363 ret
= lttng_event_serialize(ev
,
1368 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1371 ret
= -LTTNG_ERR_INVALID
;
1375 /* If no channel name, send empty string. */
1376 ret
= lttng_strncpy(
1377 lsm
.u
.disable
.channel_name
, channel_name
?: "", sizeof(lsm
.u
.disable
.channel_name
));
1379 ret
= -LTTNG_ERR_INVALID
;
1384 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1387 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1389 ret
= -LTTNG_ERR_INVALID
;
1393 /* Length of the serialized event. */
1394 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1397 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
1398 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1405 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1406 if (fd_count
== 1) {
1407 struct fd_handle
*h
= lttng_payload_view_pop_fd_handle(&view
);
1413 fd_to_send
= fd_handle_get_fd(h
);
1417 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1418 fd_count
? &fd_to_send
: NULL
,
1420 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1428 if (filter_expression
&& ctx
) {
1429 filter_bytecode_free(ctx
);
1430 filter_ir_free(ctx
);
1431 filter_parser_ctx_free(ctx
);
1433 if (free_filter_expression
) {
1435 * The filter expression has been replaced and must be freed as
1436 * it is not the original filter expression received as a
1439 free(filter_expression
);
1442 * Return directly to the caller and don't ask the sessiond since
1443 * something went wrong in the parsing of data above.
1445 lttng_payload_reset(&payload
);
1450 * Disable event(s) of a channel and domain.
1451 * If no event name is specified, all events are disabled.
1452 * If no channel name is specified, the default 'channel0' is used.
1453 * Returns size of returned session payload data or a negative error code.
1455 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
, const char *channel_name
)
1458 struct lttng_event ev
;
1460 memset(&ev
, 0, sizeof(ev
));
1462 ev
.type
= LTTNG_EVENT_ALL
;
1463 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1465 ret
= -LTTNG_ERR_INVALID
;
1469 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1474 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1476 struct lttng_channel
*channel
= NULL
;
1482 /* Validate domain. */
1483 switch (domain
->type
) {
1484 case LTTNG_DOMAIN_UST
:
1485 switch (domain
->buf_type
) {
1486 case LTTNG_BUFFER_PER_UID
:
1487 case LTTNG_BUFFER_PER_PID
:
1493 case LTTNG_DOMAIN_KERNEL
:
1494 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1502 channel
= lttng_channel_create_internal();
1507 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1512 void lttng_channel_destroy(struct lttng_channel
*channel
)
1518 if (channel
->attr
.extended
.ptr
) {
1519 free(channel
->attr
.extended
.ptr
);
1525 * Enable channel per domain
1526 * Returns size of returned session payload data or a negative error code.
1528 int lttng_enable_channel(struct lttng_handle
*handle
, struct lttng_channel
*in_chan
)
1530 enum lttng_error_code ret_code
;
1532 struct lttng_dynamic_buffer buffer
;
1533 struct lttcomm_session_msg lsm
;
1534 uint64_t total_buffer_size_needed_per_cpu
= 0;
1535 struct lttng_channel
*channel
= NULL
;
1537 lttng_dynamic_buffer_init(&buffer
);
1539 /* NULL arguments are forbidden. No default values. */
1540 if (handle
== NULL
|| in_chan
== NULL
) {
1541 ret
= -LTTNG_ERR_INVALID
;
1546 * Verify that the amount of memory required to create the requested
1547 * buffer is available on the system at the moment.
1549 if (in_chan
->attr
.num_subbuf
> UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1551 ret
= -LTTNG_ERR_OVERFLOW
;
1555 total_buffer_size_needed_per_cpu
= in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1556 ret_code
= check_enough_available_memory(total_buffer_size_needed_per_cpu
);
1557 if (ret_code
!= LTTNG_OK
) {
1562 /* Copy the channel for easier manipulation. */
1563 channel
= lttng_channel_copy(in_chan
);
1565 ret
= -LTTNG_ERR_NOMEM
;
1569 /* Populate the channel extended attribute if necessary. */
1570 if (!channel
->attr
.extended
.ptr
) {
1571 struct lttng_channel_extended
*extended
= zmalloc
<lttng_channel_extended
>();
1574 ret
= -LTTNG_ERR_NOMEM
;
1578 lttng_channel_set_default_extended_attr(&handle
->domain
, extended
);
1579 channel
->attr
.extended
.ptr
= extended
;
1582 /* Prepare the payload */
1583 memset(&lsm
, 0, sizeof(lsm
));
1585 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
;
1586 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1588 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1590 ret
= -LTTNG_ERR_INVALID
;
1594 ret
= lttng_channel_serialize(channel
, &buffer
);
1596 ret
= -LTTNG_ERR_FATAL
;
1600 lsm
.u
.channel
.length
= buffer
.size
;
1602 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
, buffer
.size
, NULL
);
1604 lttng_channel_destroy(channel
);
1605 lttng_dynamic_buffer_reset(&buffer
);
1610 * All tracing will be stopped for registered events of the channel.
1611 * Returns size of returned session payload data or a negative error code.
1613 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1616 struct lttcomm_session_msg lsm
;
1618 /* Safety check. Both are mandatory. */
1619 if (handle
== NULL
|| name
== NULL
) {
1620 return -LTTNG_ERR_INVALID
;
1623 memset(&lsm
, 0, sizeof(lsm
));
1625 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
;
1627 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
, sizeof(lsm
.u
.disable
.channel_name
));
1629 ret
= -LTTNG_ERR_INVALID
;
1633 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1635 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
1637 ret
= -LTTNG_ERR_INVALID
;
1641 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1647 * Lists all available tracepoints of domain.
1648 * Sets the contents of the events array.
1649 * Returns the number of lttng_event entries in events;
1650 * on error, returns a negative value.
1652 int lttng_list_tracepoints(struct lttng_handle
*handle
, struct lttng_event
**events
)
1654 enum lttng_error_code ret_code
;
1655 int ret
, total_payload_received
;
1656 char *reception_buffer
= NULL
;
1657 struct lttcomm_session_msg lsm
= {
1658 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
,
1664 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1665 size_t cmd_header_len
;
1666 unsigned int nb_events
= 0;
1668 if (handle
== NULL
) {
1669 ret
= -LTTNG_ERR_INVALID
;
1673 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1675 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1680 (void **) &reception_buffer
,
1681 (void **) &cmd_header
,
1687 total_payload_received
= ret
;
1690 ret
= -LTTNG_ERR_UNK
;
1694 if (cmd_header
->count
> INT_MAX
) {
1695 ret
= -LTTNG_ERR_OVERFLOW
;
1699 nb_events
= (unsigned int) cmd_header
->count
;
1702 struct lttng_buffer_view events_view
=
1703 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1704 struct lttng_payload_view events_payload_view
=
1705 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1707 ret_code
= lttng_events_create_and_flatten_from_payload(
1708 &events_payload_view
, nb_events
, events
);
1709 if (ret_code
!= LTTNG_OK
) {
1715 ret
= (int) nb_events
;
1719 free(reception_buffer
);
1724 * Lists all available tracepoint fields of domain.
1725 * Sets the contents of the event field array.
1726 * Returns the number of lttng_event_field entries in events;
1727 * on error, returns a negative value.
1729 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
, struct lttng_event_field
**fields
)
1731 enum lttng_error_code ret_code
;
1733 struct lttcomm_session_msg lsm
;
1734 const struct lttcomm_list_command_header
*cmd_header
= NULL
;
1735 unsigned int nb_event_fields
= 0;
1736 struct lttng_payload reply
;
1738 lttng_payload_init(&reply
);
1740 if (handle
== NULL
) {
1741 ret
= -LTTNG_ERR_INVALID
;
1745 memset(&lsm
, 0, sizeof(lsm
));
1746 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
;
1747 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1750 lttng_payload_view message_view
=
1751 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
1753 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1760 const lttng_buffer_view cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
1761 &reply
.buffer
, 0, sizeof(*cmd_header
));
1763 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1764 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1768 cmd_header
= (struct lttcomm_list_command_header
*) cmd_header_view
.data
;
1771 if (cmd_header
->count
> INT_MAX
) {
1772 ret
= -LTTNG_ERR_OVERFLOW
;
1776 nb_event_fields
= cmd_header
->count
;
1779 lttng_payload_view reply_view
=
1780 lttng_payload_view_from_payload(&reply
, sizeof(*cmd_header
), -1);
1782 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1783 &reply_view
, nb_event_fields
, fields
);
1784 if (ret_code
!= LTTNG_OK
) {
1790 ret
= nb_event_fields
;
1793 lttng_payload_reset(&reply
);
1798 * Lists all available kernel system calls. Allocates and sets the contents of
1801 * Returns the number of lttng_event entries in events; on error, returns a
1804 int lttng_list_syscalls(struct lttng_event
**events
)
1806 enum lttng_error_code ret_code
;
1807 int ret
, total_payload_received
;
1808 char *reception_buffer
= NULL
;
1809 struct lttcomm_session_msg lsm
= {};
1810 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1811 size_t cmd_header_len
;
1812 uint32_t nb_events
= 0;
1815 ret
= -LTTNG_ERR_INVALID
;
1819 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
;
1820 /* Force kernel domain for system calls. */
1821 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1823 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1828 (void **) &reception_buffer
,
1829 (void **) &cmd_header
,
1834 total_payload_received
= ret
;
1837 ret
= -LTTNG_ERR_UNK
;
1841 if (cmd_header
->count
> INT_MAX
) {
1842 ret
= -LTTNG_ERR_OVERFLOW
;
1846 nb_events
= (unsigned int) cmd_header
->count
;
1849 const struct lttng_buffer_view events_view
=
1850 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
1851 struct lttng_payload_view events_payload_view
=
1852 lttng_payload_view_from_buffer_view(&events_view
, 0, -1);
1854 ret_code
= lttng_events_create_and_flatten_from_payload(
1855 &events_payload_view
, nb_events
, events
);
1856 if (ret_code
!= LTTNG_OK
) {
1862 ret
= (int) nb_events
;
1865 free(reception_buffer
);
1871 * Returns a human readable string describing
1872 * the error code (positive or negative value).
1874 const char *lttng_strerror(int code
)
1880 return error_get_str(code
);
1883 enum lttng_error_code
lttng_create_session_ext(struct lttng_session_descriptor
*session_descriptor
)
1885 enum lttng_error_code ret_code
;
1886 struct lttcomm_session_msg lsm
= {
1887 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
,
1894 struct lttng_buffer_view reply_view
;
1896 bool sessiond_must_generate_ouput
;
1897 struct lttng_dynamic_buffer payload
;
1899 size_t descriptor_size
;
1900 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1902 lttng_dynamic_buffer_init(&payload
);
1903 if (!session_descriptor
) {
1904 ret_code
= LTTNG_ERR_INVALID
;
1908 sessiond_must_generate_ouput
=
1909 !lttng_session_descriptor_is_output_destination_initialized(session_descriptor
);
1910 if (sessiond_must_generate_ouput
) {
1911 const char *home_dir
= utils_get_home_dir();
1912 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1914 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1915 ret_code
= LTTNG_ERR_FATAL
;
1919 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1920 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
, home_dir_len
);
1922 ret_code
= LTTNG_ERR_NOMEM
;
1927 descriptor_size
= payload
.size
;
1928 ret
= lttng_session_descriptor_serialize(session_descriptor
, &payload
);
1930 ret_code
= LTTNG_ERR_INVALID
;
1933 descriptor_size
= payload
.size
- descriptor_size
;
1934 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1936 /* Command returns a session descriptor on success. */
1937 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1938 &lsm
, payload
.data
, payload
.size
, &reply
);
1939 if (reply_ret
< 0) {
1940 ret_code
= (lttng_error_code
) -reply_ret
;
1942 } else if (reply_ret
== 0) {
1943 /* Socket unexpectedly closed by the session daemon. */
1944 ret_code
= LTTNG_ERR_FATAL
;
1948 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1949 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
, &descriptor_reply
);
1951 ret_code
= LTTNG_ERR_FATAL
;
1954 ret_code
= LTTNG_OK
;
1955 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1958 lttng_dynamic_buffer_reset(&payload
);
1959 lttng_session_descriptor_destroy(descriptor_reply
);
1964 * Create a new session using name and url for destination.
1966 * Return 0 on success else a negative LTTng error code.
1968 int lttng_create_session(const char *name
, const char *url
)
1972 struct lttng_uri
*uris
= NULL
;
1973 struct lttng_session_descriptor
*descriptor
= NULL
;
1974 enum lttng_error_code ret_code
;
1977 ret
= -LTTNG_ERR_INVALID
;
1981 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1983 ret
= -LTTNG_ERR_INVALID
;
1988 descriptor
= lttng_session_descriptor_create(name
);
1991 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1992 ret
= -LTTNG_ERR_INVALID
;
1995 descriptor
= lttng_session_descriptor_local_create(name
, uris
[0].dst
.path
);
1998 descriptor
= lttng_session_descriptor_network_create(name
, url
, NULL
);
2001 ret
= -LTTNG_ERR_INVALID
;
2005 ret
= -LTTNG_ERR_INVALID
;
2008 ret_code
= lttng_create_session_ext(descriptor
);
2009 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2011 lttng_session_descriptor_destroy(descriptor
);
2017 * Create a session exclusively used for snapshot.
2019 * Return 0 on success else a negative LTTng error code.
2021 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2024 enum lttng_error_code ret_code
;
2026 struct lttng_uri
*uris
= NULL
;
2027 struct lttng_session_descriptor
*descriptor
= NULL
;
2030 ret
= -LTTNG_ERR_INVALID
;
2034 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2036 ret
= -LTTNG_ERR_INVALID
;
2040 * If the user does not specify a custom subdir, use the session name.
2042 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2043 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2045 PERROR("Failed to set session name as network destination sub-directory");
2046 ret
= -LTTNG_ERR_FATAL
;
2048 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2049 /* Truncated output. */
2050 ret
= -LTTNG_ERR_INVALID
;
2057 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2060 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2061 ret
= -LTTNG_ERR_INVALID
;
2064 descriptor
= lttng_session_descriptor_snapshot_local_create(name
, uris
[0].dst
.path
);
2068 lttng_session_descriptor_snapshot_network_create(name
, snapshot_url
, NULL
);
2071 ret
= -LTTNG_ERR_INVALID
;
2075 ret
= -LTTNG_ERR_INVALID
;
2078 ret_code
= lttng_create_session_ext(descriptor
);
2079 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2081 lttng_session_descriptor_destroy(descriptor
);
2087 * Create a session exclusively used for live.
2089 * Return 0 on success else a negative LTTng error code.
2091 int lttng_create_session_live(const char *name
, const char *url
, unsigned int timer_interval
)
2094 enum lttng_error_code ret_code
;
2095 struct lttng_session_descriptor
*descriptor
= NULL
;
2098 ret
= -LTTNG_ERR_INVALID
;
2103 descriptor
= lttng_session_descriptor_live_network_create(
2104 name
, url
, NULL
, timer_interval
);
2106 descriptor
= lttng_session_descriptor_live_create(name
, timer_interval
);
2109 ret
= -LTTNG_ERR_INVALID
;
2112 ret_code
= lttng_create_session_ext(descriptor
);
2113 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2115 lttng_session_descriptor_destroy(descriptor
);
2120 * Stop the session and wait for the data before destroying it
2122 * Return 0 on success else a negative LTTng error code.
2124 int lttng_destroy_session(const char *session_name
)
2127 enum lttng_error_code ret_code
;
2128 enum lttng_destruction_handle_status status
;
2129 struct lttng_destruction_handle
*handle
= NULL
;
2132 * Stop the tracing and wait for the data to be
2135 ret
= _lttng_stop_tracing(session_name
, 1);
2136 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2140 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2141 if (ret_code
!= LTTNG_OK
) {
2142 ret
= (int) -ret_code
;
2145 LTTNG_ASSERT(handle
);
2147 /* Block until the completion of the destruction of the session. */
2148 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2149 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2150 ret
= -LTTNG_ERR_UNK
;
2154 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2155 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2156 ret
= -LTTNG_ERR_UNK
;
2159 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2161 lttng_destruction_handle_destroy(handle
);
2166 * Destroy the session without waiting for the data.
2168 int lttng_destroy_session_no_wait(const char *session_name
)
2170 enum lttng_error_code ret_code
;
2172 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2173 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2177 * Ask the session daemon for all available sessions.
2178 * Sets the contents of the sessions array.
2179 * Returns the number of lttng_session entries in sessions;
2180 * on error, returns a negative value.
2182 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2185 struct lttcomm_session_msg lsm
;
2186 const size_t session_size
=
2187 sizeof(struct lttng_session
) + sizeof(struct lttng_session_extended
);
2188 size_t session_count
, i
;
2189 struct lttng_session_extended
*sessions_extended_begin
;
2190 struct lttng_session
*sessions
= NULL
;
2192 memset(&lsm
, 0, sizeof(lsm
));
2193 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
;
2195 * Initialize out_sessions to NULL so it is initialized when
2196 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2197 * be subsequently freed.
2199 *out_sessions
= NULL
;
2200 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &sessions
);
2205 ret
= -LTTNG_ERR_FATAL
;
2209 if (ret
% session_size
) {
2210 ret
= -LTTNG_ERR_UNK
;
2214 session_count
= (size_t) ret
/ session_size
;
2215 sessions_extended_begin
= (struct lttng_session_extended
*) (&sessions
[session_count
]);
2217 /* Set extended session info pointers. */
2218 for (i
= 0; i
< session_count
; i
++) {
2219 struct lttng_session
*session
= &sessions
[i
];
2220 struct lttng_session_extended
*extended
= &(sessions_extended_begin
[i
]);
2222 session
->extended
.ptr
= extended
;
2225 ret
= (int) session_count
;
2226 *out_sessions
= sessions
;
2231 enum lttng_error_code
lttng_session_get_creation_time(const struct lttng_session
*session
,
2232 uint64_t *creation_time
)
2234 enum lttng_error_code ret
= LTTNG_OK
;
2235 struct lttng_session_extended
*extended
;
2237 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2238 ret
= LTTNG_ERR_INVALID
;
2242 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2243 if (!extended
->creation_time
.is_set
) {
2244 /* Not created on the session daemon yet. */
2245 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2248 *creation_time
= extended
->creation_time
.value
;
2253 int lttng_set_session_shm_path(const char *session_name
, const char *shm_path
)
2256 struct lttcomm_session_msg lsm
;
2258 if (session_name
== NULL
) {
2259 return -LTTNG_ERR_INVALID
;
2262 memset(&lsm
, 0, sizeof(lsm
));
2263 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
;
2265 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2267 ret
= -LTTNG_ERR_INVALID
;
2271 ret
= lttng_strncpy(
2272 lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "", sizeof(lsm
.u
.set_shm_path
.shm_path
));
2274 ret
= -LTTNG_ERR_INVALID
;
2278 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2284 * Ask the session daemon for all available domains of a session.
2285 * Sets the contents of the domains array.
2286 * Returns the number of lttng_domain entries in domains;
2287 * on error, returns a negative value.
2289 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
2292 struct lttcomm_session_msg lsm
;
2294 if (session_name
== NULL
) {
2295 ret
= -LTTNG_ERR_INVALID
;
2299 memset(&lsm
, 0, sizeof(lsm
));
2300 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
;
2302 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2304 ret
= -LTTNG_ERR_INVALID
;
2308 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) domains
);
2313 return ret
/ sizeof(struct lttng_domain
);
2319 * Ask the session daemon for all available channels of a session.
2320 * Sets the contents of the channels array.
2321 * Returns the number of lttng_channel entries in channels;
2322 * on error, returns a negative value.
2324 int lttng_list_channels(struct lttng_handle
*handle
, struct lttng_channel
**channels
)
2326 int ret
, total_payload_received
;
2327 struct lttcomm_session_msg lsm
;
2328 char *reception_buffer
= NULL
;
2329 size_t cmd_header_len
= 0;
2330 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2331 struct lttng_dynamic_buffer tmp_buffer
;
2333 lttng_dynamic_buffer_init(&tmp_buffer
);
2335 if (handle
== NULL
) {
2336 ret
= -LTTNG_ERR_INVALID
;
2340 memset(&lsm
, 0, sizeof(lsm
));
2341 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
;
2342 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2344 ret
= -LTTNG_ERR_INVALID
;
2348 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2350 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
2355 (void **) &reception_buffer
,
2356 (void **) &cmd_header
,
2362 total_payload_received
= ret
;
2364 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2365 ret
= -LTTNG_ERR_FATAL
;
2370 ret
= LTTNG_ERR_UNK
;
2374 if (cmd_header
->count
> INT_MAX
) {
2375 ret
= -LTTNG_ERR_OVERFLOW
;
2380 enum lttng_error_code ret_code
;
2381 const struct lttng_buffer_view events_view
=
2382 lttng_buffer_view_init(reception_buffer
, 0, total_payload_received
);
2384 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2385 &events_view
, cmd_header
->count
, channels
);
2386 if (ret_code
!= LTTNG_OK
) {
2392 ret
= (int) cmd_header
->count
;
2395 free(reception_buffer
);
2400 * Ask the session daemon for all available events of a session channel.
2401 * Sets the contents of the events array.
2402 * Returns the number of lttng_event entries in events;
2403 * on error, returns a negative value.
2405 int lttng_list_events(struct lttng_handle
*handle
,
2406 const char *channel_name
,
2407 struct lttng_event
**events
)
2410 struct lttcomm_session_msg lsm
= {};
2411 struct lttng_payload reply
;
2412 struct lttng_payload_view lsm_view
=
2413 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
2414 unsigned int nb_events
= 0;
2416 lttng_payload_init(&reply
);
2418 /* Safety check. An handle and channel name are mandatory. */
2419 if (handle
== NULL
|| channel_name
== NULL
) {
2420 ret
= -LTTNG_ERR_INVALID
;
2424 /* Initialize command parameters. */
2425 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
;
2426 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2428 ret
= -LTTNG_ERR_INVALID
;
2432 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
, sizeof(lsm
.u
.list
.channel_name
));
2434 ret
= -LTTNG_ERR_INVALID
;
2438 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2440 /* Execute command against the session daemon. */
2441 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2447 const struct lttcomm_list_command_header
*cmd_reply_header
= NULL
;
2448 const lttng_payload_view cmd_reply_header_view
=
2449 lttng_payload_view_from_payload(&reply
, 0, sizeof(*cmd_reply_header
));
2451 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2452 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2456 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2457 cmd_reply_header_view
.buffer
.data
;
2458 if (cmd_reply_header
->count
> INT_MAX
) {
2459 ret
= -LTTNG_ERR_OVERFLOW
;
2463 nb_events
= (unsigned int) cmd_reply_header
->count
;
2467 enum lttng_error_code ret_code
;
2468 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2469 &reply
, sizeof(struct lttcomm_list_command_header
), -1);
2471 ret_code
= lttng_events_create_and_flatten_from_payload(
2472 &cmd_reply_payload
, nb_events
, events
);
2473 if (ret_code
!= LTTNG_OK
) {
2474 ret
= -((int) ret_code
);
2479 ret
= (int) nb_events
;
2481 lttng_payload_reset(&reply
);
2486 * Sets the tracing_group variable with name.
2487 * This function allocates memory pointed to by tracing_group.
2488 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2490 int lttng_set_tracing_group(const char *name
)
2496 ret
= -LTTNG_ERR_INVALID
;
2500 new_group
= strdup(name
);
2502 ret
= -LTTNG_ERR_FATAL
;
2506 free(tracing_group
);
2507 tracing_group
= new_group
;
2514 int lttng_calibrate(struct lttng_handle
*handle
__attribute__((unused
)),
2515 struct lttng_calibrate
*calibrate
__attribute__((unused
)))
2518 * This command was removed in LTTng 2.9.
2520 return -LTTNG_ERR_UND
;
2524 * Set default channel attributes.
2525 * If either or both of the arguments are null, attr content is zeroe'd.
2527 void lttng_channel_set_default_attr(struct lttng_domain
*domain
, struct lttng_channel_attr
*attr
)
2529 struct lttng_channel_extended
*extended
;
2532 if (attr
== NULL
|| domain
== NULL
) {
2536 /* Save the pointer for later use */
2537 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2538 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2540 /* Same for all domains. */
2541 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2542 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2543 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2545 switch (domain
->type
) {
2546 case LTTNG_DOMAIN_KERNEL
:
2547 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2548 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2549 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2550 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2551 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2553 case LTTNG_DOMAIN_UST
:
2554 switch (domain
->buf_type
) {
2555 case LTTNG_BUFFER_PER_UID
:
2556 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2557 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2558 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2559 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2560 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2562 case LTTNG_BUFFER_PER_PID
:
2564 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2565 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2566 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2567 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2568 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2572 /* Default behavior: leave set to 0. */
2577 lttng_channel_set_default_extended_attr(domain
, extended
);
2580 /* Reassign the extended pointer. */
2581 attr
->extended
.ptr
= extended
;
2584 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2585 uint64_t *discarded_events
)
2588 struct lttng_channel_extended
*chan_ext
;
2590 if (!channel
|| !discarded_events
) {
2591 ret
= -LTTNG_ERR_INVALID
;
2595 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2598 * This can happen since the lttng_channel structure is
2599 * used for other tasks where this pointer is never set.
2601 *discarded_events
= 0;
2605 *discarded_events
= chan_ext
->discarded_events
;
2610 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
, uint64_t *lost_packets
)
2613 struct lttng_channel_extended
*chan_ext
;
2615 if (!channel
|| !lost_packets
) {
2616 ret
= -LTTNG_ERR_INVALID
;
2620 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2623 * This can happen since the lttng_channel structure is
2624 * used for other tasks where this pointer is never set.
2630 *lost_packets
= chan_ext
->lost_packets
;
2635 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2636 uint64_t *monitor_timer_interval
)
2640 if (!chan
|| !monitor_timer_interval
) {
2641 ret
= -LTTNG_ERR_INVALID
;
2645 if (!chan
->attr
.extended
.ptr
) {
2646 ret
= -LTTNG_ERR_INVALID
;
2650 *monitor_timer_interval
=
2651 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2656 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2657 uint64_t monitor_timer_interval
)
2661 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2662 ret
= -LTTNG_ERR_INVALID
;
2666 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2667 monitor_timer_interval
;
2672 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
, int64_t *blocking_timeout
)
2676 if (!chan
|| !blocking_timeout
) {
2677 ret
= -LTTNG_ERR_INVALID
;
2681 if (!chan
->attr
.extended
.ptr
) {
2682 ret
= -LTTNG_ERR_INVALID
;
2687 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
;
2692 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
, int64_t blocking_timeout
)
2695 int64_t msec_timeout
;
2697 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2698 ret
= -LTTNG_ERR_INVALID
;
2702 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2703 ret
= -LTTNG_ERR_INVALID
;
2708 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2709 * us to accept a narrower range of values (msecs expressed as a signed
2712 msec_timeout
= blocking_timeout
/ 1000;
2713 if (msec_timeout
!= (int32_t) msec_timeout
) {
2714 ret
= -LTTNG_ERR_INVALID
;
2718 ((struct lttng_channel_extended
*) chan
->attr
.extended
.ptr
)->blocking_timeout
=
2725 * Check if session daemon is alive.
2727 * Return 1 if alive or 0 if not.
2728 * On error returns a negative value.
2730 int lttng_session_daemon_alive(void)
2734 ret
= set_session_daemon_path();
2740 if (*sessiond_sock_path
== '\0') {
2742 * No socket path set. Weird error which means the constructor
2748 ret
= try_connect_sessiond(sessiond_sock_path
);
2759 * Set URL for a consumer for a session and domain.
2761 * Return 0 on success, else a negative value.
2763 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2764 const char *control_url
,
2765 const char *data_url
)
2769 struct lttcomm_session_msg lsm
;
2770 struct lttng_uri
*uris
= NULL
;
2772 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2773 ret
= -LTTNG_ERR_INVALID
;
2777 memset(&lsm
, 0, sizeof(lsm
));
2779 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
;
2781 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
, sizeof(lsm
.session
.name
));
2783 ret
= -LTTNG_ERR_INVALID
;
2787 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2789 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2791 ret
= -LTTNG_ERR_INVALID
;
2795 lsm
.u
.uri
.size
= size
;
2797 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
2798 &lsm
, uris
, sizeof(struct lttng_uri
) * size
, NULL
);
2808 extern "C" LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2809 int lttng_enable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2817 extern "C" LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2818 int lttng_disable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2826 extern "C" LTTNG_EXPORT
int
2827 _lttng_create_session_ext(const char *name
, const char *url
, const char *datetime
);
2828 int _lttng_create_session_ext(const char *name
__attribute__((unused
)),
2829 const char *url
__attribute__((unused
)),
2830 const char *datetime
__attribute__((unused
)))
2836 * For a given session name, this call checks if the data is ready to be read
2837 * or is still being extracted by the consumer(s) hence not ready to be used by
2840 int lttng_data_pending(const char *session_name
)
2843 struct lttcomm_session_msg lsm
;
2844 uint8_t *pending
= NULL
;
2846 if (session_name
== NULL
) {
2847 return -LTTNG_ERR_INVALID
;
2850 memset(&lsm
, 0, sizeof(lsm
));
2851 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
;
2853 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2855 ret
= -LTTNG_ERR_INVALID
;
2859 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2862 } else if (ret
!= 1) {
2863 /* Unexpected payload size */
2864 ret
= -LTTNG_ERR_INVALID
;
2866 } else if (!pending
) {
2867 /* Internal error. */
2868 ret
= -LTTNG_ERR_UNK
;
2872 ret
= (int) *pending
;
2879 * Regenerate the metadata for a session.
2880 * Return 0 on success, a negative error code on error.
2882 int lttng_regenerate_metadata(const char *session_name
)
2885 struct lttcomm_session_msg lsm
;
2887 if (!session_name
) {
2888 ret
= -LTTNG_ERR_INVALID
;
2892 memset(&lsm
, 0, sizeof(lsm
));
2893 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
;
2895 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2897 ret
= -LTTNG_ERR_INVALID
;
2901 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2912 * Deprecated, replaced by lttng_regenerate_metadata.
2914 int lttng_metadata_regenerate(const char *session_name
)
2916 return lttng_regenerate_metadata(session_name
);
2920 * Regenerate the statedump of a session.
2921 * Return 0 on success, a negative error code on error.
2923 int lttng_regenerate_statedump(const char *session_name
)
2926 struct lttcomm_session_msg lsm
;
2928 if (!session_name
) {
2929 ret
= -LTTNG_ERR_INVALID
;
2933 memset(&lsm
, 0, sizeof(lsm
));
2934 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
;
2936 ret
= lttng_strncpy(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
2938 ret
= -LTTNG_ERR_INVALID
;
2942 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2953 _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
, bool generate_name
)
2956 struct lttcomm_session_msg lsm
= {
2957 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
,
2963 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
2964 struct lttcomm_session_msg
*message_lsm
;
2965 struct lttng_payload message
;
2966 struct lttng_payload reply
;
2967 struct lttng_trigger
*reply_trigger
= NULL
;
2968 enum lttng_domain_type domain_type
;
2969 const struct lttng_credentials user_creds
= {
2970 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2971 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2973 const char *unused_trigger_name
= NULL
;
2974 enum lttng_trigger_status trigger_status
;
2976 lttng_payload_init(&message
);
2977 lttng_payload_init(&reply
);
2980 ret
= -LTTNG_ERR_INVALID
;
2984 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2985 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2986 /* Re-using already registered trigger. */
2987 ret
= -LTTNG_ERR_INVALID
;
2992 trigger_status
= lttng_trigger_set_name(trigger
, name
);
2993 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2994 ret
= -LTTNG_ERR_NOMEM
;
2999 if (!trigger
->creds
.uid
.is_set
) {
3000 /* Use the client's credentials as the trigger credentials. */
3001 lttng_trigger_set_credentials(trigger
, &user_creds
);
3004 * Validate that either the current trigger credentials and the
3005 * client credentials are identical or that the current user is
3006 * root. The root user can register, unregister triggers for
3007 * himself and other users.
3009 * This check is also present on the sessiond side, using the
3010 * credentials passed on the socket. These check are all
3013 const struct lttng_credentials
*trigger_creds
=
3014 lttng_trigger_get_credentials(trigger
);
3016 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3017 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3018 ret
= -LTTNG_ERR_EPERM
;
3019 goto end_unset_name
;
3024 if (!lttng_trigger_validate(trigger
)) {
3025 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3026 goto end_unset_name
;
3029 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(trigger
);
3031 lsm
.domain
.type
= domain_type
;
3033 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3035 ret
= -LTTNG_ERR_NOMEM
;
3036 goto end_unset_name
;
3039 ret
= lttng_trigger_serialize(trigger
, &message
);
3041 ret
= -LTTNG_ERR_UNK
;
3042 goto end_unset_name
;
3046 * This is needed to populate the trigger object size for the command
3049 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3051 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3054 struct lttng_payload_view message_view
=
3055 lttng_payload_view_from_payload(&message
, 0, -1);
3057 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3058 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3060 goto end_unset_name
;
3065 struct lttng_payload_view reply_view
=
3066 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3068 ret
= lttng_trigger_create_from_payload(&reply_view
, &reply_trigger
);
3070 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3071 goto end_unset_name
;
3075 if (name
|| generate_name
) {
3076 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3078 ret
= -LTTNG_ERR_NOMEM
;
3087 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3088 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3089 ret
= -LTTNG_ERR_UNK
;
3092 lttng_payload_reset(&message
);
3093 lttng_payload_reset(&reply
);
3094 lttng_trigger_destroy(reply_trigger
);
3098 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3100 /* Register an anonymous trigger. */
3101 return _lttng_register_trigger(trigger
, NULL
, false);
3104 enum lttng_error_code
lttng_register_trigger_with_name(struct lttng_trigger
*trigger
,
3107 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3109 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3112 enum lttng_error_code
lttng_register_trigger_with_automatic_name(struct lttng_trigger
*trigger
)
3114 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3116 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) - ret
;
3119 enum lttng_error_code
lttng_error_query_execute(const struct lttng_error_query
*query
,
3120 const struct lttng_endpoint
*endpoint
,
3121 struct lttng_error_query_results
**results
)
3124 enum lttng_error_code ret_code
;
3125 struct lttcomm_session_msg lsm
= {
3126 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
,
3132 struct lttng_payload message
;
3133 struct lttng_payload reply
;
3134 struct lttcomm_session_msg
*message_lsm
;
3136 lttng_payload_init(&message
);
3137 lttng_payload_init(&reply
);
3139 if (!query
|| !results
) {
3140 ret_code
= LTTNG_ERR_INVALID
;
3144 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3145 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3149 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3151 ret_code
= LTTNG_ERR_NOMEM
;
3155 ret
= lttng_error_query_serialize(query
, &message
);
3157 ret_code
= LTTNG_ERR_UNK
;
3161 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3162 message_lsm
->u
.error_query
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3165 struct lttng_payload_view message_view
=
3166 lttng_payload_view_from_payload(&message
, 0, -1);
3168 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3169 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3171 ret_code
= (lttng_error_code
) -ret
;
3177 ssize_t reply_create_ret
;
3178 struct lttng_payload_view reply_view
=
3179 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3182 lttng_error_query_results_create_from_payload(&reply_view
, results
);
3183 if (reply_create_ret
< 0) {
3184 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3189 ret_code
= LTTNG_OK
;
3191 lttng_payload_reset(&message
);
3192 lttng_payload_reset(&reply
);
3196 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3199 struct lttcomm_session_msg lsm
;
3200 struct lttcomm_session_msg
*message_lsm
;
3201 struct lttng_payload message
;
3202 struct lttng_payload reply
;
3203 struct lttng_trigger
*copy
= NULL
;
3204 const struct lttng_credentials user_creds
= {
3205 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3206 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3209 lttng_payload_init(&message
);
3210 lttng_payload_init(&reply
);
3213 ret
= -LTTNG_ERR_INVALID
;
3217 copy
= lttng_trigger_copy(trigger
);
3219 ret
= -LTTNG_ERR_UNK
;
3223 if (!copy
->creds
.uid
.is_set
) {
3224 /* Use the client credentials as the trigger credentials */
3225 lttng_trigger_set_credentials(copy
, &user_creds
);
3228 * Validate that either the current trigger credentials and the
3229 * client credentials are identical or that the current user is
3230 * root. The root user can register, unregister triggers for
3231 * himself and other users.
3233 * This check is also present on the sessiond side, using the
3234 * credentials passed on the socket. These check are all
3237 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(copy
);
3238 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3239 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3240 ret
= -LTTNG_ERR_EPERM
;
3246 if (!lttng_trigger_validate(copy
)) {
3247 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3251 memset(&lsm
, 0, sizeof(lsm
));
3252 lsm
.cmd_type
= LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
;
3254 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3256 ret
= -LTTNG_ERR_NOMEM
;
3260 ret
= lttng_trigger_serialize(copy
, &message
);
3262 ret
= -LTTNG_ERR_UNK
;
3267 * This is needed to populate the trigger object size for the command
3268 * header and number of fds sent.
3270 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3272 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3275 struct lttng_payload_view message_view
=
3276 lttng_payload_view_from_payload(&message
, 0, -1);
3279 * Update the message header with the number of fd that will be
3282 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3284 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3292 lttng_trigger_destroy(copy
);
3293 lttng_payload_reset(&message
);
3294 lttng_payload_reset(&reply
);
3299 * Ask the session daemon for all registered triggers for the current user.
3301 * Allocates and return an lttng_triggers set.
3302 * On error, returns a suitable lttng_error_code.
3304 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3307 enum lttng_error_code ret_code
= LTTNG_OK
;
3308 struct lttcomm_session_msg lsm
= {
3309 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
,
3315 struct lttng_triggers
*local_triggers
= NULL
;
3316 struct lttng_payload reply
;
3317 struct lttng_payload_view lsm_view
=
3318 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
3320 lttng_payload_init(&reply
);
3322 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3324 ret_code
= (enum lttng_error_code
) - ret
;
3329 struct lttng_payload_view reply_view
=
3330 lttng_payload_view_from_payload(&reply
, 0, reply
.buffer
.size
);
3332 ret
= lttng_triggers_create_from_payload(&reply_view
, &local_triggers
);
3334 ret_code
= LTTNG_ERR_FATAL
;
3339 *triggers
= local_triggers
;
3340 local_triggers
= NULL
;
3342 lttng_payload_reset(&reply
);
3343 lttng_triggers_destroy(local_triggers
);
3350 static void __attribute__((constructor
)) init(void)
3352 /* Set default session group */
3353 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3359 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3361 free(tracing_group
);