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.hpp>
22 #include <common/align.hpp>
23 #include <common/common.hpp>
24 #include <common/compat/errno.hpp>
25 #include <common/compat/string.hpp>
26 #include <common/defaults.hpp>
27 #include <common/dynamic-array.hpp>
28 #include <common/dynamic-buffer.hpp>
29 #include <common/payload-view.hpp>
30 #include <common/payload.hpp>
31 #include <common/sessiond-comm/sessiond-comm.hpp>
32 #include <common/tracker.hpp>
33 #include <common/unix.hpp>
34 #include <common/uri.hpp>
35 #include <common/utils.hpp>
36 #include <lttng/channel-internal.hpp>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.hpp>
40 #include <lttng/event-internal.hpp>
41 #include <lttng/health-internal.hpp>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.hpp>
45 #include <lttng/session-internal.hpp>
46 #include <lttng/trigger/trigger-internal.hpp>
47 #include <lttng/userspace-probe-internal.hpp>
49 #include "lttng-ctl-helper.hpp"
50 #include <common/filter/filter-ast.hpp>
51 #include <common/filter/filter-parser.hpp>
52 #include <common/filter/memstream.hpp>
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
= calloc
<gid_t
>(grp_list_size
);
274 grp_id
= getgroups(grp_list_size
, grp_list
);
280 for (i
= 0; i
< grp_list_size
; i
++) {
281 if (grp_list
[i
] == tracing_gid
) {
294 static enum lttng_error_code
check_enough_available_memory(
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
;
511 buf
= zmalloc
<char>(len
);
517 ret
= recv_data_sessiond(buf
, len
);
523 ret
= -LTTNG_ERR_INVALID
;
527 /* Move ownership of command header buffer to user. */
532 /* No command header. */
548 * Ask the session daemon a specific command and put the data into buf.
549 * Takes extra var. len. data and file descriptors as input to send to the
552 * Return size of data (only payload, not header) or a negative error code.
554 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
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
= zmalloc
<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
,
915 const char *event_name
__attribute__((unused
)),
916 const char *channel_name
)
919 struct lttcomm_session_msg lsm
= {
920 .cmd_type
= LTTNG_ADD_CONTEXT
,
926 struct lttng_payload payload
;
928 lttng_payload_init(&payload
);
930 /* Safety check. Both are mandatory. */
931 if (handle
== NULL
|| ctx
== NULL
) {
932 ret
= -LTTNG_ERR_INVALID
;
936 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
, sizeof(lsm
));
938 ret
= -LTTNG_ERR_NOMEM
;
942 /* If no channel name, send empty string. */
943 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
944 sizeof(lsm
.u
.context
.channel_name
));
946 ret
= -LTTNG_ERR_INVALID
;
950 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
951 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
952 sizeof(lsm
.session
.name
));
954 ret
= -LTTNG_ERR_INVALID
;
958 ret
= lttng_event_context_serialize(ctx
, &payload
);
960 ret
= -LTTNG_ERR_INVALID
;
964 lsm
.u
.context
.length
= payload
.buffer
.size
- sizeof(lsm
);
966 /* Update message header. */
967 memcpy(payload
.buffer
.data
, &lsm
, sizeof(lsm
));
970 struct lttng_payload reply
;
971 struct lttng_payload_view payload_view
=
972 lttng_payload_view_from_payload(&payload
, 0,
975 lttng_payload_init(&reply
);
976 ret
= lttng_ctl_ask_sessiond_payload(&payload_view
, &reply
);
977 lttng_payload_reset(&reply
);
984 lttng_payload_reset(&payload
);
989 * Enable event(s) for a channel.
991 * If no event name is specified, all events are enabled.
992 * If no channel name is specified, the default 'channel0' is used.
994 * Returns size of returned session payload data or a negative error code.
996 int lttng_enable_event(struct lttng_handle
*handle
,
997 struct lttng_event
*ev
, const char *channel_name
)
999 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1004 * Create or enable an event with a filter expression.
1006 * Return negative error value on error.
1007 * Return size of returned session payload data if OK.
1009 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1010 struct lttng_event
*event
, const char *channel_name
,
1011 const char *filter_expression
)
1013 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1014 filter_expression
, 0, NULL
);
1018 * Depending on the event, return a newly allocated agent filter expression or
1019 * NULL if not applicable.
1021 * An event with NO loglevel and the name is * will return NULL.
1023 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1026 char *agent_filter
= NULL
;
1030 /* Don't add filter for the '*' event. */
1031 if (strcmp(ev
->name
, "*") != 0) {
1033 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1036 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1044 /* Add loglevel filtering if any for the JUL domain. */
1045 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1048 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1054 if (filter
|| agent_filter
) {
1057 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1058 agent_filter
? agent_filter
: filter
, op
,
1063 agent_filter
= new_filter
;
1065 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1074 return agent_filter
;
1081 * Enable event(s) for a channel, possibly with exclusions and a filter.
1082 * If no event name is specified, all events are enabled.
1083 * If no channel name is specified, the default name is used.
1084 * If filter expression is not NULL, the filter is set for the event.
1085 * If exclusion count is not zero, the exclusions are set for the event.
1086 * Returns size of returned session payload data or a negative error code.
1088 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1089 struct lttng_event
*ev
, const char *channel_name
,
1090 const char *original_filter_expression
,
1091 int exclusion_count
, char **exclusion_list
)
1093 struct lttcomm_session_msg lsm
= {
1094 .cmd_type
= LTTNG_ENABLE_EVENT
,
1100 struct lttng_payload payload
;
1102 unsigned int free_filter_expression
= 0;
1103 struct filter_parser_ctx
*ctx
= NULL
;
1104 size_t bytecode_len
= 0;
1107 * We have either a filter or some exclusions, so we need to set up
1108 * a variable-length payload from where to send the data.
1110 lttng_payload_init(&payload
);
1113 * Cast as non-const since we may replace the filter expression
1114 * by a dynamically allocated string. Otherwise, the original
1115 * string is not modified.
1117 char *filter_expression
= (char *) original_filter_expression
;
1119 if (handle
== NULL
|| ev
== NULL
) {
1120 ret
= -LTTNG_ERR_INVALID
;
1125 * Empty filter string will always be rejected by the parser
1126 * anyway, so treat this corner-case early to eliminate
1127 * lttng_fmemopen error for 0-byte allocation.
1129 if (filter_expression
&& filter_expression
[0] == '\0') {
1130 ret
= -LTTNG_ERR_INVALID
;
1134 if (ev
->name
[0] == '\0') {
1135 /* Enable all events. */
1136 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1137 LTTNG_ASSERT(ret
== 0);
1140 /* Parse filter expression. */
1141 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1142 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1143 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1144 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1145 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1146 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1149 /* Setup agent filter if needed. */
1150 agent_filter
= set_agent_filter(filter_expression
, ev
);
1151 if (!agent_filter
) {
1152 if (!filter_expression
) {
1154 * No JUL and no filter, just skip
1161 * With an agent filter, the original filter has
1162 * been added to it thus replace the filter
1165 filter_expression
= agent_filter
;
1166 free_filter_expression
= 1;
1170 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1171 LTTNG_FILTER_MAX_LEN
) {
1172 ret
= -LTTNG_ERR_FILTER_INVAL
;
1176 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1181 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1182 sizeof(ctx
->bytecode
->b
);
1183 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1184 ret
= -LTTNG_ERR_FILTER_INVAL
;
1190 ret
= lttng_event_serialize(ev
, exclusion_count
, exclusion_list
,
1191 filter_expression
, bytecode_len
,
1192 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1195 ret
= -LTTNG_ERR_INVALID
;
1199 /* If no channel name, send empty string. */
1200 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1201 sizeof(lsm
.u
.enable
.channel_name
));
1203 ret
= -LTTNG_ERR_INVALID
;
1208 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1211 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1212 sizeof(lsm
.session
.name
));
1214 ret
= -LTTNG_ERR_INVALID
;
1218 /* Length of the serialized event. */
1219 lsm
.u
.enable
.length
= (uint32_t) payload
.buffer
.size
;
1222 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1224 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1231 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1232 if (fd_count
== 1) {
1233 struct fd_handle
*h
=
1234 lttng_payload_view_pop_fd_handle(&view
);
1240 fd_to_send
= fd_handle_get_fd(h
);
1244 lsm
.fd_count
= fd_count
;
1246 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1247 fd_count
? &fd_to_send
: NULL
, fd_count
,
1248 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1249 view
.buffer
.size
, NULL
, NULL
, 0);
1253 if (filter_expression
&& ctx
) {
1254 filter_bytecode_free(ctx
);
1255 filter_ir_free(ctx
);
1256 filter_parser_ctx_free(ctx
);
1258 if (free_filter_expression
) {
1260 * The filter expression has been replaced and must be freed as
1261 * it is not the original filter expression received as a
1264 free(filter_expression
);
1267 * Return directly to the caller and don't ask the sessiond since
1268 * something went wrong in the parsing of data above.
1270 lttng_payload_reset(&payload
);
1274 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1275 struct lttng_event
*ev
, const char *channel_name
,
1276 const char *original_filter_expression
)
1278 struct lttcomm_session_msg lsm
= {
1279 .cmd_type
= LTTNG_DISABLE_EVENT
,
1285 struct lttng_payload payload
;
1287 unsigned int free_filter_expression
= 0;
1288 struct filter_parser_ctx
*ctx
= NULL
;
1289 size_t bytecode_len
= 0;
1292 * We have either a filter or some exclusions, so we need to set up
1293 * a variable-length payload from where to send the data.
1295 lttng_payload_init(&payload
);
1298 * Cast as non-const since we may replace the filter expression
1299 * by a dynamically allocated string. Otherwise, the original
1300 * string is not modified.
1302 char *filter_expression
= (char *) original_filter_expression
;
1304 if (handle
== NULL
|| ev
== NULL
) {
1305 ret
= -LTTNG_ERR_INVALID
;
1310 * Empty filter string will always be rejected by the parser
1311 * anyway, so treat this corner-case early to eliminate
1312 * lttng_fmemopen error for 0-byte allocation.
1314 if (filter_expression
&& filter_expression
[0] == '\0') {
1315 ret
= -LTTNG_ERR_INVALID
;
1319 /* Parse filter expression. */
1320 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1321 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1322 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1323 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1324 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1325 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1328 /* Setup agent filter if needed. */
1329 agent_filter
= set_agent_filter(filter_expression
, ev
);
1330 if (!agent_filter
) {
1331 if (!filter_expression
) {
1333 * No JUL and no filter, just skip
1340 * With an agent filter, the original filter has
1341 * been added to it thus replace the filter
1344 filter_expression
= agent_filter
;
1345 free_filter_expression
= 1;
1349 if (strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
) ==
1350 LTTNG_FILTER_MAX_LEN
) {
1351 ret
= -LTTNG_ERR_FILTER_INVAL
;
1355 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1360 bytecode_len
= bytecode_get_len(&ctx
->bytecode
->b
) +
1361 sizeof(ctx
->bytecode
->b
);
1362 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1363 ret
= -LTTNG_ERR_FILTER_INVAL
;
1369 ret
= lttng_event_serialize(ev
, 0, NULL
,
1370 filter_expression
, bytecode_len
,
1371 (ctx
&& bytecode_len
) ? &ctx
->bytecode
->b
: NULL
,
1374 ret
= -LTTNG_ERR_INVALID
;
1378 /* If no channel name, send empty string. */
1379 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1380 sizeof(lsm
.u
.disable
.channel_name
));
1382 ret
= -LTTNG_ERR_INVALID
;
1387 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1390 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1391 sizeof(lsm
.session
.name
));
1393 ret
= -LTTNG_ERR_INVALID
;
1397 /* Length of the serialized event. */
1398 lsm
.u
.disable
.length
= (uint32_t) payload
.buffer
.size
;
1401 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1403 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1410 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1411 if (fd_count
== 1) {
1412 struct fd_handle
*h
=
1413 lttng_payload_view_pop_fd_handle(&view
);
1419 fd_to_send
= fd_handle_get_fd(h
);
1423 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1424 fd_count
? &fd_to_send
: NULL
, fd_count
,
1425 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1426 view
.buffer
.size
, NULL
, NULL
, 0);
1430 if (filter_expression
&& ctx
) {
1431 filter_bytecode_free(ctx
);
1432 filter_ir_free(ctx
);
1433 filter_parser_ctx_free(ctx
);
1435 if (free_filter_expression
) {
1437 * The filter expression has been replaced and must be freed as
1438 * it is not the original filter expression received as a
1441 free(filter_expression
);
1444 * Return directly to the caller and don't ask the sessiond since
1445 * something went wrong in the parsing of data above.
1447 lttng_payload_reset(&payload
);
1452 * Disable event(s) of a channel and domain.
1453 * If no event name is specified, all events are disabled.
1454 * If no channel name is specified, the default 'channel0' is used.
1455 * Returns size of returned session payload data or a negative error code.
1457 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1458 const char *channel_name
)
1461 struct lttng_event ev
;
1463 memset(&ev
, 0, sizeof(ev
));
1465 ev
.type
= LTTNG_EVENT_ALL
;
1466 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1468 ret
= -LTTNG_ERR_INVALID
;
1472 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1477 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1479 struct lttng_channel
*channel
= NULL
;
1485 /* Validate domain. */
1486 switch (domain
->type
) {
1487 case LTTNG_DOMAIN_UST
:
1488 switch (domain
->buf_type
) {
1489 case LTTNG_BUFFER_PER_UID
:
1490 case LTTNG_BUFFER_PER_PID
:
1496 case LTTNG_DOMAIN_KERNEL
:
1497 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1505 channel
= lttng_channel_create_internal();
1510 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1515 void lttng_channel_destroy(struct lttng_channel
*channel
)
1521 if (channel
->attr
.extended
.ptr
) {
1522 free(channel
->attr
.extended
.ptr
);
1528 * Enable channel per domain
1529 * Returns size of returned session payload data or a negative error code.
1531 int lttng_enable_channel(struct lttng_handle
*handle
,
1532 struct lttng_channel
*in_chan
)
1534 enum lttng_error_code ret_code
;
1536 struct lttng_dynamic_buffer buffer
;
1537 struct lttcomm_session_msg lsm
;
1538 uint64_t total_buffer_size_needed_per_cpu
= 0;
1539 struct lttng_channel
*channel
= NULL
;
1541 lttng_dynamic_buffer_init(&buffer
);
1543 /* NULL arguments are forbidden. No default values. */
1544 if (handle
== NULL
|| in_chan
== NULL
) {
1545 ret
= -LTTNG_ERR_INVALID
;
1550 * Verify that the amount of memory required to create the requested
1551 * buffer is available on the system at the moment.
1553 if (in_chan
->attr
.num_subbuf
>
1554 UINT64_MAX
/ in_chan
->attr
.subbuf_size
) {
1556 ret
= -LTTNG_ERR_OVERFLOW
;
1560 total_buffer_size_needed_per_cpu
=
1561 in_chan
->attr
.num_subbuf
* in_chan
->attr
.subbuf_size
;
1562 ret_code
= check_enough_available_memory(
1563 total_buffer_size_needed_per_cpu
);
1564 if (ret_code
!= LTTNG_OK
) {
1569 /* Copy the channel for easier manipulation. */
1570 channel
= lttng_channel_copy(in_chan
);
1572 ret
= -LTTNG_ERR_NOMEM
;
1576 /* Populate the channel extended attribute if necessary. */
1577 if (!channel
->attr
.extended
.ptr
) {
1578 struct lttng_channel_extended
*extended
=
1579 zmalloc
<lttng_channel_extended
>();
1582 ret
= -LTTNG_ERR_NOMEM
;
1586 lttng_channel_set_default_extended_attr(
1587 &handle
->domain
, extended
);
1588 channel
->attr
.extended
.ptr
= extended
;
1591 /* Prepare the payload */
1592 memset(&lsm
, 0, sizeof(lsm
));
1594 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1595 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1597 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1598 sizeof(lsm
.session
.name
));
1600 ret
= -LTTNG_ERR_INVALID
;
1604 ret
= lttng_channel_serialize(channel
, &buffer
);
1606 ret
= -LTTNG_ERR_FATAL
;
1610 lsm
.u
.channel
.length
= buffer
.size
;
1612 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1613 &lsm
, buffer
.data
, buffer
.size
, NULL
);
1615 lttng_channel_destroy(channel
);
1616 lttng_dynamic_buffer_reset(&buffer
);
1621 * All tracing will be stopped for registered events of the channel.
1622 * Returns size of returned session payload data or a negative error code.
1624 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1627 struct lttcomm_session_msg lsm
;
1629 /* Safety check. Both are mandatory. */
1630 if (handle
== NULL
|| name
== NULL
) {
1631 return -LTTNG_ERR_INVALID
;
1634 memset(&lsm
, 0, sizeof(lsm
));
1636 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1638 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1639 sizeof(lsm
.u
.disable
.channel_name
));
1641 ret
= -LTTNG_ERR_INVALID
;
1645 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1647 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1648 sizeof(lsm
.session
.name
));
1650 ret
= -LTTNG_ERR_INVALID
;
1654 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1660 * Lists all available tracepoints of domain.
1661 * Sets the contents of the events array.
1662 * Returns the number of lttng_event entries in events;
1663 * on error, returns a negative value.
1665 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1666 struct lttng_event
**events
)
1668 enum lttng_error_code ret_code
;
1669 int ret
, total_payload_received
;
1670 char *reception_buffer
= NULL
;
1671 struct lttcomm_session_msg lsm
= {
1672 .cmd_type
= LTTNG_LIST_TRACEPOINTS
,
1678 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1679 size_t cmd_header_len
;
1680 unsigned int nb_events
= 0;
1682 if (handle
== NULL
) {
1683 ret
= -LTTNG_ERR_INVALID
;
1687 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1689 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1690 (void **) &reception_buffer
, (void **) &cmd_header
,
1696 total_payload_received
= ret
;
1699 ret
= -LTTNG_ERR_UNK
;
1703 if (cmd_header
->count
> INT_MAX
) {
1704 ret
= -LTTNG_ERR_OVERFLOW
;
1708 nb_events
= (unsigned int) cmd_header
->count
;
1711 struct lttng_buffer_view events_view
=
1712 lttng_buffer_view_init(reception_buffer
, 0,
1713 total_payload_received
);
1714 struct lttng_payload_view events_payload_view
=
1715 lttng_payload_view_from_buffer_view(
1716 &events_view
, 0, -1);
1718 ret_code
= lttng_events_create_and_flatten_from_payload(
1719 &events_payload_view
, nb_events
, events
);
1720 if (ret_code
!= LTTNG_OK
) {
1726 ret
= (int) nb_events
;
1730 free(reception_buffer
);
1735 * Lists all available tracepoint fields of domain.
1736 * Sets the contents of the event field array.
1737 * Returns the number of lttng_event_field entries in events;
1738 * on error, returns a negative value.
1740 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1741 struct lttng_event_field
**fields
)
1743 enum lttng_error_code ret_code
;
1745 struct lttcomm_session_msg lsm
;
1746 const struct lttcomm_list_command_header
*cmd_header
= NULL
;
1747 unsigned int nb_event_fields
= 0;
1748 struct lttng_payload reply
;
1750 lttng_payload_init(&reply
);
1752 if (handle
== NULL
) {
1753 ret
= -LTTNG_ERR_INVALID
;
1757 memset(&lsm
, 0, sizeof(lsm
));
1758 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1759 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1762 lttng_payload_view message_view
=
1763 lttng_payload_view_init_from_buffer(
1764 (const char *) &lsm
, 0,
1767 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
1774 const lttng_buffer_view cmd_header_view
=
1775 lttng_buffer_view_from_dynamic_buffer(
1776 &reply
.buffer
, 0, sizeof(*cmd_header
));
1778 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
1779 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
1783 cmd_header
= (struct lttcomm_list_command_header
*)
1784 cmd_header_view
.data
;
1787 if (cmd_header
->count
> INT_MAX
) {
1788 ret
= -LTTNG_ERR_OVERFLOW
;
1792 nb_event_fields
= cmd_header
->count
;
1795 lttng_payload_view reply_view
=
1796 lttng_payload_view_from_payload(&reply
,
1797 sizeof(*cmd_header
), -1);
1799 ret_code
= lttng_event_fields_create_and_flatten_from_payload(
1800 &reply_view
, nb_event_fields
, fields
);
1801 if (ret_code
!= LTTNG_OK
) {
1807 ret
= nb_event_fields
;
1810 lttng_payload_reset(&reply
);
1815 * Lists all available kernel system calls. Allocates and sets the contents of
1818 * Returns the number of lttng_event entries in events; on error, returns a
1821 int lttng_list_syscalls(struct lttng_event
**events
)
1823 enum lttng_error_code ret_code
;
1824 int ret
, total_payload_received
;
1825 char *reception_buffer
= NULL
;
1826 struct lttcomm_session_msg lsm
= {};
1827 struct lttcomm_list_command_header
*cmd_header
= NULL
;
1828 size_t cmd_header_len
;
1829 uint32_t nb_events
= 0;
1832 ret
= -LTTNG_ERR_INVALID
;
1836 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1837 /* Force kernel domain for system calls. */
1838 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1840 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
1841 (void **) &reception_buffer
, (void **) &cmd_header
,
1846 total_payload_received
= ret
;
1849 ret
= -LTTNG_ERR_UNK
;
1853 if (cmd_header
->count
> INT_MAX
) {
1854 ret
= -LTTNG_ERR_OVERFLOW
;
1858 nb_events
= (unsigned int) cmd_header
->count
;
1861 const struct lttng_buffer_view events_view
=
1862 lttng_buffer_view_init(reception_buffer
, 0,
1863 total_payload_received
);
1864 struct lttng_payload_view events_payload_view
=
1865 lttng_payload_view_from_buffer_view(
1866 &events_view
, 0, -1);
1868 ret_code
= lttng_events_create_and_flatten_from_payload(
1869 &events_payload_view
, nb_events
, events
);
1870 if (ret_code
!= LTTNG_OK
) {
1876 ret
= (int) nb_events
;
1879 free(reception_buffer
);
1885 * Returns a human readable string describing
1886 * the error code (positive or negative value).
1888 const char *lttng_strerror(int code
)
1894 return error_get_str(code
);
1897 enum lttng_error_code
lttng_create_session_ext(
1898 struct lttng_session_descriptor
*session_descriptor
)
1900 enum lttng_error_code ret_code
;
1901 struct lttcomm_session_msg lsm
= {
1902 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1909 struct lttng_buffer_view reply_view
;
1911 bool sessiond_must_generate_ouput
;
1912 struct lttng_payload payload
;
1914 size_t descriptor_size
;
1915 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1917 lttng_payload_init(&payload
);
1918 if (!session_descriptor
) {
1919 ret_code
= LTTNG_ERR_INVALID
;
1923 sessiond_must_generate_ouput
=
1924 !lttng_session_descriptor_is_output_destination_initialized(
1925 session_descriptor
);
1926 if (sessiond_must_generate_ouput
) {
1927 const char *home_dir
= utils_get_home_dir();
1928 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1930 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1931 ret_code
= LTTNG_ERR_FATAL
;
1935 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1936 ret
= lttng_dynamic_buffer_append(&payload
.buffer
, home_dir
, home_dir_len
);
1938 ret_code
= LTTNG_ERR_NOMEM
;
1943 descriptor_size
= payload
.buffer
.size
;
1944 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1947 ret_code
= LTTNG_ERR_INVALID
;
1950 descriptor_size
= payload
.buffer
.size
- descriptor_size
;
1951 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1953 /* Command returns a session descriptor on success. */
1954 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1955 &lsm
, payload
.buffer
.data
, payload
.buffer
.size
, &reply
);
1956 if (reply_ret
< 0) {
1957 ret_code
= (lttng_error_code
) -reply_ret
;
1959 } else if (reply_ret
== 0) {
1960 /* Socket unexpectedly closed by the session daemon. */
1961 ret_code
= LTTNG_ERR_FATAL
;
1965 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1966 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1969 ret_code
= LTTNG_ERR_FATAL
;
1972 ret_code
= LTTNG_OK
;
1973 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1976 lttng_payload_reset(&payload
);
1977 lttng_session_descriptor_destroy(descriptor_reply
);
1982 * Create a new session using name and url for destination.
1984 * Return 0 on success else a negative LTTng error code.
1986 int lttng_create_session(const char *name
, const char *url
)
1990 struct lttng_uri
*uris
= NULL
;
1991 struct lttng_session_descriptor
*descriptor
= NULL
;
1992 enum lttng_error_code ret_code
;
1995 ret
= -LTTNG_ERR_INVALID
;
1999 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2001 ret
= -LTTNG_ERR_INVALID
;
2006 descriptor
= lttng_session_descriptor_create(name
);
2009 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2010 ret
= -LTTNG_ERR_INVALID
;
2013 descriptor
= lttng_session_descriptor_local_create(name
,
2017 descriptor
= lttng_session_descriptor_network_create(name
, url
,
2021 ret
= -LTTNG_ERR_INVALID
;
2025 ret
= -LTTNG_ERR_INVALID
;
2028 ret_code
= lttng_create_session_ext(descriptor
);
2029 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2031 lttng_session_descriptor_destroy(descriptor
);
2037 * Create a session exclusively used for snapshot.
2039 * Return 0 on success else a negative LTTng error code.
2041 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2044 enum lttng_error_code ret_code
;
2046 struct lttng_uri
*uris
= NULL
;
2047 struct lttng_session_descriptor
*descriptor
= NULL
;
2050 ret
= -LTTNG_ERR_INVALID
;
2054 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2056 ret
= -LTTNG_ERR_INVALID
;
2060 * If the user does not specify a custom subdir, use the session name.
2062 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
2063 strlen(uris
[0].subdir
) == 0) {
2064 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2067 PERROR("Failed to set session name as network destination sub-directory");
2068 ret
= -LTTNG_ERR_FATAL
;
2070 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2071 /* Truncated output. */
2072 ret
= -LTTNG_ERR_INVALID
;
2079 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2082 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2083 ret
= -LTTNG_ERR_INVALID
;
2086 descriptor
= lttng_session_descriptor_snapshot_local_create(
2091 descriptor
= lttng_session_descriptor_snapshot_network_create(
2097 ret
= -LTTNG_ERR_INVALID
;
2101 ret
= -LTTNG_ERR_INVALID
;
2104 ret_code
= lttng_create_session_ext(descriptor
);
2105 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2107 lttng_session_descriptor_destroy(descriptor
);
2113 * Create a session exclusively used for live.
2115 * Return 0 on success else a negative LTTng error code.
2117 int lttng_create_session_live(const char *name
, const char *url
,
2118 unsigned int timer_interval
)
2121 enum lttng_error_code ret_code
;
2122 struct lttng_session_descriptor
*descriptor
= NULL
;
2125 ret
= -LTTNG_ERR_INVALID
;
2130 descriptor
= lttng_session_descriptor_live_network_create(
2131 name
, url
, NULL
, timer_interval
);
2133 descriptor
= lttng_session_descriptor_live_create(
2134 name
, timer_interval
);
2137 ret
= -LTTNG_ERR_INVALID
;
2140 ret_code
= lttng_create_session_ext(descriptor
);
2141 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2143 lttng_session_descriptor_destroy(descriptor
);
2148 * Stop the session and wait for the data before destroying it
2150 * Return 0 on success else a negative LTTng error code.
2152 int lttng_destroy_session(const char *session_name
)
2155 enum lttng_error_code ret_code
;
2156 enum lttng_destruction_handle_status status
;
2157 struct lttng_destruction_handle
*handle
= NULL
;
2160 * Stop the tracing and wait for the data to be
2163 ret
= _lttng_stop_tracing(session_name
, 1);
2164 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2168 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2169 if (ret_code
!= LTTNG_OK
) {
2170 ret
= (int) -ret_code
;
2173 LTTNG_ASSERT(handle
);
2175 /* Block until the completion of the destruction of the session. */
2176 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2177 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2178 ret
= -LTTNG_ERR_UNK
;
2182 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2183 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2184 ret
= -LTTNG_ERR_UNK
;
2187 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2189 lttng_destruction_handle_destroy(handle
);
2194 * Destroy the session without waiting for the data.
2196 int lttng_destroy_session_no_wait(const char *session_name
)
2198 enum lttng_error_code ret_code
;
2200 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2201 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2205 * Ask the session daemon for all available sessions.
2206 * Sets the contents of the sessions array.
2207 * Returns the number of lttng_session entries in sessions;
2208 * on error, returns a negative value.
2210 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2213 struct lttcomm_session_msg lsm
= {};
2214 struct lttng_payload reply
;
2215 struct lttng_payload_view lsm_view
=
2216 lttng_payload_view_init_from_buffer((const char *) &lsm
, 0, sizeof(lsm
));
2217 unsigned int nb_sessions
= 0;
2219 lttng_payload_init(&reply
);
2221 /* Initialize command parameters. */
2222 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2224 /* Execute command against the session daemon. */
2225 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2231 const struct lttcomm_list_command_header
*cmd_reply_header
= NULL
;
2232 const lttng_payload_view cmd_reply_header_view
= lttng_payload_view_from_payload(
2233 &reply
, 0, sizeof(*cmd_reply_header
));
2235 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2236 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2240 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2241 cmd_reply_header_view
.buffer
.data
;
2242 if (cmd_reply_header
->count
> INT_MAX
) {
2243 ret
= -LTTNG_ERR_OVERFLOW
;
2247 nb_sessions
= (unsigned int) cmd_reply_header
->count
;
2251 enum lttng_error_code ret_code
;
2252 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2253 &reply
, sizeof(struct lttcomm_list_command_header
), -1);
2255 ret_code
= lttng_sessions_create_and_flatten_from_payload(
2256 &cmd_reply_payload
, nb_sessions
, out_sessions
);
2257 if (ret_code
!= LTTNG_OK
) {
2258 ret
= -((int) ret_code
);
2263 ret
= (int) nb_sessions
;
2265 lttng_payload_reset(&reply
);
2269 enum lttng_error_code
lttng_session_get_creation_time(
2270 const struct lttng_session
*session
, uint64_t *creation_time
)
2272 enum lttng_error_code ret
= LTTNG_OK
;
2273 struct lttng_session_extended
*extended
;
2275 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2276 ret
= LTTNG_ERR_INVALID
;
2280 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2281 if (!extended
->creation_time
.is_set
) {
2282 /* Not created on the session daemon yet. */
2283 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2286 *creation_time
= extended
->creation_time
.value
;
2291 int lttng_set_session_shm_path(const char *session_name
,
2292 const char *shm_path
)
2295 struct lttcomm_session_msg lsm
;
2297 if (session_name
== NULL
) {
2298 return -LTTNG_ERR_INVALID
;
2301 memset(&lsm
, 0, sizeof(lsm
));
2302 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2304 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2305 sizeof(lsm
.session
.name
));
2307 ret
= -LTTNG_ERR_INVALID
;
2311 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2312 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2314 ret
= -LTTNG_ERR_INVALID
;
2318 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2324 * Ask the session daemon for all available domains of a session.
2325 * Sets the contents of the domains array.
2326 * Returns the number of lttng_domain entries in domains;
2327 * on error, returns a negative value.
2329 int lttng_list_domains(const char *session_name
,
2330 struct lttng_domain
**domains
)
2333 struct lttcomm_session_msg lsm
;
2335 if (session_name
== NULL
) {
2336 ret
= -LTTNG_ERR_INVALID
;
2340 memset(&lsm
, 0, sizeof(lsm
));
2341 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2343 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2344 sizeof(lsm
.session
.name
));
2346 ret
= -LTTNG_ERR_INVALID
;
2350 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2355 return ret
/ sizeof(struct lttng_domain
);
2361 * Ask the session daemon for all available channels of a session.
2362 * Sets the contents of the channels array.
2363 * Returns the number of lttng_channel entries in channels;
2364 * on error, returns a negative value.
2366 int lttng_list_channels(struct lttng_handle
*handle
,
2367 struct lttng_channel
**channels
)
2369 int ret
, total_payload_received
;
2370 struct lttcomm_session_msg lsm
;
2371 char *reception_buffer
= NULL
;
2372 size_t cmd_header_len
= 0;
2373 struct lttcomm_list_command_header
*cmd_header
= NULL
;
2374 struct lttng_dynamic_buffer tmp_buffer
;
2376 lttng_dynamic_buffer_init(&tmp_buffer
);
2378 if (handle
== NULL
) {
2379 ret
= -LTTNG_ERR_INVALID
;
2383 memset(&lsm
, 0, sizeof(lsm
));
2384 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2385 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2386 sizeof(lsm
.session
.name
));
2388 ret
= -LTTNG_ERR_INVALID
;
2392 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2394 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2395 (void **) &reception_buffer
, (void **) &cmd_header
,
2401 total_payload_received
= ret
;
2403 if (cmd_header_len
!= sizeof(*cmd_header
)) {
2404 ret
= -LTTNG_ERR_FATAL
;
2409 ret
= LTTNG_ERR_UNK
;
2413 if (cmd_header
->count
> INT_MAX
) {
2414 ret
= -LTTNG_ERR_OVERFLOW
;
2419 enum lttng_error_code ret_code
;
2420 const struct lttng_buffer_view events_view
=
2421 lttng_buffer_view_init(reception_buffer
, 0,
2422 total_payload_received
);
2424 ret_code
= lttng_channels_create_and_flatten_from_buffer(
2425 &events_view
, cmd_header
->count
, channels
);
2426 if (ret_code
!= LTTNG_OK
) {
2432 ret
= (int) cmd_header
->count
;
2435 free(reception_buffer
);
2440 * Ask the session daemon for all available events of a session channel.
2441 * Sets the contents of the events array.
2442 * Returns the number of lttng_event entries in events;
2443 * on error, returns a negative value.
2445 int lttng_list_events(struct lttng_handle
*handle
,
2446 const char *channel_name
, struct lttng_event
**events
)
2449 struct lttcomm_session_msg lsm
= {};
2450 struct lttng_payload reply
;
2451 struct lttng_payload_view lsm_view
=
2452 lttng_payload_view_init_from_buffer(
2453 (const char *) &lsm
, 0, sizeof(lsm
));
2454 unsigned int nb_events
= 0;
2456 lttng_payload_init(&reply
);
2458 /* Safety check. An handle and channel name are mandatory. */
2459 if (handle
== NULL
|| channel_name
== NULL
) {
2460 ret
= -LTTNG_ERR_INVALID
;
2464 /* Initialize command parameters. */
2465 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2466 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2467 sizeof(lsm
.session
.name
));
2469 ret
= -LTTNG_ERR_INVALID
;
2473 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2474 sizeof(lsm
.u
.list
.channel_name
));
2476 ret
= -LTTNG_ERR_INVALID
;
2480 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2482 /* Execute command against the session daemon. */
2483 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
2489 const struct lttcomm_list_command_header
*cmd_reply_header
=
2491 const lttng_payload_view cmd_reply_header_view
=
2492 lttng_payload_view_from_payload(&reply
, 0,
2493 sizeof(*cmd_reply_header
));
2495 if (!lttng_payload_view_is_valid(&cmd_reply_header_view
)) {
2496 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2500 cmd_reply_header
= (const struct lttcomm_list_command_header
*)
2501 cmd_reply_header_view
.buffer
2503 if (cmd_reply_header
->count
> INT_MAX
) {
2504 ret
= -LTTNG_ERR_OVERFLOW
;
2508 nb_events
= (unsigned int) cmd_reply_header
->count
;
2512 enum lttng_error_code ret_code
;
2513 lttng_payload_view cmd_reply_payload
= lttng_payload_view_from_payload(
2515 sizeof(struct lttcomm_list_command_header
), -1);
2517 ret_code
= lttng_events_create_and_flatten_from_payload(
2518 &cmd_reply_payload
, nb_events
, events
);
2519 if (ret_code
!= LTTNG_OK
) {
2520 ret
= -((int) ret_code
);
2525 ret
= (int) nb_events
;
2527 lttng_payload_reset(&reply
);
2532 * Sets the tracing_group variable with name.
2533 * This function allocates memory pointed to by tracing_group.
2534 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2536 int lttng_set_tracing_group(const char *name
)
2542 ret
= -LTTNG_ERR_INVALID
;
2546 new_group
= strdup(name
);
2548 ret
= -LTTNG_ERR_FATAL
;
2552 free(tracing_group
);
2553 tracing_group
= new_group
;
2560 int lttng_calibrate(struct lttng_handle
*handle
__attribute__((unused
)),
2561 struct lttng_calibrate
*calibrate
__attribute__((unused
)))
2564 * This command was removed in LTTng 2.9.
2566 return -LTTNG_ERR_UND
;
2570 * Set default channel attributes.
2571 * If either or both of the arguments are null, attr content is zeroe'd.
2573 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2574 struct lttng_channel_attr
*attr
)
2576 struct lttng_channel_extended
*extended
;
2579 if (attr
== NULL
|| domain
== NULL
) {
2583 /* Save the pointer for later use */
2584 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2585 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2587 /* Same for all domains. */
2588 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2589 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2590 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2592 switch (domain
->type
) {
2593 case LTTNG_DOMAIN_KERNEL
:
2594 attr
->switch_timer_interval
=
2595 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2596 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2597 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2598 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2599 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2601 case LTTNG_DOMAIN_UST
:
2602 switch (domain
->buf_type
) {
2603 case LTTNG_BUFFER_PER_UID
:
2604 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2605 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2606 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2607 attr
->switch_timer_interval
=
2608 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2609 attr
->read_timer_interval
=
2610 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2612 case LTTNG_BUFFER_PER_PID
:
2614 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2615 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2616 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2617 attr
->switch_timer_interval
=
2618 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2619 attr
->read_timer_interval
=
2620 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2624 /* Default behavior: leave set to 0. */
2629 lttng_channel_set_default_extended_attr(domain
, extended
);
2632 /* Reassign the extended pointer. */
2633 attr
->extended
.ptr
= extended
;
2636 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2637 uint64_t *discarded_events
)
2640 struct lttng_channel_extended
*chan_ext
;
2642 if (!channel
|| !discarded_events
) {
2643 ret
= -LTTNG_ERR_INVALID
;
2647 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2650 * This can happen since the lttng_channel structure is
2651 * used for other tasks where this pointer is never set.
2653 *discarded_events
= 0;
2657 *discarded_events
= chan_ext
->discarded_events
;
2662 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2663 uint64_t *lost_packets
)
2666 struct lttng_channel_extended
*chan_ext
;
2668 if (!channel
|| !lost_packets
) {
2669 ret
= -LTTNG_ERR_INVALID
;
2673 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2676 * This can happen since the lttng_channel structure is
2677 * used for other tasks where this pointer is never set.
2683 *lost_packets
= chan_ext
->lost_packets
;
2688 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2689 uint64_t *monitor_timer_interval
)
2693 if (!chan
|| !monitor_timer_interval
) {
2694 ret
= -LTTNG_ERR_INVALID
;
2698 if (!chan
->attr
.extended
.ptr
) {
2699 ret
= -LTTNG_ERR_INVALID
;
2703 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2704 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2709 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2710 uint64_t monitor_timer_interval
)
2714 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2715 ret
= -LTTNG_ERR_INVALID
;
2719 ((struct lttng_channel_extended
*)
2720 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2721 monitor_timer_interval
;
2726 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2727 int64_t *blocking_timeout
)
2731 if (!chan
|| !blocking_timeout
) {
2732 ret
= -LTTNG_ERR_INVALID
;
2736 if (!chan
->attr
.extended
.ptr
) {
2737 ret
= -LTTNG_ERR_INVALID
;
2741 *blocking_timeout
= ((struct lttng_channel_extended
*)
2742 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2747 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2748 int64_t blocking_timeout
)
2751 int64_t msec_timeout
;
2753 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2754 ret
= -LTTNG_ERR_INVALID
;
2758 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2759 ret
= -LTTNG_ERR_INVALID
;
2764 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2765 * us to accept a narrower range of values (msecs expressed as a signed
2768 msec_timeout
= blocking_timeout
/ 1000;
2769 if (msec_timeout
!= (int32_t) msec_timeout
) {
2770 ret
= -LTTNG_ERR_INVALID
;
2774 ((struct lttng_channel_extended
*)
2775 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2782 * Check if session daemon is alive.
2784 * Return 1 if alive or 0 if not.
2785 * On error returns a negative value.
2787 int lttng_session_daemon_alive(void)
2791 ret
= set_session_daemon_path();
2797 if (*sessiond_sock_path
== '\0') {
2799 * No socket path set. Weird error which means the constructor
2805 ret
= try_connect_sessiond(sessiond_sock_path
);
2816 * Set URL for a consumer for a session and domain.
2818 * Return 0 on success, else a negative value.
2820 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2821 const char *control_url
, const char *data_url
)
2825 struct lttcomm_session_msg lsm
;
2826 struct lttng_uri
*uris
= NULL
;
2828 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2829 ret
= -LTTNG_ERR_INVALID
;
2833 memset(&lsm
, 0, sizeof(lsm
));
2835 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2837 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2838 sizeof(lsm
.session
.name
));
2840 ret
= -LTTNG_ERR_INVALID
;
2844 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2846 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2848 ret
= -LTTNG_ERR_INVALID
;
2852 lsm
.u
.uri
.size
= size
;
2854 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2855 sizeof(struct lttng_uri
) * size
, NULL
);
2866 LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2867 int lttng_enable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2876 LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2877 int lttng_disable_consumer(struct lttng_handle
*handle
__attribute__((unused
)))
2886 LTTNG_EXPORT
int _lttng_create_session_ext(const char *name
, const char *url
,
2887 const char *datetime
);
2888 int _lttng_create_session_ext(const char *name
__attribute__((unused
)),
2889 const char *url
__attribute__((unused
)),
2890 const char *datetime
__attribute__((unused
)))
2896 * For a given session name, this call checks if the data is ready to be read
2897 * or is still being extracted by the consumer(s) hence not ready to be used by
2900 int lttng_data_pending(const char *session_name
)
2903 struct lttcomm_session_msg lsm
;
2904 uint8_t *pending
= NULL
;
2906 if (session_name
== NULL
) {
2907 return -LTTNG_ERR_INVALID
;
2910 memset(&lsm
, 0, sizeof(lsm
));
2911 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2913 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2914 sizeof(lsm
.session
.name
));
2916 ret
= -LTTNG_ERR_INVALID
;
2920 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2923 } else if (ret
!= 1) {
2924 /* Unexpected payload size */
2925 ret
= -LTTNG_ERR_INVALID
;
2927 } else if (!pending
) {
2928 /* Internal error. */
2929 ret
= -LTTNG_ERR_UNK
;
2933 ret
= (int) *pending
;
2940 * Regenerate the metadata for a session.
2941 * Return 0 on success, a negative error code on error.
2943 int lttng_regenerate_metadata(const char *session_name
)
2946 struct lttcomm_session_msg lsm
;
2948 if (!session_name
) {
2949 ret
= -LTTNG_ERR_INVALID
;
2953 memset(&lsm
, 0, sizeof(lsm
));
2954 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2956 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2957 sizeof(lsm
.session
.name
));
2959 ret
= -LTTNG_ERR_INVALID
;
2963 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2974 * Deprecated, replaced by lttng_regenerate_metadata.
2976 int lttng_metadata_regenerate(const char *session_name
)
2978 return lttng_regenerate_metadata(session_name
);
2982 * Regenerate the statedump of a session.
2983 * Return 0 on success, a negative error code on error.
2985 int lttng_regenerate_statedump(const char *session_name
)
2988 struct lttcomm_session_msg lsm
;
2990 if (!session_name
) {
2991 ret
= -LTTNG_ERR_INVALID
;
2995 memset(&lsm
, 0, sizeof(lsm
));
2996 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2998 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2999 sizeof(lsm
.session
.name
));
3001 ret
= -LTTNG_ERR_INVALID
;
3005 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3016 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3020 struct lttcomm_session_msg lsm
= {
3021 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3027 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
3028 struct lttcomm_session_msg
*message_lsm
;
3029 struct lttng_payload message
;
3030 struct lttng_payload reply
;
3031 struct lttng_trigger
*reply_trigger
= NULL
;
3032 enum lttng_domain_type domain_type
;
3033 const struct lttng_credentials user_creds
= {
3034 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3035 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3037 const char *unused_trigger_name
= NULL
;
3038 enum lttng_trigger_status trigger_status
;
3040 lttng_payload_init(&message
);
3041 lttng_payload_init(&reply
);
3044 ret
= -LTTNG_ERR_INVALID
;
3048 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3049 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3050 /* Re-using already registered trigger. */
3051 ret
= -LTTNG_ERR_INVALID
;
3056 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3057 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3058 ret
= -LTTNG_ERR_NOMEM
;
3063 if (!trigger
->creds
.uid
.is_set
) {
3064 /* Use the client's credentials as the trigger credentials. */
3065 lttng_trigger_set_credentials(trigger
, &user_creds
);
3068 * Validate that either the current trigger credentials and the
3069 * client credentials are identical or that the current user is
3070 * root. The root user can register, unregister triggers for
3071 * himself and other users.
3073 * This check is also present on the sessiond side, using the
3074 * credentials passed on the socket. These check are all
3077 const struct lttng_credentials
*trigger_creds
=
3078 lttng_trigger_get_credentials(trigger
);
3080 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3081 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3082 ret
= -LTTNG_ERR_EPERM
;
3083 goto end_unset_name
;
3088 if (!lttng_trigger_validate(trigger
)) {
3089 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3090 goto end_unset_name
;
3093 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3096 lsm
.domain
.type
= domain_type
;
3098 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3100 ret
= -LTTNG_ERR_NOMEM
;
3101 goto end_unset_name
;
3104 ret
= lttng_trigger_serialize(trigger
, &message
);
3106 ret
= -LTTNG_ERR_UNK
;
3107 goto end_unset_name
;
3111 * This is needed to populate the trigger object size for the command
3114 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3116 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3119 struct lttng_payload_view message_view
=
3120 lttng_payload_view_from_payload(
3123 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3125 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3127 goto end_unset_name
;
3132 struct lttng_payload_view reply_view
=
3133 lttng_payload_view_from_payload(
3134 &reply
, 0, reply
.buffer
.size
);
3136 ret
= lttng_trigger_create_from_payload(
3137 &reply_view
, &reply_trigger
);
3139 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3140 goto end_unset_name
;
3144 if (name
|| generate_name
) {
3145 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3147 ret
= -LTTNG_ERR_NOMEM
;
3156 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3157 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3158 ret
= -LTTNG_ERR_UNK
;
3161 lttng_payload_reset(&message
);
3162 lttng_payload_reset(&reply
);
3163 lttng_trigger_destroy(reply_trigger
);
3167 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3169 /* Register an anonymous trigger. */
3170 return _lttng_register_trigger(trigger
, NULL
, false);
3173 enum lttng_error_code
lttng_register_trigger_with_name(
3174 struct lttng_trigger
*trigger
, const char *name
)
3176 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3178 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3181 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3182 struct lttng_trigger
*trigger
)
3184 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3186 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3189 enum lttng_error_code
lttng_error_query_execute(
3190 const struct lttng_error_query
*query
,
3191 const struct lttng_endpoint
*endpoint
,
3192 struct lttng_error_query_results
**results
)
3195 enum lttng_error_code ret_code
;
3196 struct lttcomm_session_msg lsm
= {
3197 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3203 struct lttng_payload message
;
3204 struct lttng_payload reply
;
3205 struct lttcomm_session_msg
*message_lsm
;
3207 lttng_payload_init(&message
);
3208 lttng_payload_init(&reply
);
3210 if (!query
|| !results
) {
3211 ret_code
= LTTNG_ERR_INVALID
;
3215 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3216 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3220 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3222 ret_code
= LTTNG_ERR_NOMEM
;
3226 ret
= lttng_error_query_serialize(query
, &message
);
3228 ret_code
= LTTNG_ERR_UNK
;
3232 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3233 message_lsm
->u
.error_query
.length
=
3234 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3237 struct lttng_payload_view message_view
=
3238 lttng_payload_view_from_payload(
3241 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3243 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3245 ret_code
=(lttng_error_code
) -ret
;
3251 ssize_t reply_create_ret
;
3252 struct lttng_payload_view reply_view
=
3253 lttng_payload_view_from_payload(
3254 &reply
, 0, reply
.buffer
.size
);
3256 reply_create_ret
= lttng_error_query_results_create_from_payload(
3257 &reply_view
, results
);
3258 if (reply_create_ret
< 0) {
3259 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3264 ret_code
= LTTNG_OK
;
3266 lttng_payload_reset(&message
);
3267 lttng_payload_reset(&reply
);
3271 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3274 struct lttcomm_session_msg lsm
;
3275 struct lttcomm_session_msg
*message_lsm
;
3276 struct lttng_payload message
;
3277 struct lttng_payload reply
;
3278 struct lttng_trigger
*copy
= NULL
;
3279 const struct lttng_credentials user_creds
= {
3280 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3281 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3284 lttng_payload_init(&message
);
3285 lttng_payload_init(&reply
);
3288 ret
= -LTTNG_ERR_INVALID
;
3292 copy
= lttng_trigger_copy(trigger
);
3294 ret
= -LTTNG_ERR_UNK
;
3298 if (!copy
->creds
.uid
.is_set
) {
3299 /* Use the client credentials as the trigger credentials */
3300 lttng_trigger_set_credentials(copy
, &user_creds
);
3303 * Validate that either the current trigger credentials and the
3304 * client credentials are identical or that the current user is
3305 * root. The root user can register, unregister triggers for
3306 * himself and other users.
3308 * This check is also present on the sessiond side, using the
3309 * credentials passed on the socket. These check are all
3312 const struct lttng_credentials
*trigger_creds
=
3313 lttng_trigger_get_credentials(copy
);
3314 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3315 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3316 ret
= -LTTNG_ERR_EPERM
;
3322 if (!lttng_trigger_validate(copy
)) {
3323 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3327 memset(&lsm
, 0, sizeof(lsm
));
3328 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3330 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3332 ret
= -LTTNG_ERR_NOMEM
;
3336 ret
= lttng_trigger_serialize(copy
, &message
);
3338 ret
= -LTTNG_ERR_UNK
;
3343 * This is needed to populate the trigger object size for the command
3344 * header and number of fds sent.
3346 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3348 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3351 struct lttng_payload_view message_view
=
3352 lttng_payload_view_from_payload(
3356 * Update the message header with the number of fd that will be
3359 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3362 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3370 lttng_trigger_destroy(copy
);
3371 lttng_payload_reset(&message
);
3372 lttng_payload_reset(&reply
);
3377 * Ask the session daemon for all registered triggers for the current user.
3379 * Allocates and return an lttng_triggers set.
3380 * On error, returns a suitable lttng_error_code.
3382 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3385 enum lttng_error_code ret_code
= LTTNG_OK
;
3386 struct lttcomm_session_msg lsm
= {
3387 .cmd_type
= LTTNG_LIST_TRIGGERS
,
3393 struct lttng_triggers
*local_triggers
= NULL
;
3394 struct lttng_payload reply
;
3395 struct lttng_payload_view lsm_view
=
3396 lttng_payload_view_init_from_buffer(
3397 (const char *) &lsm
, 0, sizeof(lsm
));
3399 lttng_payload_init(&reply
);
3401 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3403 ret_code
= (enum lttng_error_code
) -ret
;
3408 struct lttng_payload_view reply_view
=
3409 lttng_payload_view_from_payload(
3410 &reply
, 0, reply
.buffer
.size
);
3412 ret
= lttng_triggers_create_from_payload(
3413 &reply_view
, &local_triggers
);
3415 ret_code
= LTTNG_ERR_FATAL
;
3420 *triggers
= local_triggers
;
3421 local_triggers
= NULL
;
3423 lttng_payload_reset(&reply
);
3424 lttng_triggers_destroy(local_triggers
);
3431 static void __attribute__((constructor
)) init(void)
3433 /* Set default session group */
3434 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3440 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3442 free(tracing_group
);
3445 LTTNG_EXPORT
enum lttng_error_code
lttng_domain_supports_trace_format(
3446 const struct lttng_endpoint
*session_daemon_endpoint
,
3447 enum lttng_domain_type domain_type
,
3448 const struct lttng_trace_format_descriptor
*trace_format_descriptor
)
3451 enum lttng_error_code ret_code
;
3452 struct lttcomm_session_msg lsm
= {
3453 .cmd_type
= LTTNG_EXECUTE_TRACE_FORMAT_SUPPORT_QUERY
,
3459 struct lttng_payload message
;
3460 struct lttng_payload reply
;
3461 struct lttcomm_session_msg
*message_lsm
;
3463 lttng_payload_init(&message
);
3464 lttng_payload_init(&reply
);
3466 if (!session_daemon_endpoint
|| !trace_format_descriptor
) {
3467 ret_code
= LTTNG_ERR_INVALID
;
3471 lsm
.domain
.type
= domain_type
;
3472 lsm
.u
.trace_format_support_query
.query_type
= 0;
3473 lsm
.u
.trace_format_support_query
.type_payload_length
= 0;
3475 if (session_daemon_endpoint
!= lttng_session_daemon_command_endpoint
) {
3476 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3480 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3482 ret_code
= LTTNG_ERR_NOMEM
;
3486 ret
= reinterpret_cast<const lttng::trace_format_descriptor
*>(trace_format_descriptor
)
3487 ->serialize(&message
);
3489 ret_code
= LTTNG_ERR_UNK
;
3493 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3494 message_lsm
->u
.trace_format_support_query
.trace_format_length
=
3495 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3498 struct lttng_payload_view message_view
=
3499 lttng_payload_view_from_payload(&message
, 0, -1);
3501 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3502 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3504 ret_code
= (lttng_error_code
) -ret
;
3508 /* No payload expected */
3509 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
3513 ret_code
= LTTNG_OK
;
3515 lttng_payload_reset(&message
);
3516 lttng_payload_reset(&reply
);
3520 LTTNG_EXPORT
extern enum lttng_error_code
lttng_destination_supports_trace_format(
3521 const struct lttng_endpoint
*session_daemon_endpoint
,
3522 const char *destination_url
,
3523 const struct lttng_trace_format_descriptor
*trace_format_descriptor
)
3527 enum lttng_error_code ret_code
;
3528 struct lttcomm_session_msg lsm
= {
3529 .cmd_type
= LTTNG_EXECUTE_TRACE_FORMAT_SUPPORT_QUERY
,
3535 struct lttng_payload message
;
3536 struct lttng_payload reply
;
3537 struct lttcomm_session_msg
*message_lsm
;
3538 struct lttng_uri
*uris
= NULL
;
3539 size_t size_before_payload
= 0;
3541 lttng_payload_init(&message
);
3542 lttng_payload_init(&reply
);
3544 if (!session_daemon_endpoint
|| !trace_format_descriptor
) {
3545 ret_code
= LTTNG_ERR_INVALID
;
3549 size
= uri_parse_str_urls(destination_url
, NULL
, &uris
);
3551 ret_code
= LTTNG_ERR_INVALID
;
3555 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
3557 ret_code
= LTTNG_OK
;
3561 lsm
.u
.trace_format_support_query
.query_type
= 1;
3563 if (session_daemon_endpoint
!= lttng_session_daemon_command_endpoint
) {
3564 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3568 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3570 ret_code
= LTTNG_ERR_NOMEM
;
3574 size_before_payload
= message
.buffer
.size
;
3576 ret
= reinterpret_cast<const lttng::trace_format_descriptor
*>(trace_format_descriptor
)
3577 ->serialize(&message
);
3579 ret_code
= LTTNG_ERR_UNK
;
3583 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3584 message_lsm
->u
.trace_format_support_query
.trace_format_length
=
3585 (uint32_t) message
.buffer
.size
- size_before_payload
;
3587 /* FIXME replace all this with good old serialization of sub query type
3588 * object. (internal) */
3589 size_before_payload
= message
.buffer
.size
;
3590 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &uris
[0], sizeof(*uris
));
3592 ret_code
= LTTNG_ERR_NOMEM
;
3596 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3597 message_lsm
->u
.trace_format_support_query
.type_payload_length
=
3598 (uint32_t) message
.buffer
.size
- size_before_payload
;
3601 struct lttng_payload_view message_view
=
3602 lttng_payload_view_from_payload(&message
, 0, -1);
3604 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(&message_view
);
3605 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3607 ret_code
= (lttng_error_code
) -ret
;
3611 /* No payload expected */
3612 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3617 ret_code
= LTTNG_OK
;
3619 lttng_payload_reset(&message
);
3620 lttng_payload_reset(&reply
);