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
20 #include <common/bytecode/bytecode.h>
21 #include <common/align.h>
22 #include <common/common.h>
23 #include <common/compat/errno.h>
24 #include <common/compat/string.h>
25 #include <common/defaults.h>
26 #include <common/dynamic-array.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/payload-view.h>
29 #include <common/payload.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/error-query-internal.h>
39 #include <lttng/event-internal.h>
40 #include <lttng/health-internal.h>
41 #include <lttng/lttng-error.h>
42 #include <lttng/lttng.h>
43 #include <lttng/session-descriptor-internal.h>
44 #include <lttng/session-internal.h>
45 #include <lttng/trigger/trigger-internal.h>
46 #include <lttng/userspace-probe-internal.h>
48 #include "lttng-ctl-helper.h"
49 #include <common/filter/filter-ast.h>
50 #include <common/filter/filter-parser.hpp>
51 #include <common/filter/memstream.h>
53 #define COPY_DOMAIN_PACKED(dst, src) \
55 struct lttng_domain _tmp_domain; \
57 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
61 /* Socket to session daemon for communication */
62 static int sessiond_socket
= -1;
63 static char sessiond_sock_path
[PATH_MAX
];
66 static char *tracing_group
;
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
76 LTTNG_EXPORT
int lttng_opt_quiet
;
77 LTTNG_EXPORT
int lttng_opt_verbose
;
78 LTTNG_EXPORT
int lttng_opt_mi
;
81 * Copy domain to lttcomm_session_msg domain.
83 * 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((lttcomm_sessiond_command
) 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
)
200 LTTNG_ASSERT(len
> 0);
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.
247 int lttng_check_tracing_group(void)
249 gid_t
*grp_list
, tracing_gid
;
250 int grp_list_size
, grp_id
, i
;
252 const char *grp_name
= tracing_group
;
254 /* Get GID of group 'tracing' */
255 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
256 /* If grp_tracing is NULL, the group does not exist. */
260 /* Get number of supplementary group IDs */
261 grp_list_size
= getgroups(0, NULL
);
262 if (grp_list_size
< 0) {
267 /* Alloc group list of the right size */
268 grp_list
= (gid_t
*) zmalloc(grp_list_size
* sizeof(gid_t
));
273 grp_id
= getgroups(grp_list_size
, grp_list
);
279 for (i
= 0; i
< grp_list_size
; i
++) {
280 if (grp_list
[i
] == tracing_gid
) {
293 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
297 size_t best_mem_info
;
298 size_t num_bytes_requested_total
;
301 * Get the number of CPU currently online to compute the amount of
302 * memory needed to create a buffer for every CPU.
304 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
309 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
312 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
313 * reliable estimate we can get but it is only exposed by the kernel
314 * since 3.14. (See Linux kernel commit:
315 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
317 ret
= utils_get_memory_available(&best_mem_info
);
323 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
324 * is a sanity check for obvious user error.
326 ret
= utils_get_memory_total(&best_mem_info
);
334 return best_mem_info
>= num_bytes_requested_total
;
338 * Try connect to session daemon with sock_path.
340 * Return 0 on success, else -1
342 static int try_connect_sessiond(const char *sock_path
)
346 /* If socket exist, we check if the daemon listens for connect. */
347 ret
= access(sock_path
, F_OK
);
353 ret
= lttcomm_connect_unix_sock(sock_path
);
359 ret
= lttcomm_close_unix_sock(ret
);
361 PERROR("lttcomm_close_unix_sock");
371 * Set sessiond socket path by putting it in the global sessiond_sock_path
374 * Returns 0 on success, negative value on failure (the sessiond socket path
375 * is somehow too long or ENOMEM).
377 static int set_session_daemon_path(void)
379 int in_tgroup
= 0; /* In tracing group. */
385 /* Are we in the tracing group ? */
386 in_tgroup
= lttng_check_tracing_group();
389 if ((uid
== 0) || in_tgroup
== 1) {
390 const int ret
= lttng_strncpy(sessiond_sock_path
,
391 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
392 sizeof(sessiond_sock_path
));
404 ret
= try_connect_sessiond(sessiond_sock_path
);
408 /* Global session daemon not available... */
410 /* ...or not in tracing group (and not root), default */
413 * With GNU C < 2.1, snprintf returns -1 if the target buffer
415 * With GNU C >= 2.1, snprintf returns the required size
416 * (excluding closing null)
418 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
419 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
420 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
432 * Connect to the LTTng session daemon.
434 * On success, return the socket's file descriptor. On error, return -1.
436 int connect_sessiond(void)
440 ret
= set_session_daemon_path();
445 /* Connect to the sesssion daemon. */
446 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
457 static void reset_global_sessiond_connection_state(void)
459 sessiond_socket
= -1;
464 * Clean disconnect from the session daemon.
466 * On success, return 0. On error, return -1.
468 static int disconnect_sessiond(void)
473 ret
= lttcomm_close_unix_sock(sessiond_socket
);
474 reset_global_sessiond_connection_state();
480 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
488 ret
= -LTTNG_ERR_INVALID
;
498 ret
= recv_data_sessiond(buf
, len
);
504 ret
= -LTTNG_ERR_INVALID
;
508 /* Move ownership of command header buffer to user. */
513 /* No command header. */
529 * Ask the session daemon a specific command and put the data into buf.
530 * Takes extra var. len. data and file descriptors as input to send to the
533 * Return size of data (only payload, not header) or a negative error code.
535 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
536 const int *fds
, size_t nb_fd
, const void *vardata
,
537 size_t vardata_len
, void **user_payload_buf
,
538 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
542 struct lttcomm_lttng_msg llm
;
544 ret
= connect_sessiond();
546 ret
= -LTTNG_ERR_NO_SESSIOND
;
549 sessiond_socket
= ret
;
553 ret
= send_session_msg(lsm
);
555 /* Ret value is a valid lttng error code. */
558 /* Send var len data */
559 ret
= send_session_varlen(vardata
, vardata_len
);
561 /* Ret value is a valid lttng error code. */
566 ret
= send_session_fds(fds
, nb_fd
);
568 /* Ret value is a valid lttng error code. */
572 /* Get header from data transmission */
573 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
575 /* Ret value is a valid lttng error code. */
579 /* Check error code if OK */
580 if (llm
.ret_code
!= LTTNG_OK
) {
585 /* Get command header from data transmission */
586 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
587 user_cmd_header_buf
, user_cmd_header_len
);
592 /* Get payload from data transmission */
593 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
602 disconnect_sessiond();
606 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
607 struct lttng_payload
*reply
)
610 struct lttcomm_lttng_msg llm
;
611 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
613 LTTNG_ASSERT(reply
->buffer
.size
== 0);
614 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
616 ret
= connect_sessiond();
618 ret
= -LTTNG_ERR_NO_SESSIOND
;
621 sessiond_socket
= ret
;
625 /* Send command to session daemon */
626 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
627 message
->buffer
.size
);
629 ret
= -LTTNG_ERR_FATAL
;
634 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
637 ret
= -LTTNG_ERR_FATAL
;
642 /* Get header from data transmission */
643 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
645 /* Ret value is a valid lttng error code. */
649 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
651 /* Check error code if OK */
652 if (llm
.ret_code
!= LTTNG_OK
) {
653 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
654 /* Invalid error code received. */
655 ret
= -LTTNG_ERR_UNK
;
662 if (llm
.cmd_header_size
> 0) {
663 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
669 /* Get command header from data transmission */
670 if (llm
.data_size
> 0) {
671 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
677 if (llm
.fd_count
> 0) {
678 ret
= lttcomm_recv_payload_fds_unix_sock(
679 sessiond_socket
, llm
.fd_count
, reply
);
685 /* Don't return the llm header to the caller. */
686 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
687 reply
->buffer
.size
- sizeof(llm
));
688 ret
= lttng_dynamic_buffer_set_size(
689 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
691 /* Can't happen as size is reduced. */
695 ret
= reply
->buffer
.size
;
698 disconnect_sessiond();
703 * Create lttng handle and return pointer.
705 * The returned pointer will be NULL in case of malloc() error.
707 struct lttng_handle
*lttng_create_handle(const char *session_name
,
708 struct lttng_domain
*domain
)
711 struct lttng_handle
*handle
= NULL
;
713 handle
= (lttng_handle
*) zmalloc(sizeof(struct lttng_handle
));
714 if (handle
== NULL
) {
715 PERROR("malloc handle");
719 /* Copy session name */
720 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
721 sizeof(handle
->session_name
));
726 /* Copy lttng domain or leave initialized to 0. */
728 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
739 * Destroy handle by free(3) the pointer.
741 void lttng_destroy_handle(struct lttng_handle
*handle
)
747 * Register an outside consumer.
749 * Returns size of returned session payload data or a negative error code.
751 int lttng_register_consumer(struct lttng_handle
*handle
,
752 const char *socket_path
)
755 struct lttcomm_session_msg lsm
;
757 if (handle
== NULL
|| socket_path
== NULL
) {
758 ret
= -LTTNG_ERR_INVALID
;
762 memset(&lsm
, 0, sizeof(lsm
));
763 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
764 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
765 sizeof(lsm
.session
.name
));
767 ret
= -LTTNG_ERR_INVALID
;
771 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
773 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
774 sizeof(lsm
.u
.reg
.path
));
776 ret
= -LTTNG_ERR_INVALID
;
780 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
786 * Start tracing for all traces of the session.
788 * Returns size of returned session payload data or a negative error code.
790 int lttng_start_tracing(const char *session_name
)
793 struct lttcomm_session_msg lsm
;
795 if (session_name
== NULL
) {
796 ret
= -LTTNG_ERR_INVALID
;
800 memset(&lsm
, 0, sizeof(lsm
));
801 lsm
.cmd_type
= LTTNG_START_TRACE
;
803 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
804 sizeof(lsm
.session
.name
));
806 ret
= -LTTNG_ERR_INVALID
;
810 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
816 * Stop tracing for all traces of the session.
818 static int _lttng_stop_tracing(const char *session_name
, int wait
)
821 struct lttcomm_session_msg lsm
;
823 if (session_name
== NULL
) {
824 ret
= -LTTNG_ERR_INVALID
;
828 memset(&lsm
, 0, sizeof(lsm
));
829 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
831 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
832 sizeof(lsm
.session
.name
));
834 ret
= -LTTNG_ERR_INVALID
;
838 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
839 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
847 /* Check for data availability */
849 data_ret
= lttng_data_pending(session_name
);
851 /* Return the data available call error. */
857 * Data sleep time before retrying (in usec). Don't sleep if the
858 * call returned value indicates availability.
861 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
863 } while (data_ret
!= 0);
871 * Stop tracing and wait for data availability.
873 int lttng_stop_tracing(const char *session_name
)
875 return _lttng_stop_tracing(session_name
, 1);
879 * Stop tracing but _don't_ wait for data availability.
881 int lttng_stop_tracing_no_wait(const char *session_name
)
883 return _lttng_stop_tracing(session_name
, 0);
887 * Add context to a channel.
889 * If the given channel is NULL, add the contexts to all channels.
890 * The event_name param is ignored.
892 * Returns the size of the returned payload data or a negative error code.
894 int lttng_add_context(struct lttng_handle
*handle
,
895 struct lttng_event_context
*ctx
, const char *event_name
,
896 const char *channel_name
)
901 struct lttcomm_session_msg lsm
;
903 /* Safety check. Both are mandatory. */
904 if (handle
== NULL
|| ctx
== NULL
) {
905 ret
= -LTTNG_ERR_INVALID
;
909 memset(&lsm
, 0, sizeof(lsm
));
910 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
912 /* If no channel name, send empty string. */
913 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
914 sizeof(lsm
.u
.context
.channel_name
));
916 ret
= -LTTNG_ERR_INVALID
;
920 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
921 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
922 sizeof(lsm
.session
.name
));
924 ret
= -LTTNG_ERR_INVALID
;
928 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
929 size_t provider_len
, ctx_len
;
930 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
931 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
933 if (!provider_name
|| !ctx_name
) {
934 ret
= -LTTNG_ERR_INVALID
;
938 provider_len
= strlen(provider_name
);
939 if (provider_len
== 0) {
940 ret
= -LTTNG_ERR_INVALID
;
943 lsm
.u
.context
.provider_name_len
= provider_len
;
945 ctx_len
= strlen(ctx_name
);
947 ret
= -LTTNG_ERR_INVALID
;
950 lsm
.u
.context
.context_name_len
= ctx_len
;
952 len
= provider_len
+ ctx_len
;
953 buf
= (char *) zmalloc(len
);
955 ret
= -LTTNG_ERR_NOMEM
;
959 memcpy(buf
, provider_name
, provider_len
);
960 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
962 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
964 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
966 * Don't leak application addresses to the sessiond.
967 * This is only necessary when ctx is for an app ctx otherwise
968 * the values inside the union (type & config) are overwritten.
970 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
971 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
974 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
981 * Enable event(s) for a channel.
983 * If no event name is specified, all events are enabled.
984 * If no channel name is specified, the default 'channel0' is used.
986 * Returns size of returned session payload data or a negative error code.
988 int lttng_enable_event(struct lttng_handle
*handle
,
989 struct lttng_event
*ev
, const char *channel_name
)
991 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
996 * Create or enable an event with a filter expression.
998 * Return negative error value on error.
999 * Return size of returned session payload data if OK.
1001 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1002 struct lttng_event
*event
, const char *channel_name
,
1003 const char *filter_expression
)
1005 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1006 filter_expression
, 0, NULL
);
1010 * Depending on the event, return a newly allocated agent filter expression or
1011 * NULL if not applicable.
1013 * An event with NO loglevel and the name is * will return NULL.
1015 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1018 char *agent_filter
= NULL
;
1022 /* Don't add filter for the '*' event. */
1023 if (strcmp(ev
->name
, "*") != 0) {
1025 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1028 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1036 /* Add loglevel filtering if any for the JUL domain. */
1037 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1040 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1046 if (filter
|| agent_filter
) {
1049 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1050 agent_filter
? agent_filter
: filter
, op
,
1055 agent_filter
= new_filter
;
1057 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1066 return agent_filter
;
1073 * Enable event(s) for a channel, possibly with exclusions and a filter.
1074 * If no event name is specified, all events are enabled.
1075 * If no channel name is specified, the default name is used.
1076 * If filter expression is not NULL, the filter is set for the event.
1077 * If exclusion count is not zero, the exclusions are set for the event.
1078 * Returns size of returned session payload data or a negative error code.
1080 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1081 struct lttng_event
*ev
, const char *channel_name
,
1082 const char *original_filter_expression
,
1083 int exclusion_count
, char **exclusion_list
)
1085 struct lttcomm_session_msg lsm
;
1086 struct lttng_payload payload
;
1088 unsigned int free_filter_expression
= 0;
1089 struct filter_parser_ctx
*ctx
= NULL
;
1092 * We have either a filter or some exclusions, so we need to set up
1093 * a variable-length payload from where to send the data.
1095 lttng_payload_init(&payload
);
1098 * Cast as non-const since we may replace the filter expression
1099 * by a dynamically allocated string. Otherwise, the original
1100 * string is not modified.
1102 char *filter_expression
= (char *) original_filter_expression
;
1104 if (handle
== NULL
|| ev
== NULL
) {
1105 ret
= -LTTNG_ERR_INVALID
;
1110 * Empty filter string will always be rejected by the parser
1111 * anyway, so treat this corner-case early to eliminate
1112 * lttng_fmemopen error for 0-byte allocation.
1114 if (filter_expression
&& filter_expression
[0] == '\0') {
1115 ret
= -LTTNG_ERR_INVALID
;
1119 memset(&lsm
, 0, sizeof(lsm
));
1121 /* If no channel name, send empty string. */
1122 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1123 sizeof(lsm
.u
.enable
.channel_name
));
1125 ret
= -LTTNG_ERR_INVALID
;
1129 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1130 if (ev
->name
[0] == '\0') {
1131 /* Enable all events. */
1132 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1133 LTTNG_ASSERT(ret
== 0);
1136 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1137 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1139 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1140 sizeof(lsm
.session
.name
));
1142 ret
= -LTTNG_ERR_INVALID
;
1146 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1147 lsm
.u
.enable
.bytecode_len
= 0;
1149 /* Parse filter expression. */
1150 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1151 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1152 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1153 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1154 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1155 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1158 /* Setup JUL filter if needed. */
1159 agent_filter
= set_agent_filter(filter_expression
, ev
);
1160 if (!agent_filter
) {
1161 if (!filter_expression
) {
1163 * No JUL and no filter, just skip
1170 * With an agent filter, the original filter has
1171 * been added to it thus replace the filter
1174 filter_expression
= agent_filter
;
1175 free_filter_expression
= 1;
1179 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1184 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1185 + bytecode_get_len(&ctx
->bytecode
->b
);
1186 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1189 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1190 lsm
.u
.enable
.bytecode_len
+
1191 lsm
.u
.enable
.expression_len
+
1192 LTTNG_SYMBOL_NAME_LEN
*
1195 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1199 /* Put exclusion names first in the data. */
1200 for (i
= 0; i
< exclusion_count
; i
++) {
1201 size_t exclusion_len
;
1203 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1204 LTTNG_SYMBOL_NAME_LEN
);
1205 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1206 /* Exclusion is not NULL-terminated. */
1207 ret
= -LTTNG_ERR_INVALID
;
1211 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1212 exclusion_list
[i
], exclusion_len
);
1218 * Padding the rest of the entry with zeros. Every exclusion
1219 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1221 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1222 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1228 /* Add filter expression next. */
1229 if (filter_expression
) {
1230 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1231 filter_expression
, lsm
.u
.enable
.expression_len
);
1236 /* Add filter bytecode next. */
1237 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1238 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1239 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1244 if (ev
->extended
.ptr
) {
1245 struct lttng_event_extended
*ev_ext
=
1246 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1248 if (ev_ext
->probe_location
) {
1250 * lttng_userspace_probe_location_serialize returns the
1251 * number of bytes that was appended to the buffer.
1253 ret
= lttng_userspace_probe_location_serialize(
1254 ev_ext
->probe_location
, &payload
);
1260 * Set the size of the userspace probe location element
1261 * of the buffer so that the receiving side knows where
1264 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1269 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1271 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1278 LTTNG_ASSERT(fd_count
== 0 || fd_count
== 1);
1279 if (fd_count
== 1) {
1280 struct fd_handle
*h
=
1281 lttng_payload_view_pop_fd_handle(&view
);
1287 fd_to_send
= fd_handle_get_fd(h
);
1291 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1292 fd_count
? &fd_to_send
: NULL
, fd_count
,
1293 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1294 view
.buffer
.size
, NULL
, NULL
, 0);
1298 if (filter_expression
&& ctx
) {
1299 filter_bytecode_free(ctx
);
1300 filter_ir_free(ctx
);
1301 filter_parser_ctx_free(ctx
);
1304 if (free_filter_expression
) {
1306 * The filter expression has been replaced and must be freed as
1307 * it is not the original filter expression received as a
1310 free(filter_expression
);
1314 * Return directly to the caller and don't ask the sessiond since
1315 * something went wrong in the parsing of data above.
1317 lttng_payload_reset(&payload
);
1321 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1325 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1326 struct lttng_event
*ev
, const char *channel_name
,
1327 const char *original_filter_expression
)
1329 struct lttcomm_session_msg lsm
;
1332 unsigned int free_filter_expression
= 0;
1333 struct filter_parser_ctx
*ctx
= NULL
;
1335 * Cast as non-const since we may replace the filter expression
1336 * by a dynamically allocated string. Otherwise, the original
1337 * string is not modified.
1339 char *filter_expression
= (char *) original_filter_expression
;
1341 if (handle
== NULL
|| ev
== NULL
) {
1342 ret
= -LTTNG_ERR_INVALID
;
1347 * Empty filter string will always be rejected by the parser
1348 * anyway, so treat this corner-case early to eliminate
1349 * lttng_fmemopen error for 0-byte allocation.
1351 if (filter_expression
&& filter_expression
[0] == '\0') {
1352 ret
= -LTTNG_ERR_INVALID
;
1356 memset(&lsm
, 0, sizeof(lsm
));
1358 /* If no channel name, send empty string. */
1359 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1360 sizeof(lsm
.u
.disable
.channel_name
));
1362 ret
= -LTTNG_ERR_INVALID
;
1366 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1368 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1369 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1371 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1372 sizeof(lsm
.session
.name
));
1374 ret
= -LTTNG_ERR_INVALID
;
1378 lsm
.u
.disable
.bytecode_len
= 0;
1381 * For the JUL domain, a filter is enforced except for the
1382 * disable all event. This is done to avoid having the event in
1383 * all sessions thus filtering by logger name.
1385 if (filter_expression
== NULL
&&
1386 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1387 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1388 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1393 * We have a filter, so we need to set up a variable-length
1394 * memory block from where to send the data.
1397 /* Parse filter expression */
1398 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1399 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1400 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1401 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1402 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1403 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1406 /* Setup JUL filter if needed. */
1407 agent_filter
= set_agent_filter(filter_expression
, ev
);
1408 if (!agent_filter
) {
1409 if (!filter_expression
) {
1411 * No JUL and no filter, just skip
1418 * With a JUL filter, the original filter has
1419 * been added to it thus replace the filter
1422 filter_expression
= agent_filter
;
1423 free_filter_expression
= 1;
1427 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1432 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1433 + bytecode_get_len(&ctx
->bytecode
->b
);
1434 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1437 varlen_data
= (char *) zmalloc(lsm
.u
.disable
.bytecode_len
1438 + lsm
.u
.disable
.expression_len
);
1440 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1444 /* Add filter expression. */
1445 if (lsm
.u
.disable
.expression_len
!= 0) {
1448 lsm
.u
.disable
.expression_len
);
1450 /* Add filter bytecode next. */
1451 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1453 + lsm
.u
.disable
.expression_len
,
1455 lsm
.u
.disable
.bytecode_len
);
1458 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1459 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1463 if (filter_expression
&& ctx
) {
1464 filter_bytecode_free(ctx
);
1465 filter_ir_free(ctx
);
1466 filter_parser_ctx_free(ctx
);
1469 if (free_filter_expression
) {
1471 * The filter expression has been replaced and must be freed as
1472 * it is not the original filter expression received as a
1475 free(filter_expression
);
1479 * Return directly to the caller and don't ask the sessiond since
1480 * something went wrong in the parsing of data above.
1485 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1490 * Disable event(s) of a channel and domain.
1491 * If no event name is specified, all events are disabled.
1492 * If no channel name is specified, the default 'channel0' is used.
1493 * Returns size of returned session payload data or a negative error code.
1495 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1496 const char *channel_name
)
1499 struct lttng_event ev
;
1501 memset(&ev
, 0, sizeof(ev
));
1503 ev
.type
= LTTNG_EVENT_ALL
;
1504 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1506 ret
= -LTTNG_ERR_INVALID
;
1510 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1515 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1517 struct lttng_channel
*channel
= NULL
;
1518 struct lttng_channel_extended
*extended
= NULL
;
1524 /* Validate domain. */
1525 switch (domain
->type
) {
1526 case LTTNG_DOMAIN_UST
:
1527 switch (domain
->buf_type
) {
1528 case LTTNG_BUFFER_PER_UID
:
1529 case LTTNG_BUFFER_PER_PID
:
1535 case LTTNG_DOMAIN_KERNEL
:
1536 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1544 channel
= (lttng_channel
*) zmalloc(sizeof(*channel
));
1549 extended
= (lttng_channel_extended
*) zmalloc(sizeof(*extended
));
1554 channel
->attr
.extended
.ptr
= extended
;
1556 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1564 void lttng_channel_destroy(struct lttng_channel
*channel
)
1570 if (channel
->attr
.extended
.ptr
) {
1571 free(channel
->attr
.extended
.ptr
);
1577 * Enable channel per domain
1578 * Returns size of returned session payload data or a negative error code.
1580 int lttng_enable_channel(struct lttng_handle
*handle
,
1581 struct lttng_channel
*in_chan
)
1584 struct lttcomm_session_msg lsm
;
1585 size_t total_buffer_size_needed_per_cpu
= 0;
1587 /* NULL arguments are forbidden. No default values. */
1588 if (handle
== NULL
|| in_chan
== NULL
) {
1589 return -LTTNG_ERR_INVALID
;
1592 memset(&lsm
, 0, sizeof(lsm
));
1593 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1594 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1596 if (!in_chan
->attr
.extended
.ptr
) {
1597 struct lttng_channel
*channel
;
1598 struct lttng_channel_extended
*extended
;
1600 channel
= lttng_channel_create(&handle
->domain
);
1602 return -LTTNG_ERR_NOMEM
;
1606 * Create a new channel in order to use default extended
1609 extended
= (struct lttng_channel_extended
*)
1610 channel
->attr
.extended
.ptr
;
1611 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1612 lttng_channel_destroy(channel
);
1614 struct lttng_channel_extended
*extended
;
1616 extended
= (struct lttng_channel_extended
*)
1617 in_chan
->attr
.extended
.ptr
;
1618 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1622 * Verify that the amount of memory required to create the requested
1623 * buffer is available on the system at the moment.
1625 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1626 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1627 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1628 return -LTTNG_ERR_NOMEM
;
1631 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1632 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1634 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1635 sizeof(lsm
.session
.name
));
1637 ret
= -LTTNG_ERR_INVALID
;
1641 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1647 * All tracing will be stopped for registered events of the channel.
1648 * Returns size of returned session payload data or a negative error code.
1650 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1653 struct lttcomm_session_msg lsm
;
1655 /* Safety check. Both are mandatory. */
1656 if (handle
== NULL
|| name
== NULL
) {
1657 return -LTTNG_ERR_INVALID
;
1660 memset(&lsm
, 0, sizeof(lsm
));
1662 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1664 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1665 sizeof(lsm
.u
.disable
.channel_name
));
1667 ret
= -LTTNG_ERR_INVALID
;
1671 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1673 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1674 sizeof(lsm
.session
.name
));
1676 ret
= -LTTNG_ERR_INVALID
;
1680 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1686 * Lists all available tracepoints of domain.
1687 * Sets the contents of the events array.
1688 * Returns the number of lttng_event entries in events;
1689 * on error, returns a negative value.
1691 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1692 struct lttng_event
**events
)
1695 struct lttcomm_session_msg lsm
;
1697 if (handle
== NULL
) {
1698 return -LTTNG_ERR_INVALID
;
1701 memset(&lsm
, 0, sizeof(lsm
));
1702 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1703 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1705 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1710 return ret
/ sizeof(struct lttng_event
);
1714 * Lists all available tracepoint fields of domain.
1715 * Sets the contents of the event field array.
1716 * Returns the number of lttng_event_field entries in events;
1717 * on error, returns a negative value.
1719 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1720 struct lttng_event_field
**fields
)
1723 struct lttcomm_session_msg lsm
;
1725 if (handle
== NULL
) {
1726 return -LTTNG_ERR_INVALID
;
1729 memset(&lsm
, 0, sizeof(lsm
));
1730 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1731 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1733 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1738 return ret
/ sizeof(struct lttng_event_field
);
1742 * Lists all available kernel system calls. Allocates and sets the contents of
1745 * Returns the number of lttng_event entries in events; on error, returns a
1748 int lttng_list_syscalls(struct lttng_event
**events
)
1751 struct lttcomm_session_msg lsm
;
1754 return -LTTNG_ERR_INVALID
;
1757 memset(&lsm
, 0, sizeof(lsm
));
1758 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1759 /* Force kernel domain for system calls. */
1760 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1762 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1767 return ret
/ sizeof(struct lttng_event
);
1771 * Returns a human readable string describing
1772 * the error code (positive or negative value).
1774 const char *lttng_strerror(int code
)
1780 return error_get_str(code
);
1783 enum lttng_error_code
lttng_create_session_ext(
1784 struct lttng_session_descriptor
*session_descriptor
)
1786 enum lttng_error_code ret_code
;
1787 struct lttcomm_session_msg lsm
= {
1788 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1791 struct lttng_buffer_view reply_view
;
1793 bool sessiond_must_generate_ouput
;
1794 struct lttng_dynamic_buffer payload
;
1796 size_t descriptor_size
;
1797 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1799 lttng_dynamic_buffer_init(&payload
);
1800 if (!session_descriptor
) {
1801 ret_code
= LTTNG_ERR_INVALID
;
1805 sessiond_must_generate_ouput
=
1806 !lttng_session_descriptor_is_output_destination_initialized(
1807 session_descriptor
);
1808 if (sessiond_must_generate_ouput
) {
1809 const char *home_dir
= utils_get_home_dir();
1810 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1812 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1813 ret_code
= LTTNG_ERR_FATAL
;
1817 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1818 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1821 ret_code
= LTTNG_ERR_NOMEM
;
1826 descriptor_size
= payload
.size
;
1827 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1830 ret_code
= LTTNG_ERR_INVALID
;
1833 descriptor_size
= payload
.size
- descriptor_size
;
1834 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1836 /* Command returns a session descriptor on success. */
1837 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1838 payload
.size
, &reply
);
1839 if (reply_ret
< 0) {
1840 ret_code
= (lttng_error_code
) -reply_ret
;
1842 } else if (reply_ret
== 0) {
1843 /* Socket unexpectedly closed by the session daemon. */
1844 ret_code
= LTTNG_ERR_FATAL
;
1848 reply_view
= lttng_buffer_view_init((const char *) reply
, 0, reply_ret
);
1849 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1852 ret_code
= LTTNG_ERR_FATAL
;
1855 ret_code
= LTTNG_OK
;
1856 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1859 lttng_dynamic_buffer_reset(&payload
);
1860 lttng_session_descriptor_destroy(descriptor_reply
);
1865 * Create a new session using name and url for destination.
1867 * Return 0 on success else a negative LTTng error code.
1869 int lttng_create_session(const char *name
, const char *url
)
1873 struct lttng_uri
*uris
= NULL
;
1874 struct lttng_session_descriptor
*descriptor
= NULL
;
1875 enum lttng_error_code ret_code
;
1878 ret
= -LTTNG_ERR_INVALID
;
1882 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1884 ret
= -LTTNG_ERR_INVALID
;
1889 descriptor
= lttng_session_descriptor_create(name
);
1892 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1893 ret
= -LTTNG_ERR_INVALID
;
1896 descriptor
= lttng_session_descriptor_local_create(name
,
1900 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1904 ret
= -LTTNG_ERR_INVALID
;
1908 ret
= -LTTNG_ERR_INVALID
;
1911 ret_code
= lttng_create_session_ext(descriptor
);
1912 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1914 lttng_session_descriptor_destroy(descriptor
);
1920 * Create a session exclusively used for snapshot.
1922 * Return 0 on success else a negative LTTng error code.
1924 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1927 enum lttng_error_code ret_code
;
1929 struct lttng_uri
*uris
= NULL
;
1930 struct lttng_session_descriptor
*descriptor
= NULL
;
1933 ret
= -LTTNG_ERR_INVALID
;
1937 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1939 ret
= -LTTNG_ERR_INVALID
;
1943 * If the user does not specify a custom subdir, use the session name.
1945 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1946 strlen(uris
[0].subdir
) == 0) {
1947 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1950 PERROR("Failed to set session name as network destination sub-directory");
1951 ret
= -LTTNG_ERR_FATAL
;
1953 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1954 /* Truncated output. */
1955 ret
= -LTTNG_ERR_INVALID
;
1962 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1965 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1966 ret
= -LTTNG_ERR_INVALID
;
1969 descriptor
= lttng_session_descriptor_snapshot_local_create(
1974 descriptor
= lttng_session_descriptor_snapshot_network_create(
1980 ret
= -LTTNG_ERR_INVALID
;
1984 ret
= -LTTNG_ERR_INVALID
;
1987 ret_code
= lttng_create_session_ext(descriptor
);
1988 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1990 lttng_session_descriptor_destroy(descriptor
);
1996 * Create a session exclusively used for live.
1998 * Return 0 on success else a negative LTTng error code.
2000 int lttng_create_session_live(const char *name
, const char *url
,
2001 unsigned int timer_interval
)
2004 enum lttng_error_code ret_code
;
2005 struct lttng_session_descriptor
*descriptor
= NULL
;
2008 ret
= -LTTNG_ERR_INVALID
;
2013 descriptor
= lttng_session_descriptor_live_network_create(
2014 name
, url
, NULL
, timer_interval
);
2016 descriptor
= lttng_session_descriptor_live_create(
2017 name
, timer_interval
);
2020 ret
= -LTTNG_ERR_INVALID
;
2023 ret_code
= lttng_create_session_ext(descriptor
);
2024 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2026 lttng_session_descriptor_destroy(descriptor
);
2031 * Stop the session and wait for the data before destroying it
2033 * Return 0 on success else a negative LTTng error code.
2035 int lttng_destroy_session(const char *session_name
)
2038 enum lttng_error_code ret_code
;
2039 enum lttng_destruction_handle_status status
;
2040 struct lttng_destruction_handle
*handle
= NULL
;
2043 * Stop the tracing and wait for the data to be
2046 ret
= _lttng_stop_tracing(session_name
, 1);
2047 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2051 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2052 if (ret_code
!= LTTNG_OK
) {
2053 ret
= (int) -ret_code
;
2056 LTTNG_ASSERT(handle
);
2058 /* Block until the completion of the destruction of the session. */
2059 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2060 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2061 ret
= -LTTNG_ERR_UNK
;
2065 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2066 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2067 ret
= -LTTNG_ERR_UNK
;
2070 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2072 lttng_destruction_handle_destroy(handle
);
2077 * Destroy the session without waiting for the data.
2079 int lttng_destroy_session_no_wait(const char *session_name
)
2081 enum lttng_error_code ret_code
;
2083 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2084 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2088 * Ask the session daemon for all available sessions.
2089 * Sets the contents of the sessions array.
2090 * Returns the number of lttng_session entries in sessions;
2091 * on error, returns a negative value.
2093 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2096 struct lttcomm_session_msg lsm
;
2097 const size_t session_size
= sizeof(struct lttng_session
) +
2098 sizeof(struct lttng_session_extended
);
2099 size_t session_count
, i
;
2100 struct lttng_session_extended
*sessions_extended_begin
;
2101 struct lttng_session
*sessions
= NULL
;
2103 memset(&lsm
, 0, sizeof(lsm
));
2104 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2106 * Initialize out_sessions to NULL so it is initialized when
2107 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2108 * be subsequently freed.
2110 *out_sessions
= NULL
;
2111 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2116 ret
= -LTTNG_ERR_FATAL
;
2120 if (ret
% session_size
) {
2121 ret
= -LTTNG_ERR_UNK
;
2125 session_count
= (size_t) ret
/ session_size
;
2126 sessions_extended_begin
= (struct lttng_session_extended
*)
2127 (&sessions
[session_count
]);
2129 /* Set extended session info pointers. */
2130 for (i
= 0; i
< session_count
; i
++) {
2131 struct lttng_session
*session
= &sessions
[i
];
2132 struct lttng_session_extended
*extended
=
2133 &(sessions_extended_begin
[i
]);
2135 session
->extended
.ptr
= extended
;
2138 ret
= (int) session_count
;
2139 *out_sessions
= sessions
;
2144 enum lttng_error_code
lttng_session_get_creation_time(
2145 const struct lttng_session
*session
, uint64_t *creation_time
)
2147 enum lttng_error_code ret
= LTTNG_OK
;
2148 struct lttng_session_extended
*extended
;
2150 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2151 ret
= LTTNG_ERR_INVALID
;
2155 extended
= (lttng_session_extended
*) session
->extended
.ptr
;
2156 if (!extended
->creation_time
.is_set
) {
2157 /* Not created on the session daemon yet. */
2158 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2161 *creation_time
= extended
->creation_time
.value
;
2166 int lttng_set_session_shm_path(const char *session_name
,
2167 const char *shm_path
)
2170 struct lttcomm_session_msg lsm
;
2172 if (session_name
== NULL
) {
2173 return -LTTNG_ERR_INVALID
;
2176 memset(&lsm
, 0, sizeof(lsm
));
2177 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2179 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2180 sizeof(lsm
.session
.name
));
2182 ret
= -LTTNG_ERR_INVALID
;
2186 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2187 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2189 ret
= -LTTNG_ERR_INVALID
;
2193 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2199 * Ask the session daemon for all available domains of a session.
2200 * Sets the contents of the domains array.
2201 * Returns the number of lttng_domain entries in domains;
2202 * on error, returns a negative value.
2204 int lttng_list_domains(const char *session_name
,
2205 struct lttng_domain
**domains
)
2208 struct lttcomm_session_msg lsm
;
2210 if (session_name
== NULL
) {
2211 ret
= -LTTNG_ERR_INVALID
;
2215 memset(&lsm
, 0, sizeof(lsm
));
2216 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2218 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2219 sizeof(lsm
.session
.name
));
2221 ret
= -LTTNG_ERR_INVALID
;
2225 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2230 return ret
/ sizeof(struct lttng_domain
);
2236 * Ask the session daemon for all available channels of a session.
2237 * Sets the contents of the channels array.
2238 * Returns the number of lttng_channel entries in channels;
2239 * on error, returns a negative value.
2241 int lttng_list_channels(struct lttng_handle
*handle
,
2242 struct lttng_channel
**channels
)
2245 size_t channel_count
, i
;
2246 const size_t channel_size
= sizeof(struct lttng_channel
) +
2247 sizeof(struct lttng_channel_extended
);
2248 struct lttcomm_session_msg lsm
;
2251 if (handle
== NULL
) {
2252 ret
= -LTTNG_ERR_INVALID
;
2256 memset(&lsm
, 0, sizeof(lsm
));
2257 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2258 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2259 sizeof(lsm
.session
.name
));
2261 ret
= -LTTNG_ERR_INVALID
;
2265 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2267 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2272 if (ret
% channel_size
) {
2273 ret
= -LTTNG_ERR_UNK
;
2278 channel_count
= (size_t) ret
/ channel_size
;
2280 /* Set extended info pointers */
2281 extended_at
= ((char *) *channels
) +
2282 channel_count
* sizeof(struct lttng_channel
);
2283 for (i
= 0; i
< channel_count
; i
++) {
2284 struct lttng_channel
*chan
= &(*channels
)[i
];
2286 chan
->attr
.extended
.ptr
= extended_at
;
2287 extended_at
+= sizeof(struct lttng_channel_extended
);
2290 ret
= (int) channel_count
;
2296 * Ask the session daemon for all available events of a session channel.
2297 * Sets the contents of the events array.
2298 * Returns the number of lttng_event entries in events;
2299 * on error, returns a negative value.
2301 int lttng_list_events(struct lttng_handle
*handle
,
2302 const char *channel_name
, struct lttng_event
**events
)
2305 struct lttcomm_session_msg lsm
= {};
2306 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2307 uint32_t nb_events
, i
;
2308 const char *comm_ext_at
;
2309 struct lttng_dynamic_buffer listing
;
2311 struct lttng_payload payload
;
2312 struct lttng_payload payload_copy
;
2313 struct lttng_payload_view lsm_view
=
2314 lttng_payload_view_init_from_buffer(
2315 (const char *) &lsm
, 0, sizeof(lsm
));
2316 struct lttng_buffer_view cmd_header_view
;
2317 struct lttng_buffer_view cmd_payload_view
;
2318 struct lttng_buffer_view flat_events_view
;
2319 struct lttng_buffer_view ext_view
;
2321 /* Safety check. An handle and channel name are mandatory */
2322 if (handle
== NULL
|| channel_name
== NULL
) {
2323 ret
= -LTTNG_ERR_INVALID
;
2327 lttng_payload_init(&payload
);
2328 lttng_payload_init(&payload_copy
);
2330 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2331 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2332 sizeof(lsm
.session
.name
));
2334 ret
= -LTTNG_ERR_INVALID
;
2338 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2339 sizeof(lsm
.u
.list
.channel_name
));
2341 ret
= -LTTNG_ERR_INVALID
;
2345 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2347 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2353 * A copy of the payload is performed since it will be
2354 * consumed twice. Consuming the same payload twice is invalid since
2355 * it will cause any received file descriptor to become "shared"
2356 * between different instances of the resulting objects.
2358 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2360 ret
= -LTTNG_ERR_NOMEM
;
2364 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2365 &payload
.buffer
, 0, sizeof(*cmd_header
));
2366 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2367 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2371 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2373 /* Set number of events and free command header */
2374 nb_events
= cmd_header
->nb_events
;
2375 if (nb_events
> INT_MAX
) {
2376 ret
= -LTTNG_ERR_OVERFLOW
;
2380 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2381 &payload
.buffer
, sizeof(*cmd_header
), -1);
2384 * The buffer that is returned must contain a "flat" version of
2385 * the events that are returned. In other words, all pointers
2386 * within an lttng_event must point to a location within the returned
2387 * buffer so that the user may free everything by simply calling free()
2388 * on the returned buffer. This is needed in order to maintain API
2391 * A first pass is performed to compute the size of the buffer that
2392 * must be allocated. A second pass is then performed to setup
2393 * the returned events so that their members always point within the
2396 * The layout of the returned buffer is as follows:
2397 * - struct lttng_event[nb_events],
2398 * - nb_events times the following:
2399 * - struct lttng_event_extended,
2400 * - flattened version of userspace_probe_location
2401 * - filter_expression
2403 * - padding to align to 64-bits
2405 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2406 nb_events
* sizeof(struct lttng_event
), -1);
2407 comm_ext_at
= ext_view
.data
;
2408 storage_req
= nb_events
* sizeof(struct lttng_event
);
2410 struct lttng_payload_view payload_view
=
2411 lttng_payload_view_from_payload(&payload
, 0, -1);
2413 for (i
= 0; i
< nb_events
; i
++) {
2414 const struct lttcomm_event_extended_header
*ext_comm
=
2415 (struct lttcomm_event_extended_header
*)
2417 int probe_storage_req
= 0;
2419 comm_ext_at
+= sizeof(*ext_comm
);
2420 comm_ext_at
+= ext_comm
->filter_len
;
2421 comm_ext_at
+= ext_comm
->nb_exclusions
*
2422 LTTNG_SYMBOL_NAME_LEN
;
2424 if (ext_comm
->userspace_probe_location_len
) {
2425 struct lttng_userspace_probe_location
2426 *probe_location
= NULL
;
2427 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2429 (const char *) comm_ext_at
-
2430 payload_view
.buffer
.data
,
2431 ext_comm
->userspace_probe_location_len
);
2433 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2434 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2439 * Create a temporary userspace probe location
2440 * to determine the size needed by a "flattened"
2441 * version of that same probe location.
2443 ret
= lttng_userspace_probe_location_create_from_payload(
2444 &probe_location_view
,
2447 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2451 ret
= lttng_userspace_probe_location_flatten(
2452 probe_location
, NULL
);
2453 lttng_userspace_probe_location_destroy(
2456 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2460 probe_storage_req
= ret
;
2461 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2464 storage_req
+= sizeof(struct lttng_event_extended
);
2465 storage_req
+= ext_comm
->filter_len
;
2466 storage_req
+= ext_comm
->nb_exclusions
*
2467 LTTNG_SYMBOL_NAME_LEN
;
2468 /* Padding to ensure the flat probe is aligned. */
2469 storage_req
= lttng_align_ceil(storage_req
, sizeof(uint64_t));
2470 storage_req
+= probe_storage_req
;
2474 lttng_dynamic_buffer_init(&listing
);
2476 * We must ensure that "listing" is never resized so as to preserve
2477 * the validity of the flattened objects.
2479 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2481 ret
= -LTTNG_ERR_NOMEM
;
2485 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2486 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2487 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2488 nb_events
* sizeof(struct lttng_event
));
2489 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2491 ret
= -LTTNG_ERR_NOMEM
;
2492 goto free_dynamic_buffer
;
2495 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2496 nb_events
* sizeof(struct lttng_event
), -1);
2497 comm_ext_at
= ext_view
.data
;
2500 struct lttng_payload_view payload_copy_view
=
2501 lttng_payload_view_from_payload(
2502 &payload_copy
, 0, -1);
2504 for (i
= 0; i
< nb_events
; i
++) {
2505 struct lttng_event
*event
= (typeof(event
))(
2507 (sizeof(struct lttng_event
) * i
));
2508 const struct lttcomm_event_extended_header
*ext_comm
=
2509 (typeof(ext_comm
)) comm_ext_at
;
2510 struct lttng_event_extended
*event_extended
=
2511 (typeof(event_extended
))(listing
.data
+
2514 /* Insert struct lttng_event_extended. */
2515 ret
= lttng_dynamic_buffer_set_size(&listing
,
2516 listing
.size
+ sizeof(*event_extended
));
2518 ret
= -LTTNG_ERR_NOMEM
;
2519 goto free_dynamic_buffer
;
2521 event
->extended
.ptr
= event_extended
;
2523 comm_ext_at
+= sizeof(*ext_comm
);
2525 /* Insert filter expression. */
2526 if (ext_comm
->filter_len
) {
2527 event_extended
->filter_expression
=
2528 listing
.data
+ listing
.size
;
2529 ret
= lttng_dynamic_buffer_append(&listing
,
2531 ext_comm
->filter_len
);
2533 ret
= -LTTNG_ERR_NOMEM
;
2534 goto free_dynamic_buffer
;
2536 comm_ext_at
+= ext_comm
->filter_len
;
2539 /* Insert exclusions. */
2540 if (ext_comm
->nb_exclusions
) {
2541 event_extended
->exclusions
.count
=
2542 ext_comm
->nb_exclusions
;
2543 event_extended
->exclusions
.strings
=
2544 listing
.data
+ listing
.size
;
2546 ret
= lttng_dynamic_buffer_append(&listing
,
2548 ext_comm
->nb_exclusions
*
2549 LTTNG_SYMBOL_NAME_LEN
);
2551 ret
= -LTTNG_ERR_NOMEM
;
2552 goto free_dynamic_buffer
;
2554 comm_ext_at
+= ext_comm
->nb_exclusions
*
2555 LTTNG_SYMBOL_NAME_LEN
;
2558 /* Insert padding to align to 64-bits. */
2559 ret
= lttng_dynamic_buffer_set_size(&listing
,
2560 lttng_align_ceil(listing
.size
,
2563 ret
= -LTTNG_ERR_NOMEM
;
2564 goto free_dynamic_buffer
;
2567 /* Insert flattened userspace probe location. */
2568 if (ext_comm
->userspace_probe_location_len
) {
2569 struct lttng_userspace_probe_location
2570 *probe_location
= NULL
;
2571 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2573 (const char *) comm_ext_at
-
2574 payload_copy_view
.buffer
.data
,
2575 ext_comm
->userspace_probe_location_len
);
2577 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2578 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2579 goto free_dynamic_buffer
;
2582 ret
= lttng_userspace_probe_location_create_from_payload(
2583 &probe_location_view
,
2586 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2587 goto free_dynamic_buffer
;
2590 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2593 ret
= lttng_userspace_probe_location_flatten(
2594 probe_location
, &listing
);
2595 lttng_userspace_probe_location_destroy(
2598 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2599 goto free_dynamic_buffer
;
2602 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2607 /* Don't reset listing buffer as we return its content. */
2608 *events
= (struct lttng_event
*) listing
.data
;
2609 lttng_dynamic_buffer_init(&listing
);
2610 ret
= (int) nb_events
;
2611 free_dynamic_buffer
:
2612 lttng_dynamic_buffer_reset(&listing
);
2614 lttng_payload_reset(&payload
);
2615 lttng_payload_reset(&payload_copy
);
2620 * Sets the tracing_group variable with name.
2621 * This function allocates memory pointed to by tracing_group.
2622 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2624 int lttng_set_tracing_group(const char *name
)
2630 ret
= -LTTNG_ERR_INVALID
;
2634 new_group
= strdup(name
);
2636 ret
= -LTTNG_ERR_FATAL
;
2640 free(tracing_group
);
2641 tracing_group
= new_group
;
2648 int lttng_calibrate(struct lttng_handle
*handle
,
2649 struct lttng_calibrate
*calibrate
)
2652 * This command was removed in LTTng 2.9.
2654 return -LTTNG_ERR_UND
;
2658 * Set default channel attributes.
2659 * If either or both of the arguments are null, attr content is zeroe'd.
2661 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2662 struct lttng_channel_attr
*attr
)
2664 struct lttng_channel_extended
*extended
;
2667 if (attr
== NULL
|| domain
== NULL
) {
2671 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2672 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2674 /* Same for all domains. */
2675 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2676 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2677 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2679 switch (domain
->type
) {
2680 case LTTNG_DOMAIN_KERNEL
:
2681 attr
->switch_timer_interval
=
2682 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2683 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2684 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2685 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2686 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2688 extended
->monitor_timer_interval
=
2689 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2690 extended
->blocking_timeout
=
2691 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2694 case LTTNG_DOMAIN_UST
:
2695 switch (domain
->buf_type
) {
2696 case LTTNG_BUFFER_PER_UID
:
2697 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2698 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2699 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2700 attr
->switch_timer_interval
=
2701 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2702 attr
->read_timer_interval
=
2703 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2705 extended
->monitor_timer_interval
=
2706 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2707 extended
->blocking_timeout
=
2708 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2711 case LTTNG_BUFFER_PER_PID
:
2713 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2714 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2715 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2716 attr
->switch_timer_interval
=
2717 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2718 attr
->read_timer_interval
=
2719 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2721 extended
->monitor_timer_interval
=
2722 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2723 extended
->blocking_timeout
=
2724 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2729 /* Default behavior: leave set to 0. */
2733 attr
->extended
.ptr
= extended
;
2736 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2737 uint64_t *discarded_events
)
2740 struct lttng_channel_extended
*chan_ext
;
2742 if (!channel
|| !discarded_events
) {
2743 ret
= -LTTNG_ERR_INVALID
;
2747 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2750 * This can happen since the lttng_channel structure is
2751 * used for other tasks where this pointer is never set.
2753 *discarded_events
= 0;
2757 *discarded_events
= chan_ext
->discarded_events
;
2762 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2763 uint64_t *lost_packets
)
2766 struct lttng_channel_extended
*chan_ext
;
2768 if (!channel
|| !lost_packets
) {
2769 ret
= -LTTNG_ERR_INVALID
;
2773 chan_ext
= (lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
2776 * This can happen since the lttng_channel structure is
2777 * used for other tasks where this pointer is never set.
2783 *lost_packets
= chan_ext
->lost_packets
;
2788 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2789 uint64_t *monitor_timer_interval
)
2793 if (!chan
|| !monitor_timer_interval
) {
2794 ret
= -LTTNG_ERR_INVALID
;
2798 if (!chan
->attr
.extended
.ptr
) {
2799 ret
= -LTTNG_ERR_INVALID
;
2803 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2804 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2809 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2810 uint64_t monitor_timer_interval
)
2814 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2815 ret
= -LTTNG_ERR_INVALID
;
2819 ((struct lttng_channel_extended
*)
2820 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2821 monitor_timer_interval
;
2826 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2827 int64_t *blocking_timeout
)
2831 if (!chan
|| !blocking_timeout
) {
2832 ret
= -LTTNG_ERR_INVALID
;
2836 if (!chan
->attr
.extended
.ptr
) {
2837 ret
= -LTTNG_ERR_INVALID
;
2841 *blocking_timeout
= ((struct lttng_channel_extended
*)
2842 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2847 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2848 int64_t blocking_timeout
)
2851 int64_t msec_timeout
;
2853 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2854 ret
= -LTTNG_ERR_INVALID
;
2858 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2859 ret
= -LTTNG_ERR_INVALID
;
2864 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2865 * us to accept a narrower range of values (msecs expressed as a signed
2868 msec_timeout
= blocking_timeout
/ 1000;
2869 if (msec_timeout
!= (int32_t) msec_timeout
) {
2870 ret
= -LTTNG_ERR_INVALID
;
2874 ((struct lttng_channel_extended
*)
2875 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2882 * Check if session daemon is alive.
2884 * Return 1 if alive or 0 if not.
2885 * On error returns a negative value.
2887 int lttng_session_daemon_alive(void)
2891 ret
= set_session_daemon_path();
2897 if (*sessiond_sock_path
== '\0') {
2899 * No socket path set. Weird error which means the constructor
2905 ret
= try_connect_sessiond(sessiond_sock_path
);
2916 * Set URL for a consumer for a session and domain.
2918 * Return 0 on success, else a negative value.
2920 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2921 const char *control_url
, const char *data_url
)
2925 struct lttcomm_session_msg lsm
;
2926 struct lttng_uri
*uris
= NULL
;
2928 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2929 ret
= -LTTNG_ERR_INVALID
;
2933 memset(&lsm
, 0, sizeof(lsm
));
2935 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2937 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2938 sizeof(lsm
.session
.name
));
2940 ret
= -LTTNG_ERR_INVALID
;
2944 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2946 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2948 ret
= -LTTNG_ERR_INVALID
;
2952 lsm
.u
.uri
.size
= size
;
2954 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2955 sizeof(struct lttng_uri
) * size
, NULL
);
2966 LTTNG_EXPORT
int lttng_enable_consumer(struct lttng_handle
*handle
);
2967 int lttng_enable_consumer(struct lttng_handle
*handle
)
2976 LTTNG_EXPORT
int lttng_disable_consumer(struct lttng_handle
*handle
);
2977 int lttng_disable_consumer(struct lttng_handle
*handle
)
2986 LTTNG_EXPORT
int _lttng_create_session_ext(const char *name
, const char *url
,
2987 const char *datetime
);
2988 int _lttng_create_session_ext(const char *name
, const char *url
,
2989 const char *datetime
)
2995 * For a given session name, this call checks if the data is ready to be read
2996 * or is still being extracted by the consumer(s) hence not ready to be used by
2999 int lttng_data_pending(const char *session_name
)
3002 struct lttcomm_session_msg lsm
;
3003 uint8_t *pending
= NULL
;
3005 if (session_name
== NULL
) {
3006 return -LTTNG_ERR_INVALID
;
3009 memset(&lsm
, 0, sizeof(lsm
));
3010 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
3012 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3013 sizeof(lsm
.session
.name
));
3015 ret
= -LTTNG_ERR_INVALID
;
3019 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
3022 } else if (ret
!= 1) {
3023 /* Unexpected payload size */
3024 ret
= -LTTNG_ERR_INVALID
;
3026 } else if (!pending
) {
3027 /* Internal error. */
3028 ret
= -LTTNG_ERR_UNK
;
3032 ret
= (int) *pending
;
3039 * Regenerate the metadata for a session.
3040 * Return 0 on success, a negative error code on error.
3042 int lttng_regenerate_metadata(const char *session_name
)
3045 struct lttcomm_session_msg lsm
;
3047 if (!session_name
) {
3048 ret
= -LTTNG_ERR_INVALID
;
3052 memset(&lsm
, 0, sizeof(lsm
));
3053 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3055 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3056 sizeof(lsm
.session
.name
));
3058 ret
= -LTTNG_ERR_INVALID
;
3062 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3073 * Deprecated, replaced by lttng_regenerate_metadata.
3075 int lttng_metadata_regenerate(const char *session_name
)
3077 return lttng_regenerate_metadata(session_name
);
3081 * Regenerate the statedump of a session.
3082 * Return 0 on success, a negative error code on error.
3084 int lttng_regenerate_statedump(const char *session_name
)
3087 struct lttcomm_session_msg lsm
;
3089 if (!session_name
) {
3090 ret
= -LTTNG_ERR_INVALID
;
3094 memset(&lsm
, 0, sizeof(lsm
));
3095 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3097 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3098 sizeof(lsm
.session
.name
));
3100 ret
= -LTTNG_ERR_INVALID
;
3104 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3115 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3119 struct lttcomm_session_msg lsm
= {
3120 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3122 lsm
.u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
;
3123 struct lttcomm_session_msg
*message_lsm
;
3124 struct lttng_payload message
;
3125 struct lttng_payload reply
;
3126 struct lttng_trigger
*reply_trigger
= NULL
;
3127 enum lttng_domain_type domain_type
;
3128 const struct lttng_credentials user_creds
= {
3129 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3130 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3132 const char *unused_trigger_name
= NULL
;
3133 enum lttng_trigger_status trigger_status
;
3135 lttng_payload_init(&message
);
3136 lttng_payload_init(&reply
);
3139 ret
= -LTTNG_ERR_INVALID
;
3143 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3144 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3145 /* Re-using already registered trigger. */
3146 ret
= -LTTNG_ERR_INVALID
;
3151 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3152 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3153 ret
= -LTTNG_ERR_NOMEM
;
3158 if (!trigger
->creds
.uid
.is_set
) {
3159 /* Use the client's credentials as the trigger credentials. */
3160 lttng_trigger_set_credentials(trigger
, &user_creds
);
3163 * Validate that either the current trigger credentials and the
3164 * client credentials are identical or that the current user is
3165 * root. The root user can register, unregister triggers for
3166 * himself and other users.
3168 * This check is also present on the sessiond side, using the
3169 * credentials passed on the socket. These check are all
3172 const struct lttng_credentials
*trigger_creds
=
3173 lttng_trigger_get_credentials(trigger
);
3175 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3176 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3177 ret
= -LTTNG_ERR_EPERM
;
3178 goto end_unset_name
;
3183 if (!lttng_trigger_validate(trigger
)) {
3184 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3185 goto end_unset_name
;
3188 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3191 lsm
.domain
.type
= domain_type
;
3193 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3195 ret
= -LTTNG_ERR_NOMEM
;
3196 goto end_unset_name
;
3199 ret
= lttng_trigger_serialize(trigger
, &message
);
3201 ret
= -LTTNG_ERR_UNK
;
3202 goto end_unset_name
;
3206 * This is needed to populate the trigger object size for the command
3209 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3211 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3214 struct lttng_payload_view message_view
=
3215 lttng_payload_view_from_payload(
3218 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3220 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3222 goto end_unset_name
;
3227 struct lttng_payload_view reply_view
=
3228 lttng_payload_view_from_payload(
3229 &reply
, 0, reply
.buffer
.size
);
3231 ret
= lttng_trigger_create_from_payload(
3232 &reply_view
, &reply_trigger
);
3234 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3235 goto end_unset_name
;
3239 if (name
|| generate_name
) {
3240 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3242 ret
= -LTTNG_ERR_NOMEM
;
3251 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3252 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3253 ret
= -LTTNG_ERR_UNK
;
3256 lttng_payload_reset(&message
);
3257 lttng_payload_reset(&reply
);
3258 lttng_trigger_destroy(reply_trigger
);
3262 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3264 /* Register an anonymous trigger. */
3265 return _lttng_register_trigger(trigger
, NULL
, false);
3268 enum lttng_error_code
lttng_register_trigger_with_name(
3269 struct lttng_trigger
*trigger
, const char *name
)
3271 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3273 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3276 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3277 struct lttng_trigger
*trigger
)
3279 const int ret
= _lttng_register_trigger(trigger
, nullptr, true);
3281 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3284 enum lttng_error_code
lttng_error_query_execute(
3285 const struct lttng_error_query
*query
,
3286 const struct lttng_endpoint
*endpoint
,
3287 struct lttng_error_query_results
**results
)
3290 enum lttng_error_code ret_code
;
3291 struct lttcomm_session_msg lsm
= {
3292 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3294 struct lttng_payload message
;
3295 struct lttng_payload reply
;
3296 struct lttcomm_session_msg
*message_lsm
;
3298 lttng_payload_init(&message
);
3299 lttng_payload_init(&reply
);
3301 if (!query
|| !results
) {
3302 ret_code
= LTTNG_ERR_INVALID
;
3306 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3307 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3311 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3313 ret_code
= LTTNG_ERR_NOMEM
;
3317 ret
= lttng_error_query_serialize(query
, &message
);
3319 ret_code
= LTTNG_ERR_UNK
;
3323 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3324 message_lsm
->u
.error_query
.length
=
3325 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3328 struct lttng_payload_view message_view
=
3329 lttng_payload_view_from_payload(
3332 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3334 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3336 ret_code
=(lttng_error_code
) -ret
;
3342 ssize_t reply_create_ret
;
3343 struct lttng_payload_view reply_view
=
3344 lttng_payload_view_from_payload(
3345 &reply
, 0, reply
.buffer
.size
);
3347 reply_create_ret
= lttng_error_query_results_create_from_payload(
3348 &reply_view
, results
);
3349 if (reply_create_ret
< 0) {
3350 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3355 ret_code
= LTTNG_OK
;
3357 lttng_payload_reset(&message
);
3358 lttng_payload_reset(&reply
);
3362 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3365 struct lttcomm_session_msg lsm
;
3366 struct lttcomm_session_msg
*message_lsm
;
3367 struct lttng_payload message
;
3368 struct lttng_payload reply
;
3369 struct lttng_trigger
*copy
= NULL
;
3370 const struct lttng_credentials user_creds
= {
3371 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3372 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3375 lttng_payload_init(&message
);
3376 lttng_payload_init(&reply
);
3379 ret
= -LTTNG_ERR_INVALID
;
3383 copy
= lttng_trigger_copy(trigger
);
3385 ret
= -LTTNG_ERR_UNK
;
3389 if (!copy
->creds
.uid
.is_set
) {
3390 /* Use the client credentials as the trigger credentials */
3391 lttng_trigger_set_credentials(copy
, &user_creds
);
3394 * Validate that either the current trigger credentials and the
3395 * client credentials are identical or that the current user is
3396 * root. The root user can register, unregister triggers for
3397 * himself and other users.
3399 * This check is also present on the sessiond side, using the
3400 * credentials passed on the socket. These check are all
3403 const struct lttng_credentials
*trigger_creds
=
3404 lttng_trigger_get_credentials(copy
);
3405 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3406 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3407 ret
= -LTTNG_ERR_EPERM
;
3413 if (!lttng_trigger_validate(copy
)) {
3414 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3418 memset(&lsm
, 0, sizeof(lsm
));
3419 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3421 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3423 ret
= -LTTNG_ERR_NOMEM
;
3427 ret
= lttng_trigger_serialize(copy
, &message
);
3429 ret
= -LTTNG_ERR_UNK
;
3434 * This is needed to populate the trigger object size for the command
3435 * header and number of fds sent.
3437 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3439 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3442 struct lttng_payload_view message_view
=
3443 lttng_payload_view_from_payload(
3447 * Update the message header with the number of fd that will be
3450 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3453 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3461 lttng_trigger_destroy(copy
);
3462 lttng_payload_reset(&message
);
3463 lttng_payload_reset(&reply
);
3468 * Ask the session daemon for all registered triggers for the current user.
3470 * Allocates and return an lttng_triggers set.
3471 * On error, returns a suitable lttng_error_code.
3473 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3476 enum lttng_error_code ret_code
= LTTNG_OK
;
3477 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3478 struct lttng_triggers
*local_triggers
= NULL
;
3479 struct lttng_payload reply
;
3480 struct lttng_payload_view lsm_view
=
3481 lttng_payload_view_init_from_buffer(
3482 (const char *) &lsm
, 0, sizeof(lsm
));
3484 lttng_payload_init(&reply
);
3486 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3488 ret_code
= (enum lttng_error_code
) -ret
;
3493 struct lttng_payload_view reply_view
=
3494 lttng_payload_view_from_payload(
3495 &reply
, 0, reply
.buffer
.size
);
3497 ret
= lttng_triggers_create_from_payload(
3498 &reply_view
, &local_triggers
);
3500 ret_code
= LTTNG_ERR_FATAL
;
3505 *triggers
= local_triggers
;
3506 local_triggers
= NULL
;
3508 lttng_payload_reset(&reply
);
3509 lttng_triggers_destroy(local_triggers
);
3516 static void __attribute__((constructor
)) init(void)
3518 /* Set default session group */
3519 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3525 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3527 free(tracing_group
);