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/bytecode/bytecode.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.h>
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.
77 int lttng_opt_verbose
;
81 * Copy domain to lttcomm_session_msg domain.
83 * If domain is unknown, default domain will be the kernel.
86 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
87 struct lttng_domain
*src
)
91 case LTTNG_DOMAIN_KERNEL
:
92 case LTTNG_DOMAIN_UST
:
93 case LTTNG_DOMAIN_JUL
:
94 case LTTNG_DOMAIN_LOG4J
:
95 case LTTNG_DOMAIN_PYTHON
:
96 memcpy(dst
, src
, sizeof(struct lttng_domain
));
99 memset(dst
, 0, sizeof(struct lttng_domain
));
106 * Send lttcomm_session_msg to the session daemon.
108 * On success, returns the number of bytes sent (>=0)
109 * On error, returns -1
111 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
116 ret
= -LTTNG_ERR_NO_SESSIOND
;
120 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm
->cmd_type
),
123 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
124 sizeof(struct lttcomm_session_msg
));
126 ret
= -LTTNG_ERR_FATAL
;
134 * Send var len data to the session daemon.
136 * On success, returns the number of bytes sent (>=0)
137 * On error, returns -1
139 static int send_session_varlen(const void *data
, size_t len
)
144 ret
= -LTTNG_ERR_NO_SESSIOND
;
153 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
155 ret
= -LTTNG_ERR_FATAL
;
163 * Send file descriptors to the session daemon.
165 * On success, returns the number of bytes sent (>=0)
166 * On error, returns -1
168 static int send_session_fds(const int *fds
, size_t nb_fd
)
173 ret
= -LTTNG_ERR_NO_SESSIOND
;
177 if (!fds
|| !nb_fd
) {
182 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
184 ret
= -LTTNG_ERR_FATAL
;
192 * Receive data from the sessiond socket.
194 * On success, returns the number of bytes received (>=0)
195 * On error, returns a negative lttng_error_code.
197 static int recv_data_sessiond(void *buf
, size_t len
)
204 ret
= -LTTNG_ERR_NO_SESSIOND
;
208 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
210 ret
= -LTTNG_ERR_FATAL
;
211 } else if (ret
== 0) {
212 ret
= -LTTNG_ERR_NO_SESSIOND
;
220 * Receive a payload from the session daemon by appending to an existing
222 * On success, returns the number of bytes received (>=0)
223 * On error, returns a negative lttng_error_code.
225 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
228 const size_t original_payload_size
= payload
->buffer
.size
;
230 ret
= lttng_dynamic_buffer_set_size(
231 &payload
->buffer
, payload
->buffer
.size
+ len
);
233 ret
= -LTTNG_ERR_NOMEM
;
237 ret
= recv_data_sessiond(
238 payload
->buffer
.data
+ original_payload_size
, len
);
244 * Check if we are in the specified group.
246 * If yes return 1, else return -1.
249 int lttng_check_tracing_group(void)
251 gid_t
*grp_list
, tracing_gid
;
252 int grp_list_size
, grp_id
, i
;
254 const char *grp_name
= tracing_group
;
256 /* Get GID of group 'tracing' */
257 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
258 /* If grp_tracing is NULL, the group does not exist. */
262 /* Get number of supplementary group IDs */
263 grp_list_size
= getgroups(0, NULL
);
264 if (grp_list_size
< 0) {
269 /* Alloc group list of the right size */
270 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
275 grp_id
= getgroups(grp_list_size
, grp_list
);
281 for (i
= 0; i
< grp_list_size
; i
++) {
282 if (grp_list
[i
] == tracing_gid
) {
295 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
299 size_t best_mem_info
;
300 size_t num_bytes_requested_total
;
303 * Get the number of CPU currently online to compute the amount of
304 * memory needed to create a buffer for every CPU.
306 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
311 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
314 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
315 * reliable estimate we can get but it is only exposed by the kernel
316 * since 3.14. (See Linux kernel commit:
317 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
319 ret
= utils_get_memory_available(&best_mem_info
);
325 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
326 * is a sanity check for obvious user error.
328 ret
= utils_get_memory_total(&best_mem_info
);
336 return best_mem_info
>= num_bytes_requested_total
;
340 * Try connect to session daemon with sock_path.
342 * Return 0 on success, else -1
344 static int try_connect_sessiond(const char *sock_path
)
348 /* If socket exist, we check if the daemon listens for connect. */
349 ret
= access(sock_path
, F_OK
);
355 ret
= lttcomm_connect_unix_sock(sock_path
);
361 ret
= lttcomm_close_unix_sock(ret
);
363 PERROR("lttcomm_close_unix_sock");
373 * Set sessiond socket path by putting it in the global sessiond_sock_path
376 * Returns 0 on success, negative value on failure (the sessiond socket path
377 * is somehow too long or ENOMEM).
379 static int set_session_daemon_path(void)
381 int in_tgroup
= 0; /* In tracing group. */
387 /* Are we in the tracing group ? */
388 in_tgroup
= lttng_check_tracing_group();
391 if ((uid
== 0) || in_tgroup
== 1) {
392 const int ret
= lttng_strncpy(sessiond_sock_path
,
393 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
394 sizeof(sessiond_sock_path
));
406 ret
= try_connect_sessiond(sessiond_sock_path
);
410 /* Global session daemon not available... */
412 /* ...or not in tracing group (and not root), default */
415 * With GNU C < 2.1, snprintf returns -1 if the target buffer
417 * With GNU C >= 2.1, snprintf returns the required size
418 * (excluding closing null)
420 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
421 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
422 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
434 * Connect to the LTTng session daemon.
436 * On success, return the socket's file descriptor. On error, return -1.
438 LTTNG_HIDDEN
int connect_sessiond(void)
442 ret
= set_session_daemon_path();
447 /* Connect to the sesssion daemon. */
448 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
459 static void reset_global_sessiond_connection_state(void)
461 sessiond_socket
= -1;
466 * Clean disconnect from the session daemon.
468 * On success, return 0. On error, return -1.
470 static int disconnect_sessiond(void)
475 ret
= lttcomm_close_unix_sock(sessiond_socket
);
476 reset_global_sessiond_connection_state();
482 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
490 ret
= -LTTNG_ERR_INVALID
;
500 ret
= recv_data_sessiond(buf
, len
);
506 ret
= -LTTNG_ERR_INVALID
;
510 /* Move ownership of command header buffer to user. */
515 /* No command header. */
531 * Ask the session daemon a specific command and put the data into buf.
532 * Takes extra var. len. data and file descriptors as input to send to the
535 * Return size of data (only payload, not header) or a negative error code.
538 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
539 const int *fds
, size_t nb_fd
, const void *vardata
,
540 size_t vardata_len
, void **user_payload_buf
,
541 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
545 struct lttcomm_lttng_msg llm
;
547 ret
= connect_sessiond();
549 ret
= -LTTNG_ERR_NO_SESSIOND
;
552 sessiond_socket
= ret
;
556 ret
= send_session_msg(lsm
);
558 /* Ret value is a valid lttng error code. */
561 /* Send var len data */
562 ret
= send_session_varlen(vardata
, vardata_len
);
564 /* Ret value is a valid lttng error code. */
569 ret
= send_session_fds(fds
, nb_fd
);
571 /* Ret value is a valid lttng error code. */
575 /* Get header from data transmission */
576 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
578 /* Ret value is a valid lttng error code. */
582 /* Check error code if OK */
583 if (llm
.ret_code
!= LTTNG_OK
) {
588 /* Get command header from data transmission */
589 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
590 user_cmd_header_buf
, user_cmd_header_len
);
595 /* Get payload from data transmission */
596 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
605 disconnect_sessiond();
610 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
611 struct lttng_payload
*reply
)
614 struct lttcomm_lttng_msg llm
;
615 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
617 assert(reply
->buffer
.size
== 0);
618 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
620 ret
= connect_sessiond();
622 ret
= -LTTNG_ERR_NO_SESSIOND
;
625 sessiond_socket
= ret
;
629 /* Send command to session daemon */
630 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
631 message
->buffer
.size
);
633 ret
= -LTTNG_ERR_FATAL
;
638 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
641 ret
= -LTTNG_ERR_FATAL
;
646 /* Get header from data transmission */
647 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
649 /* Ret value is a valid lttng error code. */
653 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
655 /* Check error code if OK */
656 if (llm
.ret_code
!= LTTNG_OK
) {
657 if (llm
.ret_code
< LTTNG_OK
|| llm
.ret_code
>= LTTNG_ERR_NR
) {
658 /* Invalid error code received. */
659 ret
= -LTTNG_ERR_UNK
;
666 if (llm
.cmd_header_size
> 0) {
667 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
673 /* Get command header from data transmission */
674 if (llm
.data_size
> 0) {
675 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
681 if (llm
.fd_count
> 0) {
682 ret
= lttcomm_recv_payload_fds_unix_sock(
683 sessiond_socket
, llm
.fd_count
, reply
);
689 /* Don't return the llm header to the caller. */
690 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
691 reply
->buffer
.size
- sizeof(llm
));
692 ret
= lttng_dynamic_buffer_set_size(
693 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
695 /* Can't happen as size is reduced. */
699 ret
= reply
->buffer
.size
;
702 disconnect_sessiond();
707 * Create lttng handle and return pointer.
709 * The returned pointer will be NULL in case of malloc() error.
711 struct lttng_handle
*lttng_create_handle(const char *session_name
,
712 struct lttng_domain
*domain
)
715 struct lttng_handle
*handle
= NULL
;
717 handle
= zmalloc(sizeof(struct lttng_handle
));
718 if (handle
== NULL
) {
719 PERROR("malloc handle");
723 /* Copy session name */
724 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
725 sizeof(handle
->session_name
));
730 /* Copy lttng domain or leave initialized to 0. */
732 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
743 * Destroy handle by free(3) the pointer.
745 void lttng_destroy_handle(struct lttng_handle
*handle
)
751 * Register an outside consumer.
753 * Returns size of returned session payload data or a negative error code.
755 int lttng_register_consumer(struct lttng_handle
*handle
,
756 const char *socket_path
)
759 struct lttcomm_session_msg lsm
;
761 if (handle
== NULL
|| socket_path
== NULL
) {
762 ret
= -LTTNG_ERR_INVALID
;
766 memset(&lsm
, 0, sizeof(lsm
));
767 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
768 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
769 sizeof(lsm
.session
.name
));
771 ret
= -LTTNG_ERR_INVALID
;
775 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
777 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
778 sizeof(lsm
.u
.reg
.path
));
780 ret
= -LTTNG_ERR_INVALID
;
784 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
790 * Start tracing for all traces of the session.
792 * Returns size of returned session payload data or a negative error code.
794 int lttng_start_tracing(const char *session_name
)
797 struct lttcomm_session_msg lsm
;
799 if (session_name
== NULL
) {
800 ret
= -LTTNG_ERR_INVALID
;
804 memset(&lsm
, 0, sizeof(lsm
));
805 lsm
.cmd_type
= LTTNG_START_TRACE
;
807 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
808 sizeof(lsm
.session
.name
));
810 ret
= -LTTNG_ERR_INVALID
;
814 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
820 * Stop tracing for all traces of the session.
822 static int _lttng_stop_tracing(const char *session_name
, int wait
)
825 struct lttcomm_session_msg lsm
;
827 if (session_name
== NULL
) {
828 ret
= -LTTNG_ERR_INVALID
;
832 memset(&lsm
, 0, sizeof(lsm
));
833 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
835 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
836 sizeof(lsm
.session
.name
));
838 ret
= -LTTNG_ERR_INVALID
;
842 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
843 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
851 /* Check for data availability */
853 data_ret
= lttng_data_pending(session_name
);
855 /* Return the data available call error. */
861 * Data sleep time before retrying (in usec). Don't sleep if the
862 * call returned value indicates availability.
865 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
867 } while (data_ret
!= 0);
875 * Stop tracing and wait for data availability.
877 int lttng_stop_tracing(const char *session_name
)
879 return _lttng_stop_tracing(session_name
, 1);
883 * Stop tracing but _don't_ wait for data availability.
885 int lttng_stop_tracing_no_wait(const char *session_name
)
887 return _lttng_stop_tracing(session_name
, 0);
891 * Add context to a channel.
893 * If the given channel is NULL, add the contexts to all channels.
894 * The event_name param is ignored.
896 * Returns the size of the returned payload data or a negative error code.
898 int lttng_add_context(struct lttng_handle
*handle
,
899 struct lttng_event_context
*ctx
, const char *event_name
,
900 const char *channel_name
)
905 struct lttcomm_session_msg lsm
;
907 /* Safety check. Both are mandatory. */
908 if (handle
== NULL
|| ctx
== NULL
) {
909 ret
= -LTTNG_ERR_INVALID
;
913 memset(&lsm
, 0, sizeof(lsm
));
914 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
916 /* If no channel name, send empty string. */
917 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
918 sizeof(lsm
.u
.context
.channel_name
));
920 ret
= -LTTNG_ERR_INVALID
;
924 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
925 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
926 sizeof(lsm
.session
.name
));
928 ret
= -LTTNG_ERR_INVALID
;
932 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
933 size_t provider_len
, ctx_len
;
934 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
935 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
937 if (!provider_name
|| !ctx_name
) {
938 ret
= -LTTNG_ERR_INVALID
;
942 provider_len
= strlen(provider_name
);
943 if (provider_len
== 0) {
944 ret
= -LTTNG_ERR_INVALID
;
947 lsm
.u
.context
.provider_name_len
= provider_len
;
949 ctx_len
= strlen(ctx_name
);
951 ret
= -LTTNG_ERR_INVALID
;
954 lsm
.u
.context
.context_name_len
= ctx_len
;
956 len
= provider_len
+ ctx_len
;
959 ret
= -LTTNG_ERR_NOMEM
;
963 memcpy(buf
, provider_name
, provider_len
);
964 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
966 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
968 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
970 * Don't leak application addresses to the sessiond.
971 * This is only necessary when ctx is for an app ctx otherwise
972 * the values inside the union (type & config) are overwritten.
974 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
975 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
978 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
985 * Enable event(s) for a channel.
987 * If no event name is specified, all events are enabled.
988 * If no channel name is specified, the default 'channel0' is used.
990 * Returns size of returned session payload data or a negative error code.
992 int lttng_enable_event(struct lttng_handle
*handle
,
993 struct lttng_event
*ev
, const char *channel_name
)
995 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
1000 * Create or enable an event with a filter expression.
1002 * Return negative error value on error.
1003 * Return size of returned session payload data if OK.
1005 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
1006 struct lttng_event
*event
, const char *channel_name
,
1007 const char *filter_expression
)
1009 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
1010 filter_expression
, 0, NULL
);
1014 * Depending on the event, return a newly allocated agent filter expression or
1015 * NULL if not applicable.
1017 * An event with NO loglevel and the name is * will return NULL.
1019 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
1022 char *agent_filter
= NULL
;
1026 /* Don't add filter for the '*' event. */
1027 if (strcmp(ev
->name
, "*") != 0) {
1029 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1032 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1040 /* Add loglevel filtering if any for the JUL domain. */
1041 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1044 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1050 if (filter
|| agent_filter
) {
1053 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1054 agent_filter
? agent_filter
: filter
, op
,
1059 agent_filter
= new_filter
;
1061 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1070 return agent_filter
;
1077 * Enable event(s) for a channel, possibly with exclusions and a filter.
1078 * If no event name is specified, all events are enabled.
1079 * If no channel name is specified, the default name is used.
1080 * If filter expression is not NULL, the filter is set for the event.
1081 * If exclusion count is not zero, the exclusions are set for the event.
1082 * Returns size of returned session payload data or a negative error code.
1084 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1085 struct lttng_event
*ev
, const char *channel_name
,
1086 const char *original_filter_expression
,
1087 int exclusion_count
, char **exclusion_list
)
1089 struct lttcomm_session_msg lsm
;
1090 struct lttng_payload payload
;
1092 unsigned int free_filter_expression
= 0;
1093 struct filter_parser_ctx
*ctx
= NULL
;
1096 * We have either a filter or some exclusions, so we need to set up
1097 * a variable-length payload from where to send the data.
1099 lttng_payload_init(&payload
);
1102 * Cast as non-const since we may replace the filter expression
1103 * by a dynamically allocated string. Otherwise, the original
1104 * string is not modified.
1106 char *filter_expression
= (char *) original_filter_expression
;
1108 if (handle
== NULL
|| ev
== NULL
) {
1109 ret
= -LTTNG_ERR_INVALID
;
1114 * Empty filter string will always be rejected by the parser
1115 * anyway, so treat this corner-case early to eliminate
1116 * lttng_fmemopen error for 0-byte allocation.
1118 if (filter_expression
&& filter_expression
[0] == '\0') {
1119 ret
= -LTTNG_ERR_INVALID
;
1123 memset(&lsm
, 0, sizeof(lsm
));
1125 /* If no channel name, send empty string. */
1126 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1127 sizeof(lsm
.u
.enable
.channel_name
));
1129 ret
= -LTTNG_ERR_INVALID
;
1133 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1134 if (ev
->name
[0] == '\0') {
1135 /* Enable all events. */
1136 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1140 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1141 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1143 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1144 sizeof(lsm
.session
.name
));
1146 ret
= -LTTNG_ERR_INVALID
;
1150 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1151 lsm
.u
.enable
.bytecode_len
= 0;
1153 /* Parse filter expression. */
1154 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1155 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1156 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1157 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1158 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1159 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1162 /* Setup JUL filter if needed. */
1163 agent_filter
= set_agent_filter(filter_expression
, ev
);
1164 if (!agent_filter
) {
1165 if (!filter_expression
) {
1167 * No JUL and no filter, just skip
1174 * With an agent filter, the original filter has
1175 * been added to it thus replace the filter
1178 filter_expression
= agent_filter
;
1179 free_filter_expression
= 1;
1183 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1188 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1189 + bytecode_get_len(&ctx
->bytecode
->b
);
1190 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1193 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1194 lsm
.u
.enable
.bytecode_len
+
1195 lsm
.u
.enable
.expression_len
+
1196 LTTNG_SYMBOL_NAME_LEN
*
1199 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1203 /* Put exclusion names first in the data. */
1204 for (i
= 0; i
< exclusion_count
; i
++) {
1205 size_t exclusion_len
;
1207 exclusion_len
= lttng_strnlen(exclusion_list
[i
],
1208 LTTNG_SYMBOL_NAME_LEN
);
1209 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1210 /* Exclusion is not NULL-terminated. */
1211 ret
= -LTTNG_ERR_INVALID
;
1215 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1216 exclusion_list
[i
], exclusion_len
);
1222 * Padding the rest of the entry with zeros. Every exclusion
1223 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1225 ret
= lttng_dynamic_buffer_set_size(&payload
.buffer
,
1226 LTTNG_SYMBOL_NAME_LEN
* (i
+ 1));
1232 /* Add filter expression next. */
1233 if (filter_expression
) {
1234 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1235 filter_expression
, lsm
.u
.enable
.expression_len
);
1240 /* Add filter bytecode next. */
1241 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1242 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1243 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1248 if (ev
->extended
.ptr
) {
1249 struct lttng_event_extended
*ev_ext
=
1250 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1252 if (ev_ext
->probe_location
) {
1254 * lttng_userspace_probe_location_serialize returns the
1255 * number of bytes that was appended to the buffer.
1257 ret
= lttng_userspace_probe_location_serialize(
1258 ev_ext
->probe_location
, &payload
);
1264 * Set the size of the userspace probe location element
1265 * of the buffer so that the receiving side knows where
1268 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1273 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1275 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1282 assert(fd_count
== 0 || fd_count
== 1);
1283 if (fd_count
== 1) {
1284 struct fd_handle
*h
=
1285 lttng_payload_view_pop_fd_handle(&view
);
1291 fd_to_send
= fd_handle_get_fd(h
);
1295 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1296 fd_count
? &fd_to_send
: NULL
, fd_count
,
1297 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1298 view
.buffer
.size
, NULL
, NULL
, 0);
1302 if (filter_expression
&& ctx
) {
1303 filter_bytecode_free(ctx
);
1304 filter_ir_free(ctx
);
1305 filter_parser_ctx_free(ctx
);
1308 if (free_filter_expression
) {
1310 * The filter expression has been replaced and must be freed as
1311 * it is not the original filter expression received as a
1314 free(filter_expression
);
1318 * Return directly to the caller and don't ask the sessiond since
1319 * something went wrong in the parsing of data above.
1321 lttng_payload_reset(&payload
);
1325 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1329 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1330 struct lttng_event
*ev
, const char *channel_name
,
1331 const char *original_filter_expression
)
1333 struct lttcomm_session_msg lsm
;
1336 unsigned int free_filter_expression
= 0;
1337 struct filter_parser_ctx
*ctx
= NULL
;
1339 * Cast as non-const since we may replace the filter expression
1340 * by a dynamically allocated string. Otherwise, the original
1341 * string is not modified.
1343 char *filter_expression
= (char *) original_filter_expression
;
1345 if (handle
== NULL
|| ev
== NULL
) {
1346 ret
= -LTTNG_ERR_INVALID
;
1351 * Empty filter string will always be rejected by the parser
1352 * anyway, so treat this corner-case early to eliminate
1353 * lttng_fmemopen error for 0-byte allocation.
1355 if (filter_expression
&& filter_expression
[0] == '\0') {
1356 ret
= -LTTNG_ERR_INVALID
;
1360 memset(&lsm
, 0, sizeof(lsm
));
1362 /* If no channel name, send empty string. */
1363 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1364 sizeof(lsm
.u
.disable
.channel_name
));
1366 ret
= -LTTNG_ERR_INVALID
;
1370 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1372 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1373 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1375 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1376 sizeof(lsm
.session
.name
));
1378 ret
= -LTTNG_ERR_INVALID
;
1382 lsm
.u
.disable
.bytecode_len
= 0;
1385 * For the JUL domain, a filter is enforced except for the
1386 * disable all event. This is done to avoid having the event in
1387 * all sessions thus filtering by logger name.
1389 if (filter_expression
== NULL
&&
1390 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1391 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1392 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1397 * We have a filter, so we need to set up a variable-length
1398 * memory block from where to send the data.
1401 /* Parse filter expression */
1402 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1403 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1404 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1405 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1406 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1407 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1410 /* Setup JUL filter if needed. */
1411 agent_filter
= set_agent_filter(filter_expression
, ev
);
1412 if (!agent_filter
) {
1413 if (!filter_expression
) {
1415 * No JUL and no filter, just skip
1422 * With a JUL filter, the original filter has
1423 * been added to it thus replace the filter
1426 filter_expression
= agent_filter
;
1427 free_filter_expression
= 1;
1431 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
1436 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1437 + bytecode_get_len(&ctx
->bytecode
->b
);
1438 lsm
.u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1441 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1442 + lsm
.u
.disable
.expression_len
);
1444 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1448 /* Add filter expression. */
1449 if (lsm
.u
.disable
.expression_len
!= 0) {
1452 lsm
.u
.disable
.expression_len
);
1454 /* Add filter bytecode next. */
1455 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1457 + lsm
.u
.disable
.expression_len
,
1459 lsm
.u
.disable
.bytecode_len
);
1462 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1463 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1467 if (filter_expression
&& ctx
) {
1468 filter_bytecode_free(ctx
);
1469 filter_ir_free(ctx
);
1470 filter_parser_ctx_free(ctx
);
1473 if (free_filter_expression
) {
1475 * The filter expression has been replaced and must be freed as
1476 * it is not the original filter expression received as a
1479 free(filter_expression
);
1483 * Return directly to the caller and don't ask the sessiond since
1484 * something went wrong in the parsing of data above.
1489 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1494 * Disable event(s) of a channel and domain.
1495 * If no event name is specified, all events are disabled.
1496 * If no channel name is specified, the default 'channel0' is used.
1497 * Returns size of returned session payload data or a negative error code.
1499 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1500 const char *channel_name
)
1503 struct lttng_event ev
;
1505 memset(&ev
, 0, sizeof(ev
));
1507 ev
.type
= LTTNG_EVENT_ALL
;
1508 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1510 ret
= -LTTNG_ERR_INVALID
;
1514 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1519 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1521 struct lttng_channel
*channel
= NULL
;
1522 struct lttng_channel_extended
*extended
= NULL
;
1528 /* Validate domain. */
1529 switch (domain
->type
) {
1530 case LTTNG_DOMAIN_UST
:
1531 switch (domain
->buf_type
) {
1532 case LTTNG_BUFFER_PER_UID
:
1533 case LTTNG_BUFFER_PER_PID
:
1539 case LTTNG_DOMAIN_KERNEL
:
1540 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1548 channel
= zmalloc(sizeof(*channel
));
1553 extended
= zmalloc(sizeof(*extended
));
1558 channel
->attr
.extended
.ptr
= extended
;
1560 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1568 void lttng_channel_destroy(struct lttng_channel
*channel
)
1574 if (channel
->attr
.extended
.ptr
) {
1575 free(channel
->attr
.extended
.ptr
);
1581 * Enable channel per domain
1582 * Returns size of returned session payload data or a negative error code.
1584 int lttng_enable_channel(struct lttng_handle
*handle
,
1585 struct lttng_channel
*in_chan
)
1588 struct lttcomm_session_msg lsm
;
1589 size_t total_buffer_size_needed_per_cpu
= 0;
1591 /* NULL arguments are forbidden. No default values. */
1592 if (handle
== NULL
|| in_chan
== NULL
) {
1593 return -LTTNG_ERR_INVALID
;
1596 memset(&lsm
, 0, sizeof(lsm
));
1597 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1598 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1600 if (!in_chan
->attr
.extended
.ptr
) {
1601 struct lttng_channel
*channel
;
1602 struct lttng_channel_extended
*extended
;
1604 channel
= lttng_channel_create(&handle
->domain
);
1606 return -LTTNG_ERR_NOMEM
;
1610 * Create a new channel in order to use default extended
1613 extended
= (struct lttng_channel_extended
*)
1614 channel
->attr
.extended
.ptr
;
1615 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1616 lttng_channel_destroy(channel
);
1618 struct lttng_channel_extended
*extended
;
1620 extended
= (struct lttng_channel_extended
*)
1621 in_chan
->attr
.extended
.ptr
;
1622 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1626 * Verify that the amount of memory required to create the requested
1627 * buffer is available on the system at the moment.
1629 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1630 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1631 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1632 return -LTTNG_ERR_NOMEM
;
1635 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1636 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1638 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1639 sizeof(lsm
.session
.name
));
1641 ret
= -LTTNG_ERR_INVALID
;
1645 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1651 * All tracing will be stopped for registered events of the channel.
1652 * Returns size of returned session payload data or a negative error code.
1654 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1657 struct lttcomm_session_msg lsm
;
1659 /* Safety check. Both are mandatory. */
1660 if (handle
== NULL
|| name
== NULL
) {
1661 return -LTTNG_ERR_INVALID
;
1664 memset(&lsm
, 0, sizeof(lsm
));
1666 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1668 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1669 sizeof(lsm
.u
.disable
.channel_name
));
1671 ret
= -LTTNG_ERR_INVALID
;
1675 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1677 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1678 sizeof(lsm
.session
.name
));
1680 ret
= -LTTNG_ERR_INVALID
;
1684 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1690 * Lists all available tracepoints of domain.
1691 * Sets the contents of the events array.
1692 * Returns the number of lttng_event entries in events;
1693 * on error, returns a negative value.
1695 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1696 struct lttng_event
**events
)
1699 struct lttcomm_session_msg lsm
;
1701 if (handle
== NULL
) {
1702 return -LTTNG_ERR_INVALID
;
1705 memset(&lsm
, 0, sizeof(lsm
));
1706 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1707 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1709 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1714 return ret
/ sizeof(struct lttng_event
);
1718 * Lists all available tracepoint fields of domain.
1719 * Sets the contents of the event field array.
1720 * Returns the number of lttng_event_field entries in events;
1721 * on error, returns a negative value.
1723 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1724 struct lttng_event_field
**fields
)
1727 struct lttcomm_session_msg lsm
;
1729 if (handle
== NULL
) {
1730 return -LTTNG_ERR_INVALID
;
1733 memset(&lsm
, 0, sizeof(lsm
));
1734 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1735 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1737 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1742 return ret
/ sizeof(struct lttng_event_field
);
1746 * Lists all available kernel system calls. Allocates and sets the contents of
1749 * Returns the number of lttng_event entries in events; on error, returns a
1752 int lttng_list_syscalls(struct lttng_event
**events
)
1755 struct lttcomm_session_msg lsm
;
1758 return -LTTNG_ERR_INVALID
;
1761 memset(&lsm
, 0, sizeof(lsm
));
1762 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1763 /* Force kernel domain for system calls. */
1764 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1766 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1771 return ret
/ sizeof(struct lttng_event
);
1775 * Returns a human readable string describing
1776 * the error code (a negative value).
1778 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
= -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(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
;
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
;
2105 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2110 ret
= -LTTNG_ERR_FATAL
;
2114 if (ret
% session_size
) {
2115 ret
= -LTTNG_ERR_UNK
;
2117 *out_sessions
= NULL
;
2120 session_count
= (size_t) ret
/ session_size
;
2121 sessions_extended_begin
= (struct lttng_session_extended
*)
2122 (&sessions
[session_count
]);
2124 /* Set extended session info pointers. */
2125 for (i
= 0; i
< session_count
; i
++) {
2126 struct lttng_session
*session
= &sessions
[i
];
2127 struct lttng_session_extended
*extended
=
2128 &(sessions_extended_begin
[i
]);
2130 session
->extended
.ptr
= extended
;
2133 ret
= (int) session_count
;
2134 *out_sessions
= sessions
;
2139 enum lttng_error_code
lttng_session_get_creation_time(
2140 const struct lttng_session
*session
, uint64_t *creation_time
)
2142 enum lttng_error_code ret
= LTTNG_OK
;
2143 struct lttng_session_extended
*extended
;
2145 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2146 ret
= LTTNG_ERR_INVALID
;
2150 extended
= session
->extended
.ptr
;
2151 if (!extended
->creation_time
.is_set
) {
2152 /* Not created on the session daemon yet. */
2153 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2156 *creation_time
= extended
->creation_time
.value
;
2161 int lttng_set_session_shm_path(const char *session_name
,
2162 const char *shm_path
)
2165 struct lttcomm_session_msg lsm
;
2167 if (session_name
== NULL
) {
2168 return -LTTNG_ERR_INVALID
;
2171 memset(&lsm
, 0, sizeof(lsm
));
2172 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2174 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2175 sizeof(lsm
.session
.name
));
2177 ret
= -LTTNG_ERR_INVALID
;
2181 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2182 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2184 ret
= -LTTNG_ERR_INVALID
;
2188 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2194 * Ask the session daemon for all available domains of a session.
2195 * Sets the contents of the domains array.
2196 * Returns the number of lttng_domain entries in domains;
2197 * on error, returns a negative value.
2199 int lttng_list_domains(const char *session_name
,
2200 struct lttng_domain
**domains
)
2203 struct lttcomm_session_msg lsm
;
2205 if (session_name
== NULL
) {
2206 ret
= -LTTNG_ERR_INVALID
;
2210 memset(&lsm
, 0, sizeof(lsm
));
2211 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2213 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2214 sizeof(lsm
.session
.name
));
2216 ret
= -LTTNG_ERR_INVALID
;
2220 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2225 return ret
/ sizeof(struct lttng_domain
);
2231 * Ask the session daemon for all available channels of a session.
2232 * Sets the contents of the channels array.
2233 * Returns the number of lttng_channel entries in channels;
2234 * on error, returns a negative value.
2236 int lttng_list_channels(struct lttng_handle
*handle
,
2237 struct lttng_channel
**channels
)
2240 size_t channel_count
, i
;
2241 const size_t channel_size
= sizeof(struct lttng_channel
) +
2242 sizeof(struct lttng_channel_extended
);
2243 struct lttcomm_session_msg lsm
;
2246 if (handle
== NULL
) {
2247 ret
= -LTTNG_ERR_INVALID
;
2251 memset(&lsm
, 0, sizeof(lsm
));
2252 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2253 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2254 sizeof(lsm
.session
.name
));
2256 ret
= -LTTNG_ERR_INVALID
;
2260 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2262 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2267 if (ret
% channel_size
) {
2268 ret
= -LTTNG_ERR_UNK
;
2273 channel_count
= (size_t) ret
/ channel_size
;
2275 /* Set extended info pointers */
2276 extended_at
= ((void *) *channels
) +
2277 channel_count
* sizeof(struct lttng_channel
);
2278 for (i
= 0; i
< channel_count
; i
++) {
2279 struct lttng_channel
*chan
= &(*channels
)[i
];
2281 chan
->attr
.extended
.ptr
= extended_at
;
2282 extended_at
+= sizeof(struct lttng_channel_extended
);
2285 ret
= (int) channel_count
;
2291 * Ask the session daemon for all available events of a session channel.
2292 * Sets the contents of the events array.
2293 * Returns the number of lttng_event entries in events;
2294 * on error, returns a negative value.
2296 int lttng_list_events(struct lttng_handle
*handle
,
2297 const char *channel_name
, struct lttng_event
**events
)
2300 struct lttcomm_session_msg lsm
= {};
2301 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2302 uint32_t nb_events
, i
;
2303 const void *comm_ext_at
;
2304 struct lttng_dynamic_buffer listing
;
2306 struct lttng_payload payload
;
2307 struct lttng_payload payload_copy
;
2308 struct lttng_payload_view lsm_view
=
2309 lttng_payload_view_init_from_buffer(
2310 (const char *) &lsm
, 0, sizeof(lsm
));
2311 struct lttng_buffer_view cmd_header_view
;
2312 struct lttng_buffer_view cmd_payload_view
;
2313 struct lttng_buffer_view flat_events_view
;
2314 struct lttng_buffer_view ext_view
;
2316 /* Safety check. An handle and channel name are mandatory */
2317 if (handle
== NULL
|| channel_name
== NULL
) {
2318 ret
= -LTTNG_ERR_INVALID
;
2322 lttng_payload_init(&payload
);
2323 lttng_payload_init(&payload_copy
);
2325 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2326 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2327 sizeof(lsm
.session
.name
));
2329 ret
= -LTTNG_ERR_INVALID
;
2333 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2334 sizeof(lsm
.u
.list
.channel_name
));
2336 ret
= -LTTNG_ERR_INVALID
;
2340 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2342 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2348 * A copy of the payload is performed since it will be
2349 * consumed twice. Consuming the same payload twice is invalid since
2350 * it will cause any received file descriptor to become "shared"
2351 * between different instances of the resulting objects.
2353 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2355 ret
= -LTTNG_ERR_NOMEM
;
2359 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2360 &payload
.buffer
, 0, sizeof(*cmd_header
));
2361 if (!lttng_buffer_view_is_valid(&cmd_header_view
)) {
2362 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2366 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2368 /* Set number of events and free command header */
2369 nb_events
= cmd_header
->nb_events
;
2370 if (nb_events
> INT_MAX
) {
2371 ret
= -LTTNG_ERR_OVERFLOW
;
2375 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2376 &payload
.buffer
, sizeof(*cmd_header
), -1);
2379 * The buffer that is returned must contain a "flat" version of
2380 * the events that are returned. In other words, all pointers
2381 * within an lttng_event must point to a location within the returned
2382 * buffer so that the user may free everything by simply calling free()
2383 * on the returned buffer. This is needed in order to maintain API
2386 * A first pass is performed to compute the size of the buffer that
2387 * must be allocated. A second pass is then performed to setup
2388 * the returned events so that their members always point within the
2391 * The layout of the returned buffer is as follows:
2392 * - struct lttng_event[nb_events],
2393 * - nb_events times the following:
2394 * - struct lttng_event_extended,
2395 * - flattened version of userspace_probe_location
2396 * - filter_expression
2398 * - padding to align to 64-bits
2400 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2401 nb_events
* sizeof(struct lttng_event
), -1);
2402 comm_ext_at
= ext_view
.data
;
2403 storage_req
= nb_events
* sizeof(struct lttng_event
);
2405 struct lttng_payload_view payload_view
=
2406 lttng_payload_view_from_payload(&payload
, 0, -1);
2408 for (i
= 0; i
< nb_events
; i
++) {
2409 const struct lttcomm_event_extended_header
*ext_comm
=
2410 (struct lttcomm_event_extended_header
*)
2412 int probe_storage_req
= 0;
2414 comm_ext_at
+= sizeof(*ext_comm
);
2415 comm_ext_at
+= ext_comm
->filter_len
;
2416 comm_ext_at
+= ext_comm
->nb_exclusions
*
2417 LTTNG_SYMBOL_NAME_LEN
;
2419 if (ext_comm
->userspace_probe_location_len
) {
2420 struct lttng_userspace_probe_location
2421 *probe_location
= NULL
;
2422 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2424 (const char *) comm_ext_at
-
2425 payload_view
.buffer
.data
,
2426 ext_comm
->userspace_probe_location_len
);
2428 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2429 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2434 * Create a temporary userspace probe location
2435 * to determine the size needed by a "flattened"
2436 * version of that same probe location.
2438 ret
= lttng_userspace_probe_location_create_from_payload(
2439 &probe_location_view
,
2442 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2446 ret
= lttng_userspace_probe_location_flatten(
2447 probe_location
, NULL
);
2448 lttng_userspace_probe_location_destroy(
2451 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2455 probe_storage_req
= ret
;
2456 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2459 storage_req
+= sizeof(struct lttng_event_extended
);
2460 storage_req
+= ext_comm
->filter_len
;
2461 storage_req
+= ext_comm
->nb_exclusions
*
2462 LTTNG_SYMBOL_NAME_LEN
;
2463 /* Padding to ensure the flat probe is aligned. */
2464 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2465 storage_req
+= probe_storage_req
;
2469 lttng_dynamic_buffer_init(&listing
);
2471 * We must ensure that "listing" is never resized so as to preserve
2472 * the validity of the flattened objects.
2474 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2476 ret
= -LTTNG_ERR_NOMEM
;
2480 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2481 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2482 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2483 nb_events
* sizeof(struct lttng_event
));
2484 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2486 ret
= -LTTNG_ERR_NOMEM
;
2487 goto free_dynamic_buffer
;
2490 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2491 nb_events
* sizeof(struct lttng_event
), -1);
2492 comm_ext_at
= ext_view
.data
;
2495 struct lttng_payload_view payload_copy_view
=
2496 lttng_payload_view_from_payload(
2497 &payload_copy
, 0, -1);
2499 for (i
= 0; i
< nb_events
; i
++) {
2500 struct lttng_event
*event
= (typeof(event
))(
2502 (sizeof(struct lttng_event
) * i
));
2503 const struct lttcomm_event_extended_header
*ext_comm
=
2504 (typeof(ext_comm
)) comm_ext_at
;
2505 struct lttng_event_extended
*event_extended
=
2506 (typeof(event_extended
))(listing
.data
+
2509 /* Insert struct lttng_event_extended. */
2510 ret
= lttng_dynamic_buffer_set_size(&listing
,
2511 listing
.size
+ sizeof(*event_extended
));
2513 ret
= -LTTNG_ERR_NOMEM
;
2514 goto free_dynamic_buffer
;
2516 event
->extended
.ptr
= event_extended
;
2518 comm_ext_at
+= sizeof(*ext_comm
);
2520 /* Insert filter expression. */
2521 if (ext_comm
->filter_len
) {
2522 event_extended
->filter_expression
=
2523 listing
.data
+ listing
.size
;
2524 ret
= lttng_dynamic_buffer_append(&listing
,
2526 ext_comm
->filter_len
);
2528 ret
= -LTTNG_ERR_NOMEM
;
2529 goto free_dynamic_buffer
;
2531 comm_ext_at
+= ext_comm
->filter_len
;
2534 /* Insert exclusions. */
2535 if (ext_comm
->nb_exclusions
) {
2536 event_extended
->exclusions
.count
=
2537 ext_comm
->nb_exclusions
;
2538 event_extended
->exclusions
.strings
=
2539 listing
.data
+ listing
.size
;
2541 ret
= lttng_dynamic_buffer_append(&listing
,
2543 ext_comm
->nb_exclusions
*
2544 LTTNG_SYMBOL_NAME_LEN
);
2546 ret
= -LTTNG_ERR_NOMEM
;
2547 goto free_dynamic_buffer
;
2549 comm_ext_at
+= ext_comm
->nb_exclusions
*
2550 LTTNG_SYMBOL_NAME_LEN
;
2553 /* Insert padding to align to 64-bits. */
2554 ret
= lttng_dynamic_buffer_set_size(&listing
,
2555 ALIGN_TO(listing
.size
,
2558 ret
= -LTTNG_ERR_NOMEM
;
2559 goto free_dynamic_buffer
;
2562 /* Insert flattened userspace probe location. */
2563 if (ext_comm
->userspace_probe_location_len
) {
2564 struct lttng_userspace_probe_location
2565 *probe_location
= NULL
;
2566 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2568 (const char *) comm_ext_at
-
2569 payload_copy_view
.buffer
.data
,
2570 ext_comm
->userspace_probe_location_len
);
2572 if (!lttng_payload_view_is_valid(&probe_location_view
)) {
2573 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2574 goto free_dynamic_buffer
;
2577 ret
= lttng_userspace_probe_location_create_from_payload(
2578 &probe_location_view
,
2581 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2582 goto free_dynamic_buffer
;
2585 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2588 ret
= lttng_userspace_probe_location_flatten(
2589 probe_location
, &listing
);
2590 lttng_userspace_probe_location_destroy(
2593 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2594 goto free_dynamic_buffer
;
2597 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2602 /* Don't reset listing buffer as we return its content. */
2603 *events
= (struct lttng_event
*) listing
.data
;
2604 lttng_dynamic_buffer_init(&listing
);
2605 ret
= (int) nb_events
;
2606 free_dynamic_buffer
:
2607 lttng_dynamic_buffer_reset(&listing
);
2609 lttng_payload_reset(&payload
);
2610 lttng_payload_reset(&payload_copy
);
2615 * Sets the tracing_group variable with name.
2616 * This function allocates memory pointed to by tracing_group.
2617 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2619 int lttng_set_tracing_group(const char *name
)
2622 return -LTTNG_ERR_INVALID
;
2625 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2626 return -LTTNG_ERR_FATAL
;
2632 int lttng_calibrate(struct lttng_handle
*handle
,
2633 struct lttng_calibrate
*calibrate
)
2636 * This command was removed in LTTng 2.9.
2638 return -LTTNG_ERR_UND
;
2642 * Set default channel attributes.
2643 * If either or both of the arguments are null, attr content is zeroe'd.
2645 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2646 struct lttng_channel_attr
*attr
)
2648 struct lttng_channel_extended
*extended
;
2651 if (attr
== NULL
|| domain
== NULL
) {
2655 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2656 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2658 /* Same for all domains. */
2659 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2660 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2661 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2663 switch (domain
->type
) {
2664 case LTTNG_DOMAIN_KERNEL
:
2665 attr
->switch_timer_interval
=
2666 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2667 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2668 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2669 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2670 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2672 extended
->monitor_timer_interval
=
2673 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2674 extended
->blocking_timeout
=
2675 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2678 case LTTNG_DOMAIN_UST
:
2679 switch (domain
->buf_type
) {
2680 case LTTNG_BUFFER_PER_UID
:
2681 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2682 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2683 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2684 attr
->switch_timer_interval
=
2685 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2686 attr
->read_timer_interval
=
2687 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2689 extended
->monitor_timer_interval
=
2690 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2691 extended
->blocking_timeout
=
2692 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2695 case LTTNG_BUFFER_PER_PID
:
2697 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2698 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2699 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2700 attr
->switch_timer_interval
=
2701 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2702 attr
->read_timer_interval
=
2703 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2705 extended
->monitor_timer_interval
=
2706 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2707 extended
->blocking_timeout
=
2708 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2713 /* Default behavior: leave set to 0. */
2717 attr
->extended
.ptr
= extended
;
2720 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2721 uint64_t *discarded_events
)
2724 struct lttng_channel_extended
*chan_ext
;
2726 if (!channel
|| !discarded_events
) {
2727 ret
= -LTTNG_ERR_INVALID
;
2731 chan_ext
= channel
->attr
.extended
.ptr
;
2734 * This can happen since the lttng_channel structure is
2735 * used for other tasks where this pointer is never set.
2737 *discarded_events
= 0;
2741 *discarded_events
= chan_ext
->discarded_events
;
2746 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2747 uint64_t *lost_packets
)
2750 struct lttng_channel_extended
*chan_ext
;
2752 if (!channel
|| !lost_packets
) {
2753 ret
= -LTTNG_ERR_INVALID
;
2757 chan_ext
= channel
->attr
.extended
.ptr
;
2760 * This can happen since the lttng_channel structure is
2761 * used for other tasks where this pointer is never set.
2767 *lost_packets
= chan_ext
->lost_packets
;
2772 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2773 uint64_t *monitor_timer_interval
)
2777 if (!chan
|| !monitor_timer_interval
) {
2778 ret
= -LTTNG_ERR_INVALID
;
2782 if (!chan
->attr
.extended
.ptr
) {
2783 ret
= -LTTNG_ERR_INVALID
;
2787 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2788 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2793 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2794 uint64_t monitor_timer_interval
)
2798 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2799 ret
= -LTTNG_ERR_INVALID
;
2803 ((struct lttng_channel_extended
*)
2804 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2805 monitor_timer_interval
;
2810 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2811 int64_t *blocking_timeout
)
2815 if (!chan
|| !blocking_timeout
) {
2816 ret
= -LTTNG_ERR_INVALID
;
2820 if (!chan
->attr
.extended
.ptr
) {
2821 ret
= -LTTNG_ERR_INVALID
;
2825 *blocking_timeout
= ((struct lttng_channel_extended
*)
2826 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2831 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2832 int64_t blocking_timeout
)
2835 int64_t msec_timeout
;
2837 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2838 ret
= -LTTNG_ERR_INVALID
;
2842 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2843 ret
= -LTTNG_ERR_INVALID
;
2848 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2849 * us to accept a narrower range of values (msecs expressed as a signed
2852 msec_timeout
= blocking_timeout
/ 1000;
2853 if (msec_timeout
!= (int32_t) msec_timeout
) {
2854 ret
= -LTTNG_ERR_INVALID
;
2858 ((struct lttng_channel_extended
*)
2859 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2866 * Check if session daemon is alive.
2868 * Return 1 if alive or 0 if not.
2869 * On error returns a negative value.
2871 int lttng_session_daemon_alive(void)
2875 ret
= set_session_daemon_path();
2881 if (*sessiond_sock_path
== '\0') {
2883 * No socket path set. Weird error which means the constructor
2889 ret
= try_connect_sessiond(sessiond_sock_path
);
2900 * Set URL for a consumer for a session and domain.
2902 * Return 0 on success, else a negative value.
2904 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2905 const char *control_url
, const char *data_url
)
2909 struct lttcomm_session_msg lsm
;
2910 struct lttng_uri
*uris
= NULL
;
2912 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2913 ret
= -LTTNG_ERR_INVALID
;
2917 memset(&lsm
, 0, sizeof(lsm
));
2919 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2921 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2922 sizeof(lsm
.session
.name
));
2924 ret
= -LTTNG_ERR_INVALID
;
2928 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2930 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2932 ret
= -LTTNG_ERR_INVALID
;
2936 lsm
.u
.uri
.size
= size
;
2938 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2939 sizeof(struct lttng_uri
) * size
, NULL
);
2949 int lttng_enable_consumer(struct lttng_handle
*handle
);
2950 int lttng_enable_consumer(struct lttng_handle
*handle
)
2958 int lttng_disable_consumer(struct lttng_handle
*handle
);
2959 int lttng_disable_consumer(struct lttng_handle
*handle
)
2967 int _lttng_create_session_ext(const char *name
, const char *url
,
2968 const char *datetime
);
2969 int _lttng_create_session_ext(const char *name
, const char *url
,
2970 const char *datetime
)
2976 * For a given session name, this call checks if the data is ready to be read
2977 * or is still being extracted by the consumer(s) hence not ready to be used by
2980 int lttng_data_pending(const char *session_name
)
2983 struct lttcomm_session_msg lsm
;
2984 uint8_t *pending
= NULL
;
2986 if (session_name
== NULL
) {
2987 return -LTTNG_ERR_INVALID
;
2990 memset(&lsm
, 0, sizeof(lsm
));
2991 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2993 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2994 sizeof(lsm
.session
.name
));
2996 ret
= -LTTNG_ERR_INVALID
;
3000 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
3003 } else if (ret
!= 1) {
3004 /* Unexpected payload size */
3005 ret
= -LTTNG_ERR_INVALID
;
3007 } else if (!pending
) {
3008 /* Internal error. */
3009 ret
= -LTTNG_ERR_UNK
;
3013 ret
= (int) *pending
;
3020 * Regenerate the metadata for a session.
3021 * Return 0 on success, a negative error code on error.
3023 int lttng_regenerate_metadata(const char *session_name
)
3026 struct lttcomm_session_msg lsm
;
3028 if (!session_name
) {
3029 ret
= -LTTNG_ERR_INVALID
;
3033 memset(&lsm
, 0, sizeof(lsm
));
3034 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3036 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3037 sizeof(lsm
.session
.name
));
3039 ret
= -LTTNG_ERR_INVALID
;
3043 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3054 * Deprecated, replaced by lttng_regenerate_metadata.
3056 int lttng_metadata_regenerate(const char *session_name
)
3058 return lttng_regenerate_metadata(session_name
);
3062 * Regenerate the statedump of a session.
3063 * Return 0 on success, a negative error code on error.
3065 int lttng_regenerate_statedump(const char *session_name
)
3068 struct lttcomm_session_msg lsm
;
3070 if (!session_name
) {
3071 ret
= -LTTNG_ERR_INVALID
;
3075 memset(&lsm
, 0, sizeof(lsm
));
3076 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3078 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3079 sizeof(lsm
.session
.name
));
3081 ret
= -LTTNG_ERR_INVALID
;
3085 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3096 int _lttng_register_trigger(struct lttng_trigger
*trigger
, const char *name
,
3100 struct lttcomm_session_msg lsm
= {
3101 .cmd_type
= LTTNG_REGISTER_TRIGGER
,
3102 .u
.trigger
.is_trigger_anonymous
= !name
&& !generate_name
,
3104 struct lttcomm_session_msg
*message_lsm
;
3105 struct lttng_payload message
;
3106 struct lttng_payload reply
;
3107 struct lttng_trigger
*reply_trigger
= NULL
;
3108 enum lttng_domain_type domain_type
;
3109 const struct lttng_credentials user_creds
= {
3110 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3111 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3113 const char *unused_trigger_name
= NULL
;
3114 enum lttng_trigger_status trigger_status
;
3116 lttng_payload_init(&message
);
3117 lttng_payload_init(&reply
);
3120 ret
= -LTTNG_ERR_INVALID
;
3124 trigger_status
= lttng_trigger_get_name(trigger
, &unused_trigger_name
);
3125 if (trigger_status
!= LTTNG_TRIGGER_STATUS_UNSET
) {
3126 /* Re-using already registered trigger. */
3127 ret
= -LTTNG_ERR_INVALID
;
3132 trigger_status
= lttng_trigger_set_name(trigger
, name
);
3133 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3134 ret
= -LTTNG_ERR_NOMEM
;
3139 if (!trigger
->creds
.uid
.is_set
) {
3140 /* Use the client's credentials as the trigger credentials. */
3141 lttng_trigger_set_credentials(trigger
, &user_creds
);
3144 * Validate that either the current trigger credentials and the
3145 * client credentials are identical or that the current user is
3146 * root. The root user can register, unregister triggers for
3147 * himself and other users.
3149 * This check is also present on the sessiond side, using the
3150 * credentials passed on the socket. These check are all
3153 const struct lttng_credentials
*trigger_creds
=
3154 lttng_trigger_get_credentials(trigger
);
3156 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3157 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3158 ret
= -LTTNG_ERR_EPERM
;
3159 goto end_unset_name
;
3164 if (!lttng_trigger_validate(trigger
)) {
3165 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3166 goto end_unset_name
;
3169 domain_type
= lttng_trigger_get_underlying_domain_type_restriction(
3172 lsm
.domain
.type
= domain_type
;
3174 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3176 ret
= -LTTNG_ERR_NOMEM
;
3177 goto end_unset_name
;
3180 ret
= lttng_trigger_serialize(trigger
, &message
);
3182 ret
= -LTTNG_ERR_UNK
;
3183 goto end_unset_name
;
3187 * This is needed to populate the trigger object size for the command
3190 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3192 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3195 struct lttng_payload_view message_view
=
3196 lttng_payload_view_from_payload(
3199 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3201 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3203 goto end_unset_name
;
3208 struct lttng_payload_view reply_view
=
3209 lttng_payload_view_from_payload(
3210 &reply
, 0, reply
.buffer
.size
);
3212 ret
= lttng_trigger_create_from_payload(
3213 &reply_view
, &reply_trigger
);
3215 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
3216 goto end_unset_name
;
3220 if (name
|| generate_name
) {
3221 ret
= lttng_trigger_assign_name(trigger
, reply_trigger
);
3223 ret
= -LTTNG_ERR_NOMEM
;
3232 trigger_status
= lttng_trigger_set_name(trigger
, NULL
);
3233 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
3234 ret
= -LTTNG_ERR_UNK
;
3237 lttng_payload_reset(&message
);
3238 lttng_payload_reset(&reply
);
3239 lttng_trigger_destroy(reply_trigger
);
3243 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3245 /* Register an anonymous trigger. */
3246 return _lttng_register_trigger(trigger
, NULL
, false);
3249 enum lttng_error_code
lttng_register_trigger_with_name(
3250 struct lttng_trigger
*trigger
, const char *name
)
3252 const int ret
= _lttng_register_trigger(trigger
, name
, false);
3254 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3257 enum lttng_error_code
lttng_register_trigger_with_automatic_name(
3258 struct lttng_trigger
*trigger
)
3260 const int ret
= _lttng_register_trigger(trigger
, false, true);
3262 return ret
== 0 ? LTTNG_OK
: (enum lttng_error_code
) -ret
;
3265 enum lttng_error_code
lttng_error_query_execute(
3266 const struct lttng_error_query
*query
,
3267 const struct lttng_endpoint
*endpoint
,
3268 struct lttng_error_query_results
**results
)
3271 enum lttng_error_code ret_code
;
3272 struct lttcomm_session_msg lsm
= {
3273 .cmd_type
= LTTNG_EXECUTE_ERROR_QUERY
,
3275 struct lttng_payload message
;
3276 struct lttng_payload reply
;
3277 struct lttcomm_session_msg
*message_lsm
;
3279 lttng_payload_init(&message
);
3280 lttng_payload_init(&reply
);
3282 if (!query
|| !results
) {
3283 ret_code
= LTTNG_ERR_INVALID
;
3287 if (endpoint
!= lttng_session_daemon_command_endpoint
) {
3288 ret_code
= LTTNG_ERR_INVALID_ERROR_QUERY_TARGET
;
3292 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3294 ret_code
= LTTNG_ERR_NOMEM
;
3298 ret
= lttng_error_query_serialize(query
, &message
);
3300 ret_code
= LTTNG_ERR_UNK
;
3304 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3305 message_lsm
->u
.error_query
.length
=
3306 (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3309 struct lttng_payload_view message_view
=
3310 lttng_payload_view_from_payload(
3313 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3315 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3323 ssize_t reply_create_ret
;
3324 struct lttng_payload_view reply_view
=
3325 lttng_payload_view_from_payload(
3326 &reply
, 0, reply
.buffer
.size
);
3328 reply_create_ret
= lttng_error_query_results_create_from_payload(
3329 &reply_view
, results
);
3330 if (reply_create_ret
< 0) {
3331 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3336 ret_code
= LTTNG_OK
;
3338 lttng_payload_reset(&message
);
3339 lttng_payload_reset(&reply
);
3343 int lttng_unregister_trigger(const struct lttng_trigger
*trigger
)
3346 struct lttcomm_session_msg lsm
;
3347 struct lttcomm_session_msg
*message_lsm
;
3348 struct lttng_payload message
;
3349 struct lttng_payload reply
;
3350 struct lttng_trigger
*copy
= NULL
;
3351 const struct lttng_credentials user_creds
= {
3352 .uid
= LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3353 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
3356 lttng_payload_init(&message
);
3357 lttng_payload_init(&reply
);
3360 ret
= -LTTNG_ERR_INVALID
;
3364 copy
= lttng_trigger_copy(trigger
);
3366 ret
= -LTTNG_ERR_UNK
;
3370 if (!copy
->creds
.uid
.is_set
) {
3371 /* Use the client credentials as the trigger credentials */
3372 lttng_trigger_set_credentials(copy
, &user_creds
);
3375 * Validate that either the current trigger credentials and the
3376 * client credentials are identical or that the current user is
3377 * root. The root user can register, unregister triggers for
3378 * himself and other users.
3380 * This check is also present on the sessiond side, using the
3381 * credentials passed on the socket. These check are all
3384 const struct lttng_credentials
*trigger_creds
=
3385 lttng_trigger_get_credentials(copy
);
3386 if (!lttng_credentials_is_equal_uid(trigger_creds
, &user_creds
)) {
3387 if (lttng_credentials_get_uid(&user_creds
) != 0) {
3388 ret
= -LTTNG_ERR_EPERM
;
3394 if (!lttng_trigger_validate(copy
)) {
3395 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3399 memset(&lsm
, 0, sizeof(lsm
));
3400 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3402 ret
= lttng_dynamic_buffer_append(&message
.buffer
, &lsm
, sizeof(lsm
));
3404 ret
= -LTTNG_ERR_NOMEM
;
3408 ret
= lttng_trigger_serialize(copy
, &message
);
3410 ret
= -LTTNG_ERR_UNK
;
3415 * This is needed to populate the trigger object size for the command
3416 * header and number of fds sent.
3418 message_lsm
= (struct lttcomm_session_msg
*) message
.buffer
.data
;
3420 message_lsm
->u
.trigger
.length
= (uint32_t) message
.buffer
.size
- sizeof(lsm
);
3423 struct lttng_payload_view message_view
=
3424 lttng_payload_view_from_payload(
3428 * Update the message header with the number of fd that will be
3431 message_lsm
->fd_count
= lttng_payload_view_get_fd_handle_count(
3434 ret
= lttng_ctl_ask_sessiond_payload(&message_view
, &reply
);
3442 lttng_trigger_destroy(copy
);
3443 lttng_payload_reset(&message
);
3444 lttng_payload_reset(&reply
);
3449 * Ask the session daemon for all registered triggers for the current user.
3451 * Allocates and return an lttng_triggers set.
3452 * On error, returns a suitable lttng_error_code.
3454 enum lttng_error_code
lttng_list_triggers(struct lttng_triggers
**triggers
)
3457 enum lttng_error_code ret_code
= LTTNG_OK
;
3458 struct lttcomm_session_msg lsm
= { .cmd_type
= LTTNG_LIST_TRIGGERS
};
3459 struct lttng_triggers
*local_triggers
= NULL
;
3460 struct lttng_payload reply
;
3461 struct lttng_payload_view lsm_view
=
3462 lttng_payload_view_init_from_buffer(
3463 (const char *) &lsm
, 0, sizeof(lsm
));
3465 lttng_payload_init(&reply
);
3467 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &reply
);
3469 ret_code
= (enum lttng_error_code
) -ret
;
3474 struct lttng_payload_view reply_view
=
3475 lttng_payload_view_from_payload(
3476 &reply
, 0, reply
.buffer
.size
);
3478 ret
= lttng_triggers_create_from_payload(
3479 &reply_view
, &local_triggers
);
3481 ret_code
= LTTNG_ERR_FATAL
;
3486 *triggers
= local_triggers
;
3487 local_triggers
= NULL
;
3489 lttng_payload_reset(&reply
);
3490 lttng_triggers_destroy(local_triggers
);
3497 static void __attribute__((constructor
)) init(void)
3499 /* Set default session group */
3500 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3506 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3508 free(tracing_group
);