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
21 #include <common/common.h>
22 #include <common/compat/errno.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/dynamic-array.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/tracker.h>
31 #include <common/unix.h>
32 #include <common/uri.h>
33 #include <common/utils.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/destruction-handle.h>
36 #include <lttng/endpoint.h>
37 #include <lttng/event-internal.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/lttng.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/trigger/trigger-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/lttng-error.h>
46 #include <common/filter/filter-ast.h>
47 #include <common/filter/filter-parser.h>
48 #include <common/filter/filter-bytecode.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 domain to lttcomm_session_msg domain.
82 * If domain is unknown, default domain will be the kernel.
85 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
86 struct lttng_domain
*src
)
90 case LTTNG_DOMAIN_KERNEL
:
91 case LTTNG_DOMAIN_UST
:
92 case LTTNG_DOMAIN_JUL
:
93 case LTTNG_DOMAIN_LOG4J
:
94 case LTTNG_DOMAIN_PYTHON
:
95 memcpy(dst
, src
, sizeof(struct lttng_domain
));
98 memset(dst
, 0, sizeof(struct lttng_domain
));
105 * Send lttcomm_session_msg to the session daemon.
107 * On success, returns the number of bytes sent (>=0)
108 * On error, returns -1
110 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
115 ret
= -LTTNG_ERR_NO_SESSIOND
;
119 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm
->cmd_type
),
122 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
123 sizeof(struct lttcomm_session_msg
));
125 ret
= -LTTNG_ERR_FATAL
;
133 * Send var len data to the session daemon.
135 * On success, returns the number of bytes sent (>=0)
136 * On error, returns -1
138 static int send_session_varlen(const void *data
, size_t len
)
143 ret
= -LTTNG_ERR_NO_SESSIOND
;
152 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
154 ret
= -LTTNG_ERR_FATAL
;
162 * Send file descriptors to the session daemon.
164 * On success, returns the number of bytes sent (>=0)
165 * On error, returns -1
167 static int send_session_fds(const int *fds
, size_t nb_fd
)
172 ret
= -LTTNG_ERR_NO_SESSIOND
;
176 if (!fds
|| !nb_fd
) {
181 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
183 ret
= -LTTNG_ERR_FATAL
;
191 * Receive data from the sessiond socket.
193 * On success, returns the number of bytes received (>=0)
194 * On error, returns a negative lttng_error_code.
196 static int recv_data_sessiond(void *buf
, size_t len
)
203 ret
= -LTTNG_ERR_NO_SESSIOND
;
207 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
209 ret
= -LTTNG_ERR_FATAL
;
210 } else if (ret
== 0) {
211 ret
= -LTTNG_ERR_NO_SESSIOND
;
219 * Receive a payload from the session daemon by appending to an existing
221 * On success, returns the number of bytes received (>=0)
222 * On error, returns a negative lttng_error_code.
224 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
227 const size_t original_payload_size
= payload
->buffer
.size
;
229 ret
= lttng_dynamic_buffer_set_size(
230 &payload
->buffer
, payload
->buffer
.size
+ len
);
232 ret
= -LTTNG_ERR_NOMEM
;
236 ret
= recv_data_sessiond(
237 payload
->buffer
.data
+ original_payload_size
, len
);
243 * Check if we are in the specified group.
245 * 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
= zmalloc(grp_list_size
* sizeof(gid_t
));
274 grp_id
= getgroups(grp_list_size
, grp_list
);
280 for (i
= 0; i
< grp_list_size
; i
++) {
281 if (grp_list
[i
] == tracing_gid
) {
294 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
298 size_t best_mem_info
;
299 size_t num_bytes_requested_total
;
302 * Get the number of CPU currently online to compute the amount of
303 * memory needed to create a buffer for every CPU.
305 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
310 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
313 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
314 * reliable estimate we can get but it is only exposed by the kernel
315 * since 3.14. (See Linux kernel commit:
316 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
318 ret
= utils_get_memory_available(&best_mem_info
);
324 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
325 * is a sanity check for obvious user error.
327 ret
= utils_get_memory_total(&best_mem_info
);
335 return best_mem_info
>= num_bytes_requested_total
;
339 * Try connect to session daemon with sock_path.
341 * Return 0 on success, else -1
343 static int try_connect_sessiond(const char *sock_path
)
347 /* If socket exist, we check if the daemon listens for connect. */
348 ret
= access(sock_path
, F_OK
);
354 ret
= lttcomm_connect_unix_sock(sock_path
);
360 ret
= lttcomm_close_unix_sock(ret
);
362 PERROR("lttcomm_close_unix_sock");
372 * Set sessiond socket path by putting it in the global sessiond_sock_path
375 * Returns 0 on success, negative value on failure (the sessiond socket path
376 * is somehow too long or ENOMEM).
378 static int set_session_daemon_path(void)
380 int in_tgroup
= 0; /* In tracing group. */
386 /* Are we in the tracing group ? */
387 in_tgroup
= lttng_check_tracing_group();
390 if ((uid
== 0) || in_tgroup
) {
391 const int ret
= lttng_strncpy(sessiond_sock_path
,
392 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
393 sizeof(sessiond_sock_path
));
405 ret
= try_connect_sessiond(sessiond_sock_path
);
409 /* Global session daemon not available... */
411 /* ...or not in tracing group (and not root), default */
414 * With GNU C < 2.1, snprintf returns -1 if the target buffer
416 * With GNU C >= 2.1, snprintf returns the required size
417 * (excluding closing null)
419 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
420 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
421 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
433 * Connect to the LTTng session daemon.
435 * On success, return the socket's file descriptor. On error, return -1.
437 LTTNG_HIDDEN
int connect_sessiond(void)
441 ret
= set_session_daemon_path();
446 /* Connect to the sesssion daemon. */
447 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
458 static void reset_global_sessiond_connection_state(void)
460 sessiond_socket
= -1;
465 * Clean disconnect from the session daemon.
467 * On success, return 0. On error, return -1.
469 static int disconnect_sessiond(void)
474 ret
= lttcomm_close_unix_sock(sessiond_socket
);
475 reset_global_sessiond_connection_state();
481 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
489 ret
= -LTTNG_ERR_INVALID
;
499 ret
= recv_data_sessiond(buf
, len
);
505 ret
= -LTTNG_ERR_INVALID
;
509 /* Move ownership of command header buffer to user. */
514 /* No command header. */
530 * Ask the session daemon a specific command and put the data into buf.
531 * Takes extra var. len. data and file descriptors as input to send to the
534 * Return size of data (only payload, not header) or a negative error code.
537 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
538 const int *fds
, size_t nb_fd
, const void *vardata
,
539 size_t vardata_len
, void **user_payload_buf
,
540 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
544 struct lttcomm_lttng_msg llm
;
546 ret
= connect_sessiond();
548 ret
= -LTTNG_ERR_NO_SESSIOND
;
551 sessiond_socket
= ret
;
555 ret
= send_session_msg(lsm
);
557 /* Ret value is a valid lttng error code. */
560 /* Send var len data */
561 ret
= send_session_varlen(vardata
, vardata_len
);
563 /* Ret value is a valid lttng error code. */
568 ret
= send_session_fds(fds
, nb_fd
);
570 /* Ret value is a valid lttng error code. */
574 /* Get header from data transmission */
575 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
577 /* Ret value is a valid lttng error code. */
581 /* Check error code if OK */
582 if (llm
.ret_code
!= LTTNG_OK
) {
587 /* Get command header from data transmission */
588 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
589 user_cmd_header_buf
, user_cmd_header_len
);
594 /* Get payload from data transmission */
595 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
604 disconnect_sessiond();
609 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
610 struct lttng_payload
*reply
)
613 struct lttcomm_lttng_msg llm
;
614 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
616 assert(reply
->buffer
.size
== 0);
617 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
619 ret
= connect_sessiond();
621 ret
= -LTTNG_ERR_NO_SESSIOND
;
624 sessiond_socket
= ret
;
628 /* Send command to session daemon */
629 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
630 message
->buffer
.size
);
632 ret
= -LTTNG_ERR_FATAL
;
637 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
640 ret
= -LTTNG_ERR_FATAL
;
645 /* Get header from data transmission */
646 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
648 /* Ret value is a valid lttng error code. */
652 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
654 /* Check error code if OK */
655 if (llm
.ret_code
!= LTTNG_OK
) {
660 if (llm
.cmd_header_size
> 0) {
661 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
667 /* Get command header from data transmission */
668 if (llm
.data_size
> 0) {
669 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
675 if (llm
.fd_count
> 0) {
676 ret
= lttcomm_recv_payload_fds_unix_sock(
677 sessiond_socket
, llm
.fd_count
, reply
);
683 /* Don't return the llm header to the caller. */
684 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
685 reply
->buffer
.size
- sizeof(llm
));
686 ret
= lttng_dynamic_buffer_set_size(
687 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
689 /* Can't happen as size is reduced. */
693 ret
= reply
->buffer
.size
;
696 disconnect_sessiond();
701 * Create lttng handle and return pointer.
703 * The returned pointer will be NULL in case of malloc() error.
705 struct lttng_handle
*lttng_create_handle(const char *session_name
,
706 struct lttng_domain
*domain
)
709 struct lttng_handle
*handle
= NULL
;
711 handle
= zmalloc(sizeof(struct lttng_handle
));
712 if (handle
== NULL
) {
713 PERROR("malloc handle");
717 /* Copy session name */
718 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
719 sizeof(handle
->session_name
));
724 /* Copy lttng domain or leave initialized to 0. */
726 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
737 * Destroy handle by free(3) the pointer.
739 void lttng_destroy_handle(struct lttng_handle
*handle
)
745 * Register an outside consumer.
747 * Returns size of returned session payload data or a negative error code.
749 int lttng_register_consumer(struct lttng_handle
*handle
,
750 const char *socket_path
)
753 struct lttcomm_session_msg lsm
;
755 if (handle
== NULL
|| socket_path
== NULL
) {
756 ret
= -LTTNG_ERR_INVALID
;
760 memset(&lsm
, 0, sizeof(lsm
));
761 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
762 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
763 sizeof(lsm
.session
.name
));
765 ret
= -LTTNG_ERR_INVALID
;
769 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
771 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
772 sizeof(lsm
.u
.reg
.path
));
774 ret
= -LTTNG_ERR_INVALID
;
778 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
784 * Start tracing for all traces of the session.
786 * Returns size of returned session payload data or a negative error code.
788 int lttng_start_tracing(const char *session_name
)
791 struct lttcomm_session_msg lsm
;
793 if (session_name
== NULL
) {
794 ret
= -LTTNG_ERR_INVALID
;
798 memset(&lsm
, 0, sizeof(lsm
));
799 lsm
.cmd_type
= LTTNG_START_TRACE
;
801 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
802 sizeof(lsm
.session
.name
));
804 ret
= -LTTNG_ERR_INVALID
;
808 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
814 * Stop tracing for all traces of the session.
816 static int _lttng_stop_tracing(const char *session_name
, int wait
)
819 struct lttcomm_session_msg lsm
;
821 if (session_name
== NULL
) {
822 ret
= -LTTNG_ERR_INVALID
;
826 memset(&lsm
, 0, sizeof(lsm
));
827 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
829 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
830 sizeof(lsm
.session
.name
));
832 ret
= -LTTNG_ERR_INVALID
;
836 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
837 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
845 /* Check for data availability */
847 data_ret
= lttng_data_pending(session_name
);
849 /* Return the data available call error. */
855 * Data sleep time before retrying (in usec). Don't sleep if the
856 * call returned value indicates availability.
859 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
861 } while (data_ret
!= 0);
869 * Stop tracing and wait for data availability.
871 int lttng_stop_tracing(const char *session_name
)
873 return _lttng_stop_tracing(session_name
, 1);
877 * Stop tracing but _don't_ wait for data availability.
879 int lttng_stop_tracing_no_wait(const char *session_name
)
881 return _lttng_stop_tracing(session_name
, 0);
885 * Add context to a channel.
887 * If the given channel is NULL, add the contexts to all channels.
888 * The event_name param is ignored.
890 * Returns the size of the returned payload data or a negative error code.
892 int lttng_add_context(struct lttng_handle
*handle
,
893 struct lttng_event_context
*ctx
, const char *event_name
,
894 const char *channel_name
)
899 struct lttcomm_session_msg lsm
;
901 /* Safety check. Both are mandatory. */
902 if (handle
== NULL
|| ctx
== NULL
) {
903 ret
= -LTTNG_ERR_INVALID
;
907 memset(&lsm
, 0, sizeof(lsm
));
908 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
910 /* If no channel name, send empty string. */
911 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
912 sizeof(lsm
.u
.context
.channel_name
));
914 ret
= -LTTNG_ERR_INVALID
;
918 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
919 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
920 sizeof(lsm
.session
.name
));
922 ret
= -LTTNG_ERR_INVALID
;
926 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
927 size_t provider_len
, ctx_len
;
928 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
929 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
931 if (!provider_name
|| !ctx_name
) {
932 ret
= -LTTNG_ERR_INVALID
;
936 provider_len
= strlen(provider_name
);
937 if (provider_len
== 0) {
938 ret
= -LTTNG_ERR_INVALID
;
941 lsm
.u
.context
.provider_name_len
= provider_len
;
943 ctx_len
= strlen(ctx_name
);
945 ret
= -LTTNG_ERR_INVALID
;
948 lsm
.u
.context
.context_name_len
= ctx_len
;
950 len
= provider_len
+ ctx_len
;
953 ret
= -LTTNG_ERR_NOMEM
;
957 memcpy(buf
, provider_name
, provider_len
);
958 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
960 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
962 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
964 * Don't leak application addresses to the sessiond.
965 * This is only necessary when ctx is for an app ctx otherwise
966 * the values inside the union (type & config) are overwritten.
968 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
969 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
972 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
979 * Enable event(s) for a channel.
981 * If no event name is specified, all events are enabled.
982 * If no channel name is specified, the default 'channel0' is used.
984 * Returns size of returned session payload data or a negative error code.
986 int lttng_enable_event(struct lttng_handle
*handle
,
987 struct lttng_event
*ev
, const char *channel_name
)
989 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
994 * Create or enable an event with a filter expression.
996 * Return negative error value on error.
997 * Return size of returned session payload data if OK.
999 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1000 struct lttng_event
*event
, const char *channel_name
,
1001 const char *filter_expression
)
1003 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1004 filter_expression
, 0, NULL
);
1008 * Depending on the event, return a newly allocated agent filter expression or
1009 * NULL if not applicable.
1011 * An event with NO loglevel and the name is * will return NULL.
1013 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1016 char *agent_filter
= NULL
;
1020 /* Don't add filter for the '*' event. */
1021 if (strcmp(ev
->name
, "*") != 0) {
1023 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1026 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1034 /* Add loglevel filtering if any for the JUL domain. */
1035 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1038 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1044 if (filter
|| agent_filter
) {
1047 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1048 agent_filter
? agent_filter
: filter
, op
,
1053 agent_filter
= new_filter
;
1055 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1064 return agent_filter
;
1071 * Enable event(s) for a channel, possibly with exclusions and a filter.
1072 * If no event name is specified, all events are enabled.
1073 * If no channel name is specified, the default name is used.
1074 * If filter expression is not NULL, the filter is set for the event.
1075 * If exclusion count is not zero, the exclusions are set for the event.
1076 * Returns size of returned session payload data or a negative error code.
1078 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1079 struct lttng_event
*ev
, const char *channel_name
,
1080 const char *original_filter_expression
,
1081 int exclusion_count
, char **exclusion_list
)
1083 struct lttcomm_session_msg lsm
;
1084 struct lttng_payload payload
;
1086 unsigned int free_filter_expression
= 0;
1087 struct filter_parser_ctx
*ctx
= NULL
;
1090 * We have either a filter or some exclusions, so we need to set up
1091 * a variable-length payload from where to send the data.
1093 lttng_payload_init(&payload
);
1096 * Cast as non-const since we may replace the filter expression
1097 * by a dynamically allocated string. Otherwise, the original
1098 * string is not modified.
1100 char *filter_expression
= (char *) original_filter_expression
;
1102 if (handle
== NULL
|| ev
== NULL
) {
1103 ret
= -LTTNG_ERR_INVALID
;
1108 * Empty filter string will always be rejected by the parser
1109 * anyway, so treat this corner-case early to eliminate
1110 * lttng_fmemopen error for 0-byte allocation.
1112 if (filter_expression
&& filter_expression
[0] == '\0') {
1113 ret
= -LTTNG_ERR_INVALID
;
1117 memset(&lsm
, 0, sizeof(lsm
));
1119 /* If no channel name, send empty string. */
1120 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1121 sizeof(lsm
.u
.enable
.channel_name
));
1123 ret
= -LTTNG_ERR_INVALID
;
1127 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1128 if (ev
->name
[0] == '\0') {
1129 /* Enable all events. */
1130 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1134 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1135 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1137 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1138 sizeof(lsm
.session
.name
));
1140 ret
= -LTTNG_ERR_INVALID
;
1144 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1145 lsm
.u
.enable
.bytecode_len
= 0;
1147 /* Parse filter expression. */
1148 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1149 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1150 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1151 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1152 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1153 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1156 /* Setup JUL filter if needed. */
1157 agent_filter
= set_agent_filter(filter_expression
, ev
);
1158 if (!agent_filter
) {
1159 if (!filter_expression
) {
1161 * No JUL and no filter, just skip
1168 * With an agent filter, the original filter has
1169 * been added to it thus replace the filter
1172 filter_expression
= agent_filter
;
1173 free_filter_expression
= 1;
1177 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1182 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1183 + bytecode_get_len(&ctx
->bytecode
->b
);
1184 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1187 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1188 lsm
.u
.enable
.bytecode_len
+
1189 lsm
.u
.enable
.expression_len
+
1190 LTTNG_SYMBOL_NAME_LEN
*
1193 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1197 /* Put exclusion names first in the data. */
1198 for (i
= 0; i
< exclusion_count
; i
++) {
1199 size_t exclusion_len
;
1201 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1202 LTTNG_SYMBOL_NAME_LEN
);
1203 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1204 /* Exclusion is not NULL-terminated. */
1205 ret
= -LTTNG_ERR_INVALID
;
1209 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1210 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1216 /* Add filter expression next. */
1217 if (filter_expression
) {
1218 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1219 filter_expression
, lsm
.u
.enable
.expression_len
);
1224 /* Add filter bytecode next. */
1225 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1226 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1227 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1232 if (ev
->extended
.ptr
) {
1233 struct lttng_event_extended
*ev_ext
=
1234 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1236 if (ev_ext
->probe_location
) {
1238 * lttng_userspace_probe_location_serialize returns the
1239 * number of bytes that was appended to the buffer.
1241 ret
= lttng_userspace_probe_location_serialize(
1242 ev_ext
->probe_location
, &payload
);
1248 * Set the size of the userspace probe location element
1249 * of the buffer so that the receiving side knows where
1252 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1257 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1259 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1266 assert(fd_count
== 0 || fd_count
== 1);
1267 if (fd_count
== 1) {
1268 struct fd_handle
*handle
=
1269 lttng_payload_view_pop_fd_handle(&view
);
1275 fd_to_send
= fd_handle_get_fd(handle
);
1276 fd_handle_put(handle
);
1279 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1280 fd_count
? &fd_to_send
: NULL
, fd_count
,
1281 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1282 view
.buffer
.size
, NULL
, NULL
, 0);
1286 if (filter_expression
&& ctx
) {
1287 filter_bytecode_free(ctx
);
1288 filter_ir_free(ctx
);
1289 filter_parser_ctx_free(ctx
);
1292 if (free_filter_expression
) {
1294 * The filter expression has been replaced and must be freed as
1295 * it is not the original filter expression received as a
1298 free(filter_expression
);
1302 * Return directly to the caller and don't ask the sessiond since
1303 * something went wrong in the parsing of data above.
1305 lttng_payload_reset(&payload
);
1309 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1313 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1314 struct lttng_event
*ev
, const char *channel_name
,
1315 const char *original_filter_expression
)
1317 struct lttcomm_session_msg lsm
;
1320 unsigned int free_filter_expression
= 0;
1321 struct filter_parser_ctx
*ctx
= NULL
;
1323 * Cast as non-const since we may replace the filter expression
1324 * by a dynamically allocated string. Otherwise, the original
1325 * string is not modified.
1327 char *filter_expression
= (char *) original_filter_expression
;
1329 if (handle
== NULL
|| ev
== NULL
) {
1330 ret
= -LTTNG_ERR_INVALID
;
1335 * Empty filter string will always be rejected by the parser
1336 * anyway, so treat this corner-case early to eliminate
1337 * lttng_fmemopen error for 0-byte allocation.
1339 if (filter_expression
&& filter_expression
[0] == '\0') {
1340 ret
= -LTTNG_ERR_INVALID
;
1344 memset(&lsm
, 0, sizeof(lsm
));
1346 /* If no channel name, send empty string. */
1347 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1348 sizeof(lsm
.u
.disable
.channel_name
));
1350 ret
= -LTTNG_ERR_INVALID
;
1354 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1356 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1357 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1359 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1360 sizeof(lsm
.session
.name
));
1362 ret
= -LTTNG_ERR_INVALID
;
1366 lsm
.u
.disable
.bytecode_len
= 0;
1369 * For the JUL domain, a filter is enforced except for the
1370 * disable all event. This is done to avoid having the event in
1371 * all sessions thus filtering by logger name.
1373 if (filter_expression
== NULL
&&
1374 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1375 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1376 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1381 * We have a filter, so we need to set up a variable-length
1382 * memory block from where to send the data.
1385 /* Parse filter expression */
1386 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1387 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1388 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1389 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1390 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1391 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1394 /* Setup JUL filter if needed. */
1395 agent_filter
= set_agent_filter(filter_expression
, ev
);
1396 if (!agent_filter
) {
1397 if (!filter_expression
) {
1399 * No JUL and no filter, just skip
1406 * With a JUL filter, the original filter has
1407 * been added to it thus replace the filter
1410 filter_expression
= agent_filter
;
1411 free_filter_expression
= 1;
1415 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1420 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1421 + bytecode_get_len(&ctx
->bytecode
->b
);
1422 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1425 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1426 + lsm
.u
.disable
.expression_len
);
1428 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1432 /* Add filter expression. */
1433 if (lsm
.u
.disable
.expression_len
!= 0) {
1436 lsm
.u
.disable
.expression_len
);
1438 /* Add filter bytecode next. */
1439 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1441 + lsm
.u
.disable
.expression_len
,
1443 lsm
.u
.disable
.bytecode_len
);
1446 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1447 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1451 if (filter_expression
&& ctx
) {
1452 filter_bytecode_free(ctx
);
1453 filter_ir_free(ctx
);
1454 filter_parser_ctx_free(ctx
);
1457 if (free_filter_expression
) {
1459 * The filter expression has been replaced and must be freed as
1460 * it is not the original filter expression received as a
1463 free(filter_expression
);
1467 * Return directly to the caller and don't ask the sessiond since
1468 * something went wrong in the parsing of data above.
1473 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1478 * Disable event(s) of a channel and domain.
1479 * If no event name is specified, all events are disabled.
1480 * If no channel name is specified, the default 'channel0' is used.
1481 * Returns size of returned session payload data or a negative error code.
1483 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1484 const char *channel_name
)
1487 struct lttng_event ev
;
1489 memset(&ev
, 0, sizeof(ev
));
1491 ev
.type
= LTTNG_EVENT_ALL
;
1492 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1494 ret
= -LTTNG_ERR_INVALID
;
1498 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1503 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1505 struct lttng_channel
*channel
= NULL
;
1506 struct lttng_channel_extended
*extended
= NULL
;
1512 /* Validate domain. */
1513 switch (domain
->type
) {
1514 case LTTNG_DOMAIN_UST
:
1515 switch (domain
->buf_type
) {
1516 case LTTNG_BUFFER_PER_UID
:
1517 case LTTNG_BUFFER_PER_PID
:
1523 case LTTNG_DOMAIN_KERNEL
:
1524 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1532 channel
= zmalloc(sizeof(*channel
));
1537 extended
= zmalloc(sizeof(*extended
));
1542 channel
->attr
.extended
.ptr
= extended
;
1544 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1552 void lttng_channel_destroy(struct lttng_channel
*channel
)
1558 if (channel
->attr
.extended
.ptr
) {
1559 free(channel
->attr
.extended
.ptr
);
1565 * Enable channel per domain
1566 * Returns size of returned session payload data or a negative error code.
1568 int lttng_enable_channel(struct lttng_handle
*handle
,
1569 struct lttng_channel
*in_chan
)
1572 struct lttcomm_session_msg lsm
;
1573 size_t total_buffer_size_needed_per_cpu
= 0;
1575 /* NULL arguments are forbidden. No default values. */
1576 if (handle
== NULL
|| in_chan
== NULL
) {
1577 return -LTTNG_ERR_INVALID
;
1580 memset(&lsm
, 0, sizeof(lsm
));
1581 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1582 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1584 if (!in_chan
->attr
.extended
.ptr
) {
1585 struct lttng_channel
*channel
;
1586 struct lttng_channel_extended
*extended
;
1588 channel
= lttng_channel_create(&handle
->domain
);
1590 return -LTTNG_ERR_NOMEM
;
1594 * Create a new channel in order to use default extended
1597 extended
= (struct lttng_channel_extended
*)
1598 channel
->attr
.extended
.ptr
;
1599 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1600 lttng_channel_destroy(channel
);
1602 struct lttng_channel_extended
*extended
;
1604 extended
= (struct lttng_channel_extended
*)
1605 in_chan
->attr
.extended
.ptr
;
1606 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1610 * Verify that the amount of memory required to create the requested
1611 * buffer is available on the system at the moment.
1613 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1614 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1615 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1616 return -LTTNG_ERR_NOMEM
;
1619 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1620 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1622 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1623 sizeof(lsm
.session
.name
));
1625 ret
= -LTTNG_ERR_INVALID
;
1629 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1635 * All tracing will be stopped for registered events of the channel.
1636 * Returns size of returned session payload data or a negative error code.
1638 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1641 struct lttcomm_session_msg lsm
;
1643 /* Safety check. Both are mandatory. */
1644 if (handle
== NULL
|| name
== NULL
) {
1645 return -LTTNG_ERR_INVALID
;
1648 memset(&lsm
, 0, sizeof(lsm
));
1650 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1652 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1653 sizeof(lsm
.u
.disable
.channel_name
));
1655 ret
= -LTTNG_ERR_INVALID
;
1659 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1661 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1662 sizeof(lsm
.session
.name
));
1664 ret
= -LTTNG_ERR_INVALID
;
1668 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1674 * Lists all available tracepoints of domain.
1675 * Sets the contents of the events array.
1676 * Returns the number of lttng_event entries in events;
1677 * on error, returns a negative value.
1679 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1680 struct lttng_event
**events
)
1683 struct lttcomm_session_msg lsm
;
1685 if (handle
== NULL
) {
1686 return -LTTNG_ERR_INVALID
;
1689 memset(&lsm
, 0, sizeof(lsm
));
1690 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1691 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1693 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1698 return ret
/ sizeof(struct lttng_event
);
1702 * Lists all available tracepoint fields of domain.
1703 * Sets the contents of the event field array.
1704 * Returns the number of lttng_event_field entries in events;
1705 * on error, returns a negative value.
1707 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1708 struct lttng_event_field
**fields
)
1711 struct lttcomm_session_msg lsm
;
1713 if (handle
== NULL
) {
1714 return -LTTNG_ERR_INVALID
;
1717 memset(&lsm
, 0, sizeof(lsm
));
1718 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1719 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1721 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1726 return ret
/ sizeof(struct lttng_event_field
);
1730 * Lists all available kernel system calls. Allocates and sets the contents of
1733 * Returns the number of lttng_event entries in events; on error, returns a
1736 int lttng_list_syscalls(struct lttng_event
**events
)
1739 struct lttcomm_session_msg lsm
;
1742 return -LTTNG_ERR_INVALID
;
1745 memset(&lsm
, 0, sizeof(lsm
));
1746 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1747 /* Force kernel domain for system calls. */
1748 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1750 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1755 return ret
/ sizeof(struct lttng_event
);
1759 * Returns a human readable string describing
1760 * the error code (a negative value).
1762 const char *lttng_strerror(int code
)
1764 return error_get_str(code
);
1767 enum lttng_error_code
lttng_create_session_ext(
1768 struct lttng_session_descriptor
*session_descriptor
)
1770 enum lttng_error_code ret_code
;
1771 struct lttcomm_session_msg lsm
= {
1772 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1775 struct lttng_buffer_view reply_view
;
1777 bool sessiond_must_generate_ouput
;
1778 struct lttng_dynamic_buffer payload
;
1780 size_t descriptor_size
;
1781 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1783 lttng_dynamic_buffer_init(&payload
);
1784 if (!session_descriptor
) {
1785 ret_code
= LTTNG_ERR_INVALID
;
1789 sessiond_must_generate_ouput
=
1790 !lttng_session_descriptor_is_output_destination_initialized(
1791 session_descriptor
);
1792 if (sessiond_must_generate_ouput
) {
1793 const char *home_dir
= utils_get_home_dir();
1794 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1796 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1797 ret_code
= LTTNG_ERR_FATAL
;
1801 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1802 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1805 ret_code
= LTTNG_ERR_NOMEM
;
1810 descriptor_size
= payload
.size
;
1811 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1814 ret_code
= LTTNG_ERR_INVALID
;
1817 descriptor_size
= payload
.size
- descriptor_size
;
1818 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1820 /* Command returns a session descriptor on success. */
1821 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1822 payload
.size
, &reply
);
1823 if (reply_ret
< 0) {
1824 ret_code
= -reply_ret
;
1826 } else if (reply_ret
== 0) {
1827 /* Socket unexpectedly closed by the session daemon. */
1828 ret_code
= LTTNG_ERR_FATAL
;
1832 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1833 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1836 ret_code
= LTTNG_ERR_FATAL
;
1839 ret_code
= LTTNG_OK
;
1840 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1843 lttng_dynamic_buffer_reset(&payload
);
1844 lttng_session_descriptor_destroy(descriptor_reply
);
1849 * Create a new session using name and url for destination.
1851 * Return 0 on success else a negative LTTng error code.
1853 int lttng_create_session(const char *name
, const char *url
)
1857 struct lttng_uri
*uris
= NULL
;
1858 struct lttng_session_descriptor
*descriptor
= NULL
;
1859 enum lttng_error_code ret_code
;
1862 ret
= -LTTNG_ERR_INVALID
;
1866 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1868 ret
= -LTTNG_ERR_INVALID
;
1873 descriptor
= lttng_session_descriptor_create(name
);
1876 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1877 ret
= -LTTNG_ERR_INVALID
;
1880 descriptor
= lttng_session_descriptor_local_create(name
,
1884 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1888 ret
= -LTTNG_ERR_INVALID
;
1892 ret
= -LTTNG_ERR_INVALID
;
1895 ret_code
= lttng_create_session_ext(descriptor
);
1896 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1898 lttng_session_descriptor_destroy(descriptor
);
1904 * Create a session exclusively used for snapshot.
1906 * Return 0 on success else a negative LTTng error code.
1908 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1911 enum lttng_error_code ret_code
;
1913 struct lttng_uri
*uris
= NULL
;
1914 struct lttng_session_descriptor
*descriptor
= NULL
;
1917 ret
= -LTTNG_ERR_INVALID
;
1921 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1923 ret
= -LTTNG_ERR_INVALID
;
1927 * If the user does not specify a custom subdir, use the session name.
1929 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1930 strlen(uris
[0].subdir
) == 0) {
1931 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1934 PERROR("Failed to set session name as network destination sub-directory");
1935 ret
= -LTTNG_ERR_FATAL
;
1937 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1938 /* Truncated output. */
1939 ret
= -LTTNG_ERR_INVALID
;
1946 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1949 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1950 ret
= -LTTNG_ERR_INVALID
;
1953 descriptor
= lttng_session_descriptor_snapshot_local_create(
1958 descriptor
= lttng_session_descriptor_snapshot_network_create(
1964 ret
= -LTTNG_ERR_INVALID
;
1968 ret
= -LTTNG_ERR_INVALID
;
1971 ret_code
= lttng_create_session_ext(descriptor
);
1972 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1974 lttng_session_descriptor_destroy(descriptor
);
1980 * Create a session exclusively used for live.
1982 * Return 0 on success else a negative LTTng error code.
1984 int lttng_create_session_live(const char *name
, const char *url
,
1985 unsigned int timer_interval
)
1988 enum lttng_error_code ret_code
;
1989 struct lttng_session_descriptor
*descriptor
= NULL
;
1992 ret
= -LTTNG_ERR_INVALID
;
1997 descriptor
= lttng_session_descriptor_live_network_create(
1998 name
, url
, NULL
, timer_interval
);
2000 descriptor
= lttng_session_descriptor_live_create(
2001 name
, timer_interval
);
2004 ret
= -LTTNG_ERR_INVALID
;
2007 ret_code
= lttng_create_session_ext(descriptor
);
2008 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2010 lttng_session_descriptor_destroy(descriptor
);
2015 * Stop the session and wait for the data before destroying it
2017 * Return 0 on success else a negative LTTng error code.
2019 int lttng_destroy_session(const char *session_name
)
2022 enum lttng_error_code ret_code
;
2023 enum lttng_destruction_handle_status status
;
2024 struct lttng_destruction_handle
*handle
= NULL
;
2027 * Stop the tracing and wait for the data to be
2030 ret
= _lttng_stop_tracing(session_name
, 1);
2031 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2035 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2036 if (ret_code
!= LTTNG_OK
) {
2037 ret
= (int) -ret_code
;
2042 /* Block until the completion of the destruction of the session. */
2043 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2044 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2045 ret
= -LTTNG_ERR_UNK
;
2049 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2050 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2051 ret
= -LTTNG_ERR_UNK
;
2054 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2056 lttng_destruction_handle_destroy(handle
);
2061 * Destroy the session without waiting for the data.
2063 int lttng_destroy_session_no_wait(const char *session_name
)
2065 enum lttng_error_code ret_code
;
2067 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2068 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2072 * Ask the session daemon for all available sessions.
2073 * Sets the contents of the sessions array.
2074 * Returns the number of lttng_session entries in sessions;
2075 * on error, returns a negative value.
2077 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2080 struct lttcomm_session_msg lsm
;
2081 const size_t session_size
= sizeof(struct lttng_session
) +
2082 sizeof(struct lttng_session_extended
);
2083 size_t session_count
, i
;
2084 struct lttng_session_extended
*sessions_extended_begin
;
2085 struct lttng_session
*sessions
= NULL
;
2087 memset(&lsm
, 0, sizeof(lsm
));
2088 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2089 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2094 ret
= -LTTNG_ERR_FATAL
;
2098 if (ret
% session_size
) {
2099 ret
= -LTTNG_ERR_UNK
;
2101 *out_sessions
= NULL
;
2104 session_count
= (size_t) ret
/ session_size
;
2105 sessions_extended_begin
= (struct lttng_session_extended
*)
2106 (&sessions
[session_count
]);
2108 /* Set extended session info pointers. */
2109 for (i
= 0; i
< session_count
; i
++) {
2110 struct lttng_session
*session
= &sessions
[i
];
2111 struct lttng_session_extended
*extended
=
2112 &(sessions_extended_begin
[i
]);
2114 session
->extended
.ptr
= extended
;
2117 ret
= (int) session_count
;
2118 *out_sessions
= sessions
;
2123 enum lttng_error_code
lttng_session_get_creation_time(
2124 const struct lttng_session
*session
, uint64_t *creation_time
)
2126 enum lttng_error_code ret
= LTTNG_OK
;
2127 struct lttng_session_extended
*extended
;
2129 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2130 ret
= LTTNG_ERR_INVALID
;
2134 extended
= session
->extended
.ptr
;
2135 if (!extended
->creation_time
.is_set
) {
2136 /* Not created on the session daemon yet. */
2137 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2140 *creation_time
= extended
->creation_time
.value
;
2145 int lttng_set_session_shm_path(const char *session_name
,
2146 const char *shm_path
)
2149 struct lttcomm_session_msg lsm
;
2151 if (session_name
== NULL
) {
2152 return -LTTNG_ERR_INVALID
;
2155 memset(&lsm
, 0, sizeof(lsm
));
2156 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2158 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2159 sizeof(lsm
.session
.name
));
2161 ret
= -LTTNG_ERR_INVALID
;
2165 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2166 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2168 ret
= -LTTNG_ERR_INVALID
;
2172 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2178 * Ask the session daemon for all available domains of a session.
2179 * Sets the contents of the domains array.
2180 * Returns the number of lttng_domain entries in domains;
2181 * on error, returns a negative value.
2183 int lttng_list_domains(const char *session_name
,
2184 struct lttng_domain
**domains
)
2187 struct lttcomm_session_msg lsm
;
2189 if (session_name
== NULL
) {
2190 ret
= -LTTNG_ERR_INVALID
;
2194 memset(&lsm
, 0, sizeof(lsm
));
2195 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2197 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2198 sizeof(lsm
.session
.name
));
2200 ret
= -LTTNG_ERR_INVALID
;
2204 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2209 return ret
/ sizeof(struct lttng_domain
);
2215 * Ask the session daemon for all available channels of a session.
2216 * Sets the contents of the channels array.
2217 * Returns the number of lttng_channel entries in channels;
2218 * on error, returns a negative value.
2220 int lttng_list_channels(struct lttng_handle
*handle
,
2221 struct lttng_channel
**channels
)
2224 size_t channel_count
, i
;
2225 const size_t channel_size
= sizeof(struct lttng_channel
) +
2226 sizeof(struct lttng_channel_extended
);
2227 struct lttcomm_session_msg lsm
;
2230 if (handle
== NULL
) {
2231 ret
= -LTTNG_ERR_INVALID
;
2235 memset(&lsm
, 0, sizeof(lsm
));
2236 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2237 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2238 sizeof(lsm
.session
.name
));
2240 ret
= -LTTNG_ERR_INVALID
;
2244 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2246 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2251 if (ret
% channel_size
) {
2252 ret
= -LTTNG_ERR_UNK
;
2257 channel_count
= (size_t) ret
/ channel_size
;
2259 /* Set extended info pointers */
2260 extended_at
= ((void *) *channels
) +
2261 channel_count
* sizeof(struct lttng_channel
);
2262 for (i
= 0; i
< channel_count
; i
++) {
2263 struct lttng_channel
*chan
= &(*channels
)[i
];
2265 chan
->attr
.extended
.ptr
= extended_at
;
2266 extended_at
+= sizeof(struct lttng_channel_extended
);
2269 ret
= (int) channel_count
;
2275 * Ask the session daemon for all available events of a session channel.
2276 * Sets the contents of the events array.
2277 * Returns the number of lttng_event entries in events;
2278 * on error, returns a negative value.
2280 int lttng_list_events(struct lttng_handle
*handle
,
2281 const char *channel_name
, struct lttng_event
**events
)
2284 struct lttcomm_session_msg lsm
= {};
2285 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2286 uint32_t nb_events
, i
;
2287 const void *comm_ext_at
;
2288 struct lttng_dynamic_buffer listing
;
2290 struct lttng_payload payload
;
2291 struct lttng_payload payload_copy
;
2292 struct lttng_payload_view lsm_view
=
2293 lttng_payload_view_init_from_buffer(
2294 (const char *) &lsm
, 0, sizeof(lsm
));
2295 struct lttng_buffer_view cmd_header_view
;
2296 struct lttng_buffer_view cmd_payload_view
;
2297 struct lttng_buffer_view flat_events_view
;
2298 struct lttng_buffer_view ext_view
;
2300 /* Safety check. An handle and channel name are mandatory */
2301 if (handle
== NULL
|| channel_name
== NULL
) {
2302 ret
= -LTTNG_ERR_INVALID
;
2306 lttng_payload_init(&payload
);
2307 lttng_payload_init(&payload_copy
);
2309 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2310 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2311 sizeof(lsm
.session
.name
));
2313 ret
= -LTTNG_ERR_INVALID
;
2317 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2318 sizeof(lsm
.u
.list
.channel_name
));
2320 ret
= -LTTNG_ERR_INVALID
;
2324 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2326 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2332 * A copy of the payload is performed since it will be
2333 * consumed twice. Consuming the same payload twice is invalid since
2334 * it will cause any received file descriptor to become "shared"
2335 * between different instances of the resulting objects.
2337 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2339 ret
= -LTTNG_ERR_NOMEM
;
2343 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2344 &payload
.buffer
, 0, sizeof(*cmd_header
));
2345 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2346 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2350 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2352 /* Set number of events and free command header */
2353 nb_events
= cmd_header
->nb_events
;
2354 if (nb_events
> INT_MAX
) {
2355 ret
= -LTTNG_ERR_OVERFLOW
;
2359 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2360 &payload
.buffer
, sizeof(*cmd_header
), -1);
2363 * The buffer that is returned must contain a "flat" version of
2364 * the events that are returned. In other words, all pointers
2365 * within an lttng_event must point to a location within the returned
2366 * buffer so that the user may free everything by simply calling free()
2367 * on the returned buffer. This is needed in order to maintain API
2370 * A first pass is performed to compute the size of the buffer that
2371 * must be allocated. A second pass is then performed to setup
2372 * the returned events so that their members always point within the
2375 * The layout of the returned buffer is as follows:
2376 * - struct lttng_event[nb_events],
2377 * - nb_events times the following:
2378 * - struct lttng_event_extended,
2379 * - flattened version of userspace_probe_location
2380 * - filter_expression
2382 * - padding to align to 64-bits
2384 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2385 nb_events
* sizeof(struct lttng_event
), -1);
2386 comm_ext_at
= ext_view
.data
;
2387 storage_req
= nb_events
* sizeof(struct lttng_event
);
2389 struct lttng_payload_view payload_view
=
2390 lttng_payload_view_from_payload(&payload
, 0, -1);
2392 for (i
= 0; i
< nb_events
; i
++) {
2393 const struct lttcomm_event_extended_header
*ext_comm
=
2394 (struct lttcomm_event_extended_header
*)
2396 int probe_storage_req
= 0;
2398 comm_ext_at
+= sizeof(*ext_comm
);
2399 comm_ext_at
+= ext_comm
->filter_len
;
2400 comm_ext_at
+= ext_comm
->nb_exclusions
*
2401 LTTNG_SYMBOL_NAME_LEN
;
2403 if (ext_comm
->userspace_probe_location_len
) {
2404 struct lttng_userspace_probe_location
2405 *probe_location
= NULL
;
2406 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2408 (const char *) comm_ext_at
-
2409 payload_view
.buffer
.data
,
2410 ext_comm
->userspace_probe_location_len
);
2412 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2413 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2418 * Create a temporary userspace probe location
2419 * to determine the size needed by a "flattened"
2420 * version of that same probe location.
2422 ret
= lttng_userspace_probe_location_create_from_payload(
2423 &probe_location_view
,
2426 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2430 ret
= lttng_userspace_probe_location_flatten(
2431 probe_location
, NULL
);
2432 lttng_userspace_probe_location_destroy(
2435 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2439 probe_storage_req
= ret
;
2440 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2443 storage_req
+= sizeof(struct lttng_event_extended
);
2444 storage_req
+= ext_comm
->filter_len
;
2445 storage_req
+= ext_comm
->nb_exclusions
*
2446 LTTNG_SYMBOL_NAME_LEN
;
2447 /* Padding to ensure the flat probe is aligned. */
2448 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2449 storage_req
+= probe_storage_req
;
2453 lttng_dynamic_buffer_init(&listing
);
2455 * We must ensure that "listing" is never resized so as to preserve
2456 * the validity of the flattened objects.
2458 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2460 ret
= -LTTNG_ERR_NOMEM
;
2464 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2465 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2466 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2467 nb_events
* sizeof(struct lttng_event
));
2468 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2470 ret
= -LTTNG_ERR_NOMEM
;
2471 goto free_dynamic_buffer
;
2474 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2475 nb_events
* sizeof(struct lttng_event
), -1);
2476 comm_ext_at
= ext_view
.data
;
2479 struct lttng_payload_view payload_copy_view
=
2480 lttng_payload_view_from_payload(
2481 &payload_copy
, 0, -1);
2483 for (i
= 0; i
< nb_events
; i
++) {
2484 struct lttng_event
*event
= (typeof(event
))(
2486 (sizeof(struct lttng_event
) * i
));
2487 const struct lttcomm_event_extended_header
*ext_comm
=
2488 (typeof(ext_comm
)) comm_ext_at
;
2489 struct lttng_event_extended
*event_extended
=
2490 (typeof(event_extended
))(listing
.data
+
2493 /* Insert struct lttng_event_extended. */
2494 ret
= lttng_dynamic_buffer_set_size(&listing
,
2495 listing
.size
+ sizeof(*event_extended
));
2497 ret
= -LTTNG_ERR_NOMEM
;
2498 goto free_dynamic_buffer
;
2500 event
->extended
.ptr
= event_extended
;
2502 comm_ext_at
+= sizeof(*ext_comm
);
2504 /* Insert filter expression. */
2505 if (ext_comm
->filter_len
) {
2506 event_extended
->filter_expression
=
2507 listing
.data
+ listing
.size
;
2508 ret
= lttng_dynamic_buffer_append(&listing
,
2510 ext_comm
->filter_len
);
2512 ret
= -LTTNG_ERR_NOMEM
;
2513 goto free_dynamic_buffer
;
2515 comm_ext_at
+= ext_comm
->filter_len
;
2518 /* Insert exclusions. */
2519 if (ext_comm
->nb_exclusions
) {
2520 event_extended
->exclusions
.count
=
2521 ext_comm
->nb_exclusions
;
2522 event_extended
->exclusions
.strings
=
2523 listing
.data
+ listing
.size
;
2525 ret
= lttng_dynamic_buffer_append(&listing
,
2527 ext_comm
->nb_exclusions
*
2528 LTTNG_SYMBOL_NAME_LEN
);
2530 ret
= -LTTNG_ERR_NOMEM
;
2531 goto free_dynamic_buffer
;
2533 comm_ext_at
+= ext_comm
->nb_exclusions
*
2534 LTTNG_SYMBOL_NAME_LEN
;
2537 /* Insert padding to align to 64-bits. */
2538 ret
= lttng_dynamic_buffer_set_size(&listing
,
2539 ALIGN_TO(listing
.size
,
2542 ret
= -LTTNG_ERR_NOMEM
;
2543 goto free_dynamic_buffer
;
2546 /* Insert flattened userspace probe location. */
2547 if (ext_comm
->userspace_probe_location_len
) {
2548 struct lttng_userspace_probe_location
2549 *probe_location
= NULL
;
2550 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2552 (const char *) comm_ext_at
-
2553 payload_copy_view
.buffer
.data
,
2554 ext_comm
->userspace_probe_location_len
);
2556 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2557 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2558 goto free_dynamic_buffer
;
2561 ret
= lttng_userspace_probe_location_create_from_payload(
2562 &probe_location_view
,
2565 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2566 goto free_dynamic_buffer
;
2569 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2572 ret
= lttng_userspace_probe_location_flatten(
2573 probe_location
, &listing
);
2574 lttng_userspace_probe_location_destroy(
2577 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2578 goto free_dynamic_buffer
;
2581 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2586 /* Don't reset listing buffer as we return its content. */
2587 *events
= (struct lttng_event
*) listing
.data
;
2588 lttng_dynamic_buffer_init(&listing
);
2589 ret
= (int) nb_events
;
2590 free_dynamic_buffer
:
2591 lttng_dynamic_buffer_reset(&listing
);
2593 lttng_payload_reset(&payload
);
2594 lttng_payload_reset(&payload_copy
);
2599 * Sets the tracing_group variable with name.
2600 * This function allocates memory pointed to by tracing_group.
2601 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2603 int lttng_set_tracing_group(const char *name
)
2606 return -LTTNG_ERR_INVALID
;
2609 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2610 return -LTTNG_ERR_FATAL
;
2616 int lttng_calibrate(struct lttng_handle
*handle
,
2617 struct lttng_calibrate
*calibrate
)
2620 * This command was removed in LTTng 2.9.
2622 return -LTTNG_ERR_UND
;
2626 * Set default channel attributes.
2627 * If either or both of the arguments are null, attr content is zeroe'd.
2629 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2630 struct lttng_channel_attr
*attr
)
2632 struct lttng_channel_extended
*extended
;
2635 if (attr
== NULL
|| domain
== NULL
) {
2639 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2640 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2642 /* Same for all domains. */
2643 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2644 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2645 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2647 switch (domain
->type
) {
2648 case LTTNG_DOMAIN_KERNEL
:
2649 attr
->switch_timer_interval
=
2650 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2651 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2652 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2653 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2654 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2656 extended
->monitor_timer_interval
=
2657 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2658 extended
->blocking_timeout
=
2659 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2662 case LTTNG_DOMAIN_UST
:
2663 switch (domain
->buf_type
) {
2664 case LTTNG_BUFFER_PER_UID
:
2665 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2666 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2667 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2668 attr
->switch_timer_interval
=
2669 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2670 attr
->read_timer_interval
=
2671 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2673 extended
->monitor_timer_interval
=
2674 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2675 extended
->blocking_timeout
=
2676 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2679 case LTTNG_BUFFER_PER_PID
:
2681 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2682 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2683 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2684 attr
->switch_timer_interval
=
2685 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2686 attr
->read_timer_interval
=
2687 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2689 extended
->monitor_timer_interval
=
2690 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2691 extended
->blocking_timeout
=
2692 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2697 /* Default behavior: leave set to 0. */
2701 attr
->extended
.ptr
= extended
;
2704 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2705 uint64_t *discarded_events
)
2708 struct lttng_channel_extended
*chan_ext
;
2710 if (!channel
|| !discarded_events
) {
2711 ret
= -LTTNG_ERR_INVALID
;
2715 chan_ext
= channel
->attr
.extended
.ptr
;
2718 * This can happen since the lttng_channel structure is
2719 * used for other tasks where this pointer is never set.
2721 *discarded_events
= 0;
2725 *discarded_events
= chan_ext
->discarded_events
;
2730 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2731 uint64_t *lost_packets
)
2734 struct lttng_channel_extended
*chan_ext
;
2736 if (!channel
|| !lost_packets
) {
2737 ret
= -LTTNG_ERR_INVALID
;
2741 chan_ext
= channel
->attr
.extended
.ptr
;
2744 * This can happen since the lttng_channel structure is
2745 * used for other tasks where this pointer is never set.
2751 *lost_packets
= chan_ext
->lost_packets
;
2756 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2757 uint64_t *monitor_timer_interval
)
2761 if (!chan
|| !monitor_timer_interval
) {
2762 ret
= -LTTNG_ERR_INVALID
;
2766 if (!chan
->attr
.extended
.ptr
) {
2767 ret
= -LTTNG_ERR_INVALID
;
2771 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2772 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2777 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2778 uint64_t monitor_timer_interval
)
2782 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2783 ret
= -LTTNG_ERR_INVALID
;
2787 ((struct lttng_channel_extended
*)
2788 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2789 monitor_timer_interval
;
2794 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2795 int64_t *blocking_timeout
)
2799 if (!chan
|| !blocking_timeout
) {
2800 ret
= -LTTNG_ERR_INVALID
;
2804 if (!chan
->attr
.extended
.ptr
) {
2805 ret
= -LTTNG_ERR_INVALID
;
2809 *blocking_timeout
= ((struct lttng_channel_extended
*)
2810 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2815 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2816 int64_t blocking_timeout
)
2819 int64_t msec_timeout
;
2821 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2822 ret
= -LTTNG_ERR_INVALID
;
2826 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2827 ret
= -LTTNG_ERR_INVALID
;
2832 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2833 * us to accept a narrower range of values (msecs expressed as a signed
2836 msec_timeout
= blocking_timeout
/ 1000;
2837 if (msec_timeout
!= (int32_t) msec_timeout
) {
2838 ret
= -LTTNG_ERR_INVALID
;
2842 ((struct lttng_channel_extended
*)
2843 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2850 * Check if session daemon is alive.
2852 * Return 1 if alive or 0 if not.
2853 * On error returns a negative value.
2855 int lttng_session_daemon_alive(void)
2859 ret
= set_session_daemon_path();
2865 if (*sessiond_sock_path
== '\0') {
2867 * No socket path set. Weird error which means the constructor
2873 ret
= try_connect_sessiond(sessiond_sock_path
);
2884 * Set URL for a consumer for a session and domain.
2886 * Return 0 on success, else a negative value.
2888 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2889 const char *control_url
, const char *data_url
)
2893 struct lttcomm_session_msg lsm
;
2894 struct lttng_uri
*uris
= NULL
;
2896 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2897 ret
= -LTTNG_ERR_INVALID
;
2901 memset(&lsm
, 0, sizeof(lsm
));
2903 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2905 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2906 sizeof(lsm
.session
.name
));
2908 ret
= -LTTNG_ERR_INVALID
;
2912 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2914 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2916 ret
= -LTTNG_ERR_INVALID
;
2920 lsm
.u
.uri
.size
= size
;
2922 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2923 sizeof(struct lttng_uri
) * size
, NULL
);
2933 int lttng_enable_consumer(struct lttng_handle
*handle
);
2934 int lttng_enable_consumer(struct lttng_handle
*handle
)
2942 int lttng_disable_consumer(struct lttng_handle
*handle
);
2943 int lttng_disable_consumer(struct lttng_handle
*handle
)
2951 int _lttng_create_session_ext(const char *name
, const char *url
,
2952 const char *datetime
);
2953 int _lttng_create_session_ext(const char *name
, const char *url
,
2954 const char *datetime
)
2960 * For a given session name, this call checks if the data is ready to be read
2961 * or is still being extracted by the consumer(s) hence not ready to be used by
2964 int lttng_data_pending(const char *session_name
)
2967 struct lttcomm_session_msg lsm
;
2968 uint8_t *pending
= NULL
;
2970 if (session_name
== NULL
) {
2971 return -LTTNG_ERR_INVALID
;
2974 memset(&lsm
, 0, sizeof(lsm
));
2975 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2977 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2978 sizeof(lsm
.session
.name
));
2980 ret
= -LTTNG_ERR_INVALID
;
2984 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2987 } else if (ret
!= 1) {
2988 /* Unexpected payload size */
2989 ret
= -LTTNG_ERR_INVALID
;
2991 } else if (!pending
) {
2992 /* Internal error. */
2993 ret
= -LTTNG_ERR_UNK
;
2997 ret
= (int) *pending
;
3004 * Regenerate the metadata for a session.
3005 * Return 0 on success, a negative error code on error.
3007 int lttng_regenerate_metadata(const char *session_name
)
3010 struct lttcomm_session_msg lsm
;
3012 if (!session_name
) {
3013 ret
= -LTTNG_ERR_INVALID
;
3017 memset(&lsm
, 0, sizeof(lsm
));
3018 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3020 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3021 sizeof(lsm
.session
.name
));
3023 ret
= -LTTNG_ERR_INVALID
;
3027 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3038 * Deprecated, replaced by lttng_regenerate_metadata.
3040 int lttng_metadata_regenerate(const char *session_name
)
3042 return lttng_regenerate_metadata(session_name
);
3046 * Regenerate the statedump of a session.
3047 * Return 0 on success, a negative error code on error.
3049 int lttng_regenerate_statedump(const char *session_name
)
3052 struct lttcomm_session_msg lsm
;
3054 if (!session_name
) {
3055 ret
= -LTTNG_ERR_INVALID
;
3059 memset(&lsm
, 0, sizeof(lsm
));
3060 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3062 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3063 sizeof(lsm
.session
.name
));
3065 ret
= -LTTNG_ERR_INVALID
;
3069 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3079 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3082 struct lttcomm_session_msg lsm
= {
3083 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3085 struct lttcomm_session_msg
*message_lsm
;
3086 struct lttng_payload message
;
3087 struct lttng_payload reply
;
3088 struct lttng_trigger
*reply_trigger
= NULL
;
3089 enum lttng_domain_type domain_type
;
3090 const struct lttng_credentials user_creds
= {
3091 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3092 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3096 lttng_payload_init(&message
);
3097 lttng_payload_init(&reply
);
3100 ret
= -LTTNG_ERR_INVALID
;
3104 if (!trigger
->creds
.uid
.is_set
) {
3105 /* Use the client's credentials as the trigger credentials. */
3106 lttng_trigger_set_credentials(trigger
, &user_creds
);
3109 * Validate that either the current trigger credentials and the
3110 * client credentials are identical or that the current user is
3111 * root. The root user can register, unregister triggers for
3112 * himself and other users.
3114 * This check is also present on the sessiond side, using the
3115 * credentials passed on the socket. These check are all
3118 const struct lttng_credentials
*trigger_creds
=
3119 lttng_trigger_get_credentials(trigger
);
3121 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3122 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3123 ret
= -LTTNG_ERR_EPERM
;
3129 if (!lttng_trigger_validate(trigger
)) {
3130 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3134 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3137 lsm
.domain
.type
= domain_type
;
3139 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3141 ret
= -LTTNG_ERR_NOMEM
;
3146 * This is needed to populate the trigger object size for the command
3149 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3151 ret
= lttng_trigger_serialize(trigger
, &message
);
3153 ret
= -LTTNG_ERR_UNK
;
3157 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3160 struct lttng_payload_view message_view
=
3161 lttng_payload_view_from_payload(
3164 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3166 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3173 struct lttng_payload_view reply_view
=
3174 lttng_payload_view_from_payload(
3175 &reply
, 0, reply
.buffer
.size
);
3177 ret
= lttng_trigger_create_from_payload(
3178 &reply_view
, &reply_trigger
);
3180 ret
= -LTTNG_ERR_FATAL
;
3185 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3187 ret
= -LTTNG_ERR_FATAL
;
3193 lttng_payload_reset(&message
);
3194 lttng_payload_reset(&reply
);
3195 lttng_trigger_destroy(reply_trigger
);
3199 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3202 struct lttcomm_session_msg lsm
;
3203 struct lttcomm_session_msg
*message_lsm
;
3204 struct lttng_payload message
;
3205 struct lttng_payload reply
;
3206 const struct lttng_credentials user_creds
= {
3207 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3208 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3211 lttng_payload_init(&message
);
3212 lttng_payload_init(&reply
);
3215 ret
= -LTTNG_ERR_INVALID
;
3219 if (!trigger
->creds
.uid
.is_set
) {
3220 /* Use the client's credentials as the trigger credentials. */
3221 lttng_trigger_set_credentials(trigger
, &user_creds
);
3224 * Validate that either the current trigger credentials and the
3225 * client credentials are identical or that the current user is
3226 * root. The root user can register, unregister triggers for
3227 * himself and other users.
3229 * This check is also present on the sessiond side, using the
3230 * credentials passed on the socket. These check are all
3233 const struct lttng_credentials
*trigger_creds
=
3234 lttng_trigger_get_credentials(trigger
);
3236 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3237 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3238 ret
= -LTTNG_ERR_EPERM
;
3244 if (!lttng_trigger_validate(trigger
)) {
3245 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3249 memset(&lsm
, 0, sizeof(lsm
));
3250 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3252 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3254 ret
= -LTTNG_ERR_NOMEM
;
3259 * This is needed to populate the trigger object size for the command
3260 * header and number of fds sent.
3262 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3264 ret
= lttng_trigger_serialize(trigger
, &message
);
3266 ret
= -LTTNG_ERR_UNK
;
3270 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3273 struct lttng_payload_view message_view
=
3274 lttng_payload_view_from_payload(
3278 * Update the message header with the number of fd that will be
3281 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3284 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3292 lttng_payload_reset(&message
);
3293 lttng_payload_reset(&reply
);
3298 * Ask the session daemon for all registered triggers for the current user.
3300 * Allocates and return an lttng_triggers set.
3301 * On error, returns a suitable lttng_error_code.
3303 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3306 enum lttng_error_code ret_code
= LTTNG_OK
;
3307 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3308 struct lttng_triggers
*local_triggers
= NULL
;
3309 struct lttng_payload reply
;
3310 struct lttng_payload_view lsm_view
=
3311 lttng_payload_view_init_from_buffer(
3312 (const char *) &lsm
, 0, sizeof(lsm
));
3314 lttng_payload_init(&reply
);
3316 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3318 ret_code
= (enum lttng_error_code
) -ret
;
3323 struct lttng_payload_view reply_view
=
3324 lttng_payload_view_from_payload(
3325 &reply
, 0, reply
.buffer
.size
);
3327 ret
= lttng_triggers_create_from_payload(
3328 &reply_view
, &local_triggers
);
3330 ret_code
= LTTNG_ERR_FATAL
;
3335 *triggers
= local_triggers
;
3336 local_triggers
= NULL
;
3338 lttng_payload_reset(&reply
);
3339 lttng_triggers_destroy(local_triggers
);
3346 static void __attribute__((constructor
)) init(void)
3348 /* Set default session group */
3349 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3355 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3357 free(tracing_group
);