4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 EfficiOS Inc.
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
21 #include <common/bytecode/bytecode.h>
22 #include <common/align.h>
23 #include <common/common.h>
24 #include <common/compat/errno.h>
25 #include <common/compat/string.h>
26 #include <common/defaults.h>
27 #include <common/dynamic-array.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/payload-view.h>
30 #include <common/payload.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/tracker.h>
33 #include <common/unix.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/channel-internal.h>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-internal.h>
41 #include <lttng/health-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/trigger/trigger-internal.h>
47 #include <lttng/userspace-probe-internal.h>
49 #include "lttng-ctl-helper.h"
50 #include <common/filter/filter-ast.h>
51 #include <common/filter/filter-parser.hpp>
52 #include <common/filter/memstream.h>
54 #define COPY_DOMAIN_PACKED(dst, src) \
56 struct lttng_domain _tmp_domain; \
58 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
62 /* Socket to session daemon for communication */
63 static int sessiond_socket
= -1;
64 static char sessiond_sock_path
[PATH_MAX
];
67 static char *tracing_group
;
73 * Those two variables are used by error.h to silent or control the verbosity of
74 * error message. They are global to the library so application linking with it
75 * are able to compile correctly and also control verbosity of the library.
77 LTTNG_EXPORT
int lttng_opt_quiet
;
78 LTTNG_EXPORT
int lttng_opt_verbose
;
79 LTTNG_EXPORT
int lttng_opt_mi
;
82 * Copy domain to lttcomm_session_msg domain.
84 * If domain is unknown, default domain will be the kernel.
86 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
87 struct lttng_domain
*src
)
91 case LTTNG_DOMAIN_KERNEL
:
92 case LTTNG_DOMAIN_UST
:
93 case LTTNG_DOMAIN_JUL
:
94 case LTTNG_DOMAIN_LOG4J
:
95 case LTTNG_DOMAIN_PYTHON
:
96 memcpy(dst
, src
, sizeof(struct lttng_domain
));
99 memset(dst
, 0, sizeof(struct lttng_domain
));
106 * Send lttcomm_session_msg to the session daemon.
108 * On success, returns the number of bytes sent (>=0)
109 * On error, returns -1
111 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
116 ret
= -LTTNG_ERR_NO_SESSIOND
;
120 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str((lttcomm_sessiond_command
) lsm
->cmd_type
),
123 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
124 sizeof(struct lttcomm_session_msg
));
126 ret
= -LTTNG_ERR_FATAL
;
134 * Send var len data to the session daemon.
136 * On success, returns the number of bytes sent (>=0)
137 * On error, returns -1
139 static int send_session_varlen(const void *data
, size_t len
)
144 ret
= -LTTNG_ERR_NO_SESSIOND
;
153 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
155 ret
= -LTTNG_ERR_FATAL
;
163 * Send file descriptors to the session daemon.
165 * On success, returns the number of bytes sent (>=0)
166 * On error, returns -1
168 static int send_session_fds(const int *fds
, size_t nb_fd
)
173 ret
= -LTTNG_ERR_NO_SESSIOND
;
177 if (!fds
|| !nb_fd
) {
182 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
184 ret
= -LTTNG_ERR_FATAL
;
192 * Receive data from the sessiond socket.
194 * On success, returns the number of bytes received (>=0)
195 * On error, returns a negative lttng_error_code.
197 static int recv_data_sessiond(void *buf
, size_t len
)
201 LTTNG_ASSERT(len
> 0);
204 ret
= -LTTNG_ERR_NO_SESSIOND
;
208 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
210 ret
= -LTTNG_ERR_FATAL
;
211 } else if (ret
== 0) {
212 ret
= -LTTNG_ERR_NO_SESSIOND
;
220 * Receive a payload from the session daemon by appending to an existing
222 * On success, returns the number of bytes received (>=0)
223 * On error, returns a negative lttng_error_code.
225 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
228 const size_t original_payload_size
= payload
->buffer
.size
;
230 ret
= lttng_dynamic_buffer_set_size(
231 &payload
->buffer
, payload
->buffer
.size
+ len
);
233 ret
= -LTTNG_ERR_NOMEM
;
237 ret
= recv_data_sessiond(
238 payload
->buffer
.data
+ original_payload_size
, len
);
244 * Check if we are in the specified group.
246 * If yes return 1, else return -1.
248 int lttng_check_tracing_group(void)
250 gid_t
*grp_list
, tracing_gid
;
251 int grp_list_size
, grp_id
, i
;
253 const char *grp_name
= tracing_group
;
255 /* Get GID of group 'tracing' */
256 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
257 /* If grp_tracing is NULL, the group does not exist. */
261 /* Get number of supplementary group IDs */
262 grp_list_size
= getgroups(0, NULL
);
263 if (grp_list_size
< 0) {
268 /* Alloc group list of the right size */
269 grp_list
= (gid_t
*) zmalloc(grp_list_size
* sizeof(gid_t
));
274 grp_id
= getgroups(grp_list_size
, grp_list
);
280 for (i
= 0; i
< grp_list_size
; i
++) {
281 if (grp_list
[i
] == tracing_gid
) {
294 static enum lttng_error_code
check_enough_available_memory(
295 uint64_t num_bytes_requested_per_cpu
)
298 enum lttng_error_code ret_code
;
300 uint64_t best_mem_info
;
301 uint64_t num_bytes_requested_total
;
304 * Get the number of CPU currently online to compute the amount of
305 * memory needed to create a buffer for every CPU.
307 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
309 ret_code
= LTTNG_ERR_FATAL
;
313 if (num_bytes_requested_per_cpu
> UINT64_MAX
/ (uint64_t) num_cpu
) {
315 ret_code
= LTTNG_ERR_OVERFLOW
;
319 num_bytes_requested_total
=
320 num_bytes_requested_per_cpu
* (uint64_t) num_cpu
;
323 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
324 * reliable estimate we can get but it is only exposed by the kernel
325 * since 3.14. (See Linux kernel commit:
326 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
328 ret
= utils_get_memory_available(&best_mem_info
);
334 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
335 * is a sanity check for obvious user error.
337 ret
= utils_get_memory_total(&best_mem_info
);
342 /* No valid source of information. */
343 ret_code
= LTTNG_ERR_NOMEM
;
347 if (best_mem_info
>= num_bytes_requested_total
) {
350 ret_code
= LTTNG_ERR_NOMEM
;
357 * Try connect to session daemon with sock_path.
359 * Return 0 on success, else -1
361 static int try_connect_sessiond(const char *sock_path
)
365 /* If socket exist, we check if the daemon listens for connect. */
366 ret
= access(sock_path
, F_OK
);
372 ret
= lttcomm_connect_unix_sock(sock_path
);
378 ret
= lttcomm_close_unix_sock(ret
);
380 PERROR("lttcomm_close_unix_sock");
390 * Set sessiond socket path by putting it in the global sessiond_sock_path
393 * Returns 0 on success, negative value on failure (the sessiond socket path
394 * is somehow too long or ENOMEM).
396 static int set_session_daemon_path(void)
398 int in_tgroup
= 0; /* In tracing group. */
404 /* Are we in the tracing group ? */
405 in_tgroup
= lttng_check_tracing_group();
408 if ((uid
== 0) || in_tgroup
== 1) {
409 const int ret
= lttng_strncpy(sessiond_sock_path
,
410 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
411 sizeof(sessiond_sock_path
));
423 ret
= try_connect_sessiond(sessiond_sock_path
);
427 /* Global session daemon not available... */
429 /* ...or not in tracing group (and not root), default */
432 * With GNU C < 2.1, snprintf returns -1 if the target buffer
434 * With GNU C >= 2.1, snprintf returns the required size
435 * (excluding closing null)
437 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
439 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
451 * Connect to the LTTng session daemon.
453 * On success, return the socket's file descriptor. On error, return -1.
455 int connect_sessiond(void)
459 ret
= set_session_daemon_path();
464 /* Connect to the sesssion daemon. */
465 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
476 static void reset_global_sessiond_connection_state(void)
478 sessiond_socket
= -1;
483 * Clean disconnect from the session daemon.
485 * On success, return 0. On error, return -1.
487 static int disconnect_sessiond(void)
492 ret
= lttcomm_close_unix_sock(sessiond_socket
);
493 reset_global_sessiond_connection_state();
499 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
507 ret
= -LTTNG_ERR_INVALID
;
517 ret
= recv_data_sessiond(buf
, len
);
523 ret
= -LTTNG_ERR_INVALID
;
527 /* Move ownership of command header buffer to user. */
532 /* No command header. */
548 * Ask the session daemon a specific command and put the data into buf.
549 * Takes extra var. len. data and file descriptors as input to send to the
552 * Return size of data (only payload, not header) or a negative error code.
554 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
555 const int *fds
, size_t nb_fd
, const void *vardata
,
556 size_t vardata_len
, void **user_payload_buf
,
557 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
561 struct lttcomm_lttng_msg llm
;
563 ret
= connect_sessiond();
565 ret
= -LTTNG_ERR_NO_SESSIOND
;
568 sessiond_socket
= ret
;
572 ret
= send_session_msg(lsm
);
574 /* Ret value is a valid lttng error code. */
577 /* Send var len data */
578 ret
= send_session_varlen(vardata
, vardata_len
);
580 /* Ret value is a valid lttng error code. */
585 ret
= send_session_fds(fds
, nb_fd
);
587 /* Ret value is a valid lttng error code. */
591 /* Get header from data transmission */
592 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
594 /* Ret value is a valid lttng error code. */
598 /* Check error code if OK */
599 if (llm
.ret_code
!= LTTNG_OK
) {
604 /* Get command header from data transmission */
605 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
606 user_cmd_header_buf
, user_cmd_header_len
);
611 /* Get payload from data transmission */
612 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
621 disconnect_sessiond();
625 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
626 struct lttng_payload
*reply
)
629 struct lttcomm_lttng_msg llm
;
630 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
632 LTTNG_ASSERT(reply
->buffer
.size
== 0);
633 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
635 ret
= connect_sessiond();
637 ret
= -LTTNG_ERR_NO_SESSIOND
;
640 sessiond_socket
= ret
;
644 /* Send command to session daemon */
645 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
646 message
->buffer
.size
);
648 ret
= -LTTNG_ERR_FATAL
;
653 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
656 ret
= -LTTNG_ERR_FATAL
;
661 /* Get header from data transmission */
662 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
664 /* Ret value is a valid lttng error code. */
668 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
670 /* Check error code if OK */
671 if (llm
.ret_code
!= LTTNG_OK
) {
672 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
673 /* Invalid error code received. */
674 ret
= -LTTNG_ERR_UNK
;
681 if (llm
.cmd_header_size
> 0) {
682 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
688 /* Get command header from data transmission */
689 if (llm
.data_size
> 0) {
690 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
696 if (llm
.fd_count
> 0) {
697 ret
= lttcomm_recv_payload_fds_unix_sock(
698 sessiond_socket
, llm
.fd_count
, reply
);
704 /* Don't return the llm header to the caller. */
705 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
706 reply
->buffer
.size
- sizeof(llm
));
707 ret
= lttng_dynamic_buffer_set_size(
708 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
710 /* Can't happen as size is reduced. */
714 ret
= reply
->buffer
.size
;
717 disconnect_sessiond();
722 * Create lttng handle and return pointer.
724 * The returned pointer will be NULL in case of malloc() error.
726 struct lttng_handle
*lttng_create_handle(const char *session_name
,
727 struct lttng_domain
*domain
)
730 struct lttng_handle
*handle
= NULL
;
732 handle
= (lttng_handle
*) zmalloc(sizeof(struct lttng_handle
));
733 if (handle
== NULL
) {
734 PERROR("malloc handle");
738 /* Copy session name */
739 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
740 sizeof(handle
->session_name
));
745 /* Copy lttng domain or leave initialized to 0. */
747 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
758 * Destroy handle by free(3) the pointer.
760 void lttng_destroy_handle(struct lttng_handle
*handle
)
766 * Register an outside consumer.
768 * Returns size of returned session payload data or a negative error code.
770 int lttng_register_consumer(struct lttng_handle
*handle
,
771 const char *socket_path
)
774 struct lttcomm_session_msg lsm
;
776 if (handle
== NULL
|| socket_path
== NULL
) {
777 ret
= -LTTNG_ERR_INVALID
;
781 memset(&lsm
, 0, sizeof(lsm
));
782 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
783 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
784 sizeof(lsm
.session
.name
));
786 ret
= -LTTNG_ERR_INVALID
;
790 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
792 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
793 sizeof(lsm
.u
.reg
.path
));
795 ret
= -LTTNG_ERR_INVALID
;
799 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
805 * Start tracing for all traces of the session.
807 * Returns size of returned session payload data or a negative error code.
809 int lttng_start_tracing(const char *session_name
)
812 struct lttcomm_session_msg lsm
;
814 if (session_name
== NULL
) {
815 ret
= -LTTNG_ERR_INVALID
;
819 memset(&lsm
, 0, sizeof(lsm
));
820 lsm
.cmd_type
= LTTNG_START_TRACE
;
822 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
823 sizeof(lsm
.session
.name
));
825 ret
= -LTTNG_ERR_INVALID
;
829 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
835 * Stop tracing for all traces of the session.
837 static int _lttng_stop_tracing(const char *session_name
, int wait
)
840 struct lttcomm_session_msg lsm
;
842 if (session_name
== NULL
) {
843 ret
= -LTTNG_ERR_INVALID
;
847 memset(&lsm
, 0, sizeof(lsm
));
848 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
850 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
851 sizeof(lsm
.session
.name
));
853 ret
= -LTTNG_ERR_INVALID
;
857 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
858 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
866 /* Check for data availability */
868 data_ret
= lttng_data_pending(session_name
);
870 /* Return the data available call error. */
876 * Data sleep time before retrying (in usec). Don't sleep if the
877 * call returned value indicates availability.
880 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
882 } while (data_ret
!= 0);
890 * Stop tracing and wait for data availability.
892 int lttng_stop_tracing(const char *session_name
)
894 return _lttng_stop_tracing(session_name
, 1);
898 * Stop tracing but _don't_ wait for data availability.
900 int lttng_stop_tracing_no_wait(const char *session_name
)
902 return _lttng_stop_tracing(session_name
, 0);
906 * Add context to a channel.
908 * If the given channel is NULL, add the contexts to all channels.
909 * The event_name param is ignored.
911 * Returns the size of the returned payload data or a negative error code.
913 int lttng_add_context(struct lttng_handle
*handle
,
914 struct lttng_event_context
*ctx
, const char *event_name
,
915 const char *channel_name
)
918 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_ADD_CONTEXT
};
919 struct lttng_payload payload
;
921 lttng_payload_init(&payload
);
923 /* Safety check. Both are mandatory. */
924 if (handle
== NULL
|| ctx
== NULL
) {
925 ret
= -LTTNG_ERR_INVALID
;
929 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
931 ret
= -LTTNG_ERR_NOMEM
;
935 /* If no channel name, send empty string. */
936 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
937 sizeof(lsm
.u
.context
.channel_name
));
939 ret
= -LTTNG_ERR_INVALID
;
943 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
944 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
945 sizeof(lsm
.session
.name
));
947 ret
= -LTTNG_ERR_INVALID
;
951 ret
= lttng_event_context_serialize(ctx
, &payload
);
953 ret
= -LTTNG_ERR_INVALID
;
957 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
959 /* Update message header. */
960 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
963 struct lttng_payload reply
;
964 struct lttng_payload_view payload_view
=
965 lttng_payload_view_from_payload(&payload
, 0,
968 lttng_payload_init(&reply
);
969 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
970 lttng_payload_reset(&reply
);
977 lttng_payload_reset(&payload
);
982 * Enable event(s) for a channel.
984 * If no event name is specified, all events are enabled.
985 * If no channel name is specified, the default 'channel0' is used.
987 * Returns size of returned session payload data or a negative error code.
989 int lttng_enable_event(struct lttng_handle
*handle
,
990 struct lttng_event
*ev
, const char *channel_name
)
992 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
997 * Create or enable an event with a filter expression.
999 * Return negative error value on error.
1000 * Return size of returned session payload data if OK.
1002 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1003 struct lttng_event
*event
, const char *channel_name
,
1004 const char *filter_expression
)
1006 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1007 filter_expression
, 0, NULL
);
1011 * Depending on the event, return a newly allocated agent filter expression or
1012 * NULL if not applicable.
1014 * An event with NO loglevel and the name is * will return NULL.
1016 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1019 char *agent_filter
= NULL
;
1023 /* Don't add filter for the '*' event. */
1024 if (strcmp(ev
->name
, "*") != 0) {
1026 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1029 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1037 /* Add loglevel filtering if any for the JUL domain. */
1038 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1041 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1047 if (filter
|| agent_filter
) {
1050 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1051 agent_filter
? agent_filter
: filter
, op
,
1056 agent_filter
= new_filter
;
1058 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1067 return agent_filter
;
1074 * Enable event(s) for a channel, possibly with exclusions and a filter.
1075 * If no event name is specified, all events are enabled.
1076 * If no channel name is specified, the default name is used.
1077 * If filter expression is not NULL, the filter is set for the event.
1078 * If exclusion count is not zero, the exclusions are set for the event.
1079 * Returns size of returned session payload data or a negative error code.
1081 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1082 struct lttng_event
*ev
, const char *channel_name
,
1083 const char *original_filter_expression
,
1084 int exclusion_count
, char **exclusion_list
)
1086 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_ENABLE_EVENT
};
1087 struct lttng_payload payload
;
1089 unsigned int free_filter_expression
= 0;
1090 struct filter_parser_ctx
*ctx
= NULL
;
1091 size_t bytecode_len
= 0;
1094 * We have either a filter or some exclusions, so we need to set up
1095 * a variable-length payload from where to send the data.
1097 lttng_payload_init(&payload
);
1100 * Cast as non-const since we may replace the filter expression
1101 * by a dynamically allocated string. Otherwise, the original
1102 * string is not modified.
1104 char *filter_expression
= (char *) original_filter_expression
;
1106 if (handle
== NULL
|| ev
== NULL
) {
1107 ret
= -LTTNG_ERR_INVALID
;
1112 * Empty filter string will always be rejected by the parser
1113 * anyway, so treat this corner-case early to eliminate
1114 * lttng_fmemopen error for 0-byte allocation.
1116 if (filter_expression
&& filter_expression
[0] == '\0') {
1117 ret
= -LTTNG_ERR_INVALID
;
1121 if (ev
->name
[0] == '\0') {
1122 /* Enable all events. */
1123 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1124 LTTNG_ASSERT(ret
== 0);
1127 /* Parse filter expression. */
1128 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1129 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1130 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1131 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1132 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1133 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1136 /* Setup agent filter if needed. */
1137 agent_filter
= set_agent_filter(filter_expression
, ev
);
1138 if (!agent_filter
) {
1139 if (!filter_expression
) {
1141 * No JUL and no filter, just skip
1148 * With an agent filter, the original filter has
1149 * been added to it thus replace the filter
1152 filter_expression
= agent_filter
;
1153 free_filter_expression
= 1;
1157 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1158 LTTNG_FILTER_MAX_LEN
) {
1159 ret
= -LTTNG_ERR_FILTER_INVAL
;
1163 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1168 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1169 sizeof(ctx
->bytecode
->b
);
1170 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1171 ret
= -LTTNG_ERR_FILTER_INVAL
;
1177 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1178 filter_expression
, bytecode_len
,
1179 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1182 ret
= -LTTNG_ERR_INVALID
;
1186 /* If no channel name, send empty string. */
1187 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1188 sizeof(lsm
.u
.enable
.channel_name
));
1190 ret
= -LTTNG_ERR_INVALID
;
1195 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1198 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1199 sizeof(lsm
.session
.name
));
1201 ret
= -LTTNG_ERR_INVALID
;
1205 /* Length of the serialized event. */
1206 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1209 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1211 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1218 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1219 if (fd_count
== 1) {
1220 struct fd_handle
*h
=
1221 lttng_payload_view_pop_fd_handle(&view
);
1227 fd_to_send
= fd_handle_get_fd(h
);
1231 lsm
.fd_count
= fd_count
;
1233 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1234 fd_count
? &fd_to_send
: NULL
, fd_count
,
1235 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1236 view
.buffer
.size
, NULL
, NULL
, 0);
1240 if (filter_expression
&& ctx
) {
1241 filter_bytecode_free(ctx
);
1242 filter_ir_free(ctx
);
1243 filter_parser_ctx_free(ctx
);
1245 if (free_filter_expression
) {
1247 * The filter expression has been replaced and must be freed as
1248 * it is not the original filter expression received as a
1251 free(filter_expression
);
1254 * Return directly to the caller and don't ask the sessiond since
1255 * something went wrong in the parsing of data above.
1257 lttng_payload_reset(&payload
);
1261 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1262 struct lttng_event
*ev
, const char *channel_name
,
1263 const char *original_filter_expression
)
1265 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_DISABLE_EVENT
};
1266 struct lttng_payload payload
;
1268 unsigned int free_filter_expression
= 0;
1269 struct filter_parser_ctx
*ctx
= NULL
;
1270 size_t bytecode_len
= 0;
1273 * We have either a filter or some exclusions, so we need to set up
1274 * a variable-length payload from where to send the data.
1276 lttng_payload_init(&payload
);
1279 * Cast as non-const since we may replace the filter expression
1280 * by a dynamically allocated string. Otherwise, the original
1281 * string is not modified.
1283 char *filter_expression
= (char *) original_filter_expression
;
1285 if (handle
== NULL
|| ev
== NULL
) {
1286 ret
= -LTTNG_ERR_INVALID
;
1291 * Empty filter string will always be rejected by the parser
1292 * anyway, so treat this corner-case early to eliminate
1293 * lttng_fmemopen error for 0-byte allocation.
1295 if (filter_expression
&& filter_expression
[0] == '\0') {
1296 ret
= -LTTNG_ERR_INVALID
;
1300 /* Parse filter expression. */
1301 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1302 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1303 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1304 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1305 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1306 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1309 /* Setup agent filter if needed. */
1310 agent_filter
= set_agent_filter(filter_expression
, ev
);
1311 if (!agent_filter
) {
1312 if (!filter_expression
) {
1314 * No JUL and no filter, just skip
1321 * With an agent filter, the original filter has
1322 * been added to it thus replace the filter
1325 filter_expression
= agent_filter
;
1326 free_filter_expression
= 1;
1330 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1331 LTTNG_FILTER_MAX_LEN
) {
1332 ret
= -LTTNG_ERR_FILTER_INVAL
;
1336 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1341 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1342 sizeof(ctx
->bytecode
->b
);
1343 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1344 ret
= -LTTNG_ERR_FILTER_INVAL
;
1350 ret
= lttng_event_serialize(ev
, 0, NULL
,
1351 filter_expression
, bytecode_len
,
1352 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1355 ret
= -LTTNG_ERR_INVALID
;
1359 /* If no channel name, send empty string. */
1360 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1361 sizeof(lsm
.u
.disable
.channel_name
));
1363 ret
= -LTTNG_ERR_INVALID
;
1368 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1371 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1372 sizeof(lsm
.session
.name
));
1374 ret
= -LTTNG_ERR_INVALID
;
1378 /* Length of the serialized event. */
1379 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1382 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1384 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1391 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1392 if (fd_count
== 1) {
1393 struct fd_handle
*h
=
1394 lttng_payload_view_pop_fd_handle(&view
);
1400 fd_to_send
= fd_handle_get_fd(h
);
1404 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1405 fd_count
? &fd_to_send
: NULL
, fd_count
,
1406 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1407 view
.buffer
.size
, NULL
, NULL
, 0);
1411 if (filter_expression
&& ctx
) {
1412 filter_bytecode_free(ctx
);
1413 filter_ir_free(ctx
);
1414 filter_parser_ctx_free(ctx
);
1416 if (free_filter_expression
) {
1418 * The filter expression has been replaced and must be freed as
1419 * it is not the original filter expression received as a
1422 free(filter_expression
);
1425 * Return directly to the caller and don't ask the sessiond since
1426 * something went wrong in the parsing of data above.
1428 lttng_payload_reset(&payload
);
1433 * Disable event(s) of a channel and domain.
1434 * If no event name is specified, all events are disabled.
1435 * If no channel name is specified, the default 'channel0' is used.
1436 * Returns size of returned session payload data or a negative error code.
1438 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1439 const char *channel_name
)
1442 struct lttng_event ev
;
1444 memset(&ev
, 0, sizeof(ev
));
1446 ev
.type
= LTTNG_EVENT_ALL
;
1447 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1449 ret
= -LTTNG_ERR_INVALID
;
1453 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1458 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1460 struct lttng_channel
*channel
= NULL
;
1466 /* Validate domain. */
1467 switch (domain
->type
) {
1468 case LTTNG_DOMAIN_UST
:
1469 switch (domain
->buf_type
) {
1470 case LTTNG_BUFFER_PER_UID
:
1471 case LTTNG_BUFFER_PER_PID
:
1477 case LTTNG_DOMAIN_KERNEL
:
1478 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1486 channel
= lttng_channel_create_internal();
1491 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1496 void lttng_channel_destroy(struct lttng_channel
*channel
)
1502 if (channel
->attr
.extended
.ptr
) {
1503 free(channel
->attr
.extended
.ptr
);
1509 * Enable channel per domain
1510 * Returns size of returned session payload data or a negative error code.
1512 int lttng_enable_channel(struct lttng_handle
*handle
,
1513 struct lttng_channel
*in_chan
)
1515 enum lttng_error_code ret_code
;
1517 struct lttng_dynamic_buffer buffer
;
1518 struct lttcomm_session_msg lsm
;
1519 uint64_t total_buffer_size_needed_per_cpu
= 0;
1520 struct lttng_channel
*channel
= NULL
;
1522 lttng_dynamic_buffer_init(&buffer
);
1524 /* NULL arguments are forbidden. No default values. */
1525 if (handle
== NULL
|| in_chan
== NULL
) {
1526 ret
= -LTTNG_ERR_INVALID
;
1531 * Verify that the amount of memory required to create the requested
1532 * buffer is available on the system at the moment.
1534 if (in_chan
->attr
.num_subbuf
>
1535 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1537 ret
= -LTTNG_ERR_OVERFLOW
;
1541 total_buffer_size_needed_per_cpu
=
1542 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1543 ret_code
= check_enough_available_memory(
1544 total_buffer_size_needed_per_cpu
);
1545 if (ret_code
!= LTTNG_OK
) {
1550 /* Copy the channel for easier manipulation. */
1551 channel
= lttng_channel_copy(in_chan
);
1553 ret
= -LTTNG_ERR_NOMEM
;
1557 /* Populate the channel extended attribute if necessary. */
1558 if (!channel
->attr
.extended
.ptr
) {
1559 struct lttng_channel_extended
*extended
=
1560 (struct lttng_channel_extended
*) zmalloc(
1564 ret
= -LTTNG_ERR_NOMEM
;
1567 lttng_channel_set_default_extended_attr(
1568 &handle
->domain
, extended
);
1569 channel
->attr
.extended
.ptr
= extended
;
1572 /* Prepare the payload */
1573 memset(&lsm
, 0, sizeof(lsm
));
1575 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1576 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1578 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1579 sizeof(lsm
.session
.name
));
1581 ret
= -LTTNG_ERR_INVALID
;
1585 ret
= lttng_channel_serialize(channel
, &buffer
);
1587 ret
= -LTTNG_ERR_FATAL
;
1591 lsm
.u
.channel
.length
= buffer
.size
;
1593 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1594 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1596 lttng_channel_destroy(channel
);
1597 lttng_dynamic_buffer_reset(&buffer
);
1602 * All tracing will be stopped for registered events of the channel.
1603 * Returns size of returned session payload data or a negative error code.
1605 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1608 struct lttcomm_session_msg lsm
;
1610 /* Safety check. Both are mandatory. */
1611 if (handle
== NULL
|| name
== NULL
) {
1612 return -LTTNG_ERR_INVALID
;
1615 memset(&lsm
, 0, sizeof(lsm
));
1617 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1619 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1620 sizeof(lsm
.u
.disable
.channel_name
));
1622 ret
= -LTTNG_ERR_INVALID
;
1626 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1628 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1629 sizeof(lsm
.session
.name
));
1631 ret
= -LTTNG_ERR_INVALID
;
1635 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1641 * Lists all available tracepoints of domain.
1642 * Sets the contents of the events array.
1643 * Returns the number of lttng_event entries in events;
1644 * on error, returns a negative value.
1646 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1647 struct lttng_event
**events
)
1649 enum lttng_error_code ret_code
;
1650 int ret
, total_payload_received
;
1651 char *reception_buffer
= NULL
;
1652 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRACEPOINTS
};
1653 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1654 size_t cmd_header_len
;
1655 unsigned int nb_events
= 0;
1657 if (handle
== NULL
) {
1658 ret
= -LTTNG_ERR_INVALID
;
1662 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1664 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1665 (void **) &reception_buffer
, (void **) &cmd_header
,
1671 total_payload_received
= ret
;
1674 ret
= -LTTNG_ERR_UNK
;
1678 if (cmd_header
->count
> INT_MAX
) {
1679 ret
= -LTTNG_ERR_OVERFLOW
;
1683 nb_events
= (unsigned int) cmd_header
->count
;
1686 struct lttng_buffer_view events_view
=
1687 lttng_buffer_view_init(reception_buffer
, 0,
1688 total_payload_received
);
1689 struct lttng_payload_view events_payload_view
=
1690 lttng_payload_view_from_buffer_view(
1691 &events_view
, 0, -1);
1693 ret_code
= lttng_events_create_and_flatten_from_payload(
1694 &events_payload_view
, nb_events
, events
);
1695 if (ret_code
!= LTTNG_OK
) {
1701 ret
= (int) nb_events
;
1705 free(reception_buffer
);
1710 * Lists all available tracepoint fields of domain.
1711 * Sets the contents of the event field array.
1712 * Returns the number of lttng_event_field entries in events;
1713 * on error, returns a negative value.
1715 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1716 struct lttng_event_field
**fields
)
1719 struct lttcomm_session_msg lsm
;
1721 if (handle
== NULL
) {
1722 return -LTTNG_ERR_INVALID
;
1725 memset(&lsm
, 0, sizeof(lsm
));
1726 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1727 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1729 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1734 return ret
/ sizeof(struct lttng_event_field
);
1738 * Lists all available kernel system calls. Allocates and sets the contents of
1741 * Returns the number of lttng_event entries in events; on error, returns a
1744 int lttng_list_syscalls(struct lttng_event
**events
)
1746 enum lttng_error_code ret_code
;
1747 int ret
, total_payload_received
;
1748 char *reception_buffer
= NULL
;
1749 struct lttcomm_session_msg lsm
= {};
1750 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1751 size_t cmd_header_len
;
1752 uint32_t nb_events
= 0;
1755 ret
= -LTTNG_ERR_INVALID
;
1759 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1760 /* Force kernel domain for system calls. */
1761 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1763 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1764 (void **) &reception_buffer
, (void **) &cmd_header
,
1769 total_payload_received
= ret
;
1772 ret
= -LTTNG_ERR_UNK
;
1776 if (cmd_header
->count
> INT_MAX
) {
1777 ret
= -LTTNG_ERR_OVERFLOW
;
1781 nb_events
= (unsigned int) cmd_header
->count
;
1784 const struct lttng_buffer_view events_view
=
1785 lttng_buffer_view_init(reception_buffer
, 0,
1786 total_payload_received
);
1787 struct lttng_payload_view events_payload_view
=
1788 lttng_payload_view_from_buffer_view(
1789 &events_view
, 0, -1);
1791 ret_code
= lttng_events_create_and_flatten_from_payload(
1792 &events_payload_view
, nb_events
, events
);
1793 if (ret_code
!= LTTNG_OK
) {
1799 ret
= (int) nb_events
;
1802 free(reception_buffer
);
1808 * Returns a human readable string describing
1809 * the error code (positive or negative value).
1811 const char *lttng_strerror(int code
)
1817 return error_get_str(code
);
1820 enum lttng_error_code
lttng_create_session_ext(
1821 struct lttng_session_descriptor
*session_descriptor
)
1823 enum lttng_error_code ret_code
;
1824 struct lttcomm_session_msg lsm
= {
1825 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1828 struct lttng_buffer_view reply_view
;
1830 bool sessiond_must_generate_ouput
;
1831 struct lttng_dynamic_buffer payload
;
1833 size_t descriptor_size
;
1834 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1836 lttng_dynamic_buffer_init(&payload
);
1837 if (!session_descriptor
) {
1838 ret_code
= LTTNG_ERR_INVALID
;
1842 sessiond_must_generate_ouput
=
1843 !lttng_session_descriptor_is_output_destination_initialized(
1844 session_descriptor
);
1845 if (sessiond_must_generate_ouput
) {
1846 const char *home_dir
= utils_get_home_dir();
1847 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1849 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1850 ret_code
= LTTNG_ERR_FATAL
;
1854 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1855 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1858 ret_code
= LTTNG_ERR_NOMEM
;
1863 descriptor_size
= payload
.size
;
1864 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1867 ret_code
= LTTNG_ERR_INVALID
;
1870 descriptor_size
= payload
.size
- descriptor_size
;
1871 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1873 /* Command returns a session descriptor on success. */
1874 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1875 payload
.size
, &reply
);
1876 if (reply_ret
< 0) {
1877 ret_code
= (lttng_error_code
) -reply_ret
;
1879 } else if (reply_ret
== 0) {
1880 /* Socket unexpectedly closed by the session daemon. */
1881 ret_code
= LTTNG_ERR_FATAL
;
1885 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1886 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1889 ret_code
= LTTNG_ERR_FATAL
;
1892 ret_code
= LTTNG_OK
;
1893 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1896 lttng_dynamic_buffer_reset(&payload
);
1897 lttng_session_descriptor_destroy(descriptor_reply
);
1902 * Create a new session using name and url for destination.
1904 * Return 0 on success else a negative LTTng error code.
1906 int lttng_create_session(const char *name
, const char *url
)
1910 struct lttng_uri
*uris
= NULL
;
1911 struct lttng_session_descriptor
*descriptor
= NULL
;
1912 enum lttng_error_code ret_code
;
1915 ret
= -LTTNG_ERR_INVALID
;
1919 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1921 ret
= -LTTNG_ERR_INVALID
;
1926 descriptor
= lttng_session_descriptor_create(name
);
1929 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1930 ret
= -LTTNG_ERR_INVALID
;
1933 descriptor
= lttng_session_descriptor_local_create(name
,
1937 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1941 ret
= -LTTNG_ERR_INVALID
;
1945 ret
= -LTTNG_ERR_INVALID
;
1948 ret_code
= lttng_create_session_ext(descriptor
);
1949 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1951 lttng_session_descriptor_destroy(descriptor
);
1957 * Create a session exclusively used for snapshot.
1959 * Return 0 on success else a negative LTTng error code.
1961 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1964 enum lttng_error_code ret_code
;
1966 struct lttng_uri
*uris
= NULL
;
1967 struct lttng_session_descriptor
*descriptor
= NULL
;
1970 ret
= -LTTNG_ERR_INVALID
;
1974 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1976 ret
= -LTTNG_ERR_INVALID
;
1980 * If the user does not specify a custom subdir, use the session name.
1982 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1983 strlen(uris
[0].subdir
) == 0) {
1984 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1987 PERROR("Failed to set session name as network destination sub-directory");
1988 ret
= -LTTNG_ERR_FATAL
;
1990 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1991 /* Truncated output. */
1992 ret
= -LTTNG_ERR_INVALID
;
1999 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2002 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2003 ret
= -LTTNG_ERR_INVALID
;
2006 descriptor
= lttng_session_descriptor_snapshot_local_create(
2011 descriptor
= lttng_session_descriptor_snapshot_network_create(
2017 ret
= -LTTNG_ERR_INVALID
;
2021 ret
= -LTTNG_ERR_INVALID
;
2024 ret_code
= lttng_create_session_ext(descriptor
);
2025 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2027 lttng_session_descriptor_destroy(descriptor
);
2033 * Create a session exclusively used for live.
2035 * Return 0 on success else a negative LTTng error code.
2037 int lttng_create_session_live(const char *name
, const char *url
,
2038 unsigned int timer_interval
)
2041 enum lttng_error_code ret_code
;
2042 struct lttng_session_descriptor
*descriptor
= NULL
;
2045 ret
= -LTTNG_ERR_INVALID
;
2050 descriptor
= lttng_session_descriptor_live_network_create(
2051 name
, url
, NULL
, timer_interval
);
2053 descriptor
= lttng_session_descriptor_live_create(
2054 name
, timer_interval
);
2057 ret
= -LTTNG_ERR_INVALID
;
2060 ret_code
= lttng_create_session_ext(descriptor
);
2061 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2063 lttng_session_descriptor_destroy(descriptor
);
2068 * Stop the session and wait for the data before destroying it
2070 * Return 0 on success else a negative LTTng error code.
2072 int lttng_destroy_session(const char *session_name
)
2075 enum lttng_error_code ret_code
;
2076 enum lttng_destruction_handle_status status
;
2077 struct lttng_destruction_handle
*handle
= NULL
;
2080 * Stop the tracing and wait for the data to be
2083 ret
= _lttng_stop_tracing(session_name
, 1);
2084 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2088 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2089 if (ret_code
!= LTTNG_OK
) {
2090 ret
= (int) -ret_code
;
2093 LTTNG_ASSERT(handle
);
2095 /* Block until the completion of the destruction of the session. */
2096 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2097 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2098 ret
= -LTTNG_ERR_UNK
;
2102 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2103 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2104 ret
= -LTTNG_ERR_UNK
;
2107 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2109 lttng_destruction_handle_destroy(handle
);
2114 * Destroy the session without waiting for the data.
2116 int lttng_destroy_session_no_wait(const char *session_name
)
2118 enum lttng_error_code ret_code
;
2120 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2121 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2125 * Ask the session daemon for all available sessions.
2126 * Sets the contents of the sessions array.
2127 * Returns the number of lttng_session entries in sessions;
2128 * on error, returns a negative value.
2130 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2133 struct lttcomm_session_msg lsm
;
2134 const size_t session_size
= sizeof(struct lttng_session
) +
2135 sizeof(struct lttng_session_extended
);
2136 size_t session_count
, i
;
2137 struct lttng_session_extended
*sessions_extended_begin
;
2138 struct lttng_session
*sessions
= NULL
;
2140 memset(&lsm
, 0, sizeof(lsm
));
2141 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2143 * Initialize out_sessions to NULL so it is initialized when
2144 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2145 * be subsequently freed.
2147 *out_sessions
= NULL
;
2148 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2153 ret
= -LTTNG_ERR_FATAL
;
2157 if (ret
% session_size
) {
2158 ret
= -LTTNG_ERR_UNK
;
2162 session_count
= (size_t) ret
/ session_size
;
2163 sessions_extended_begin
= (struct lttng_session_extended
*)
2164 (&sessions
[session_count
]);
2166 /* Set extended session info pointers. */
2167 for (i
= 0; i
< session_count
; i
++) {
2168 struct lttng_session
*session
= &sessions
[i
];
2169 struct lttng_session_extended
*extended
=
2170 &(sessions_extended_begin
[i
]);
2172 session
->extended
.ptr
= extended
;
2175 ret
= (int) session_count
;
2176 *out_sessions
= sessions
;
2181 enum lttng_error_code
lttng_session_get_creation_time(
2182 const struct lttng_session
*session
, uint64_t *creation_time
)
2184 enum lttng_error_code ret
= LTTNG_OK
;
2185 struct lttng_session_extended
*extended
;
2187 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2188 ret
= LTTNG_ERR_INVALID
;
2192 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2193 if (!extended
->creation_time
.is_set
) {
2194 /* Not created on the session daemon yet. */
2195 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2198 *creation_time
= extended
->creation_time
.value
;
2203 int lttng_set_session_shm_path(const char *session_name
,
2204 const char *shm_path
)
2207 struct lttcomm_session_msg lsm
;
2209 if (session_name
== NULL
) {
2210 return -LTTNG_ERR_INVALID
;
2213 memset(&lsm
, 0, sizeof(lsm
));
2214 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2216 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2217 sizeof(lsm
.session
.name
));
2219 ret
= -LTTNG_ERR_INVALID
;
2223 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2224 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2226 ret
= -LTTNG_ERR_INVALID
;
2230 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2236 * Ask the session daemon for all available domains of a session.
2237 * Sets the contents of the domains array.
2238 * Returns the number of lttng_domain entries in domains;
2239 * on error, returns a negative value.
2241 int lttng_list_domains(const char *session_name
,
2242 struct lttng_domain
**domains
)
2245 struct lttcomm_session_msg lsm
;
2247 if (session_name
== NULL
) {
2248 ret
= -LTTNG_ERR_INVALID
;
2252 memset(&lsm
, 0, sizeof(lsm
));
2253 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2255 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2256 sizeof(lsm
.session
.name
));
2258 ret
= -LTTNG_ERR_INVALID
;
2262 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2267 return ret
/ sizeof(struct lttng_domain
);
2273 * Ask the session daemon for all available channels of a session.
2274 * Sets the contents of the channels array.
2275 * Returns the number of lttng_channel entries in channels;
2276 * on error, returns a negative value.
2278 int lttng_list_channels(struct lttng_handle
*handle
,
2279 struct lttng_channel
**channels
)
2281 int ret
, total_payload_received
;
2282 struct lttcomm_session_msg lsm
;
2283 char *reception_buffer
= NULL
;
2284 size_t cmd_header_len
= 0;
2285 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2286 struct lttng_dynamic_buffer tmp_buffer
;
2288 lttng_dynamic_buffer_init(&tmp_buffer
);
2290 if (handle
== NULL
) {
2291 ret
= -LTTNG_ERR_INVALID
;
2295 memset(&lsm
, 0, sizeof(lsm
));
2296 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2297 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2298 sizeof(lsm
.session
.name
));
2300 ret
= -LTTNG_ERR_INVALID
;
2304 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2306 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2307 (void **) &reception_buffer
, (void **) &cmd_header
,
2313 total_payload_received
= ret
;
2315 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2316 ret
= -LTTNG_ERR_FATAL
;
2321 ret
= LTTNG_ERR_UNK
;
2325 if (cmd_header
->count
> INT_MAX
) {
2326 ret
= -LTTNG_ERR_OVERFLOW
;
2331 enum lttng_error_code ret_code
;
2332 const struct lttng_buffer_view events_view
=
2333 lttng_buffer_view_init(reception_buffer
, 0,
2334 total_payload_received
);
2336 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2337 &events_view
, cmd_header
->count
, channels
);
2338 if (ret_code
!= LTTNG_OK
) {
2344 ret
= (int) cmd_header
->count
;
2347 free(reception_buffer
);
2352 * Ask the session daemon for all available events of a session channel.
2353 * Sets the contents of the events array.
2354 * Returns the number of lttng_event entries in events;
2355 * on error, returns a negative value.
2357 int lttng_list_events(struct lttng_handle
*handle
,
2358 const char *channel_name
, struct lttng_event
**events
)
2361 struct lttcomm_session_msg lsm
= {};
2362 struct lttng_payload reply
;
2363 struct lttng_payload_view lsm_view
=
2364 lttng_payload_view_init_from_buffer(
2365 (const char *) &lsm
, 0, sizeof(lsm
));
2366 unsigned int nb_events
= 0;
2368 /* Safety check. An handle and channel name are mandatory. */
2369 if (handle
== NULL
|| channel_name
== NULL
) {
2370 ret
= -LTTNG_ERR_INVALID
;
2374 lttng_payload_init(&reply
);
2376 /* Initialize command parameters. */
2377 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2378 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2379 sizeof(lsm
.session
.name
));
2381 ret
= -LTTNG_ERR_INVALID
;
2385 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2386 sizeof(lsm
.u
.list
.channel_name
));
2388 ret
= -LTTNG_ERR_INVALID
;
2392 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2394 /* Execute command against the session daemon. */
2395 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2401 const struct lttcomm_list_command_header
*cmd_reply_header
=
2403 const lttng_payload_view cmd_reply_header_view
=
2404 lttng_payload_view_from_payload(&reply
, 0,
2405 sizeof(*cmd_reply_header
));
2407 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2408 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2412 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2413 cmd_reply_header_view
.buffer
2415 if (cmd_reply_header
->count
> INT_MAX
) {
2416 ret
= -LTTNG_ERR_OVERFLOW
;
2420 nb_events
= (unsigned int) cmd_reply_header
->count
;
2424 enum lttng_error_code ret_code
;
2425 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2427 sizeof(struct lttcomm_list_command_header
), -1);
2429 ret_code
= lttng_events_create_and_flatten_from_payload(
2430 &cmd_reply_payload
, nb_events
, events
);
2431 if (ret_code
!= LTTNG_OK
) {
2432 ret
= -((int) ret_code
);
2437 ret
= (int) nb_events
;
2439 lttng_payload_reset(&reply
);
2444 * Sets the tracing_group variable with name.
2445 * This function allocates memory pointed to by tracing_group.
2446 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2448 int lttng_set_tracing_group(const char *name
)
2454 ret
= -LTTNG_ERR_INVALID
;
2458 new_group
= strdup(name
);
2460 ret
= -LTTNG_ERR_FATAL
;
2464 free(tracing_group
);
2465 tracing_group
= new_group
;
2472 int lttng_calibrate(struct lttng_handle
*handle
,
2473 struct lttng_calibrate
*calibrate
)
2476 * This command was removed in LTTng 2.9.
2478 return -LTTNG_ERR_UND
;
2482 * Set default channel attributes.
2483 * If either or both of the arguments are null, attr content is zeroe'd.
2485 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2486 struct lttng_channel_attr
*attr
)
2488 struct lttng_channel_extended
*extended
;
2491 if (attr
== NULL
|| domain
== NULL
) {
2495 /* Save the pointer for later use */
2496 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2497 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2499 /* Same for all domains. */
2500 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2501 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2502 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2504 switch (domain
->type
) {
2505 case LTTNG_DOMAIN_KERNEL
:
2506 attr
->switch_timer_interval
=
2507 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2508 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2509 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2510 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2511 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2513 case LTTNG_DOMAIN_UST
:
2514 switch (domain
->buf_type
) {
2515 case LTTNG_BUFFER_PER_UID
:
2516 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2517 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2518 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2519 attr
->switch_timer_interval
=
2520 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2521 attr
->read_timer_interval
=
2522 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2524 case LTTNG_BUFFER_PER_PID
:
2526 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2527 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2528 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2529 attr
->switch_timer_interval
=
2530 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2531 attr
->read_timer_interval
=
2532 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2536 /* Default behavior: leave set to 0. */
2541 lttng_channel_set_default_extended_attr(domain
, extended
);
2544 /* Reassign the extended pointer. */
2545 attr
->extended
.ptr
= extended
;
2548 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2549 uint64_t *discarded_events
)
2552 struct lttng_channel_extended
*chan_ext
;
2554 if (!channel
|| !discarded_events
) {
2555 ret
= -LTTNG_ERR_INVALID
;
2559 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2562 * This can happen since the lttng_channel structure is
2563 * used for other tasks where this pointer is never set.
2565 *discarded_events
= 0;
2569 *discarded_events
= chan_ext
->discarded_events
;
2574 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2575 uint64_t *lost_packets
)
2578 struct lttng_channel_extended
*chan_ext
;
2580 if (!channel
|| !lost_packets
) {
2581 ret
= -LTTNG_ERR_INVALID
;
2585 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2588 * This can happen since the lttng_channel structure is
2589 * used for other tasks where this pointer is never set.
2595 *lost_packets
= chan_ext
->lost_packets
;
2600 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2601 uint64_t *monitor_timer_interval
)
2605 if (!chan
|| !monitor_timer_interval
) {
2606 ret
= -LTTNG_ERR_INVALID
;
2610 if (!chan
->attr
.extended
.ptr
) {
2611 ret
= -LTTNG_ERR_INVALID
;
2615 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2616 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2621 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2622 uint64_t monitor_timer_interval
)
2626 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2627 ret
= -LTTNG_ERR_INVALID
;
2631 ((struct lttng_channel_extended
*)
2632 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2633 monitor_timer_interval
;
2638 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2639 int64_t *blocking_timeout
)
2643 if (!chan
|| !blocking_timeout
) {
2644 ret
= -LTTNG_ERR_INVALID
;
2648 if (!chan
->attr
.extended
.ptr
) {
2649 ret
= -LTTNG_ERR_INVALID
;
2653 *blocking_timeout
= ((struct lttng_channel_extended
*)
2654 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2659 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2660 int64_t blocking_timeout
)
2663 int64_t msec_timeout
;
2665 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2666 ret
= -LTTNG_ERR_INVALID
;
2670 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2671 ret
= -LTTNG_ERR_INVALID
;
2676 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2677 * us to accept a narrower range of values (msecs expressed as a signed
2680 msec_timeout
= blocking_timeout
/ 1000;
2681 if (msec_timeout
!= (int32_t) msec_timeout
) {
2682 ret
= -LTTNG_ERR_INVALID
;
2686 ((struct lttng_channel_extended
*)
2687 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2694 * Check if session daemon is alive.
2696 * Return 1 if alive or 0 if not.
2697 * On error returns a negative value.
2699 int lttng_session_daemon_alive(void)
2703 ret
= set_session_daemon_path();
2709 if (*sessiond_sock_path
== '\0') {
2711 * No socket path set. Weird error which means the constructor
2717 ret
= try_connect_sessiond(sessiond_sock_path
);
2728 * Set URL for a consumer for a session and domain.
2730 * Return 0 on success, else a negative value.
2732 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2733 const char *control_url
, const char *data_url
)
2737 struct lttcomm_session_msg lsm
;
2738 struct lttng_uri
*uris
= NULL
;
2740 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2741 ret
= -LTTNG_ERR_INVALID
;
2745 memset(&lsm
, 0, sizeof(lsm
));
2747 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2749 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2750 sizeof(lsm
.session
.name
));
2752 ret
= -LTTNG_ERR_INVALID
;
2756 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2758 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2760 ret
= -LTTNG_ERR_INVALID
;
2764 lsm
.u
.uri
.size
= size
;
2766 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2767 sizeof(struct lttng_uri
) * size
, NULL
);
2778 LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2779 int lttng_enable_consumer(struct lttng_handle
*handle
)
2788 LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2789 int lttng_disable_consumer(struct lttng_handle
*handle
)
2798 LTTNG_EXPORT
int _lttng_create_session_ext(const char *name
, const char *url
,
2799 const char *datetime
);
2800 int _lttng_create_session_ext(const char *name
, const char *url
,
2801 const char *datetime
)
2807 * For a given session name, this call checks if the data is ready to be read
2808 * or is still being extracted by the consumer(s) hence not ready to be used by
2811 int lttng_data_pending(const char *session_name
)
2814 struct lttcomm_session_msg lsm
;
2815 uint8_t *pending
= NULL
;
2817 if (session_name
== NULL
) {
2818 return -LTTNG_ERR_INVALID
;
2821 memset(&lsm
, 0, sizeof(lsm
));
2822 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2824 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2825 sizeof(lsm
.session
.name
));
2827 ret
= -LTTNG_ERR_INVALID
;
2831 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2834 } else if (ret
!= 1) {
2835 /* Unexpected payload size */
2836 ret
= -LTTNG_ERR_INVALID
;
2838 } else if (!pending
) {
2839 /* Internal error. */
2840 ret
= -LTTNG_ERR_UNK
;
2844 ret
= (int) *pending
;
2851 * Regenerate the metadata for a session.
2852 * Return 0 on success, a negative error code on error.
2854 int lttng_regenerate_metadata(const char *session_name
)
2857 struct lttcomm_session_msg lsm
;
2859 if (!session_name
) {
2860 ret
= -LTTNG_ERR_INVALID
;
2864 memset(&lsm
, 0, sizeof(lsm
));
2865 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2867 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2868 sizeof(lsm
.session
.name
));
2870 ret
= -LTTNG_ERR_INVALID
;
2874 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2885 * Deprecated, replaced by lttng_regenerate_metadata.
2887 int lttng_metadata_regenerate(const char *session_name
)
2889 return lttng_regenerate_metadata(session_name
);
2893 * Regenerate the statedump of a session.
2894 * Return 0 on success, a negative error code on error.
2896 int lttng_regenerate_statedump(const char *session_name
)
2899 struct lttcomm_session_msg lsm
;
2901 if (!session_name
) {
2902 ret
= -LTTNG_ERR_INVALID
;
2906 memset(&lsm
, 0, sizeof(lsm
));
2907 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2909 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2910 sizeof(lsm
.session
.name
));
2912 ret
= -LTTNG_ERR_INVALID
;
2916 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2927 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
2931 struct lttcomm_session_msg lsm
= {
2932 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2934 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
2935 struct lttcomm_session_msg
*message_lsm
;
2936 struct lttng_payload message
;
2937 struct lttng_payload reply
;
2938 struct lttng_trigger
*reply_trigger
= NULL
;
2939 enum lttng_domain_type domain_type
;
2940 const struct lttng_credentials user_creds
= {
2941 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2942 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2944 const char *unused_trigger_name
= NULL
;
2945 enum lttng_trigger_status trigger_status
;
2947 lttng_payload_init(&message
);
2948 lttng_payload_init(&reply
);
2951 ret
= -LTTNG_ERR_INVALID
;
2955 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
2956 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
2957 /* Re-using already registered trigger. */
2958 ret
= -LTTNG_ERR_INVALID
;
2963 trigger_status
= lttng_trigger_set_name(trigger
, name
);
2964 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2965 ret
= -LTTNG_ERR_NOMEM
;
2970 if (!trigger
->creds
.uid
.is_set
) {
2971 /* Use the client's credentials as the trigger credentials. */
2972 lttng_trigger_set_credentials(trigger
, &user_creds
);
2975 * Validate that either the current trigger credentials and the
2976 * client credentials are identical or that the current user is
2977 * root. The root user can register, unregister triggers for
2978 * himself and other users.
2980 * This check is also present on the sessiond side, using the
2981 * credentials passed on the socket. These check are all
2984 const struct lttng_credentials
*trigger_creds
=
2985 lttng_trigger_get_credentials(trigger
);
2987 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
2988 if (lttng_credentials_get_uid(&user_creds
) != 0) {
2989 ret
= -LTTNG_ERR_EPERM
;
2990 goto end_unset_name
;
2995 if (!lttng_trigger_validate(trigger
)) {
2996 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2997 goto end_unset_name
;
3000 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3003 lsm
.domain
.type
= domain_type
;
3005 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3007 ret
= -LTTNG_ERR_NOMEM
;
3008 goto end_unset_name
;
3011 ret
= lttng_trigger_serialize(trigger
, &message
);
3013 ret
= -LTTNG_ERR_UNK
;
3014 goto end_unset_name
;
3018 * This is needed to populate the trigger object size for the command
3021 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3023 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3026 struct lttng_payload_view message_view
=
3027 lttng_payload_view_from_payload(
3030 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3032 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3034 goto end_unset_name
;
3039 struct lttng_payload_view reply_view
=
3040 lttng_payload_view_from_payload(
3041 &reply
, 0, reply
.buffer
.size
);
3043 ret
= lttng_trigger_create_from_payload(
3044 &reply_view
, &reply_trigger
);
3046 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3047 goto end_unset_name
;
3051 if (name
|| generate_name
) {
3052 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3054 ret
= -LTTNG_ERR_NOMEM
;
3063 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3064 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3065 ret
= -LTTNG_ERR_UNK
;
3068 lttng_payload_reset(&message
);
3069 lttng_payload_reset(&reply
);
3070 lttng_trigger_destroy(reply_trigger
);
3074 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3076 /* Register an anonymous trigger. */
3077 return _lttng_register_trigger(trigger
, NULL
, false);
3080 enum lttng_error_code
lttng_register_trigger_with_name(
3081 struct lttng_trigger
*trigger
, const char *name
)
3083 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3085 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3088 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3089 struct lttng_trigger
*trigger
)
3091 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3093 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3096 enum lttng_error_code
lttng_error_query_execute(
3097 const struct lttng_error_query
*query
,
3098 const struct lttng_endpoint
*endpoint
,
3099 struct lttng_error_query_results
**results
)
3102 enum lttng_error_code ret_code
;
3103 struct lttcomm_session_msg lsm
= {
3104 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3106 struct lttng_payload message
;
3107 struct lttng_payload reply
;
3108 struct lttcomm_session_msg
*message_lsm
;
3110 lttng_payload_init(&message
);
3111 lttng_payload_init(&reply
);
3113 if (!query
|| !results
) {
3114 ret_code
= LTTNG_ERR_INVALID
;
3118 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3119 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3123 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3125 ret_code
= LTTNG_ERR_NOMEM
;
3129 ret
= lttng_error_query_serialize(query
, &message
);
3131 ret_code
= LTTNG_ERR_UNK
;
3135 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3136 message_lsm
->u
.error_query
.length
=
3137 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3140 struct lttng_payload_view message_view
=
3141 lttng_payload_view_from_payload(
3144 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3146 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3148 ret_code
=(lttng_error_code
) -ret
;
3154 ssize_t reply_create_ret
;
3155 struct lttng_payload_view reply_view
=
3156 lttng_payload_view_from_payload(
3157 &reply
, 0, reply
.buffer
.size
);
3159 reply_create_ret
= lttng_error_query_results_create_from_payload(
3160 &reply_view
, results
);
3161 if (reply_create_ret
< 0) {
3162 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3167 ret_code
= LTTNG_OK
;
3169 lttng_payload_reset(&message
);
3170 lttng_payload_reset(&reply
);
3174 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3177 struct lttcomm_session_msg lsm
;
3178 struct lttcomm_session_msg
*message_lsm
;
3179 struct lttng_payload message
;
3180 struct lttng_payload reply
;
3181 struct lttng_trigger
*copy
= NULL
;
3182 const struct lttng_credentials user_creds
= {
3183 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3184 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3187 lttng_payload_init(&message
);
3188 lttng_payload_init(&reply
);
3191 ret
= -LTTNG_ERR_INVALID
;
3195 copy
= lttng_trigger_copy(trigger
);
3197 ret
= -LTTNG_ERR_UNK
;
3201 if (!copy
->creds
.uid
.is_set
) {
3202 /* Use the client credentials as the trigger credentials */
3203 lttng_trigger_set_credentials(copy
, &user_creds
);
3206 * Validate that either the current trigger credentials and the
3207 * client credentials are identical or that the current user is
3208 * root. The root user can register, unregister triggers for
3209 * himself and other users.
3211 * This check is also present on the sessiond side, using the
3212 * credentials passed on the socket. These check are all
3215 const struct lttng_credentials
*trigger_creds
=
3216 lttng_trigger_get_credentials(copy
);
3217 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3218 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3219 ret
= -LTTNG_ERR_EPERM
;
3225 if (!lttng_trigger_validate(copy
)) {
3226 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3230 memset(&lsm
, 0, sizeof(lsm
));
3231 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3233 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3235 ret
= -LTTNG_ERR_NOMEM
;
3239 ret
= lttng_trigger_serialize(copy
, &message
);
3241 ret
= -LTTNG_ERR_UNK
;
3246 * This is needed to populate the trigger object size for the command
3247 * header and number of fds sent.
3249 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3251 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3254 struct lttng_payload_view message_view
=
3255 lttng_payload_view_from_payload(
3259 * Update the message header with the number of fd that will be
3262 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3265 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3273 lttng_trigger_destroy(copy
);
3274 lttng_payload_reset(&message
);
3275 lttng_payload_reset(&reply
);
3280 * Ask the session daemon for all registered triggers for the current user.
3282 * Allocates and return an lttng_triggers set.
3283 * On error, returns a suitable lttng_error_code.
3285 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3288 enum lttng_error_code ret_code
= LTTNG_OK
;
3289 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3290 struct lttng_triggers
*local_triggers
= NULL
;
3291 struct lttng_payload reply
;
3292 struct lttng_payload_view lsm_view
=
3293 lttng_payload_view_init_from_buffer(
3294 (const char *) &lsm
, 0, sizeof(lsm
));
3296 lttng_payload_init(&reply
);
3298 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3300 ret_code
= (enum lttng_error_code
) -ret
;
3305 struct lttng_payload_view reply_view
=
3306 lttng_payload_view_from_payload(
3307 &reply
, 0, reply
.buffer
.size
);
3309 ret
= lttng_triggers_create_from_payload(
3310 &reply_view
, &local_triggers
);
3312 ret_code
= LTTNG_ERR_FATAL
;
3317 *triggers
= local_triggers
;
3318 local_triggers
= NULL
;
3320 lttng_payload_reset(&reply
);
3321 lttng_triggers_destroy(local_triggers
);
3328 static void __attribute__((constructor
)) init(void)
3330 /* Set default session group */
3331 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3337 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3339 free(tracing_group
);