4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/bytecode/bytecode.h>
23 #include <common/common.h>
24 #include <common/compat/string.h>
25 #include <common/defaults.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/dynamic-array.h>
28 #include <common/payload.h>
29 #include <common/payload-view.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/tracker.h>
32 #include <common/unix.h>
33 #include <common/uri.h>
34 #include <common/utils.h>
35 #include <lttng/channel-internal.h>
36 #include <lttng/destruction-handle.h>
37 #include <lttng/endpoint.h>
38 #include <lttng/event-internal.h>
39 #include <lttng/health-internal.h>
40 #include <lttng/lttng.h>
41 #include <lttng/session-descriptor-internal.h>
42 #include <lttng/session-internal.h>
43 #include <lttng/trigger/trigger-internal.h>
44 #include <lttng/userspace-probe-internal.h>
45 #include <lttng/lttng-error.h>
47 #include <common/filter/filter-ast.h>
48 #include <common/filter/filter-parser.h>
49 #include <common/filter/memstream.h>
50 #include "lttng-ctl-helper.h"
52 #define COPY_DOMAIN_PACKED(dst, src) \
54 struct lttng_domain _tmp_domain; \
56 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
= -1;
62 static char sessiond_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
80 * Copy string from src to dst and enforce null terminated byte.
83 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
86 strncpy(dst
, src
, len
);
87 /* Enforce the NULL terminated byte */
95 * Copy domain to lttcomm_session_msg domain.
97 * If domain is unknown, default domain will be the kernel.
100 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
101 struct lttng_domain
*src
)
105 case LTTNG_DOMAIN_KERNEL
:
106 case LTTNG_DOMAIN_UST
:
107 case LTTNG_DOMAIN_JUL
:
108 case LTTNG_DOMAIN_LOG4J
:
109 case LTTNG_DOMAIN_PYTHON
:
110 memcpy(dst
, src
, sizeof(struct lttng_domain
));
113 memset(dst
, 0, sizeof(struct lttng_domain
));
120 * Send lttcomm_session_msg to the session daemon.
122 * On success, returns the number of bytes sent (>=0)
123 * On error, returns -1
125 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
130 ret
= -LTTNG_ERR_NO_SESSIOND
;
134 DBG("LSM cmd type : %d", lsm
->cmd_type
);
136 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
137 sizeof(struct lttcomm_session_msg
));
139 ret
= -LTTNG_ERR_FATAL
;
147 * Send var len data to the session daemon.
149 * On success, returns the number of bytes sent (>=0)
150 * On error, returns -1
152 static int send_session_varlen(const void *data
, size_t len
)
157 ret
= -LTTNG_ERR_NO_SESSIOND
;
166 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
168 ret
= -LTTNG_ERR_FATAL
;
176 * Send file descriptors to the session daemon.
178 * On success, returns the number of bytes sent (>=0)
179 * On error, returns -1
181 static int send_session_fds(const int *fds
, size_t nb_fd
)
186 ret
= -LTTNG_ERR_NO_SESSIOND
;
190 if (!fds
|| !nb_fd
) {
195 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
197 ret
= -LTTNG_ERR_FATAL
;
205 * Receive data from the sessiond socket.
207 * On success, returns the number of bytes received (>=0)
208 * On error, returns a negative lttng_error_code.
210 static int recv_data_sessiond(void *buf
, size_t len
)
215 ret
= -LTTNG_ERR_NO_SESSIOND
;
219 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
221 ret
= -LTTNG_ERR_FATAL
;
229 * Receive a payload from the session daemon by appending to an existing
231 * On success, returns the number of bytes received (>=0)
232 * On error, returns a negative lttng_error_code.
234 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
237 const size_t original_payload_size
= payload
->buffer
.size
;
239 ret
= lttng_dynamic_buffer_set_size(
240 &payload
->buffer
, payload
->buffer
.size
+ len
);
242 ret
= -LTTNG_ERR_NOMEM
;
246 ret
= recv_data_sessiond(
247 payload
->buffer
.data
+ original_payload_size
, len
);
253 * Check if we are in the specified group.
255 * If yes return 1, else return -1.
258 int lttng_check_tracing_group(void)
260 gid_t
*grp_list
, tracing_gid
;
261 int grp_list_size
, grp_id
, i
;
263 const char *grp_name
= tracing_group
;
265 /* Get GID of group 'tracing' */
266 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
267 /* If grp_tracing is NULL, the group does not exist. */
271 /* Get number of supplementary group IDs */
272 grp_list_size
= getgroups(0, NULL
);
273 if (grp_list_size
< 0) {
278 /* Alloc group list of the right size */
279 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
284 grp_id
= getgroups(grp_list_size
, grp_list
);
290 for (i
= 0; i
< grp_list_size
; i
++) {
291 if (grp_list
[i
] == tracing_gid
) {
304 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
308 size_t best_mem_info
;
309 size_t num_bytes_requested_total
;
312 * Get the number of CPU currently online to compute the amount of
313 * memory needed to create a buffer for every CPU.
315 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
320 num_bytes_requested_total
= num_bytes_requested_per_cpu
* 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
);
345 return best_mem_info
>= num_bytes_requested_total
;
349 * Try connect to session daemon with sock_path.
351 * Return 0 on success, else -1
353 static int try_connect_sessiond(const char *sock_path
)
357 /* If socket exist, we check if the daemon listens for connect. */
358 ret
= access(sock_path
, F_OK
);
364 ret
= lttcomm_connect_unix_sock(sock_path
);
370 ret
= lttcomm_close_unix_sock(ret
);
372 PERROR("lttcomm_close_unix_sock");
382 * Set sessiond socket path by putting it in the global sessiond_sock_path
385 * Returns 0 on success, negative value on failure (the sessiond socket path
386 * is somehow too long or ENOMEM).
388 static int set_session_daemon_path(void)
390 int in_tgroup
= 0; /* In tracing group. */
396 /* Are we in the tracing group ? */
397 in_tgroup
= lttng_check_tracing_group();
400 if ((uid
== 0) || in_tgroup
) {
401 lttng_ctl_copy_string(sessiond_sock_path
,
402 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
410 ret
= try_connect_sessiond(sessiond_sock_path
);
414 /* Global session daemon not available... */
416 /* ...or not in tracing group (and not root), default */
419 * With GNU C < 2.1, snprintf returns -1 if the target buffer
421 * With GNU C >= 2.1, snprintf returns the required size
422 * (excluding closing null)
424 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
425 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
426 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
438 * Connect to the LTTng session daemon.
440 * On success, return the socket's file descriptor. On error, return -1.
442 LTTNG_HIDDEN
int connect_sessiond(void)
446 ret
= set_session_daemon_path();
451 /* Connect to the sesssion daemon. */
452 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
463 static void reset_global_sessiond_connection_state(void)
465 sessiond_socket
= -1;
470 * Clean disconnect from the session daemon.
472 * On success, return 0. On error, return -1.
474 static int disconnect_sessiond(void)
479 ret
= lttcomm_close_unix_sock(sessiond_socket
);
480 reset_global_sessiond_connection_state();
486 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
494 ret
= -LTTNG_ERR_INVALID
;
504 ret
= recv_data_sessiond(buf
, len
);
510 ret
= -LTTNG_ERR_INVALID
;
514 /* Move ownership of command header buffer to user. */
519 /* No command header. */
535 * Ask the session daemon a specific command and put the data into buf.
536 * Takes extra var. len. data and file descriptors as input to send to the
539 * Return size of data (only payload, not header) or a negative error code.
542 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
543 const int *fds
, size_t nb_fd
, const void *vardata
,
544 size_t vardata_len
, void **user_payload_buf
,
545 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
549 struct lttcomm_lttng_msg llm
;
551 ret
= connect_sessiond();
553 ret
= -LTTNG_ERR_NO_SESSIOND
;
556 sessiond_socket
= ret
;
560 ret
= send_session_msg(lsm
);
562 /* Ret value is a valid lttng error code. */
565 /* Send var len data */
566 ret
= send_session_varlen(vardata
, vardata_len
);
568 /* Ret value is a valid lttng error code. */
573 ret
= send_session_fds(fds
, nb_fd
);
575 /* Ret value is a valid lttng error code. */
579 /* Get header from data transmission */
580 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
582 /* Ret value is a valid lttng error code. */
586 /* Check error code if OK */
587 if (llm
.ret_code
!= LTTNG_OK
) {
592 /* Get command header from data transmission */
593 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
594 user_cmd_header_buf
, user_cmd_header_len
);
599 /* Get payload from data transmission */
600 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
609 disconnect_sessiond();
614 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
615 struct lttng_payload
*reply
)
618 struct lttcomm_lttng_msg llm
;
619 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
621 assert(reply
->buffer
.size
== 0);
622 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
624 ret
= connect_sessiond();
626 ret
= -LTTNG_ERR_NO_SESSIOND
;
629 sessiond_socket
= ret
;
633 /* Send command to session daemon */
634 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
635 message
->buffer
.size
);
637 ret
= -LTTNG_ERR_FATAL
;
642 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
645 ret
= -LTTNG_ERR_FATAL
;
650 /* Get header from data transmission */
651 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
653 /* Ret value is a valid lttng error code. */
657 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
659 /* Check error code if OK */
660 if (llm
.ret_code
!= LTTNG_OK
) {
665 if (llm
.cmd_header_size
> 0) {
666 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
672 /* Get command header from data transmission */
673 if (llm
.data_size
> 0) {
674 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
680 if (llm
.fd_count
> 0) {
681 ret
= lttcomm_recv_payload_fds_unix_sock(
682 sessiond_socket
, llm
.fd_count
, reply
);
688 /* Don't return the llm header to the caller. */
689 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
690 reply
->buffer
.size
- sizeof(llm
));
691 ret
= lttng_dynamic_buffer_set_size(
692 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
694 /* Can't happen as size is reduced. */
698 ret
= reply
->buffer
.size
;
701 disconnect_sessiond();
706 * Create lttng handle and return pointer.
708 * The returned pointer will be NULL in case of malloc() error.
710 struct lttng_handle
*lttng_create_handle(const char *session_name
,
711 struct lttng_domain
*domain
)
713 struct lttng_handle
*handle
= NULL
;
715 handle
= zmalloc(sizeof(struct lttng_handle
));
716 if (handle
== NULL
) {
717 PERROR("malloc handle");
721 /* Copy session name */
722 lttng_ctl_copy_string(handle
->session_name
, session_name
,
723 sizeof(handle
->session_name
));
725 /* Copy lttng domain or leave initialized to 0. */
727 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
735 * Destroy handle by free(3) the pointer.
737 void lttng_destroy_handle(struct lttng_handle
*handle
)
743 * Register an outside consumer.
745 * Returns size of returned session payload data or a negative error code.
747 int lttng_register_consumer(struct lttng_handle
*handle
,
748 const char *socket_path
)
750 struct lttcomm_session_msg lsm
;
752 if (handle
== NULL
|| socket_path
== NULL
) {
753 return -LTTNG_ERR_INVALID
;
756 memset(&lsm
, 0, sizeof(lsm
));
757 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
758 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
759 sizeof(lsm
.session
.name
));
760 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
762 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
763 sizeof(lsm
.u
.reg
.path
));
765 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
769 * Start tracing for all traces of the session.
771 * Returns size of returned session payload data or a negative error code.
773 int lttng_start_tracing(const char *session_name
)
775 struct lttcomm_session_msg lsm
;
777 if (session_name
== NULL
) {
778 return -LTTNG_ERR_INVALID
;
781 memset(&lsm
, 0, sizeof(lsm
));
782 lsm
.cmd_type
= LTTNG_START_TRACE
;
784 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
785 sizeof(lsm
.session
.name
));
787 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
791 * Stop tracing for all traces of the session.
793 static int _lttng_stop_tracing(const char *session_name
, int wait
)
796 struct lttcomm_session_msg lsm
;
798 if (session_name
== NULL
) {
799 return -LTTNG_ERR_INVALID
;
802 memset(&lsm
, 0, sizeof(lsm
));
803 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
805 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
806 sizeof(lsm
.session
.name
));
808 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
809 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
817 /* Check for data availability */
819 data_ret
= lttng_data_pending(session_name
);
821 /* Return the data available call error. */
827 * Data sleep time before retrying (in usec). Don't sleep if the
828 * call returned value indicates availability.
831 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
833 } while (data_ret
!= 0);
841 * Stop tracing and wait for data availability.
843 int lttng_stop_tracing(const char *session_name
)
845 return _lttng_stop_tracing(session_name
, 1);
849 * Stop tracing but _don't_ wait for data availability.
851 int lttng_stop_tracing_no_wait(const char *session_name
)
853 return _lttng_stop_tracing(session_name
, 0);
857 * Add context to a channel.
859 * If the given channel is NULL, add the contexts to all channels.
860 * The event_name param is ignored.
862 * Returns the size of the returned payload data or a negative error code.
864 int lttng_add_context(struct lttng_handle
*handle
,
865 struct lttng_event_context
*ctx
, const char *event_name
,
866 const char *channel_name
)
871 struct lttcomm_session_msg lsm
;
873 /* Safety check. Both are mandatory. */
874 if (handle
== NULL
|| ctx
== NULL
) {
875 ret
= -LTTNG_ERR_INVALID
;
879 memset(&lsm
, 0, sizeof(lsm
));
880 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
882 /* If no channel name, send empty string. */
883 if (channel_name
== NULL
) {
884 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
885 sizeof(lsm
.u
.context
.channel_name
));
887 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
888 sizeof(lsm
.u
.context
.channel_name
));
891 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
892 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
893 sizeof(lsm
.session
.name
));
895 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
896 size_t provider_len
, ctx_len
;
897 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
898 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
900 if (!provider_name
|| !ctx_name
) {
901 ret
= -LTTNG_ERR_INVALID
;
905 provider_len
= strlen(provider_name
);
906 if (provider_len
== 0) {
907 ret
= -LTTNG_ERR_INVALID
;
910 lsm
.u
.context
.provider_name_len
= provider_len
;
912 ctx_len
= strlen(ctx_name
);
914 ret
= -LTTNG_ERR_INVALID
;
917 lsm
.u
.context
.context_name_len
= ctx_len
;
919 len
= provider_len
+ ctx_len
;
922 ret
= -LTTNG_ERR_NOMEM
;
926 memcpy(buf
, provider_name
, provider_len
);
927 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
929 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
931 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
933 * Don't leak application addresses to the sessiond.
934 * This is only necessary when ctx is for an app ctx otherwise
935 * the values inside the union (type & config) are overwritten.
937 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
938 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
941 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
948 * Enable event(s) for a channel.
950 * If no event name is specified, all events are enabled.
951 * If no channel name is specified, the default 'channel0' is used.
953 * Returns size of returned session payload data or a negative error code.
955 int lttng_enable_event(struct lttng_handle
*handle
,
956 struct lttng_event
*ev
, const char *channel_name
)
958 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
963 * Create or enable an event with a filter expression.
965 * Return negative error value on error.
966 * Return size of returned session payload data if OK.
968 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
969 struct lttng_event
*event
, const char *channel_name
,
970 const char *filter_expression
)
972 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
973 filter_expression
, 0, NULL
);
977 * Depending on the event, return a newly allocated agent filter expression or
978 * NULL if not applicable.
980 * An event with NO loglevel and the name is * will return NULL.
982 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
985 char *agent_filter
= NULL
;
989 /* Don't add filter for the '*' event. */
990 if (strcmp(ev
->name
, "*") != 0) {
992 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
995 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1003 /* Add loglevel filtering if any for the JUL domain. */
1004 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1007 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1013 if (filter
|| agent_filter
) {
1016 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1017 agent_filter
? agent_filter
: filter
, op
,
1022 agent_filter
= new_filter
;
1024 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1033 return agent_filter
;
1040 * Enable event(s) for a channel, possibly with exclusions and a filter.
1041 * If no event name is specified, all events are enabled.
1042 * If no channel name is specified, the default name is used.
1043 * If filter expression is not NULL, the filter is set for the event.
1044 * If exclusion count is not zero, the exclusions are set for the event.
1045 * Returns size of returned session payload data or a negative error code.
1047 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1048 struct lttng_event
*ev
, const char *channel_name
,
1049 const char *original_filter_expression
,
1050 int exclusion_count
, char **exclusion_list
)
1052 struct lttcomm_session_msg lsm
;
1053 struct lttng_payload payload
;
1055 unsigned int free_filter_expression
= 0;
1056 struct filter_parser_ctx
*ctx
= NULL
;
1059 * We have either a filter or some exclusions, so we need to set up
1060 * a variable-length payload from where to send the data.
1062 lttng_payload_init(&payload
);
1065 * Cast as non-const since we may replace the filter expression
1066 * by a dynamically allocated string. Otherwise, the original
1067 * string is not modified.
1069 char *filter_expression
= (char *) original_filter_expression
;
1071 if (handle
== NULL
|| ev
== NULL
) {
1072 ret
= -LTTNG_ERR_INVALID
;
1077 * Empty filter string will always be rejected by the parser
1078 * anyway, so treat this corner-case early to eliminate
1079 * lttng_fmemopen error for 0-byte allocation.
1081 if (filter_expression
&& filter_expression
[0] == '\0') {
1082 ret
= -LTTNG_ERR_INVALID
;
1086 memset(&lsm
, 0, sizeof(lsm
));
1088 /* If no channel name, send empty string. */
1089 if (channel_name
== NULL
) {
1090 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1091 sizeof(lsm
.u
.enable
.channel_name
));
1093 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1094 sizeof(lsm
.u
.enable
.channel_name
));
1097 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1098 if (ev
->name
[0] == '\0') {
1099 /* Enable all events */
1100 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1103 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1104 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1106 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1107 sizeof(lsm
.session
.name
));
1108 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1109 lsm
.u
.enable
.bytecode_len
= 0;
1111 /* Parse filter expression. */
1112 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1113 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1114 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1115 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1116 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1117 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1120 /* Setup JUL filter if needed. */
1121 agent_filter
= set_agent_filter(filter_expression
, ev
);
1122 if (!agent_filter
) {
1123 if (!filter_expression
) {
1125 * No JUL and no filter, just skip
1132 * With an agent filter, the original filter has
1133 * been added to it thus replace the filter
1136 filter_expression
= agent_filter
;
1137 free_filter_expression
= 1;
1141 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1146 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1147 + bytecode_get_len(&ctx
->bytecode
->b
);
1148 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1151 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1152 lsm
.u
.enable
.bytecode_len
+
1153 lsm
.u
.enable
.expression_len
+
1154 LTTNG_SYMBOL_NAME_LEN
*
1157 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1161 /* Put exclusion names first in the data. */
1162 for (i
= 0; i
< exclusion_count
; i
++) {
1163 size_t exclusion_len
;
1165 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1166 LTTNG_SYMBOL_NAME_LEN
);
1167 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1168 /* Exclusion is not NULL-terminated. */
1169 ret
= -LTTNG_ERR_INVALID
;
1173 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1174 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1180 /* Add filter expression next. */
1181 if (filter_expression
) {
1182 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1183 filter_expression
, lsm
.u
.enable
.expression_len
);
1188 /* Add filter bytecode next. */
1189 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1190 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1191 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1196 if (ev
->extended
.ptr
) {
1197 struct lttng_event_extended
*ev_ext
=
1198 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1200 if (ev_ext
->probe_location
) {
1202 * lttng_userspace_probe_location_serialize returns the
1203 * number of bytes that was appended to the buffer.
1205 ret
= lttng_userspace_probe_location_serialize(
1206 ev_ext
->probe_location
, &payload
);
1212 * Set the size of the userspace probe location element
1213 * of the buffer so that the receiving side knows where
1216 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1221 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1223 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1230 assert(fd_count
== 0 || fd_count
== 1);
1231 if (fd_count
== 1) {
1232 struct fd_handle
*handle
=
1233 lttng_payload_view_pop_fd_handle(&view
);
1239 fd_to_send
= fd_handle_get_fd(handle
);
1240 fd_handle_put(handle
);
1243 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1244 fd_count
? &fd_to_send
: NULL
, fd_count
,
1245 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1246 view
.buffer
.size
, NULL
, NULL
, 0);
1250 if (filter_expression
&& ctx
) {
1251 filter_bytecode_free(ctx
);
1252 filter_ir_free(ctx
);
1253 filter_parser_ctx_free(ctx
);
1256 if (free_filter_expression
) {
1258 * The filter expression has been replaced and must be freed as
1259 * it is not the original filter expression received as a
1262 free(filter_expression
);
1266 * Return directly to the caller and don't ask the sessiond since
1267 * something went wrong in the parsing of data above.
1269 lttng_payload_reset(&payload
);
1273 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1277 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1278 struct lttng_event
*ev
, const char *channel_name
,
1279 const char *original_filter_expression
)
1281 struct lttcomm_session_msg lsm
;
1284 unsigned int free_filter_expression
= 0;
1285 struct filter_parser_ctx
*ctx
= NULL
;
1287 * Cast as non-const since we may replace the filter expression
1288 * by a dynamically allocated string. Otherwise, the original
1289 * string is not modified.
1291 char *filter_expression
= (char *) original_filter_expression
;
1293 if (handle
== NULL
|| ev
== NULL
) {
1294 ret
= -LTTNG_ERR_INVALID
;
1299 * Empty filter string will always be rejected by the parser
1300 * anyway, so treat this corner-case early to eliminate
1301 * lttng_fmemopen error for 0-byte allocation.
1303 if (filter_expression
&& filter_expression
[0] == '\0') {
1304 ret
= -LTTNG_ERR_INVALID
;
1308 memset(&lsm
, 0, sizeof(lsm
));
1310 /* If no channel name, send empty string. */
1311 if (channel_name
== NULL
) {
1312 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1313 sizeof(lsm
.u
.disable
.channel_name
));
1315 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1316 sizeof(lsm
.u
.disable
.channel_name
));
1319 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1321 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1322 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1324 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1325 sizeof(lsm
.session
.name
));
1326 lsm
.u
.disable
.bytecode_len
= 0;
1329 * For the JUL domain, a filter is enforced except for the
1330 * disable all event. This is done to avoid having the event in
1331 * all sessions thus filtering by logger name.
1333 if (filter_expression
== NULL
&&
1334 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1335 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1336 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1341 * We have a filter, so we need to set up a variable-length
1342 * memory block from where to send the data.
1345 /* Parse filter expression */
1346 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1347 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1348 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1349 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1350 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1351 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1354 /* Setup JUL filter if needed. */
1355 agent_filter
= set_agent_filter(filter_expression
, ev
);
1356 if (!agent_filter
) {
1357 if (!filter_expression
) {
1359 * No JUL and no filter, just skip
1366 * With a JUL filter, the original filter has
1367 * been added to it thus replace the filter
1370 filter_expression
= agent_filter
;
1371 free_filter_expression
= 1;
1375 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1380 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1381 + bytecode_get_len(&ctx
->bytecode
->b
);
1382 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1385 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1386 + lsm
.u
.disable
.expression_len
);
1388 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1392 /* Add filter expression. */
1393 if (lsm
.u
.disable
.expression_len
!= 0) {
1396 lsm
.u
.disable
.expression_len
);
1398 /* Add filter bytecode next. */
1399 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1401 + lsm
.u
.disable
.expression_len
,
1403 lsm
.u
.disable
.bytecode_len
);
1406 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1407 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1411 if (filter_expression
&& ctx
) {
1412 filter_bytecode_free(ctx
);
1413 filter_ir_free(ctx
);
1414 filter_parser_ctx_free(ctx
);
1417 if (free_filter_expression
) {
1419 * The filter expression has been replaced and must be freed as
1420 * it is not the original filter expression received as a
1423 free(filter_expression
);
1427 * Return directly to the caller and don't ask the sessiond since
1428 * something went wrong in the parsing of data above.
1433 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1438 * Disable event(s) of a channel and domain.
1439 * If no event name is specified, all events are disabled.
1440 * If no channel name is specified, the default 'channel0' is used.
1441 * Returns size of returned session payload data or a negative error code.
1443 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1444 const char *channel_name
)
1446 struct lttng_event ev
;
1448 memset(&ev
, 0, sizeof(ev
));
1450 ev
.type
= LTTNG_EVENT_ALL
;
1451 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1452 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1455 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1457 struct lttng_channel
*channel
= NULL
;
1458 struct lttng_channel_extended
*extended
= NULL
;
1464 /* Validate domain. */
1465 switch (domain
->type
) {
1466 case LTTNG_DOMAIN_UST
:
1467 switch (domain
->buf_type
) {
1468 case LTTNG_BUFFER_PER_UID
:
1469 case LTTNG_BUFFER_PER_PID
:
1475 case LTTNG_DOMAIN_KERNEL
:
1476 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1484 channel
= zmalloc(sizeof(*channel
));
1489 extended
= zmalloc(sizeof(*extended
));
1494 channel
->attr
.extended
.ptr
= extended
;
1496 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1504 void lttng_channel_destroy(struct lttng_channel
*channel
)
1510 if (channel
->attr
.extended
.ptr
) {
1511 free(channel
->attr
.extended
.ptr
);
1517 * Enable channel per domain
1518 * Returns size of returned session payload data or a negative error code.
1520 int lttng_enable_channel(struct lttng_handle
*handle
,
1521 struct lttng_channel
*in_chan
)
1523 struct lttcomm_session_msg lsm
;
1524 size_t total_buffer_size_needed_per_cpu
= 0;
1526 /* NULL arguments are forbidden. No default values. */
1527 if (handle
== NULL
|| in_chan
== NULL
) {
1528 return -LTTNG_ERR_INVALID
;
1531 memset(&lsm
, 0, sizeof(lsm
));
1532 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1533 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1535 if (!in_chan
->attr
.extended
.ptr
) {
1536 struct lttng_channel
*channel
;
1537 struct lttng_channel_extended
*extended
;
1539 channel
= lttng_channel_create(&handle
->domain
);
1541 return -LTTNG_ERR_NOMEM
;
1545 * Create a new channel in order to use default extended
1548 extended
= (struct lttng_channel_extended
*)
1549 channel
->attr
.extended
.ptr
;
1550 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1551 lttng_channel_destroy(channel
);
1553 struct lttng_channel_extended
*extended
;
1555 extended
= (struct lttng_channel_extended
*)
1556 in_chan
->attr
.extended
.ptr
;
1557 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1561 * Verify that the amount of memory required to create the requested
1562 * buffer is available on the system at the moment.
1564 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1565 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1566 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1567 return -LTTNG_ERR_NOMEM
;
1570 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1571 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1573 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1574 sizeof(lsm
.session
.name
));
1576 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1580 * All tracing will be stopped for registered events of the channel.
1581 * Returns size of returned session payload data or a negative error code.
1583 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1585 struct lttcomm_session_msg lsm
;
1587 /* Safety check. Both are mandatory. */
1588 if (handle
== NULL
|| name
== NULL
) {
1589 return -LTTNG_ERR_INVALID
;
1592 memset(&lsm
, 0, sizeof(lsm
));
1594 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1596 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1597 sizeof(lsm
.u
.disable
.channel_name
));
1599 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1601 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1602 sizeof(lsm
.session
.name
));
1604 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1608 * Lists all available tracepoints of domain.
1609 * Sets the contents of the events array.
1610 * Returns the number of lttng_event entries in events;
1611 * on error, returns a negative value.
1613 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1614 struct lttng_event
**events
)
1617 struct lttcomm_session_msg lsm
;
1619 if (handle
== NULL
) {
1620 return -LTTNG_ERR_INVALID
;
1623 memset(&lsm
, 0, sizeof(lsm
));
1624 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1625 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1627 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1632 return ret
/ sizeof(struct lttng_event
);
1636 * Lists all available tracepoint fields of domain.
1637 * Sets the contents of the event field array.
1638 * Returns the number of lttng_event_field entries in events;
1639 * on error, returns a negative value.
1641 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1642 struct lttng_event_field
**fields
)
1645 struct lttcomm_session_msg lsm
;
1647 if (handle
== NULL
) {
1648 return -LTTNG_ERR_INVALID
;
1651 memset(&lsm
, 0, sizeof(lsm
));
1652 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1653 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1655 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1660 return ret
/ sizeof(struct lttng_event_field
);
1664 * Lists all available kernel system calls. Allocates and sets the contents of
1667 * Returns the number of lttng_event entries in events; on error, returns a
1670 int lttng_list_syscalls(struct lttng_event
**events
)
1673 struct lttcomm_session_msg lsm
;
1676 return -LTTNG_ERR_INVALID
;
1679 memset(&lsm
, 0, sizeof(lsm
));
1680 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1681 /* Force kernel domain for system calls. */
1682 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1684 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1689 return ret
/ sizeof(struct lttng_event
);
1693 * Returns a human readable string describing
1694 * the error code (a negative value).
1696 const char *lttng_strerror(int code
)
1698 return error_get_str(code
);
1701 enum lttng_error_code
lttng_create_session_ext(
1702 struct lttng_session_descriptor
*session_descriptor
)
1704 enum lttng_error_code ret_code
;
1705 struct lttcomm_session_msg lsm
= {
1706 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1709 struct lttng_buffer_view reply_view
;
1711 bool sessiond_must_generate_ouput
;
1712 struct lttng_dynamic_buffer payload
;
1714 size_t descriptor_size
;
1715 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1717 lttng_dynamic_buffer_init(&payload
);
1718 if (!session_descriptor
) {
1719 ret_code
= LTTNG_ERR_INVALID
;
1723 sessiond_must_generate_ouput
=
1724 !lttng_session_descriptor_is_output_destination_initialized(
1725 session_descriptor
);
1726 if (sessiond_must_generate_ouput
) {
1727 const char *home_dir
= utils_get_home_dir();
1728 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1730 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1731 ret_code
= LTTNG_ERR_FATAL
;
1735 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1736 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1739 ret_code
= LTTNG_ERR_NOMEM
;
1744 descriptor_size
= payload
.size
;
1745 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1748 ret_code
= LTTNG_ERR_INVALID
;
1751 descriptor_size
= payload
.size
- descriptor_size
;
1752 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1754 /* Command returns a session descriptor on success. */
1755 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1756 payload
.size
, &reply
);
1757 if (reply_ret
< 0) {
1758 ret_code
= -reply_ret
;
1760 } else if (reply_ret
== 0) {
1761 /* Socket unexpectedly closed by the session daemon. */
1762 ret_code
= LTTNG_ERR_FATAL
;
1766 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1767 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1770 ret_code
= LTTNG_ERR_FATAL
;
1773 ret_code
= LTTNG_OK
;
1774 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1777 lttng_dynamic_buffer_reset(&payload
);
1778 lttng_session_descriptor_destroy(descriptor_reply
);
1783 * Create a new session using name and url for destination.
1785 * Return 0 on success else a negative LTTng error code.
1787 int lttng_create_session(const char *name
, const char *url
)
1791 struct lttng_uri
*uris
= NULL
;
1792 struct lttng_session_descriptor
*descriptor
= NULL
;
1793 enum lttng_error_code ret_code
;
1796 ret
= -LTTNG_ERR_INVALID
;
1800 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1802 ret
= -LTTNG_ERR_INVALID
;
1807 descriptor
= lttng_session_descriptor_create(name
);
1810 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1811 ret
= -LTTNG_ERR_INVALID
;
1814 descriptor
= lttng_session_descriptor_local_create(name
,
1818 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1822 ret
= -LTTNG_ERR_INVALID
;
1826 ret
= -LTTNG_ERR_INVALID
;
1829 ret_code
= lttng_create_session_ext(descriptor
);
1830 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1832 lttng_session_descriptor_destroy(descriptor
);
1838 * Create a session exclusively used for snapshot.
1840 * Return 0 on success else a negative LTTng error code.
1842 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1845 enum lttng_error_code ret_code
;
1847 struct lttng_uri
*uris
= NULL
;
1848 struct lttng_session_descriptor
*descriptor
= NULL
;
1851 ret
= -LTTNG_ERR_INVALID
;
1855 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1857 ret
= -LTTNG_ERR_INVALID
;
1861 * If the user does not specify a custom subdir, use the session name.
1863 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1864 strlen(uris
[0].subdir
) == 0) {
1865 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1868 PERROR("Failed to set session name as network destination sub-directory");
1869 ret
= -LTTNG_ERR_FATAL
;
1871 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1872 /* Truncated output. */
1873 ret
= -LTTNG_ERR_INVALID
;
1880 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1883 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1884 ret
= -LTTNG_ERR_INVALID
;
1887 descriptor
= lttng_session_descriptor_snapshot_local_create(
1892 descriptor
= lttng_session_descriptor_snapshot_network_create(
1898 ret
= -LTTNG_ERR_INVALID
;
1902 ret
= -LTTNG_ERR_INVALID
;
1905 ret_code
= lttng_create_session_ext(descriptor
);
1906 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1908 lttng_session_descriptor_destroy(descriptor
);
1914 * Create a session exclusively used for live.
1916 * Return 0 on success else a negative LTTng error code.
1918 int lttng_create_session_live(const char *name
, const char *url
,
1919 unsigned int timer_interval
)
1922 enum lttng_error_code ret_code
;
1923 struct lttng_session_descriptor
*descriptor
= NULL
;
1926 ret
= -LTTNG_ERR_INVALID
;
1931 descriptor
= lttng_session_descriptor_live_network_create(
1932 name
, url
, NULL
, timer_interval
);
1934 descriptor
= lttng_session_descriptor_live_create(
1935 name
, timer_interval
);
1938 ret
= -LTTNG_ERR_INVALID
;
1941 ret_code
= lttng_create_session_ext(descriptor
);
1942 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1944 lttng_session_descriptor_destroy(descriptor
);
1949 * Stop the session and wait for the data before destroying it
1951 * Return 0 on success else a negative LTTng error code.
1953 int lttng_destroy_session(const char *session_name
)
1956 enum lttng_error_code ret_code
;
1957 enum lttng_destruction_handle_status status
;
1958 struct lttng_destruction_handle
*handle
= NULL
;
1961 * Stop the tracing and wait for the data to be
1964 ret
= _lttng_stop_tracing(session_name
, 1);
1965 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1969 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
1970 if (ret_code
!= LTTNG_OK
) {
1971 ret
= (int) -ret_code
;
1976 /* Block until the completion of the destruction of the session. */
1977 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
1978 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
1979 ret
= -LTTNG_ERR_UNK
;
1983 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
1984 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
1985 ret
= -LTTNG_ERR_UNK
;
1988 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1990 lttng_destruction_handle_destroy(handle
);
1995 * Destroy the session without waiting for the data.
1997 int lttng_destroy_session_no_wait(const char *session_name
)
1999 enum lttng_error_code ret_code
;
2001 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2002 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2006 * Ask the session daemon for all available sessions.
2007 * Sets the contents of the sessions array.
2008 * Returns the number of lttng_session entries in sessions;
2009 * on error, returns a negative value.
2011 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2014 struct lttcomm_session_msg lsm
;
2015 const size_t session_size
= sizeof(struct lttng_session
) +
2016 sizeof(struct lttng_session_extended
);
2017 size_t session_count
, i
;
2018 struct lttng_session_extended
*sessions_extended_begin
;
2019 struct lttng_session
*sessions
= NULL
;
2021 memset(&lsm
, 0, sizeof(lsm
));
2022 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2023 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2028 ret
= -LTTNG_ERR_FATAL
;
2032 if (ret
% session_size
) {
2033 ret
= -LTTNG_ERR_UNK
;
2035 *out_sessions
= NULL
;
2038 session_count
= (size_t) ret
/ session_size
;
2039 sessions_extended_begin
= (struct lttng_session_extended
*)
2040 (&sessions
[session_count
]);
2042 /* Set extended session info pointers. */
2043 for (i
= 0; i
< session_count
; i
++) {
2044 struct lttng_session
*session
= &sessions
[i
];
2045 struct lttng_session_extended
*extended
=
2046 &(sessions_extended_begin
[i
]);
2048 session
->extended
.ptr
= extended
;
2051 ret
= (int) session_count
;
2052 *out_sessions
= sessions
;
2057 enum lttng_error_code
lttng_session_get_creation_time(
2058 const struct lttng_session
*session
, uint64_t *creation_time
)
2060 enum lttng_error_code ret
= LTTNG_OK
;
2061 struct lttng_session_extended
*extended
;
2063 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2064 ret
= LTTNG_ERR_INVALID
;
2068 extended
= session
->extended
.ptr
;
2069 if (!extended
->creation_time
.is_set
) {
2070 /* Not created on the session daemon yet. */
2071 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2074 *creation_time
= extended
->creation_time
.value
;
2079 int lttng_set_session_shm_path(const char *session_name
,
2080 const char *shm_path
)
2082 struct lttcomm_session_msg lsm
;
2084 if (session_name
== NULL
) {
2085 return -LTTNG_ERR_INVALID
;
2088 memset(&lsm
, 0, sizeof(lsm
));
2089 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2091 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2092 sizeof(lsm
.session
.name
));
2093 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2094 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2096 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2100 * Ask the session daemon for all available domains of a session.
2101 * Sets the contents of the domains array.
2102 * Returns the number of lttng_domain entries in domains;
2103 * on error, returns a negative value.
2105 int lttng_list_domains(const char *session_name
,
2106 struct lttng_domain
**domains
)
2109 struct lttcomm_session_msg lsm
;
2111 if (session_name
== NULL
) {
2112 return -LTTNG_ERR_INVALID
;
2115 memset(&lsm
, 0, sizeof(lsm
));
2116 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2118 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2119 sizeof(lsm
.session
.name
));
2121 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2126 return ret
/ sizeof(struct lttng_domain
);
2130 * Ask the session daemon for all available channels of a session.
2131 * Sets the contents of the channels array.
2132 * Returns the number of lttng_channel entries in channels;
2133 * on error, returns a negative value.
2135 int lttng_list_channels(struct lttng_handle
*handle
,
2136 struct lttng_channel
**channels
)
2139 size_t channel_count
, i
;
2140 const size_t channel_size
= sizeof(struct lttng_channel
) +
2141 sizeof(struct lttng_channel_extended
);
2142 struct lttcomm_session_msg lsm
;
2145 if (handle
== NULL
) {
2146 ret
= -LTTNG_ERR_INVALID
;
2150 memset(&lsm
, 0, sizeof(lsm
));
2151 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2152 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2153 sizeof(lsm
.session
.name
));
2155 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2157 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2162 if (ret
% channel_size
) {
2163 ret
= -LTTNG_ERR_UNK
;
2168 channel_count
= (size_t) ret
/ channel_size
;
2170 /* Set extended info pointers */
2171 extended_at
= ((void *) *channels
) +
2172 channel_count
* sizeof(struct lttng_channel
);
2173 for (i
= 0; i
< channel_count
; i
++) {
2174 struct lttng_channel
*chan
= &(*channels
)[i
];
2176 chan
->attr
.extended
.ptr
= extended_at
;
2177 extended_at
+= sizeof(struct lttng_channel_extended
);
2180 ret
= (int) channel_count
;
2186 * Ask the session daemon for all available events of a session channel.
2187 * Sets the contents of the events array.
2188 * Returns the number of lttng_event entries in events;
2189 * on error, returns a negative value.
2191 int lttng_list_events(struct lttng_handle
*handle
,
2192 const char *channel_name
, struct lttng_event
**events
)
2195 struct lttcomm_session_msg lsm
= {};
2196 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2197 uint32_t nb_events
, i
;
2198 const void *comm_ext_at
;
2199 struct lttng_dynamic_buffer listing
;
2201 struct lttng_payload payload
;
2202 struct lttng_payload payload_copy
;
2203 struct lttng_payload_view lsm_view
=
2204 lttng_payload_view_init_from_buffer(
2205 (const char *) &lsm
, 0, sizeof(lsm
));
2206 struct lttng_buffer_view cmd_header_view
;
2207 struct lttng_buffer_view cmd_payload_view
;
2208 struct lttng_buffer_view flat_events_view
;
2209 struct lttng_buffer_view ext_view
;
2211 /* Safety check. An handle and channel name are mandatory */
2212 if (handle
== NULL
|| channel_name
== NULL
) {
2213 ret
= -LTTNG_ERR_INVALID
;
2217 lttng_payload_init(&payload
);
2218 lttng_payload_init(&payload_copy
);
2220 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2221 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2222 sizeof(lsm
.session
.name
));
2223 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2224 sizeof(lsm
.u
.list
.channel_name
));
2225 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2227 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2233 * A copy of the payload is performed since it will be
2234 * consumed twice. Consuming the same payload twice is invalid since
2235 * it will cause any received file descriptor to become "shared"
2236 * between different instances of the resulting objects.
2238 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2240 ret
= -LTTNG_ERR_NOMEM
;
2244 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2245 &payload
.buffer
, 0, sizeof(*cmd_header
));
2246 if (!cmd_header_view
.data
) {
2247 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2251 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2253 /* Set number of events and free command header */
2254 nb_events
= cmd_header
->nb_events
;
2255 if (nb_events
> INT_MAX
) {
2256 ret
= -LTTNG_ERR_OVERFLOW
;
2260 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2261 &payload
.buffer
, sizeof(*cmd_header
), -1);
2264 * The buffer that is returned must contain a "flat" version of
2265 * the events that are returned. In other words, all pointers
2266 * within an lttng_event must point to a location within the returned
2267 * buffer so that the user may free everything by simply calling free()
2268 * on the returned buffer. This is needed in order to maintain API
2271 * A first pass is performed to compute the size of the buffer that
2272 * must be allocated. A second pass is then performed to setup
2273 * the returned events so that their members always point within the
2276 * The layout of the returned buffer is as follows:
2277 * - struct lttng_event[nb_events],
2278 * - nb_events times the following:
2279 * - struct lttng_event_extended,
2280 * - flattened version of userspace_probe_location
2281 * - filter_expression
2283 * - padding to align to 64-bits
2285 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2286 nb_events
* sizeof(struct lttng_event
), -1);
2287 comm_ext_at
= ext_view
.data
;
2288 storage_req
= nb_events
* sizeof(struct lttng_event
);
2290 struct lttng_payload_view payload_view
=
2291 lttng_payload_view_from_payload(&payload
, 0, -1);
2293 for (i
= 0; i
< nb_events
; i
++) {
2294 const struct lttcomm_event_extended_header
*ext_comm
=
2295 (struct lttcomm_event_extended_header
*)
2297 int probe_storage_req
= 0;
2299 comm_ext_at
+= sizeof(*ext_comm
);
2300 comm_ext_at
+= ext_comm
->filter_len
;
2301 comm_ext_at
+= ext_comm
->nb_exclusions
*
2302 LTTNG_SYMBOL_NAME_LEN
;
2304 if (ext_comm
->userspace_probe_location_len
) {
2305 struct lttng_userspace_probe_location
2306 *probe_location
= NULL
;
2307 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2309 (const char *) comm_ext_at
-
2310 payload_view
.buffer
.data
,
2311 ext_comm
->userspace_probe_location_len
);
2314 * Create a temporary userspace probe location
2315 * to determine the size needed by a "flattened"
2316 * version of that same probe location.
2318 ret
= lttng_userspace_probe_location_create_from_payload(
2319 &probe_location_view
,
2322 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2326 ret
= lttng_userspace_probe_location_flatten(
2327 probe_location
, NULL
);
2328 lttng_userspace_probe_location_destroy(
2331 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2335 probe_storage_req
= ret
;
2336 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2339 storage_req
+= sizeof(struct lttng_event_extended
);
2340 storage_req
+= ext_comm
->filter_len
;
2341 storage_req
+= ext_comm
->nb_exclusions
*
2342 LTTNG_SYMBOL_NAME_LEN
;
2343 /* Padding to ensure the flat probe is aligned. */
2344 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2345 storage_req
+= probe_storage_req
;
2349 lttng_dynamic_buffer_init(&listing
);
2351 * We must ensure that "listing" is never resized so as to preserve
2352 * the validity of the flattened objects.
2354 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2356 ret
= -LTTNG_ERR_NOMEM
;
2360 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2361 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2362 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2363 nb_events
* sizeof(struct lttng_event
));
2364 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2366 ret
= -LTTNG_ERR_NOMEM
;
2367 goto free_dynamic_buffer
;
2370 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2371 nb_events
* sizeof(struct lttng_event
), -1);
2372 comm_ext_at
= ext_view
.data
;
2375 struct lttng_payload_view payload_copy_view
=
2376 lttng_payload_view_from_payload(
2377 &payload_copy
, 0, -1);
2379 for (i
= 0; i
< nb_events
; i
++) {
2380 struct lttng_event
*event
= (typeof(event
))(
2382 (sizeof(struct lttng_event
) * i
));
2383 const struct lttcomm_event_extended_header
*ext_comm
=
2384 (typeof(ext_comm
)) comm_ext_at
;
2385 struct lttng_event_extended
*event_extended
=
2386 (typeof(event_extended
))(listing
.data
+
2389 /* Insert struct lttng_event_extended. */
2390 ret
= lttng_dynamic_buffer_set_size(&listing
,
2391 listing
.size
+ sizeof(*event_extended
));
2393 ret
= -LTTNG_ERR_NOMEM
;
2394 goto free_dynamic_buffer
;
2396 event
->extended
.ptr
= event_extended
;
2398 comm_ext_at
+= sizeof(*ext_comm
);
2400 /* Insert filter expression. */
2401 if (ext_comm
->filter_len
) {
2402 event_extended
->filter_expression
=
2403 listing
.data
+ listing
.size
;
2404 ret
= lttng_dynamic_buffer_append(&listing
,
2406 ext_comm
->filter_len
);
2408 ret
= -LTTNG_ERR_NOMEM
;
2409 goto free_dynamic_buffer
;
2411 comm_ext_at
+= ext_comm
->filter_len
;
2414 /* Insert exclusions. */
2415 if (ext_comm
->nb_exclusions
) {
2416 event_extended
->exclusions
.count
=
2417 ext_comm
->nb_exclusions
;
2418 event_extended
->exclusions
.strings
=
2419 listing
.data
+ listing
.size
;
2421 ret
= lttng_dynamic_buffer_append(&listing
,
2423 ext_comm
->nb_exclusions
*
2424 LTTNG_SYMBOL_NAME_LEN
);
2426 ret
= -LTTNG_ERR_NOMEM
;
2427 goto free_dynamic_buffer
;
2429 comm_ext_at
+= ext_comm
->nb_exclusions
*
2430 LTTNG_SYMBOL_NAME_LEN
;
2433 /* Insert padding to align to 64-bits. */
2434 ret
= lttng_dynamic_buffer_set_size(&listing
,
2435 ALIGN_TO(listing
.size
,
2438 ret
= -LTTNG_ERR_NOMEM
;
2439 goto free_dynamic_buffer
;
2442 /* Insert flattened userspace probe location. */
2443 if (ext_comm
->userspace_probe_location_len
) {
2444 struct lttng_userspace_probe_location
2445 *probe_location
= NULL
;
2446 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2448 (const char *) comm_ext_at
-
2449 payload_copy_view
.buffer
.data
,
2450 ext_comm
->userspace_probe_location_len
);
2452 ret
= lttng_userspace_probe_location_create_from_payload(
2453 &probe_location_view
,
2456 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2457 goto free_dynamic_buffer
;
2460 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2463 ret
= lttng_userspace_probe_location_flatten(
2464 probe_location
, &listing
);
2465 lttng_userspace_probe_location_destroy(
2468 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2469 goto free_dynamic_buffer
;
2472 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2477 /* Don't reset listing buffer as we return its content. */
2478 *events
= (struct lttng_event
*) listing
.data
;
2479 lttng_dynamic_buffer_init(&listing
);
2480 ret
= (int) nb_events
;
2481 free_dynamic_buffer
:
2482 lttng_dynamic_buffer_reset(&listing
);
2484 lttng_payload_reset(&payload
);
2485 lttng_payload_reset(&payload_copy
);
2490 * Sets the tracing_group variable with name.
2491 * This function allocates memory pointed to by tracing_group.
2492 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2494 int lttng_set_tracing_group(const char *name
)
2497 return -LTTNG_ERR_INVALID
;
2500 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2501 return -LTTNG_ERR_FATAL
;
2507 int lttng_calibrate(struct lttng_handle
*handle
,
2508 struct lttng_calibrate
*calibrate
)
2511 * This command was removed in LTTng 2.9.
2513 return -LTTNG_ERR_UND
;
2517 * Set default channel attributes.
2518 * If either or both of the arguments are null, attr content is zeroe'd.
2520 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2521 struct lttng_channel_attr
*attr
)
2523 struct lttng_channel_extended
*extended
;
2526 if (attr
== NULL
|| domain
== NULL
) {
2530 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2531 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2533 /* Same for all domains. */
2534 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2535 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2536 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2538 switch (domain
->type
) {
2539 case LTTNG_DOMAIN_KERNEL
:
2540 attr
->switch_timer_interval
=
2541 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2542 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2543 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2544 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2545 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2547 extended
->monitor_timer_interval
=
2548 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2549 extended
->blocking_timeout
=
2550 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2553 case LTTNG_DOMAIN_UST
:
2554 switch (domain
->buf_type
) {
2555 case LTTNG_BUFFER_PER_UID
:
2556 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2557 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2558 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2559 attr
->switch_timer_interval
=
2560 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2561 attr
->read_timer_interval
=
2562 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2564 extended
->monitor_timer_interval
=
2565 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2566 extended
->blocking_timeout
=
2567 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2570 case LTTNG_BUFFER_PER_PID
:
2572 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2573 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2574 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2575 attr
->switch_timer_interval
=
2576 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2577 attr
->read_timer_interval
=
2578 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2580 extended
->monitor_timer_interval
=
2581 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2582 extended
->blocking_timeout
=
2583 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2588 /* Default behavior: leave set to 0. */
2592 attr
->extended
.ptr
= extended
;
2595 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2596 uint64_t *discarded_events
)
2599 struct lttng_channel_extended
*chan_ext
;
2601 if (!channel
|| !discarded_events
) {
2602 ret
= -LTTNG_ERR_INVALID
;
2606 chan_ext
= channel
->attr
.extended
.ptr
;
2609 * This can happen since the lttng_channel structure is
2610 * used for other tasks where this pointer is never set.
2612 *discarded_events
= 0;
2616 *discarded_events
= chan_ext
->discarded_events
;
2621 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2622 uint64_t *lost_packets
)
2625 struct lttng_channel_extended
*chan_ext
;
2627 if (!channel
|| !lost_packets
) {
2628 ret
= -LTTNG_ERR_INVALID
;
2632 chan_ext
= channel
->attr
.extended
.ptr
;
2635 * This can happen since the lttng_channel structure is
2636 * used for other tasks where this pointer is never set.
2642 *lost_packets
= chan_ext
->lost_packets
;
2647 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2648 uint64_t *monitor_timer_interval
)
2652 if (!chan
|| !monitor_timer_interval
) {
2653 ret
= -LTTNG_ERR_INVALID
;
2657 if (!chan
->attr
.extended
.ptr
) {
2658 ret
= -LTTNG_ERR_INVALID
;
2662 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2663 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2668 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2669 uint64_t monitor_timer_interval
)
2673 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2674 ret
= -LTTNG_ERR_INVALID
;
2678 ((struct lttng_channel_extended
*)
2679 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2680 monitor_timer_interval
;
2685 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2686 int64_t *blocking_timeout
)
2690 if (!chan
|| !blocking_timeout
) {
2691 ret
= -LTTNG_ERR_INVALID
;
2695 if (!chan
->attr
.extended
.ptr
) {
2696 ret
= -LTTNG_ERR_INVALID
;
2700 *blocking_timeout
= ((struct lttng_channel_extended
*)
2701 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2706 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2707 int64_t blocking_timeout
)
2710 int64_t msec_timeout
;
2712 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2713 ret
= -LTTNG_ERR_INVALID
;
2717 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2718 ret
= -LTTNG_ERR_INVALID
;
2723 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2724 * us to accept a narrower range of values (msecs expressed as a signed
2727 msec_timeout
= blocking_timeout
/ 1000;
2728 if (msec_timeout
!= (int32_t) msec_timeout
) {
2729 ret
= -LTTNG_ERR_INVALID
;
2733 ((struct lttng_channel_extended
*)
2734 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2741 * Check if session daemon is alive.
2743 * Return 1 if alive or 0 if not.
2744 * On error returns a negative value.
2746 int lttng_session_daemon_alive(void)
2750 ret
= set_session_daemon_path();
2756 if (*sessiond_sock_path
== '\0') {
2758 * No socket path set. Weird error which means the constructor
2764 ret
= try_connect_sessiond(sessiond_sock_path
);
2775 * Set URL for a consumer for a session and domain.
2777 * Return 0 on success, else a negative value.
2779 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2780 const char *control_url
, const char *data_url
)
2784 struct lttcomm_session_msg lsm
;
2785 struct lttng_uri
*uris
= NULL
;
2787 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2788 return -LTTNG_ERR_INVALID
;
2791 memset(&lsm
, 0, sizeof(lsm
));
2793 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2795 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2796 sizeof(lsm
.session
.name
));
2797 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2799 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2801 return -LTTNG_ERR_INVALID
;
2804 lsm
.u
.uri
.size
= size
;
2806 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2807 sizeof(struct lttng_uri
) * size
, NULL
);
2816 int lttng_enable_consumer(struct lttng_handle
*handle
);
2817 int lttng_enable_consumer(struct lttng_handle
*handle
)
2825 int lttng_disable_consumer(struct lttng_handle
*handle
);
2826 int lttng_disable_consumer(struct lttng_handle
*handle
)
2834 int _lttng_create_session_ext(const char *name
, const char *url
,
2835 const char *datetime
);
2836 int _lttng_create_session_ext(const char *name
, const char *url
,
2837 const char *datetime
)
2843 * For a given session name, this call checks if the data is ready to be read
2844 * or is still being extracted by the consumer(s) hence not ready to be used by
2847 int lttng_data_pending(const char *session_name
)
2850 struct lttcomm_session_msg lsm
;
2851 uint8_t *pending
= NULL
;
2853 if (session_name
== NULL
) {
2854 return -LTTNG_ERR_INVALID
;
2857 memset(&lsm
, 0, sizeof(lsm
));
2858 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2860 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2861 sizeof(lsm
.session
.name
));
2863 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2866 } else if (ret
!= 1) {
2867 /* Unexpected payload size */
2868 ret
= -LTTNG_ERR_INVALID
;
2870 } else if (!pending
) {
2871 /* Internal error. */
2872 ret
= -LTTNG_ERR_UNK
;
2876 ret
= (int) *pending
;
2883 * Regenerate the metadata for a session.
2884 * Return 0 on success, a negative error code on error.
2886 int lttng_regenerate_metadata(const char *session_name
)
2889 struct lttcomm_session_msg lsm
;
2891 if (!session_name
) {
2892 ret
= -LTTNG_ERR_INVALID
;
2896 memset(&lsm
, 0, sizeof(lsm
));
2897 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2899 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2900 sizeof(lsm
.session
.name
));
2902 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2913 * Deprecated, replaced by lttng_regenerate_metadata.
2915 int lttng_metadata_regenerate(const char *session_name
)
2917 return lttng_regenerate_metadata(session_name
);
2921 * Regenerate the statedump of a session.
2922 * Return 0 on success, a negative error code on error.
2924 int lttng_regenerate_statedump(const char *session_name
)
2927 struct lttcomm_session_msg lsm
;
2929 if (!session_name
) {
2930 ret
= -LTTNG_ERR_INVALID
;
2934 memset(&lsm
, 0, sizeof(lsm
));
2935 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2937 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2938 sizeof(lsm
.session
.name
));
2940 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2950 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2953 struct lttcomm_session_msg lsm
= {
2954 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
2956 struct lttcomm_session_msg
*message_lsm
;
2957 struct lttng_payload message
;
2958 struct lttng_payload reply
;
2959 struct lttng_trigger
*reply_trigger
= NULL
;
2960 enum lttng_domain_type domain_type
;
2961 struct lttng_credentials creds
= {
2962 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2963 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
2967 lttng_payload_init(&message
);
2968 lttng_payload_init(&reply
);
2971 ret
= -LTTNG_ERR_INVALID
;
2975 if (!trigger
->creds
.uid
.is_set
) {
2976 /* Use the client credentials as the trigger credentials */
2977 lttng_trigger_set_credentials(trigger
, &creds
);
2980 * Validate that either the current trigger credentials and the
2981 * client credentials are identical or that the current user is
2982 * root. The root user can register, unregister triggers for
2983 * himself and other users.
2984 * This check is also present on the sessiond side, using the
2985 * credentials passed on the socket. These check are all
2988 const struct lttng_credentials
*trigger_creds
=
2989 lttng_trigger_get_credentials(trigger
);
2990 if (!lttng_credentials_is_equal_uid(trigger_creds
, &creds
)) {
2991 if (lttng_credentials_get_uid(&creds
) != 0) {
2992 ret
= -LTTNG_ERR_EPERM
;
2998 if (!lttng_trigger_validate(trigger
)) {
2999 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3003 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3006 lsm
.domain
.type
= domain_type
;
3008 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3010 ret
= -LTTNG_ERR_NOMEM
;
3015 * This is needed to populate the trigger object size for the command
3018 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3020 ret
= lttng_trigger_serialize(trigger
, &message
);
3022 ret
= -LTTNG_ERR_UNK
;
3026 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3028 if (getenv("LTTNG_REGISTER_TRIGGER_DRY_RUN")) {
3030 * Don't really send the request, just deserialize, validate
3031 * that it is equal to the original trigger (to test
3032 * serialization and deserialization), and return.
3036 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3037 &message
, sizeof(lsm
), -1);
3038 sz
= lttng_trigger_create_from_payload(&pv
, &reply_trigger
);
3039 if (sz
!= pv
.buffer
.size
- sizeof(lsm
)) {
3040 ret
= -LTTNG_ERR_UNK
;
3044 if (!reply_trigger
) {
3045 ret
= -LTTNG_ERR_UNK
;
3049 if (!lttng_trigger_is_equal(trigger
, reply_trigger
)) {
3050 ret
= -LTTNG_ERR_UNK
;
3054 /* Give it a dummy name. */
3055 lttng_trigger_set_name(trigger
, "yop");
3062 struct lttng_payload_view message_view
=
3063 lttng_payload_view_from_payload(
3066 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3068 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3075 struct lttng_payload_view reply_view
=
3076 lttng_payload_view_from_payload(
3077 &reply
, 0, reply
.buffer
.size
);
3079 ret
= lttng_trigger_create_from_payload(
3080 &reply_view
, &reply_trigger
);
3082 ret
= -LTTNG_ERR_FATAL
;
3087 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3089 ret
= -LTTNG_ERR_FATAL
;
3095 lttng_payload_reset(&message
);
3096 lttng_payload_reset(&reply
);
3097 lttng_trigger_destroy(reply_trigger
);
3101 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3104 struct lttcomm_session_msg lsm
;
3105 struct lttcomm_session_msg
*message_lsm
;
3106 struct lttng_payload message
;
3107 struct lttng_payload reply
;
3108 struct lttng_trigger
*copy
= NULL
;
3109 struct lttng_credentials creds
= {
3110 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3111 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3114 lttng_payload_init(&message
);
3115 lttng_payload_init(&reply
);
3118 ret
= -LTTNG_ERR_INVALID
;
3122 copy
= lttng_trigger_copy(trigger
);
3124 ret
= -LTTNG_ERR_UNK
;
3128 if (!copy
->creds
.uid
.is_set
) {
3129 /* Use the client credentials as the trigger credentials */
3130 lttng_trigger_set_credentials(copy
, &creds
);
3133 * Validate that either the current trigger credentials and the
3134 * client credentials are identical or that the current user is
3135 * root. The root user can register, unregister triggers for
3136 * himself and other users.
3137 * This check is also present on the sessiond side, using the
3138 * credentials passed on the socket. These check are all
3141 const struct lttng_credentials
*trigger_creds
=
3142 lttng_trigger_get_credentials(copy
);
3143 if (!lttng_credentials_is_equal_uid(trigger_creds
, &creds
)) {
3144 if (lttng_credentials_get_uid(&creds
) != 0) {
3145 ret
= -LTTNG_ERR_EPERM
;
3151 if (!lttng_trigger_validate(copy
)) {
3152 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3156 memset(&lsm
, 0, sizeof(lsm
));
3157 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3159 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3161 ret
= -LTTNG_ERR_NOMEM
;
3166 * This is needed to populate the trigger object size for the command
3167 * header and number of fds sent.
3169 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3171 ret
= lttng_trigger_serialize(copy
, &message
);
3173 ret
= -LTTNG_ERR_UNK
;
3177 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3180 struct lttng_payload_view message_view
=
3181 lttng_payload_view_from_payload(
3185 * Update the message header with the number of fd that will be
3188 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3191 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3199 lttng_trigger_destroy(copy
);
3200 lttng_payload_reset(&message
);
3201 lttng_payload_reset(&reply
);
3206 * Ask the session daemon for all registered triggers.
3207 * Allocate a lttng_triggers collection.
3208 * On error, returns a negative value.
3210 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3213 struct lttcomm_session_msg lsm
;
3214 struct lttng_payload_view lsm_view
=
3215 lttng_payload_view_init_from_buffer(
3216 (const char *) &lsm
, 0, sizeof(lsm
));
3217 struct lttng_triggers
*local_triggers
= NULL
;
3218 struct lttng_payload reply
;
3220 lttng_payload_init(&reply
);
3222 memset(&lsm
, 0, sizeof(lsm
));
3223 lsm
.cmd_type
= LTTNG_LIST_TRIGGERS
;
3225 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3231 struct lttng_payload_view reply_view
=
3232 lttng_payload_view_from_payload(
3233 &reply
, 0, reply
.buffer
.size
);
3234 ret
= lttng_triggers_create_from_payload(
3235 &reply_view
, &local_triggers
);
3237 ret
= -LTTNG_ERR_FATAL
;
3242 *triggers
= local_triggers
;
3243 local_triggers
= NULL
;
3246 lttng_payload_reset(&reply
);
3247 lttng_triggers_destroy(local_triggers
);
3254 static void __attribute__((constructor
)) init(void)
3256 /* Set default session group */
3257 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3263 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3265 free(tracing_group
);