4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
22 #include <common/common.h>
23 #include <common/compat/string.h>
24 #include <common/defaults.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/dynamic-array.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/tracker.h>
31 #include <common/unix.h>
32 #include <common/uri.h>
33 #include <common/utils.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/destruction-handle.h>
36 #include <lttng/endpoint.h>
37 #include <lttng/event-internal.h>
38 #include <lttng/health-internal.h>
39 #include <lttng/lttng.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/trigger/trigger-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/lttng-error.h>
46 #include "filter/filter-ast.h"
47 #include "filter/filter-parser.h"
48 #include "filter/filter-bytecode.h"
49 #include "filter/memstream.h"
50 #include "lttng-ctl-helper.h"
53 static const int print_xml
= 1;
54 #define dbg_printf(fmt, args...) \
55 printf("[debug liblttng-ctl] " fmt, ## args)
57 static const int print_xml
= 0;
58 #define dbg_printf(fmt, args...) \
60 /* do nothing but check printf format */ \
62 printf("[debug liblttnctl] " fmt, ## args); \
66 #define COPY_DOMAIN_PACKED(dst, src) \
68 struct lttng_domain _tmp_domain; \
70 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
74 /* Socket to session daemon for communication */
75 static int sessiond_socket
= -1;
76 static char sessiond_sock_path
[PATH_MAX
];
79 static char *tracing_group
;
85 * Those two variables are used by error.h to silent or control the verbosity of
86 * error message. They are global to the library so application linking with it
87 * are able to compile correctly and also control verbosity of the library.
90 int lttng_opt_verbose
;
94 * Copy string from src to dst and enforce null terminated byte.
97 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
100 strncpy(dst
, src
, len
);
101 /* Enforce the NULL terminated byte */
109 * Copy domain to lttcomm_session_msg domain.
111 * If domain is unknown, default domain will be the kernel.
114 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
115 struct lttng_domain
*src
)
119 case LTTNG_DOMAIN_KERNEL
:
120 case LTTNG_DOMAIN_UST
:
121 case LTTNG_DOMAIN_JUL
:
122 case LTTNG_DOMAIN_LOG4J
:
123 case LTTNG_DOMAIN_PYTHON
:
124 memcpy(dst
, src
, sizeof(struct lttng_domain
));
127 memset(dst
, 0, sizeof(struct lttng_domain
));
134 * Send lttcomm_session_msg to the session daemon.
136 * On success, returns the number of bytes sent (>=0)
137 * On error, returns -1
139 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
144 ret
= -LTTNG_ERR_NO_SESSIOND
;
148 DBG("LSM cmd type : %d", lsm
->cmd_type
);
150 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
151 sizeof(struct lttcomm_session_msg
));
153 ret
= -LTTNG_ERR_FATAL
;
161 * Send var len data to the session daemon.
163 * On success, returns the number of bytes sent (>=0)
164 * On error, returns -1
166 static int send_session_varlen(const void *data
, size_t len
)
171 ret
= -LTTNG_ERR_NO_SESSIOND
;
180 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
182 ret
= -LTTNG_ERR_FATAL
;
190 * Send file descriptors to the session daemon.
192 * On success, returns the number of bytes sent (>=0)
193 * On error, returns -1
195 static int send_session_fds(const int *fds
, size_t nb_fd
)
200 ret
= -LTTNG_ERR_NO_SESSIOND
;
204 if (!fds
|| !nb_fd
) {
209 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
211 ret
= -LTTNG_ERR_FATAL
;
219 * Receive data from the sessiond socket.
221 * On success, returns the number of bytes received (>=0)
222 * On error, returns a negative lttng_error_code.
224 static int recv_data_sessiond(void *buf
, size_t len
)
229 ret
= -LTTNG_ERR_NO_SESSIOND
;
233 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
235 ret
= -LTTNG_ERR_FATAL
;
243 * Receive a payload from the session daemon by appending to an existing
245 * On success, returns the number of bytes received (>=0)
246 * On error, returns a negative lttng_error_code.
248 static int recv_payload_sessiond(struct lttng_payload
*payload
, size_t len
)
251 const size_t original_payload_size
= payload
->buffer
.size
;
253 ret
= lttng_dynamic_buffer_set_size(
254 &payload
->buffer
, payload
->buffer
.size
+ len
);
256 ret
= -LTTNG_ERR_NOMEM
;
260 ret
= recv_data_sessiond(
261 payload
->buffer
.data
+ original_payload_size
, len
);
267 * Check if we are in the specified group.
269 * If yes return 1, else return -1.
272 int lttng_check_tracing_group(void)
274 gid_t
*grp_list
, tracing_gid
;
275 int grp_list_size
, grp_id
, i
;
277 const char *grp_name
= tracing_group
;
279 /* Get GID of group 'tracing' */
280 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
281 /* If grp_tracing is NULL, the group does not exist. */
285 /* Get number of supplementary group IDs */
286 grp_list_size
= getgroups(0, NULL
);
287 if (grp_list_size
< 0) {
292 /* Alloc group list of the right size */
293 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
298 grp_id
= getgroups(grp_list_size
, grp_list
);
304 for (i
= 0; i
< grp_list_size
; i
++) {
305 if (grp_list
[i
] == tracing_gid
) {
318 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
322 size_t best_mem_info
;
323 size_t num_bytes_requested_total
;
326 * Get the number of CPU currently online to compute the amount of
327 * memory needed to create a buffer for every CPU.
329 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
334 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
337 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
338 * reliable estimate we can get but it is only exposed by the kernel
339 * since 3.14. (See Linux kernel commit:
340 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
342 ret
= utils_get_memory_available(&best_mem_info
);
348 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
349 * is a sanity check for obvious user error.
351 ret
= utils_get_memory_total(&best_mem_info
);
359 return best_mem_info
>= num_bytes_requested_total
;
363 * Try connect to session daemon with sock_path.
365 * Return 0 on success, else -1
367 static int try_connect_sessiond(const char *sock_path
)
371 /* If socket exist, we check if the daemon listens for connect. */
372 ret
= access(sock_path
, F_OK
);
378 ret
= lttcomm_connect_unix_sock(sock_path
);
384 ret
= lttcomm_close_unix_sock(ret
);
386 PERROR("lttcomm_close_unix_sock");
396 * Set sessiond socket path by putting it in the global sessiond_sock_path
399 * Returns 0 on success, negative value on failure (the sessiond socket path
400 * is somehow too long or ENOMEM).
402 static int set_session_daemon_path(void)
404 int in_tgroup
= 0; /* In tracing group. */
410 /* Are we in the tracing group ? */
411 in_tgroup
= lttng_check_tracing_group();
414 if ((uid
== 0) || in_tgroup
) {
415 lttng_ctl_copy_string(sessiond_sock_path
,
416 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
424 ret
= try_connect_sessiond(sessiond_sock_path
);
428 /* Global session daemon not available... */
430 /* ...or not in tracing group (and not root), default */
433 * With GNU C < 2.1, snprintf returns -1 if the target buffer
435 * With GNU C >= 2.1, snprintf returns the required size
436 * (excluding closing null)
438 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
439 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
440 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
452 * Connect to the LTTng session daemon.
454 * On success, return the socket's file descriptor. On error, return -1.
456 LTTNG_HIDDEN
int connect_sessiond(void)
460 ret
= set_session_daemon_path();
465 /* Connect to the sesssion daemon. */
466 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
477 static void reset_global_sessiond_connection_state(void)
479 sessiond_socket
= -1;
484 * Clean disconnect from the session daemon.
486 * On success, return 0. On error, return -1.
488 static int disconnect_sessiond(void)
493 ret
= lttcomm_close_unix_sock(sessiond_socket
);
494 reset_global_sessiond_connection_state();
500 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
508 ret
= -LTTNG_ERR_INVALID
;
518 ret
= recv_data_sessiond(buf
, len
);
524 ret
= -LTTNG_ERR_INVALID
;
528 /* Move ownership of command header buffer to user. */
533 /* No command header. */
549 * Ask the session daemon a specific command and put the data into buf.
550 * Takes extra var. len. data and file descriptors as input to send to the
553 * Return size of data (only payload, not header) or a negative error code.
556 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
557 const int *fds
, size_t nb_fd
, const void *vardata
,
558 size_t vardata_len
, void **user_payload_buf
,
559 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
563 struct lttcomm_lttng_msg llm
;
565 ret
= connect_sessiond();
567 ret
= -LTTNG_ERR_NO_SESSIOND
;
570 sessiond_socket
= ret
;
574 ret
= send_session_msg(lsm
);
576 /* Ret value is a valid lttng error code. */
579 /* Send var len data */
580 ret
= send_session_varlen(vardata
, vardata_len
);
582 /* Ret value is a valid lttng error code. */
587 ret
= send_session_fds(fds
, nb_fd
);
589 /* Ret value is a valid lttng error code. */
593 /* Get header from data transmission */
594 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
596 /* Ret value is a valid lttng error code. */
600 /* Check error code if OK */
601 if (llm
.ret_code
!= LTTNG_OK
) {
606 /* Get command header from data transmission */
607 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
608 user_cmd_header_buf
, user_cmd_header_len
);
613 /* Get payload from data transmission */
614 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
623 disconnect_sessiond();
628 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view
*message
,
629 struct lttng_payload
*reply
)
632 struct lttcomm_lttng_msg llm
;
633 const int fd_count
= lttng_payload_view_get_fd_handle_count(message
);
635 assert(reply
->buffer
.size
== 0);
636 assert(lttng_dynamic_pointer_array_get_count(&reply
->_fd_handles
) == 0);
638 ret
= connect_sessiond();
640 ret
= -LTTNG_ERR_NO_SESSIOND
;
643 sessiond_socket
= ret
;
647 /* Send command to session daemon */
648 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, message
->buffer
.data
,
649 message
->buffer
.size
);
651 ret
= -LTTNG_ERR_FATAL
;
656 ret
= lttcomm_send_payload_view_fds_unix_sock(sessiond_socket
,
659 ret
= -LTTNG_ERR_FATAL
;
664 /* Get header from data transmission */
665 ret
= recv_payload_sessiond(reply
, sizeof(llm
));
667 /* Ret value is a valid lttng error code. */
671 llm
= *((typeof(llm
) *) reply
->buffer
.data
);
673 /* Check error code if OK */
674 if (llm
.ret_code
!= LTTNG_OK
) {
679 if (llm
.cmd_header_size
> 0) {
680 ret
= recv_payload_sessiond(reply
, llm
.cmd_header_size
);
686 /* Get command header from data transmission */
687 if (llm
.data_size
> 0) {
688 ret
= recv_payload_sessiond(reply
, llm
.data_size
);
694 if (llm
.fd_count
> 0) {
695 ret
= lttcomm_recv_payload_fds_unix_sock(
696 sessiond_socket
, llm
.fd_count
, reply
);
702 /* Don't return the llm header to the caller. */
703 memmove(reply
->buffer
.data
, reply
->buffer
.data
+ sizeof(llm
),
704 reply
->buffer
.size
- sizeof(llm
));
705 ret
= lttng_dynamic_buffer_set_size(
706 &reply
->buffer
, reply
->buffer
.size
- sizeof(llm
));
708 /* Can't happen as size is reduced. */
712 ret
= reply
->buffer
.size
;
715 disconnect_sessiond();
720 * Create lttng handle and return pointer.
722 * The returned pointer will be NULL in case of malloc() error.
724 struct lttng_handle
*lttng_create_handle(const char *session_name
,
725 struct lttng_domain
*domain
)
727 struct lttng_handle
*handle
= NULL
;
729 handle
= zmalloc(sizeof(struct lttng_handle
));
730 if (handle
== NULL
) {
731 PERROR("malloc handle");
735 /* Copy session name */
736 lttng_ctl_copy_string(handle
->session_name
, session_name
,
737 sizeof(handle
->session_name
));
739 /* Copy lttng domain or leave initialized to 0. */
741 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
749 * Destroy handle by free(3) the pointer.
751 void lttng_destroy_handle(struct lttng_handle
*handle
)
757 * Register an outside consumer.
759 * Returns size of returned session payload data or a negative error code.
761 int lttng_register_consumer(struct lttng_handle
*handle
,
762 const char *socket_path
)
764 struct lttcomm_session_msg lsm
;
766 if (handle
== NULL
|| socket_path
== NULL
) {
767 return -LTTNG_ERR_INVALID
;
770 memset(&lsm
, 0, sizeof(lsm
));
771 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
772 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
773 sizeof(lsm
.session
.name
));
774 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
776 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
777 sizeof(lsm
.u
.reg
.path
));
779 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
783 * Start tracing for all traces of the session.
785 * Returns size of returned session payload data or a negative error code.
787 int lttng_start_tracing(const char *session_name
)
789 struct lttcomm_session_msg lsm
;
791 if (session_name
== NULL
) {
792 return -LTTNG_ERR_INVALID
;
795 memset(&lsm
, 0, sizeof(lsm
));
796 lsm
.cmd_type
= LTTNG_START_TRACE
;
798 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
799 sizeof(lsm
.session
.name
));
801 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
805 * Stop tracing for all traces of the session.
807 static int _lttng_stop_tracing(const char *session_name
, int wait
)
810 struct lttcomm_session_msg lsm
;
812 if (session_name
== NULL
) {
813 return -LTTNG_ERR_INVALID
;
816 memset(&lsm
, 0, sizeof(lsm
));
817 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
819 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
820 sizeof(lsm
.session
.name
));
822 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
823 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
831 /* Check for data availability */
833 data_ret
= lttng_data_pending(session_name
);
835 /* Return the data available call error. */
841 * Data sleep time before retrying (in usec). Don't sleep if the
842 * call returned value indicates availability.
845 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
847 } while (data_ret
!= 0);
855 * Stop tracing and wait for data availability.
857 int lttng_stop_tracing(const char *session_name
)
859 return _lttng_stop_tracing(session_name
, 1);
863 * Stop tracing but _don't_ wait for data availability.
865 int lttng_stop_tracing_no_wait(const char *session_name
)
867 return _lttng_stop_tracing(session_name
, 0);
871 * Add context to a channel.
873 * If the given channel is NULL, add the contexts to all channels.
874 * The event_name param is ignored.
876 * Returns the size of the returned payload data or a negative error code.
878 int lttng_add_context(struct lttng_handle
*handle
,
879 struct lttng_event_context
*ctx
, const char *event_name
,
880 const char *channel_name
)
885 struct lttcomm_session_msg lsm
;
887 /* Safety check. Both are mandatory. */
888 if (handle
== NULL
|| ctx
== NULL
) {
889 ret
= -LTTNG_ERR_INVALID
;
893 memset(&lsm
, 0, sizeof(lsm
));
894 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
896 /* If no channel name, send empty string. */
897 if (channel_name
== NULL
) {
898 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
899 sizeof(lsm
.u
.context
.channel_name
));
901 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
902 sizeof(lsm
.u
.context
.channel_name
));
905 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
906 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
907 sizeof(lsm
.session
.name
));
909 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
910 size_t provider_len
, ctx_len
;
911 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
912 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
914 if (!provider_name
|| !ctx_name
) {
915 ret
= -LTTNG_ERR_INVALID
;
919 provider_len
= strlen(provider_name
);
920 if (provider_len
== 0) {
921 ret
= -LTTNG_ERR_INVALID
;
924 lsm
.u
.context
.provider_name_len
= provider_len
;
926 ctx_len
= strlen(ctx_name
);
928 ret
= -LTTNG_ERR_INVALID
;
931 lsm
.u
.context
.context_name_len
= ctx_len
;
933 len
= provider_len
+ ctx_len
;
936 ret
= -LTTNG_ERR_NOMEM
;
940 memcpy(buf
, provider_name
, provider_len
);
941 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
943 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
945 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
947 * Don't leak application addresses to the sessiond.
948 * This is only necessary when ctx is for an app ctx otherwise
949 * the values inside the union (type & config) are overwritten.
951 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
952 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
955 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
962 * Enable event(s) for a channel.
964 * If no event name is specified, all events are enabled.
965 * If no channel name is specified, the default 'channel0' is used.
967 * Returns size of returned session payload data or a negative error code.
969 int lttng_enable_event(struct lttng_handle
*handle
,
970 struct lttng_event
*ev
, const char *channel_name
)
972 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
977 * Create or enable an event with a filter expression.
979 * Return negative error value on error.
980 * Return size of returned session payload data if OK.
982 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
983 struct lttng_event
*event
, const char *channel_name
,
984 const char *filter_expression
)
986 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
987 filter_expression
, 0, NULL
);
991 * Depending on the event, return a newly allocated agent filter expression or
992 * NULL if not applicable.
994 * An event with NO loglevel and the name is * will return NULL.
996 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
999 char *agent_filter
= NULL
;
1003 /* Don't add filter for the '*' event. */
1004 if (strcmp(ev
->name
, "*") != 0) {
1006 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
1009 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
1017 /* Add loglevel filtering if any for the JUL domain. */
1018 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1021 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
1027 if (filter
|| agent_filter
) {
1030 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
1031 agent_filter
? agent_filter
: filter
, op
,
1036 agent_filter
= new_filter
;
1038 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
1047 return agent_filter
;
1054 * Generate the filter bytecode from a given filter expression string. Put the
1055 * newly allocated parser context in ctxp and populate the lsm object with the
1058 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
1060 static int generate_filter(char *filter_expression
,
1061 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
1064 struct filter_parser_ctx
*ctx
= NULL
;
1067 assert(filter_expression
);
1072 * Casting const to non-const, as the underlying function will use it in
1075 fmem
= lttng_fmemopen((void *) filter_expression
,
1076 strlen(filter_expression
), "r");
1078 fprintf(stderr
, "Error opening memory as stream\n");
1079 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1082 ctx
= filter_parser_ctx_alloc(fmem
);
1084 fprintf(stderr
, "Error allocating parser\n");
1085 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1086 goto filter_alloc_error
;
1088 ret
= filter_parser_ctx_append_ast(ctx
);
1090 fprintf(stderr
, "Parse error\n");
1091 ret
= -LTTNG_ERR_FILTER_INVAL
;
1095 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
1098 fprintf(stderr
, "XML print error\n");
1099 ret
= -LTTNG_ERR_FILTER_INVAL
;
1104 dbg_printf("Generating IR... ");
1106 ret
= filter_visitor_ir_generate(ctx
);
1108 fprintf(stderr
, "Generate IR error\n");
1109 ret
= -LTTNG_ERR_FILTER_INVAL
;
1112 dbg_printf("done\n");
1114 dbg_printf("Validating IR... ");
1116 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1118 ret
= -LTTNG_ERR_FILTER_INVAL
;
1122 /* Normalize globbing patterns in the expression. */
1123 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1125 ret
= -LTTNG_ERR_FILTER_INVAL
;
1129 /* Validate strings used as literals in the expression. */
1130 ret
= filter_visitor_ir_validate_string(ctx
);
1132 ret
= -LTTNG_ERR_FILTER_INVAL
;
1136 /* Validate globbing patterns in the expression. */
1137 ret
= filter_visitor_ir_validate_globbing(ctx
);
1139 ret
= -LTTNG_ERR_FILTER_INVAL
;
1143 dbg_printf("done\n");
1145 dbg_printf("Generating bytecode... ");
1147 ret
= filter_visitor_bytecode_generate(ctx
);
1149 fprintf(stderr
, "Generate bytecode error\n");
1150 ret
= -LTTNG_ERR_FILTER_INVAL
;
1153 dbg_printf("done\n");
1154 dbg_printf("Size of bytecode generated: %u bytes.\n",
1155 bytecode_get_len(&ctx
->bytecode
->b
));
1157 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1158 + bytecode_get_len(&ctx
->bytecode
->b
);
1159 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1161 /* No need to keep the memory stream. */
1162 if (fclose(fmem
) != 0) {
1170 filter_ir_free(ctx
);
1171 filter_parser_ctx_free(ctx
);
1173 if (fclose(fmem
) != 0) {
1181 * Enable event(s) for a channel, possibly with exclusions and a filter.
1182 * If no event name is specified, all events are enabled.
1183 * If no channel name is specified, the default name is used.
1184 * If filter expression is not NULL, the filter is set for the event.
1185 * If exclusion count is not zero, the exclusions are set for the event.
1186 * Returns size of returned session payload data or a negative error code.
1188 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1189 struct lttng_event
*ev
, const char *channel_name
,
1190 const char *original_filter_expression
,
1191 int exclusion_count
, char **exclusion_list
)
1193 struct lttcomm_session_msg lsm
;
1194 struct lttng_payload payload
;
1196 unsigned int free_filter_expression
= 0;
1197 struct filter_parser_ctx
*ctx
= NULL
;
1200 * We have either a filter or some exclusions, so we need to set up
1201 * a variable-length payload from where to send the data.
1203 lttng_payload_init(&payload
);
1206 * Cast as non-const since we may replace the filter expression
1207 * by a dynamically allocated string. Otherwise, the original
1208 * string is not modified.
1210 char *filter_expression
= (char *) original_filter_expression
;
1212 if (handle
== NULL
|| ev
== NULL
) {
1213 ret
= -LTTNG_ERR_INVALID
;
1218 * Empty filter string will always be rejected by the parser
1219 * anyway, so treat this corner-case early to eliminate
1220 * lttng_fmemopen error for 0-byte allocation.
1222 if (filter_expression
&& filter_expression
[0] == '\0') {
1223 ret
= -LTTNG_ERR_INVALID
;
1227 memset(&lsm
, 0, sizeof(lsm
));
1229 /* If no channel name, send empty string. */
1230 if (channel_name
== NULL
) {
1231 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1232 sizeof(lsm
.u
.enable
.channel_name
));
1234 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1235 sizeof(lsm
.u
.enable
.channel_name
));
1238 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1239 if (ev
->name
[0] == '\0') {
1240 /* Enable all events */
1241 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1244 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1245 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1247 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1248 sizeof(lsm
.session
.name
));
1249 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1250 lsm
.u
.enable
.bytecode_len
= 0;
1252 /* Parse filter expression. */
1253 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1254 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1255 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1256 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1257 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1258 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1261 /* Setup JUL filter if needed. */
1262 agent_filter
= set_agent_filter(filter_expression
, ev
);
1263 if (!agent_filter
) {
1264 if (!filter_expression
) {
1266 * No JUL and no filter, just skip
1273 * With an agent filter, the original filter has
1274 * been added to it thus replace the filter
1277 filter_expression
= agent_filter
;
1278 free_filter_expression
= 1;
1282 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1288 ret
= lttng_dynamic_buffer_set_capacity(&payload
.buffer
,
1289 lsm
.u
.enable
.bytecode_len
+
1290 lsm
.u
.enable
.expression_len
+
1291 LTTNG_SYMBOL_NAME_LEN
*
1294 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1298 /* Put exclusion names first in the data. */
1299 for (i
= 0; i
< exclusion_count
; i
++) {
1300 size_t exclusion_len
;
1302 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1303 LTTNG_SYMBOL_NAME_LEN
);
1304 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1305 /* Exclusion is not NULL-terminated. */
1306 ret
= -LTTNG_ERR_INVALID
;
1310 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1311 *(exclusion_list
+ i
), LTTNG_SYMBOL_NAME_LEN
);
1317 /* Add filter expression next. */
1318 if (filter_expression
) {
1319 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1320 filter_expression
, lsm
.u
.enable
.expression_len
);
1325 /* Add filter bytecode next. */
1326 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1327 ret
= lttng_dynamic_buffer_append(&payload
.buffer
,
1328 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1333 if (ev
->extended
.ptr
) {
1334 struct lttng_event_extended
*ev_ext
=
1335 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1337 if (ev_ext
->probe_location
) {
1339 * lttng_userspace_probe_location_serialize returns the
1340 * number of bytes that was appended to the buffer.
1342 ret
= lttng_userspace_probe_location_serialize(
1343 ev_ext
->probe_location
, &payload
);
1349 * Set the size of the userspace probe location element
1350 * of the buffer so that the receiving side knows where
1353 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1358 struct lttng_payload_view view
= lttng_payload_view_from_payload(
1360 int fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
1367 assert(fd_count
== 0 || fd_count
== 1);
1368 if (fd_count
== 1) {
1369 struct fd_handle
*handle
=
1370 lttng_payload_view_pop_fd_handle(&view
);
1376 fd_to_send
= fd_handle_get_fd(handle
);
1377 fd_handle_put(handle
);
1380 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1381 fd_count
? &fd_to_send
: NULL
, fd_count
,
1382 view
.buffer
.size
? view
.buffer
.data
: NULL
,
1383 view
.buffer
.size
, NULL
, NULL
, 0);
1387 if (filter_expression
&& ctx
) {
1388 filter_bytecode_free(ctx
);
1389 filter_ir_free(ctx
);
1390 filter_parser_ctx_free(ctx
);
1393 if (free_filter_expression
) {
1395 * The filter expression has been replaced and must be freed as
1396 * it is not the original filter expression received as a
1399 free(filter_expression
);
1403 * Return directly to the caller and don't ask the sessiond since
1404 * something went wrong in the parsing of data above.
1406 lttng_payload_reset(&payload
);
1410 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1414 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1415 struct lttng_event
*ev
, const char *channel_name
,
1416 const char *original_filter_expression
)
1418 struct lttcomm_session_msg lsm
;
1421 unsigned int free_filter_expression
= 0;
1422 struct filter_parser_ctx
*ctx
= NULL
;
1424 * Cast as non-const since we may replace the filter expression
1425 * by a dynamically allocated string. Otherwise, the original
1426 * string is not modified.
1428 char *filter_expression
= (char *) original_filter_expression
;
1430 if (handle
== NULL
|| ev
== NULL
) {
1431 ret
= -LTTNG_ERR_INVALID
;
1436 * Empty filter string will always be rejected by the parser
1437 * anyway, so treat this corner-case early to eliminate
1438 * lttng_fmemopen error for 0-byte allocation.
1440 if (filter_expression
&& filter_expression
[0] == '\0') {
1441 ret
= -LTTNG_ERR_INVALID
;
1445 memset(&lsm
, 0, sizeof(lsm
));
1447 /* If no channel name, send empty string. */
1448 if (channel_name
== NULL
) {
1449 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1450 sizeof(lsm
.u
.disable
.channel_name
));
1452 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1453 sizeof(lsm
.u
.disable
.channel_name
));
1456 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1458 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1459 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1461 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1462 sizeof(lsm
.session
.name
));
1463 lsm
.u
.disable
.bytecode_len
= 0;
1466 * For the JUL domain, a filter is enforced except for the
1467 * disable all event. This is done to avoid having the event in
1468 * all sessions thus filtering by logger name.
1470 if (filter_expression
== NULL
&&
1471 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1472 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1473 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1478 * We have a filter, so we need to set up a variable-length
1479 * memory block from where to send the data.
1482 /* Parse filter expression */
1483 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1484 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1485 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1486 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1487 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1488 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1491 /* Setup JUL filter if needed. */
1492 agent_filter
= set_agent_filter(filter_expression
, ev
);
1493 if (!agent_filter
) {
1494 if (!filter_expression
) {
1496 * No JUL and no filter, just skip
1503 * With a JUL filter, the original filter has
1504 * been added to it thus replace the filter
1507 filter_expression
= agent_filter
;
1508 free_filter_expression
= 1;
1512 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1518 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1519 + lsm
.u
.disable
.expression_len
);
1521 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1525 /* Add filter expression. */
1526 if (lsm
.u
.disable
.expression_len
!= 0) {
1529 lsm
.u
.disable
.expression_len
);
1531 /* Add filter bytecode next. */
1532 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1534 + lsm
.u
.disable
.expression_len
,
1536 lsm
.u
.disable
.bytecode_len
);
1539 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1540 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1544 if (filter_expression
&& ctx
) {
1545 filter_bytecode_free(ctx
);
1546 filter_ir_free(ctx
);
1547 filter_parser_ctx_free(ctx
);
1550 if (free_filter_expression
) {
1552 * The filter expression has been replaced and must be freed as
1553 * it is not the original filter expression received as a
1556 free(filter_expression
);
1560 * Return directly to the caller and don't ask the sessiond since
1561 * something went wrong in the parsing of data above.
1566 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1571 * Disable event(s) of a channel and domain.
1572 * If no event name is specified, all events are disabled.
1573 * If no channel name is specified, the default 'channel0' is used.
1574 * Returns size of returned session payload data or a negative error code.
1576 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1577 const char *channel_name
)
1579 struct lttng_event ev
;
1581 memset(&ev
, 0, sizeof(ev
));
1583 ev
.type
= LTTNG_EVENT_ALL
;
1584 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1585 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1588 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1590 struct lttng_channel
*channel
= NULL
;
1591 struct lttng_channel_extended
*extended
= NULL
;
1597 /* Validate domain. */
1598 switch (domain
->type
) {
1599 case LTTNG_DOMAIN_UST
:
1600 switch (domain
->buf_type
) {
1601 case LTTNG_BUFFER_PER_UID
:
1602 case LTTNG_BUFFER_PER_PID
:
1608 case LTTNG_DOMAIN_KERNEL
:
1609 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1617 channel
= zmalloc(sizeof(*channel
));
1622 extended
= zmalloc(sizeof(*extended
));
1627 channel
->attr
.extended
.ptr
= extended
;
1629 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1637 void lttng_channel_destroy(struct lttng_channel
*channel
)
1643 if (channel
->attr
.extended
.ptr
) {
1644 free(channel
->attr
.extended
.ptr
);
1650 * Enable channel per domain
1651 * Returns size of returned session payload data or a negative error code.
1653 int lttng_enable_channel(struct lttng_handle
*handle
,
1654 struct lttng_channel
*in_chan
)
1656 struct lttcomm_session_msg lsm
;
1657 size_t total_buffer_size_needed_per_cpu
= 0;
1659 /* NULL arguments are forbidden. No default values. */
1660 if (handle
== NULL
|| in_chan
== NULL
) {
1661 return -LTTNG_ERR_INVALID
;
1664 memset(&lsm
, 0, sizeof(lsm
));
1665 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1666 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1668 if (!in_chan
->attr
.extended
.ptr
) {
1669 struct lttng_channel
*channel
;
1670 struct lttng_channel_extended
*extended
;
1672 channel
= lttng_channel_create(&handle
->domain
);
1674 return -LTTNG_ERR_NOMEM
;
1678 * Create a new channel in order to use default extended
1681 extended
= (struct lttng_channel_extended
*)
1682 channel
->attr
.extended
.ptr
;
1683 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1684 lttng_channel_destroy(channel
);
1686 struct lttng_channel_extended
*extended
;
1688 extended
= (struct lttng_channel_extended
*)
1689 in_chan
->attr
.extended
.ptr
;
1690 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1694 * Verify that the amount of memory required to create the requested
1695 * buffer is available on the system at the moment.
1697 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1698 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1699 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1700 return -LTTNG_ERR_NOMEM
;
1703 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1704 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1706 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1707 sizeof(lsm
.session
.name
));
1709 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1713 * All tracing will be stopped for registered events of the channel.
1714 * Returns size of returned session payload data or a negative error code.
1716 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1718 struct lttcomm_session_msg lsm
;
1720 /* Safety check. Both are mandatory. */
1721 if (handle
== NULL
|| name
== NULL
) {
1722 return -LTTNG_ERR_INVALID
;
1725 memset(&lsm
, 0, sizeof(lsm
));
1727 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1729 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1730 sizeof(lsm
.u
.disable
.channel_name
));
1732 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1734 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1735 sizeof(lsm
.session
.name
));
1737 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1741 * Lists all available tracepoints of domain.
1742 * Sets the contents of the events array.
1743 * Returns the number of lttng_event entries in events;
1744 * on error, returns a negative value.
1746 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1747 struct lttng_event
**events
)
1750 struct lttcomm_session_msg lsm
;
1752 if (handle
== NULL
) {
1753 return -LTTNG_ERR_INVALID
;
1756 memset(&lsm
, 0, sizeof(lsm
));
1757 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1758 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1760 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1765 return ret
/ sizeof(struct lttng_event
);
1769 * Lists all available tracepoint fields of domain.
1770 * Sets the contents of the event field array.
1771 * Returns the number of lttng_event_field entries in events;
1772 * on error, returns a negative value.
1774 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1775 struct lttng_event_field
**fields
)
1778 struct lttcomm_session_msg lsm
;
1780 if (handle
== NULL
) {
1781 return -LTTNG_ERR_INVALID
;
1784 memset(&lsm
, 0, sizeof(lsm
));
1785 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1786 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1788 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1793 return ret
/ sizeof(struct lttng_event_field
);
1797 * Lists all available kernel system calls. Allocates and sets the contents of
1800 * Returns the number of lttng_event entries in events; on error, returns a
1803 int lttng_list_syscalls(struct lttng_event
**events
)
1806 struct lttcomm_session_msg lsm
;
1809 return -LTTNG_ERR_INVALID
;
1812 memset(&lsm
, 0, sizeof(lsm
));
1813 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1814 /* Force kernel domain for system calls. */
1815 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1817 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1822 return ret
/ sizeof(struct lttng_event
);
1826 * Returns a human readable string describing
1827 * the error code (a negative value).
1829 const char *lttng_strerror(int code
)
1831 return error_get_str(code
);
1834 enum lttng_error_code
lttng_create_session_ext(
1835 struct lttng_session_descriptor
*session_descriptor
)
1837 enum lttng_error_code ret_code
;
1838 struct lttcomm_session_msg lsm
= {
1839 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1842 struct lttng_buffer_view reply_view
;
1844 bool sessiond_must_generate_ouput
;
1845 struct lttng_dynamic_buffer payload
;
1847 size_t descriptor_size
;
1848 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1850 lttng_dynamic_buffer_init(&payload
);
1851 if (!session_descriptor
) {
1852 ret_code
= LTTNG_ERR_INVALID
;
1856 sessiond_must_generate_ouput
=
1857 !lttng_session_descriptor_is_output_destination_initialized(
1858 session_descriptor
);
1859 if (sessiond_must_generate_ouput
) {
1860 const char *home_dir
= utils_get_home_dir();
1861 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1863 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1864 ret_code
= LTTNG_ERR_FATAL
;
1868 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1869 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1872 ret_code
= LTTNG_ERR_NOMEM
;
1877 descriptor_size
= payload
.size
;
1878 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1881 ret_code
= LTTNG_ERR_INVALID
;
1884 descriptor_size
= payload
.size
- descriptor_size
;
1885 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1887 /* Command returns a session descriptor on success. */
1888 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1889 payload
.size
, &reply
);
1890 if (reply_ret
< 0) {
1891 ret_code
= -reply_ret
;
1893 } else if (reply_ret
== 0) {
1894 /* Socket unexpectedly closed by the session daemon. */
1895 ret_code
= LTTNG_ERR_FATAL
;
1899 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1900 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1903 ret_code
= LTTNG_ERR_FATAL
;
1906 ret_code
= LTTNG_OK
;
1907 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1910 lttng_dynamic_buffer_reset(&payload
);
1911 lttng_session_descriptor_destroy(descriptor_reply
);
1916 * Create a new session using name and url for destination.
1918 * Return 0 on success else a negative LTTng error code.
1920 int lttng_create_session(const char *name
, const char *url
)
1924 struct lttng_uri
*uris
= NULL
;
1925 struct lttng_session_descriptor
*descriptor
= NULL
;
1926 enum lttng_error_code ret_code
;
1929 ret
= -LTTNG_ERR_INVALID
;
1933 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1935 ret
= -LTTNG_ERR_INVALID
;
1940 descriptor
= lttng_session_descriptor_create(name
);
1943 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1944 ret
= -LTTNG_ERR_INVALID
;
1947 descriptor
= lttng_session_descriptor_local_create(name
,
1951 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1955 ret
= -LTTNG_ERR_INVALID
;
1959 ret
= -LTTNG_ERR_INVALID
;
1962 ret_code
= lttng_create_session_ext(descriptor
);
1963 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1965 lttng_session_descriptor_destroy(descriptor
);
1971 * Create a session exclusively used for snapshot.
1973 * Return 0 on success else a negative LTTng error code.
1975 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1978 enum lttng_error_code ret_code
;
1980 struct lttng_uri
*uris
= NULL
;
1981 struct lttng_session_descriptor
*descriptor
= NULL
;
1984 ret
= -LTTNG_ERR_INVALID
;
1988 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1990 ret
= -LTTNG_ERR_INVALID
;
1994 * If the user does not specify a custom subdir, use the session name.
1996 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1997 strlen(uris
[0].subdir
) == 0) {
1998 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
2001 PERROR("Failed to set session name as network destination sub-directory");
2002 ret
= -LTTNG_ERR_FATAL
;
2004 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2005 /* Truncated output. */
2006 ret
= -LTTNG_ERR_INVALID
;
2013 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2016 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2017 ret
= -LTTNG_ERR_INVALID
;
2020 descriptor
= lttng_session_descriptor_snapshot_local_create(
2025 descriptor
= lttng_session_descriptor_snapshot_network_create(
2031 ret
= -LTTNG_ERR_INVALID
;
2035 ret
= -LTTNG_ERR_INVALID
;
2038 ret_code
= lttng_create_session_ext(descriptor
);
2039 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2041 lttng_session_descriptor_destroy(descriptor
);
2047 * Create a session exclusively used for live.
2049 * Return 0 on success else a negative LTTng error code.
2051 int lttng_create_session_live(const char *name
, const char *url
,
2052 unsigned int timer_interval
)
2055 enum lttng_error_code ret_code
;
2056 struct lttng_session_descriptor
*descriptor
= NULL
;
2059 ret
= -LTTNG_ERR_INVALID
;
2064 descriptor
= lttng_session_descriptor_live_network_create(
2065 name
, url
, NULL
, timer_interval
);
2067 descriptor
= lttng_session_descriptor_live_create(
2068 name
, timer_interval
);
2071 ret
= -LTTNG_ERR_INVALID
;
2074 ret_code
= lttng_create_session_ext(descriptor
);
2075 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2077 lttng_session_descriptor_destroy(descriptor
);
2082 * Stop the session and wait for the data before destroying it
2084 * Return 0 on success else a negative LTTng error code.
2086 int lttng_destroy_session(const char *session_name
)
2089 enum lttng_error_code ret_code
;
2090 enum lttng_destruction_handle_status status
;
2091 struct lttng_destruction_handle
*handle
= NULL
;
2094 * Stop the tracing and wait for the data to be
2097 ret
= _lttng_stop_tracing(session_name
, 1);
2098 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2102 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2103 if (ret_code
!= LTTNG_OK
) {
2104 ret
= (int) -ret_code
;
2109 /* Block until the completion of the destruction of the session. */
2110 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2111 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2112 ret
= -LTTNG_ERR_UNK
;
2116 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2117 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2118 ret
= -LTTNG_ERR_UNK
;
2121 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2123 lttng_destruction_handle_destroy(handle
);
2128 * Destroy the session without waiting for the data.
2130 int lttng_destroy_session_no_wait(const char *session_name
)
2132 enum lttng_error_code ret_code
;
2134 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2135 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
2139 * Ask the session daemon for all available sessions.
2140 * Sets the contents of the sessions array.
2141 * Returns the number of lttng_session entries in sessions;
2142 * on error, returns a negative value.
2144 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2147 struct lttcomm_session_msg lsm
;
2148 const size_t session_size
= sizeof(struct lttng_session
) +
2149 sizeof(struct lttng_session_extended
);
2150 size_t session_count
, i
;
2151 struct lttng_session_extended
*sessions_extended_begin
;
2152 struct lttng_session
*sessions
= NULL
;
2154 memset(&lsm
, 0, sizeof(lsm
));
2155 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2156 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2161 ret
= -LTTNG_ERR_FATAL
;
2165 if (ret
% session_size
) {
2166 ret
= -LTTNG_ERR_UNK
;
2168 *out_sessions
= NULL
;
2171 session_count
= (size_t) ret
/ session_size
;
2172 sessions_extended_begin
= (struct lttng_session_extended
*)
2173 (&sessions
[session_count
]);
2175 /* Set extended session info pointers. */
2176 for (i
= 0; i
< session_count
; i
++) {
2177 struct lttng_session
*session
= &sessions
[i
];
2178 struct lttng_session_extended
*extended
=
2179 &(sessions_extended_begin
[i
]);
2181 session
->extended
.ptr
= extended
;
2184 ret
= (int) session_count
;
2185 *out_sessions
= sessions
;
2190 enum lttng_error_code
lttng_session_get_creation_time(
2191 const struct lttng_session
*session
, uint64_t *creation_time
)
2193 enum lttng_error_code ret
= LTTNG_OK
;
2194 struct lttng_session_extended
*extended
;
2196 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2197 ret
= LTTNG_ERR_INVALID
;
2201 extended
= session
->extended
.ptr
;
2202 if (!extended
->creation_time
.is_set
) {
2203 /* Not created on the session daemon yet. */
2204 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2207 *creation_time
= extended
->creation_time
.value
;
2212 int lttng_set_session_shm_path(const char *session_name
,
2213 const char *shm_path
)
2215 struct lttcomm_session_msg lsm
;
2217 if (session_name
== NULL
) {
2218 return -LTTNG_ERR_INVALID
;
2221 memset(&lsm
, 0, sizeof(lsm
));
2222 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2224 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2225 sizeof(lsm
.session
.name
));
2226 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2227 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2229 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2233 * Ask the session daemon for all available domains of a session.
2234 * Sets the contents of the domains array.
2235 * Returns the number of lttng_domain entries in domains;
2236 * on error, returns a negative value.
2238 int lttng_list_domains(const char *session_name
,
2239 struct lttng_domain
**domains
)
2242 struct lttcomm_session_msg lsm
;
2244 if (session_name
== NULL
) {
2245 return -LTTNG_ERR_INVALID
;
2248 memset(&lsm
, 0, sizeof(lsm
));
2249 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2251 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2252 sizeof(lsm
.session
.name
));
2254 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2259 return ret
/ sizeof(struct lttng_domain
);
2263 * Ask the session daemon for all available channels of a session.
2264 * Sets the contents of the channels array.
2265 * Returns the number of lttng_channel entries in channels;
2266 * on error, returns a negative value.
2268 int lttng_list_channels(struct lttng_handle
*handle
,
2269 struct lttng_channel
**channels
)
2272 size_t channel_count
, i
;
2273 const size_t channel_size
= sizeof(struct lttng_channel
) +
2274 sizeof(struct lttng_channel_extended
);
2275 struct lttcomm_session_msg lsm
;
2278 if (handle
== NULL
) {
2279 ret
= -LTTNG_ERR_INVALID
;
2283 memset(&lsm
, 0, sizeof(lsm
));
2284 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2285 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2286 sizeof(lsm
.session
.name
));
2288 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2290 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2295 if (ret
% channel_size
) {
2296 ret
= -LTTNG_ERR_UNK
;
2301 channel_count
= (size_t) ret
/ channel_size
;
2303 /* Set extended info pointers */
2304 extended_at
= ((void *) *channels
) +
2305 channel_count
* sizeof(struct lttng_channel
);
2306 for (i
= 0; i
< channel_count
; i
++) {
2307 struct lttng_channel
*chan
= &(*channels
)[i
];
2309 chan
->attr
.extended
.ptr
= extended_at
;
2310 extended_at
+= sizeof(struct lttng_channel_extended
);
2313 ret
= (int) channel_count
;
2319 * Ask the session daemon for all available events of a session channel.
2320 * Sets the contents of the events array.
2321 * Returns the number of lttng_event entries in events;
2322 * on error, returns a negative value.
2324 int lttng_list_events(struct lttng_handle
*handle
,
2325 const char *channel_name
, struct lttng_event
**events
)
2328 struct lttcomm_session_msg lsm
= {};
2329 const struct lttcomm_event_command_header
*cmd_header
= NULL
;
2330 uint32_t nb_events
, i
;
2331 const void *comm_ext_at
;
2332 struct lttng_dynamic_buffer listing
;
2334 struct lttng_payload payload
;
2335 struct lttng_payload payload_copy
;
2336 struct lttng_payload_view lsm_view
=
2337 lttng_payload_view_init_from_buffer(
2338 (const char *) &lsm
, 0, sizeof(lsm
));
2339 struct lttng_buffer_view cmd_header_view
;
2340 struct lttng_buffer_view cmd_payload_view
;
2341 struct lttng_buffer_view flat_events_view
;
2342 struct lttng_buffer_view ext_view
;
2344 /* Safety check. An handle and channel name are mandatory */
2345 if (handle
== NULL
|| channel_name
== NULL
) {
2346 ret
= -LTTNG_ERR_INVALID
;
2350 lttng_payload_init(&payload
);
2351 lttng_payload_init(&payload_copy
);
2353 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2354 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2355 sizeof(lsm
.session
.name
));
2356 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2357 sizeof(lsm
.u
.list
.channel_name
));
2358 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2360 ret
= lttng_ctl_ask_sessiond_payload(&lsm_view
, &payload
);
2366 * A copy of the payload is performed since it will be
2367 * consumed twice. Consuming the same payload twice is invalid since
2368 * it will cause any received file descriptor to become "shared"
2369 * between different instances of the resulting objects.
2371 ret
= lttng_payload_copy(&payload
, &payload_copy
);
2373 ret
= -LTTNG_ERR_NOMEM
;
2377 cmd_header_view
= lttng_buffer_view_from_dynamic_buffer(
2378 &payload
.buffer
, 0, sizeof(*cmd_header
));
2379 if (!cmd_header_view
.data
) {
2380 ret
= -LTTNG_ERR_INVALID_PROTOCOL
;
2384 cmd_header
= (typeof(cmd_header
)) cmd_header_view
.data
;
2386 /* Set number of events and free command header */
2387 nb_events
= cmd_header
->nb_events
;
2388 if (nb_events
> INT_MAX
) {
2389 ret
= -LTTNG_ERR_OVERFLOW
;
2393 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2394 &payload
.buffer
, sizeof(*cmd_header
), -1);
2397 * The buffer that is returned must contain a "flat" version of
2398 * the events that are returned. In other words, all pointers
2399 * within an lttng_event must point to a location within the returned
2400 * buffer so that the user may free everything by simply calling free()
2401 * on the returned buffer. This is needed in order to maintain API
2404 * A first pass is performed to compute the size of the buffer that
2405 * must be allocated. A second pass is then performed to setup
2406 * the returned events so that their members always point within the
2409 * The layout of the returned buffer is as follows:
2410 * - struct lttng_event[nb_events],
2411 * - nb_events times the following:
2412 * - struct lttng_event_extended,
2413 * - flattened version of userspace_probe_location
2414 * - filter_expression
2416 * - padding to align to 64-bits
2418 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2419 nb_events
* sizeof(struct lttng_event
), -1);
2420 comm_ext_at
= ext_view
.data
;
2421 storage_req
= nb_events
* sizeof(struct lttng_event
);
2423 struct lttng_payload_view payload_view
=
2424 lttng_payload_view_from_payload(&payload
, 0, -1);
2426 for (i
= 0; i
< nb_events
; i
++) {
2427 const struct lttcomm_event_extended_header
*ext_comm
=
2428 (struct lttcomm_event_extended_header
*)
2430 int probe_storage_req
= 0;
2432 comm_ext_at
+= sizeof(*ext_comm
);
2433 comm_ext_at
+= ext_comm
->filter_len
;
2434 comm_ext_at
+= ext_comm
->nb_exclusions
*
2435 LTTNG_SYMBOL_NAME_LEN
;
2437 if (ext_comm
->userspace_probe_location_len
) {
2438 struct lttng_userspace_probe_location
2439 *probe_location
= NULL
;
2440 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2442 (const char *) comm_ext_at
-
2443 payload_view
.buffer
.data
,
2444 ext_comm
->userspace_probe_location_len
);
2447 * Create a temporary userspace probe location
2448 * to determine the size needed by a "flattened"
2449 * version of that same probe location.
2451 ret
= lttng_userspace_probe_location_create_from_payload(
2452 &probe_location_view
,
2455 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2459 ret
= lttng_userspace_probe_location_flatten(
2460 probe_location
, NULL
);
2461 lttng_userspace_probe_location_destroy(
2464 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2468 probe_storage_req
= ret
;
2469 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2472 storage_req
+= sizeof(struct lttng_event_extended
);
2473 storage_req
+= ext_comm
->filter_len
;
2474 storage_req
+= ext_comm
->nb_exclusions
*
2475 LTTNG_SYMBOL_NAME_LEN
;
2476 /* Padding to ensure the flat probe is aligned. */
2477 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2478 storage_req
+= probe_storage_req
;
2482 lttng_dynamic_buffer_init(&listing
);
2484 * We must ensure that "listing" is never resized so as to preserve
2485 * the validity of the flattened objects.
2487 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2489 ret
= -LTTNG_ERR_NOMEM
;
2493 cmd_payload_view
= lttng_buffer_view_from_dynamic_buffer(
2494 &payload_copy
.buffer
, sizeof(*cmd_header
), -1);
2495 flat_events_view
= lttng_buffer_view_from_view(&cmd_payload_view
, 0,
2496 nb_events
* sizeof(struct lttng_event
));
2497 ret
= lttng_dynamic_buffer_append_view(&listing
, &flat_events_view
);
2499 ret
= -LTTNG_ERR_NOMEM
;
2500 goto free_dynamic_buffer
;
2503 ext_view
= lttng_buffer_view_from_view(&cmd_payload_view
,
2504 nb_events
* sizeof(struct lttng_event
), -1);
2505 comm_ext_at
= ext_view
.data
;
2508 struct lttng_payload_view payload_copy_view
=
2509 lttng_payload_view_from_payload(
2510 &payload_copy
, 0, -1);
2512 for (i
= 0; i
< nb_events
; i
++) {
2513 struct lttng_event
*event
= (typeof(event
))(
2515 (sizeof(struct lttng_event
) * i
));
2516 const struct lttcomm_event_extended_header
*ext_comm
=
2517 (typeof(ext_comm
)) comm_ext_at
;
2518 struct lttng_event_extended
*event_extended
=
2519 (typeof(event_extended
))(listing
.data
+
2522 /* Insert struct lttng_event_extended. */
2523 ret
= lttng_dynamic_buffer_set_size(&listing
,
2524 listing
.size
+ sizeof(*event_extended
));
2526 ret
= -LTTNG_ERR_NOMEM
;
2527 goto free_dynamic_buffer
;
2529 event
->extended
.ptr
= event_extended
;
2531 comm_ext_at
+= sizeof(*ext_comm
);
2533 /* Insert filter expression. */
2534 if (ext_comm
->filter_len
) {
2535 event_extended
->filter_expression
=
2536 listing
.data
+ listing
.size
;
2537 ret
= lttng_dynamic_buffer_append(&listing
,
2539 ext_comm
->filter_len
);
2541 ret
= -LTTNG_ERR_NOMEM
;
2542 goto free_dynamic_buffer
;
2544 comm_ext_at
+= ext_comm
->filter_len
;
2547 /* Insert exclusions. */
2548 if (ext_comm
->nb_exclusions
) {
2549 event_extended
->exclusions
.count
=
2550 ext_comm
->nb_exclusions
;
2551 event_extended
->exclusions
.strings
=
2552 listing
.data
+ listing
.size
;
2554 ret
= lttng_dynamic_buffer_append(&listing
,
2556 ext_comm
->nb_exclusions
*
2557 LTTNG_SYMBOL_NAME_LEN
);
2559 ret
= -LTTNG_ERR_NOMEM
;
2560 goto free_dynamic_buffer
;
2562 comm_ext_at
+= ext_comm
->nb_exclusions
*
2563 LTTNG_SYMBOL_NAME_LEN
;
2566 /* Insert padding to align to 64-bits. */
2567 ret
= lttng_dynamic_buffer_set_size(&listing
,
2568 ALIGN_TO(listing
.size
,
2571 ret
= -LTTNG_ERR_NOMEM
;
2572 goto free_dynamic_buffer
;
2575 /* Insert flattened userspace probe location. */
2576 if (ext_comm
->userspace_probe_location_len
) {
2577 struct lttng_userspace_probe_location
2578 *probe_location
= NULL
;
2579 struct lttng_payload_view probe_location_view
= lttng_payload_view_from_view(
2581 (const char *) comm_ext_at
-
2582 payload_copy_view
.buffer
.data
,
2583 ext_comm
->userspace_probe_location_len
);
2585 ret
= lttng_userspace_probe_location_create_from_payload(
2586 &probe_location_view
,
2589 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2590 goto free_dynamic_buffer
;
2593 event_extended
->probe_location
= (struct lttng_userspace_probe_location
2596 ret
= lttng_userspace_probe_location_flatten(
2597 probe_location
, &listing
);
2598 lttng_userspace_probe_location_destroy(
2601 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2602 goto free_dynamic_buffer
;
2605 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2610 /* Don't reset listing buffer as we return its content. */
2611 *events
= (struct lttng_event
*) listing
.data
;
2612 lttng_dynamic_buffer_init(&listing
);
2613 ret
= (int) nb_events
;
2614 free_dynamic_buffer
:
2615 lttng_dynamic_buffer_reset(&listing
);
2617 lttng_payload_reset(&payload
);
2618 lttng_payload_reset(&payload_copy
);
2623 * Sets the tracing_group variable with name.
2624 * This function allocates memory pointed to by tracing_group.
2625 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2627 int lttng_set_tracing_group(const char *name
)
2630 return -LTTNG_ERR_INVALID
;
2633 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2634 return -LTTNG_ERR_FATAL
;
2640 int lttng_calibrate(struct lttng_handle
*handle
,
2641 struct lttng_calibrate
*calibrate
)
2644 * This command was removed in LTTng 2.9.
2646 return -LTTNG_ERR_UND
;
2650 * Set default channel attributes.
2651 * If either or both of the arguments are null, attr content is zeroe'd.
2653 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2654 struct lttng_channel_attr
*attr
)
2656 struct lttng_channel_extended
*extended
;
2659 if (attr
== NULL
|| domain
== NULL
) {
2663 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2664 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2666 /* Same for all domains. */
2667 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2668 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2669 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2671 switch (domain
->type
) {
2672 case LTTNG_DOMAIN_KERNEL
:
2673 attr
->switch_timer_interval
=
2674 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2675 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2676 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2677 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2678 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2680 extended
->monitor_timer_interval
=
2681 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2682 extended
->blocking_timeout
=
2683 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2686 case LTTNG_DOMAIN_UST
:
2687 switch (domain
->buf_type
) {
2688 case LTTNG_BUFFER_PER_UID
:
2689 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2690 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2691 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2692 attr
->switch_timer_interval
=
2693 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2694 attr
->read_timer_interval
=
2695 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2697 extended
->monitor_timer_interval
=
2698 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2699 extended
->blocking_timeout
=
2700 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2703 case LTTNG_BUFFER_PER_PID
:
2705 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2706 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2707 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2708 attr
->switch_timer_interval
=
2709 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2710 attr
->read_timer_interval
=
2711 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2713 extended
->monitor_timer_interval
=
2714 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2715 extended
->blocking_timeout
=
2716 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2721 /* Default behavior: leave set to 0. */
2725 attr
->extended
.ptr
= extended
;
2728 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2729 uint64_t *discarded_events
)
2732 struct lttng_channel_extended
*chan_ext
;
2734 if (!channel
|| !discarded_events
) {
2735 ret
= -LTTNG_ERR_INVALID
;
2739 chan_ext
= channel
->attr
.extended
.ptr
;
2742 * This can happen since the lttng_channel structure is
2743 * used for other tasks where this pointer is never set.
2745 *discarded_events
= 0;
2749 *discarded_events
= chan_ext
->discarded_events
;
2754 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2755 uint64_t *lost_packets
)
2758 struct lttng_channel_extended
*chan_ext
;
2760 if (!channel
|| !lost_packets
) {
2761 ret
= -LTTNG_ERR_INVALID
;
2765 chan_ext
= channel
->attr
.extended
.ptr
;
2768 * This can happen since the lttng_channel structure is
2769 * used for other tasks where this pointer is never set.
2775 *lost_packets
= chan_ext
->lost_packets
;
2780 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2781 uint64_t *monitor_timer_interval
)
2785 if (!chan
|| !monitor_timer_interval
) {
2786 ret
= -LTTNG_ERR_INVALID
;
2790 if (!chan
->attr
.extended
.ptr
) {
2791 ret
= -LTTNG_ERR_INVALID
;
2795 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2796 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2801 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2802 uint64_t monitor_timer_interval
)
2806 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2807 ret
= -LTTNG_ERR_INVALID
;
2811 ((struct lttng_channel_extended
*)
2812 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2813 monitor_timer_interval
;
2818 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2819 int64_t *blocking_timeout
)
2823 if (!chan
|| !blocking_timeout
) {
2824 ret
= -LTTNG_ERR_INVALID
;
2828 if (!chan
->attr
.extended
.ptr
) {
2829 ret
= -LTTNG_ERR_INVALID
;
2833 *blocking_timeout
= ((struct lttng_channel_extended
*)
2834 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2839 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2840 int64_t blocking_timeout
)
2843 int64_t msec_timeout
;
2845 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2846 ret
= -LTTNG_ERR_INVALID
;
2850 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2851 ret
= -LTTNG_ERR_INVALID
;
2856 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2857 * us to accept a narrower range of values (msecs expressed as a signed
2860 msec_timeout
= blocking_timeout
/ 1000;
2861 if (msec_timeout
!= (int32_t) msec_timeout
) {
2862 ret
= -LTTNG_ERR_INVALID
;
2866 ((struct lttng_channel_extended
*)
2867 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2874 * Check if session daemon is alive.
2876 * Return 1 if alive or 0 if not.
2877 * On error returns a negative value.
2879 int lttng_session_daemon_alive(void)
2883 ret
= set_session_daemon_path();
2889 if (*sessiond_sock_path
== '\0') {
2891 * No socket path set. Weird error which means the constructor
2897 ret
= try_connect_sessiond(sessiond_sock_path
);
2908 * Set URL for a consumer for a session and domain.
2910 * Return 0 on success, else a negative value.
2912 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2913 const char *control_url
, const char *data_url
)
2917 struct lttcomm_session_msg lsm
;
2918 struct lttng_uri
*uris
= NULL
;
2920 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2921 return -LTTNG_ERR_INVALID
;
2924 memset(&lsm
, 0, sizeof(lsm
));
2926 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2928 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2929 sizeof(lsm
.session
.name
));
2930 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2932 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2934 return -LTTNG_ERR_INVALID
;
2937 lsm
.u
.uri
.size
= size
;
2939 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2940 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 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2994 sizeof(lsm
.session
.name
));
2996 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2999 } else if (ret
!= 1) {
3000 /* Unexpected payload size */
3001 ret
= -LTTNG_ERR_INVALID
;
3003 } else if (!pending
) {
3004 /* Internal error. */
3005 ret
= -LTTNG_ERR_UNK
;
3009 ret
= (int) *pending
;
3016 * Regenerate the metadata for a session.
3017 * Return 0 on success, a negative error code on error.
3019 int lttng_regenerate_metadata(const char *session_name
)
3022 struct lttcomm_session_msg lsm
;
3024 if (!session_name
) {
3025 ret
= -LTTNG_ERR_INVALID
;
3029 memset(&lsm
, 0, sizeof(lsm
));
3030 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3032 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3033 sizeof(lsm
.session
.name
));
3035 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3046 * Deprecated, replaced by lttng_regenerate_metadata.
3048 int lttng_metadata_regenerate(const char *session_name
)
3050 return lttng_regenerate_metadata(session_name
);
3054 * Regenerate the statedump of a session.
3055 * Return 0 on success, a negative error code on error.
3057 int lttng_regenerate_statedump(const char *session_name
)
3060 struct lttcomm_session_msg lsm
;
3062 if (!session_name
) {
3063 ret
= -LTTNG_ERR_INVALID
;
3067 memset(&lsm
, 0, sizeof(lsm
));
3068 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3070 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3071 sizeof(lsm
.session
.name
));
3073 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3083 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3086 struct lttcomm_session_msg lsm
;
3087 struct lttng_payload payload
;
3089 lttng_payload_init(&payload
);
3091 ret
= -LTTNG_ERR_INVALID
;
3095 if (!lttng_trigger_validate(trigger
)) {
3096 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3100 ret
= lttng_trigger_serialize(trigger
, &payload
);
3102 ret
= -LTTNG_ERR_UNK
;
3106 memset(&lsm
, 0, sizeof(lsm
));
3107 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3108 lsm
.u
.trigger
.length
= (uint32_t) payload
.buffer
.size
;
3109 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3110 &lsm
, payload
.buffer
.data
, payload
.buffer
.size
, NULL
);
3112 lttng_payload_reset(&payload
);
3116 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3119 struct lttcomm_session_msg lsm
;
3120 struct lttng_payload payload
;
3122 lttng_payload_init(&payload
);
3124 ret
= -LTTNG_ERR_INVALID
;
3128 if (!lttng_trigger_validate(trigger
)) {
3129 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3133 ret
= lttng_trigger_serialize(trigger
, &payload
);
3135 ret
= -LTTNG_ERR_UNK
;
3139 memset(&lsm
, 0, sizeof(lsm
));
3140 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3141 lsm
.u
.trigger
.length
= (uint32_t) payload
.buffer
.size
;
3142 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3143 &lsm
, payload
.buffer
.data
, payload
.buffer
.size
, NULL
);
3145 lttng_payload_reset(&payload
);
3152 static void __attribute__((constructor
)) init(void)
3154 /* Set default session group */
3155 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3161 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3163 free(tracing_group
);