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/sessiond-comm/sessiond-comm.h>
26 #include <common/uri.h>
27 #include <common/utils.h>
28 #include <common/dynamic-buffer.h>
29 #include <lttng/lttng.h>
30 #include <lttng/health-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/endpoint.h>
33 #include <lttng/channel-internal.h>
34 #include <lttng/event-internal.h>
35 #include <lttng/userspace-probe-internal.h>
36 #include <lttng/session-internal.h>
37 #include <lttng/session-descriptor-internal.h>
38 #include <lttng/destruction-handle.h>
39 #include <lttng/tracker-internal.h>
41 #include "filter/filter-ast.h"
42 #include "filter/filter-parser.h"
43 #include "filter/filter-bytecode.h"
44 #include "filter/memstream.h"
45 #include "lttng-ctl-helper.h"
48 static const int print_xml
= 1;
49 #define dbg_printf(fmt, args...) \
50 printf("[debug liblttng-ctl] " fmt, ## args)
52 static const int print_xml
= 0;
53 #define dbg_printf(fmt, args...) \
55 /* do nothing but check printf format */ \
57 printf("[debug liblttnctl] " fmt, ## args); \
61 #define COPY_DOMAIN_PACKED(dst, src) \
63 struct lttng_domain _tmp_domain; \
65 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
69 /* Socket to session daemon for communication */
70 static int sessiond_socket
= -1;
71 static char sessiond_sock_path
[PATH_MAX
];
74 static char *tracing_group
;
80 * Those two variables are used by error.h to silent or control the verbosity of
81 * error message. They are global to the library so application linking with it
82 * are able to compile correctly and also control verbosity of the library.
85 int lttng_opt_verbose
;
89 * Copy string from src to dst and enforce null terminated byte.
92 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
95 strncpy(dst
, src
, len
);
96 /* Enforce the NULL terminated byte */
104 * Copy domain to lttcomm_session_msg domain.
106 * If domain is unknown, default domain will be the kernel.
109 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
110 struct lttng_domain
*src
)
114 case LTTNG_DOMAIN_KERNEL
:
115 case LTTNG_DOMAIN_UST
:
116 case LTTNG_DOMAIN_JUL
:
117 case LTTNG_DOMAIN_LOG4J
:
118 case LTTNG_DOMAIN_PYTHON
:
119 memcpy(dst
, src
, sizeof(struct lttng_domain
));
122 memset(dst
, 0, sizeof(struct lttng_domain
));
129 * Send lttcomm_session_msg to the session daemon.
131 * On success, returns the number of bytes sent (>=0)
132 * On error, returns -1
134 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
139 ret
= -LTTNG_ERR_NO_SESSIOND
;
143 DBG("LSM cmd type : %d", lsm
->cmd_type
);
145 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
146 sizeof(struct lttcomm_session_msg
));
148 ret
= -LTTNG_ERR_FATAL
;
156 * Send var len data to the session daemon.
158 * On success, returns the number of bytes sent (>=0)
159 * On error, returns -1
161 static int send_session_varlen(const void *data
, size_t len
)
166 ret
= -LTTNG_ERR_NO_SESSIOND
;
175 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
177 ret
= -LTTNG_ERR_FATAL
;
185 * Send file descriptors to the session daemon.
187 * On success, returns the number of bytes sent (>=0)
188 * On error, returns -1
190 static int send_session_fds(const int *fds
, size_t nb_fd
)
195 ret
= -LTTNG_ERR_NO_SESSIOND
;
199 if (!fds
|| !nb_fd
) {
204 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
206 ret
= -LTTNG_ERR_FATAL
;
214 * Receive data from the sessiond socket.
216 * On success, returns the number of bytes received (>=0)
217 * On error, returns -1 (recvmsg() error) or -ENOTCONN
219 static int recv_data_sessiond(void *buf
, size_t len
)
224 ret
= -LTTNG_ERR_NO_SESSIOND
;
228 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
230 ret
= -LTTNG_ERR_FATAL
;
238 * Check if we are in the specified group.
240 * If yes return 1, else return -1.
243 int lttng_check_tracing_group(void)
245 gid_t
*grp_list
, tracing_gid
;
246 int grp_list_size
, grp_id
, i
;
248 const char *grp_name
= tracing_group
;
250 /* Get GID of group 'tracing' */
251 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
252 /* If grp_tracing is NULL, the group does not exist. */
256 /* Get number of supplementary group IDs */
257 grp_list_size
= getgroups(0, NULL
);
258 if (grp_list_size
< 0) {
263 /* Alloc group list of the right size */
264 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
269 grp_id
= getgroups(grp_list_size
, grp_list
);
275 for (i
= 0; i
< grp_list_size
; i
++) {
276 if (grp_list
[i
] == tracing_gid
) {
289 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
293 size_t best_mem_info
;
294 size_t num_bytes_requested_total
;
297 * Get the number of CPU currently online to compute the amount of
298 * memory needed to create a buffer for every CPU.
300 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
305 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
308 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
309 * reliable estimate we can get but it is only exposed by the kernel
310 * since 3.14. (See Linux kernel commit:
311 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
313 ret
= utils_get_memory_available(&best_mem_info
);
319 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
320 * is a sanity check for obvious user error.
322 ret
= utils_get_memory_total(&best_mem_info
);
330 return best_mem_info
>= num_bytes_requested_total
;
334 * Try connect to session daemon with sock_path.
336 * Return 0 on success, else -1
338 static int try_connect_sessiond(const char *sock_path
)
342 /* If socket exist, we check if the daemon listens for connect. */
343 ret
= access(sock_path
, F_OK
);
349 ret
= lttcomm_connect_unix_sock(sock_path
);
355 ret
= lttcomm_close_unix_sock(ret
);
357 PERROR("lttcomm_close_unix_sock");
367 * Set sessiond socket path by putting it in the global sessiond_sock_path
370 * Returns 0 on success, negative value on failure (the sessiond socket path
371 * is somehow too long or ENOMEM).
373 static int set_session_daemon_path(void)
375 int in_tgroup
= 0; /* In tracing group. */
381 /* Are we in the tracing group ? */
382 in_tgroup
= lttng_check_tracing_group();
385 if ((uid
== 0) || in_tgroup
) {
386 lttng_ctl_copy_string(sessiond_sock_path
,
387 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
395 ret
= try_connect_sessiond(sessiond_sock_path
);
399 /* Global session daemon not available... */
401 /* ...or not in tracing group (and not root), default */
404 * With GNU C < 2.1, snprintf returns -1 if the target buffer
406 * With GNU C >= 2.1, snprintf returns the required size
407 * (excluding closing null)
409 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
410 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
411 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
423 * Connect to the LTTng session daemon.
425 * On success, return the socket's file descriptor. On error, return -1.
427 LTTNG_HIDDEN
int connect_sessiond(void)
431 ret
= set_session_daemon_path();
436 /* Connect to the sesssion daemon. */
437 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
448 static void reset_global_sessiond_connection_state(void)
450 sessiond_socket
= -1;
455 * Clean disconnect from the session daemon.
457 * On success, return 0. On error, return -1.
459 static int disconnect_sessiond(void)
464 ret
= lttcomm_close_unix_sock(sessiond_socket
);
465 reset_global_sessiond_connection_state();
471 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
479 ret
= -LTTNG_ERR_INVALID
;
489 ret
= recv_data_sessiond(buf
, len
);
495 ret
= -LTTNG_ERR_INVALID
;
499 /* Move ownership of command header buffer to user. */
504 /* No command header. */
520 * Ask the session daemon a specific command and put the data into buf.
521 * Takes extra var. len. data and file descriptors as input to send to the
524 * Return size of data (only payload, not header) or a negative error code.
527 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
528 const int *fds
, size_t nb_fd
, const void *vardata
,
529 size_t vardata_len
, void **user_payload_buf
,
530 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
534 struct lttcomm_lttng_msg llm
;
536 ret
= connect_sessiond();
538 ret
= -LTTNG_ERR_NO_SESSIOND
;
541 sessiond_socket
= ret
;
545 /* Send command to session daemon */
546 ret
= send_session_msg(lsm
);
548 /* Ret value is a valid lttng error code. */
551 /* Send var len data */
552 ret
= send_session_varlen(vardata
, vardata_len
);
554 /* Ret value is a valid lttng error code. */
559 ret
= send_session_fds(fds
, nb_fd
);
561 /* Ret value is a valid lttng error code. */
565 /* Get header from data transmission */
566 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
568 /* Ret value is a valid lttng error code. */
572 /* Check error code if OK */
573 if (llm
.ret_code
!= LTTNG_OK
) {
578 /* Get command header from data transmission */
579 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
580 user_cmd_header_buf
, user_cmd_header_len
);
585 /* Get payload from data transmission */
586 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
595 disconnect_sessiond();
600 * Create lttng handle and return pointer.
602 * The returned pointer will be NULL in case of malloc() error.
604 struct lttng_handle
*lttng_create_handle(const char *session_name
,
605 struct lttng_domain
*domain
)
607 struct lttng_handle
*handle
= NULL
;
609 handle
= zmalloc(sizeof(struct lttng_handle
));
610 if (handle
== NULL
) {
611 PERROR("malloc handle");
615 /* Copy session name */
616 lttng_ctl_copy_string(handle
->session_name
, session_name
,
617 sizeof(handle
->session_name
));
619 /* Copy lttng domain or leave initialized to 0. */
621 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
629 * Destroy handle by free(3) the pointer.
631 void lttng_destroy_handle(struct lttng_handle
*handle
)
637 * Register an outside consumer.
639 * Returns size of returned session payload data or a negative error code.
641 int lttng_register_consumer(struct lttng_handle
*handle
,
642 const char *socket_path
)
644 struct lttcomm_session_msg lsm
;
646 if (handle
== NULL
|| socket_path
== NULL
) {
647 return -LTTNG_ERR_INVALID
;
650 memset(&lsm
, 0, sizeof(lsm
));
651 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
652 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
653 sizeof(lsm
.session
.name
));
654 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
656 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
657 sizeof(lsm
.u
.reg
.path
));
659 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
663 * Start tracing for all traces of the session.
665 * Returns size of returned session payload data or a negative error code.
667 int lttng_start_tracing(const char *session_name
)
669 struct lttcomm_session_msg lsm
;
671 if (session_name
== NULL
) {
672 return -LTTNG_ERR_INVALID
;
675 memset(&lsm
, 0, sizeof(lsm
));
676 lsm
.cmd_type
= LTTNG_START_TRACE
;
678 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
679 sizeof(lsm
.session
.name
));
681 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
685 * Stop tracing for all traces of the session.
687 static int _lttng_stop_tracing(const char *session_name
, int wait
)
690 struct lttcomm_session_msg lsm
;
692 if (session_name
== NULL
) {
693 return -LTTNG_ERR_INVALID
;
696 memset(&lsm
, 0, sizeof(lsm
));
697 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
699 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
700 sizeof(lsm
.session
.name
));
702 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
703 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
711 /* Check for data availability */
713 data_ret
= lttng_data_pending(session_name
);
715 /* Return the data available call error. */
721 * Data sleep time before retrying (in usec). Don't sleep if the
722 * call returned value indicates availability.
725 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
727 } while (data_ret
!= 0);
735 * Stop tracing and wait for data availability.
737 int lttng_stop_tracing(const char *session_name
)
739 return _lttng_stop_tracing(session_name
, 1);
743 * Stop tracing but _don't_ wait for data availability.
745 int lttng_stop_tracing_no_wait(const char *session_name
)
747 return _lttng_stop_tracing(session_name
, 0);
751 * Add context to a channel.
753 * If the given channel is NULL, add the contexts to all channels.
754 * The event_name param is ignored.
756 * Returns the size of the returned payload data or a negative error code.
758 int lttng_add_context(struct lttng_handle
*handle
,
759 struct lttng_event_context
*ctx
, const char *event_name
,
760 const char *channel_name
)
765 struct lttcomm_session_msg lsm
;
767 /* Safety check. Both are mandatory. */
768 if (handle
== NULL
|| ctx
== NULL
) {
769 ret
= -LTTNG_ERR_INVALID
;
773 memset(&lsm
, 0, sizeof(lsm
));
774 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
776 /* If no channel name, send empty string. */
777 if (channel_name
== NULL
) {
778 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
779 sizeof(lsm
.u
.context
.channel_name
));
781 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
782 sizeof(lsm
.u
.context
.channel_name
));
785 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
786 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
787 sizeof(lsm
.session
.name
));
789 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
790 size_t provider_len
, ctx_len
;
791 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
792 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
794 if (!provider_name
|| !ctx_name
) {
795 ret
= -LTTNG_ERR_INVALID
;
799 provider_len
= strlen(provider_name
);
800 if (provider_len
== 0) {
801 ret
= -LTTNG_ERR_INVALID
;
804 lsm
.u
.context
.provider_name_len
= provider_len
;
806 ctx_len
= strlen(ctx_name
);
808 ret
= -LTTNG_ERR_INVALID
;
811 lsm
.u
.context
.context_name_len
= ctx_len
;
813 len
= provider_len
+ ctx_len
;
816 ret
= -LTTNG_ERR_NOMEM
;
820 memcpy(buf
, provider_name
, provider_len
);
821 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
823 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
825 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
827 * Don't leak application addresses to the sessiond.
828 * This is only necessary when ctx is for an app ctx otherwise
829 * the values inside the union (type & config) are overwritten.
831 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
832 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
835 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
842 * Enable event(s) for a channel.
844 * If no event name is specified, all events are enabled.
845 * If no channel name is specified, the default 'channel0' is used.
847 * Returns size of returned session payload data or a negative error code.
849 int lttng_enable_event(struct lttng_handle
*handle
,
850 struct lttng_event
*ev
, const char *channel_name
)
852 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
857 * Create or enable an event with a filter expression.
859 * Return negative error value on error.
860 * Return size of returned session payload data if OK.
862 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
863 struct lttng_event
*event
, const char *channel_name
,
864 const char *filter_expression
)
866 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
867 filter_expression
, 0, NULL
);
871 * Depending on the event, return a newly allocated agent filter expression or
872 * NULL if not applicable.
874 * An event with NO loglevel and the name is * will return NULL.
876 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
879 char *agent_filter
= NULL
;
883 /* Don't add filter for the '*' event. */
884 if (strcmp(ev
->name
, "*") != 0) {
886 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
889 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
897 /* Add loglevel filtering if any for the JUL domain. */
898 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
901 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
907 if (filter
|| agent_filter
) {
910 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
911 agent_filter
? agent_filter
: filter
, op
,
916 agent_filter
= new_filter
;
918 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
934 * Generate the filter bytecode from a given filter expression string. Put the
935 * newly allocated parser context in ctxp and populate the lsm object with the
938 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
940 static int generate_filter(char *filter_expression
,
941 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
944 struct filter_parser_ctx
*ctx
= NULL
;
947 assert(filter_expression
);
952 * Casting const to non-const, as the underlying function will use it in
955 fmem
= lttng_fmemopen((void *) filter_expression
,
956 strlen(filter_expression
), "r");
958 fprintf(stderr
, "Error opening memory as stream\n");
959 ret
= -LTTNG_ERR_FILTER_NOMEM
;
962 ctx
= filter_parser_ctx_alloc(fmem
);
964 fprintf(stderr
, "Error allocating parser\n");
965 ret
= -LTTNG_ERR_FILTER_NOMEM
;
966 goto filter_alloc_error
;
968 ret
= filter_parser_ctx_append_ast(ctx
);
970 fprintf(stderr
, "Parse error\n");
971 ret
= -LTTNG_ERR_FILTER_INVAL
;
975 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
978 fprintf(stderr
, "XML print error\n");
979 ret
= -LTTNG_ERR_FILTER_INVAL
;
984 dbg_printf("Generating IR... ");
986 ret
= filter_visitor_ir_generate(ctx
);
988 fprintf(stderr
, "Generate IR error\n");
989 ret
= -LTTNG_ERR_FILTER_INVAL
;
992 dbg_printf("done\n");
994 dbg_printf("Validating IR... ");
996 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
998 ret
= -LTTNG_ERR_FILTER_INVAL
;
1002 /* Normalize globbing patterns in the expression. */
1003 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1005 ret
= -LTTNG_ERR_FILTER_INVAL
;
1009 /* Validate strings used as literals in the expression. */
1010 ret
= filter_visitor_ir_validate_string(ctx
);
1012 ret
= -LTTNG_ERR_FILTER_INVAL
;
1016 /* Validate globbing patterns in the expression. */
1017 ret
= filter_visitor_ir_validate_globbing(ctx
);
1019 ret
= -LTTNG_ERR_FILTER_INVAL
;
1023 dbg_printf("done\n");
1025 dbg_printf("Generating bytecode... ");
1027 ret
= filter_visitor_bytecode_generate(ctx
);
1029 fprintf(stderr
, "Generate bytecode error\n");
1030 ret
= -LTTNG_ERR_FILTER_INVAL
;
1033 dbg_printf("done\n");
1034 dbg_printf("Size of bytecode generated: %u bytes.\n",
1035 bytecode_get_len(&ctx
->bytecode
->b
));
1037 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1038 + bytecode_get_len(&ctx
->bytecode
->b
);
1039 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1041 /* No need to keep the memory stream. */
1042 if (fclose(fmem
) != 0) {
1050 filter_ir_free(ctx
);
1051 filter_parser_ctx_free(ctx
);
1053 if (fclose(fmem
) != 0) {
1061 * Enable event(s) for a channel, possibly with exclusions and a filter.
1062 * If no event name is specified, all events are enabled.
1063 * If no channel name is specified, the default name is used.
1064 * If filter expression is not NULL, the filter is set for the event.
1065 * If exclusion count is not zero, the exclusions are set for the event.
1066 * Returns size of returned session payload data or a negative error code.
1068 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1069 struct lttng_event
*ev
, const char *channel_name
,
1070 const char *original_filter_expression
,
1071 int exclusion_count
, char **exclusion_list
)
1073 struct lttcomm_session_msg lsm
;
1074 struct lttng_dynamic_buffer send_buffer
;
1075 int ret
= 0, i
, fd_to_send
= -1;
1076 bool send_fd
= false;
1077 unsigned int free_filter_expression
= 0;
1078 struct filter_parser_ctx
*ctx
= NULL
;
1081 * We have either a filter or some exclusions, so we need to set up
1082 * a variable-length memory block from where to send the data.
1084 lttng_dynamic_buffer_init(&send_buffer
);
1087 * Cast as non-const since we may replace the filter expression
1088 * by a dynamically allocated string. Otherwise, the original
1089 * string is not modified.
1091 char *filter_expression
= (char *) original_filter_expression
;
1093 if (handle
== NULL
|| ev
== NULL
) {
1094 ret
= -LTTNG_ERR_INVALID
;
1099 * Empty filter string will always be rejected by the parser
1100 * anyway, so treat this corner-case early to eliminate
1101 * lttng_fmemopen error for 0-byte allocation.
1103 if (filter_expression
&& filter_expression
[0] == '\0') {
1104 ret
= -LTTNG_ERR_INVALID
;
1108 memset(&lsm
, 0, sizeof(lsm
));
1110 /* If no channel name, send empty string. */
1111 if (channel_name
== NULL
) {
1112 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1113 sizeof(lsm
.u
.enable
.channel_name
));
1115 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1116 sizeof(lsm
.u
.enable
.channel_name
));
1119 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1120 if (ev
->name
[0] == '\0') {
1121 /* Enable all events */
1122 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1125 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1126 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1128 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1129 sizeof(lsm
.session
.name
));
1130 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1131 lsm
.u
.enable
.bytecode_len
= 0;
1133 /* Parse filter expression. */
1134 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1135 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1136 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1137 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1138 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1139 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1142 /* Setup JUL filter if needed. */
1143 agent_filter
= set_agent_filter(filter_expression
, ev
);
1144 if (!agent_filter
) {
1145 if (!filter_expression
) {
1147 * No JUL and no filter, just skip
1154 * With an agent filter, the original filter has
1155 * been added to it thus replace the filter
1158 filter_expression
= agent_filter
;
1159 free_filter_expression
= 1;
1163 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1169 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1170 lsm
.u
.enable
.bytecode_len
1171 + lsm
.u
.enable
.expression_len
1172 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1174 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1178 /* Put exclusion names first in the data. */
1179 for (i
= 0; i
< exclusion_count
; i
++) {
1180 size_t exclusion_len
;
1182 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1183 LTTNG_SYMBOL_NAME_LEN
);
1184 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1185 /* Exclusion is not NULL-terminated. */
1186 ret
= -LTTNG_ERR_INVALID
;
1190 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1191 *(exclusion_list
+ i
),
1192 LTTNG_SYMBOL_NAME_LEN
);
1198 /* Add filter expression next. */
1199 if (filter_expression
) {
1200 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1201 filter_expression
, lsm
.u
.enable
.expression_len
);
1206 /* Add filter bytecode next. */
1207 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1208 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1209 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1214 if (ev
->extended
.ptr
) {
1215 struct lttng_event_extended
*ev_ext
=
1216 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1218 if (ev_ext
->probe_location
) {
1220 * lttng_userspace_probe_location_serialize returns the
1221 * number of bytes that was appended to the buffer.
1223 ret
= lttng_userspace_probe_location_serialize(
1224 ev_ext
->probe_location
, &send_buffer
,
1230 send_fd
= fd_to_send
>= 0;
1232 * Set the size of the userspace probe location element
1233 * of the buffer so that the receiving side knows where
1236 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1240 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1241 send_fd
? &fd_to_send
: NULL
,
1243 send_buffer
.size
? send_buffer
.data
: NULL
,
1244 send_buffer
.size
, NULL
, NULL
, 0);
1247 if (filter_expression
&& ctx
) {
1248 filter_bytecode_free(ctx
);
1249 filter_ir_free(ctx
);
1250 filter_parser_ctx_free(ctx
);
1253 if (free_filter_expression
) {
1255 * The filter expression has been replaced and must be freed as
1256 * it is not the original filter expression received as a
1259 free(filter_expression
);
1263 * Return directly to the caller and don't ask the sessiond since
1264 * something went wrong in the parsing of data above.
1266 lttng_dynamic_buffer_reset(&send_buffer
);
1270 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1274 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1275 struct lttng_event
*ev
, const char *channel_name
,
1276 const char *original_filter_expression
)
1278 struct lttcomm_session_msg lsm
;
1281 unsigned int free_filter_expression
= 0;
1282 struct filter_parser_ctx
*ctx
= NULL
;
1284 * Cast as non-const since we may replace the filter expression
1285 * by a dynamically allocated string. Otherwise, the original
1286 * string is not modified.
1288 char *filter_expression
= (char *) original_filter_expression
;
1290 if (handle
== NULL
|| ev
== NULL
) {
1291 ret
= -LTTNG_ERR_INVALID
;
1296 * Empty filter string will always be rejected by the parser
1297 * anyway, so treat this corner-case early to eliminate
1298 * lttng_fmemopen error for 0-byte allocation.
1300 if (filter_expression
&& filter_expression
[0] == '\0') {
1301 ret
= -LTTNG_ERR_INVALID
;
1305 memset(&lsm
, 0, sizeof(lsm
));
1307 /* If no channel name, send empty string. */
1308 if (channel_name
== NULL
) {
1309 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1310 sizeof(lsm
.u
.disable
.channel_name
));
1312 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1313 sizeof(lsm
.u
.disable
.channel_name
));
1316 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1318 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1319 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1321 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1322 sizeof(lsm
.session
.name
));
1323 lsm
.u
.disable
.bytecode_len
= 0;
1326 * For the JUL domain, a filter is enforced except for the
1327 * disable all event. This is done to avoid having the event in
1328 * all sessions thus filtering by logger name.
1330 if (filter_expression
== NULL
&&
1331 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1332 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1333 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1338 * We have a filter, so we need to set up a variable-length
1339 * memory block from where to send the data.
1342 /* Parse filter expression */
1343 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1344 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1345 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1346 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1347 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1348 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1351 /* Setup JUL filter if needed. */
1352 agent_filter
= set_agent_filter(filter_expression
, ev
);
1353 if (!agent_filter
) {
1354 if (!filter_expression
) {
1356 * No JUL and no filter, just skip
1363 * With a JUL filter, the original filter has
1364 * been added to it thus replace the filter
1367 filter_expression
= agent_filter
;
1368 free_filter_expression
= 1;
1372 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1378 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1379 + lsm
.u
.disable
.expression_len
);
1381 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1385 /* Add filter expression. */
1386 if (lsm
.u
.disable
.expression_len
!= 0) {
1389 lsm
.u
.disable
.expression_len
);
1391 /* Add filter bytecode next. */
1392 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1394 + lsm
.u
.disable
.expression_len
,
1396 lsm
.u
.disable
.bytecode_len
);
1399 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1400 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1404 if (filter_expression
&& ctx
) {
1405 filter_bytecode_free(ctx
);
1406 filter_ir_free(ctx
);
1407 filter_parser_ctx_free(ctx
);
1410 if (free_filter_expression
) {
1412 * The filter expression has been replaced and must be freed as
1413 * it is not the original filter expression received as a
1416 free(filter_expression
);
1420 * Return directly to the caller and don't ask the sessiond since
1421 * something went wrong in the parsing of data above.
1426 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1431 * Disable event(s) of a channel and domain.
1432 * If no event name is specified, all events are disabled.
1433 * If no channel name is specified, the default 'channel0' is used.
1434 * Returns size of returned session payload data or a negative error code.
1436 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1437 const char *channel_name
)
1439 struct lttng_event ev
;
1441 memset(&ev
, 0, sizeof(ev
));
1443 ev
.type
= LTTNG_EVENT_ALL
;
1444 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1445 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1448 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1450 struct lttng_channel
*channel
= NULL
;
1451 struct lttng_channel_extended
*extended
= NULL
;
1457 /* Validate domain. */
1458 switch (domain
->type
) {
1459 case LTTNG_DOMAIN_UST
:
1460 switch (domain
->buf_type
) {
1461 case LTTNG_BUFFER_PER_UID
:
1462 case LTTNG_BUFFER_PER_PID
:
1468 case LTTNG_DOMAIN_KERNEL
:
1469 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1477 channel
= zmalloc(sizeof(*channel
));
1482 extended
= zmalloc(sizeof(*extended
));
1487 channel
->attr
.extended
.ptr
= extended
;
1489 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1497 void lttng_channel_destroy(struct lttng_channel
*channel
)
1503 if (channel
->attr
.extended
.ptr
) {
1504 free(channel
->attr
.extended
.ptr
);
1510 * Enable channel per domain
1511 * Returns size of returned session payload data or a negative error code.
1513 int lttng_enable_channel(struct lttng_handle
*handle
,
1514 struct lttng_channel
*in_chan
)
1516 struct lttcomm_session_msg lsm
;
1517 size_t total_buffer_size_needed_per_cpu
= 0;
1519 /* NULL arguments are forbidden. No default values. */
1520 if (handle
== NULL
|| in_chan
== NULL
) {
1521 return -LTTNG_ERR_INVALID
;
1524 memset(&lsm
, 0, sizeof(lsm
));
1525 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1526 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1528 if (!in_chan
->attr
.extended
.ptr
) {
1529 struct lttng_channel
*channel
;
1530 struct lttng_channel_extended
*extended
;
1532 channel
= lttng_channel_create(&handle
->domain
);
1534 return -LTTNG_ERR_NOMEM
;
1538 * Create a new channel in order to use default extended
1541 extended
= (struct lttng_channel_extended
*)
1542 channel
->attr
.extended
.ptr
;
1543 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1544 lttng_channel_destroy(channel
);
1546 struct lttng_channel_extended
*extended
;
1548 extended
= (struct lttng_channel_extended
*)
1549 in_chan
->attr
.extended
.ptr
;
1550 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1554 * Verify that the amount of memory required to create the requested
1555 * buffer is available on the system at the moment.
1557 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1558 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1559 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1560 return -LTTNG_ERR_NOMEM
;
1563 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1564 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1566 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1567 sizeof(lsm
.session
.name
));
1569 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1573 * All tracing will be stopped for registered events of the channel.
1574 * Returns size of returned session payload data or a negative error code.
1576 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1578 struct lttcomm_session_msg lsm
;
1580 /* Safety check. Both are mandatory. */
1581 if (handle
== NULL
|| name
== NULL
) {
1582 return -LTTNG_ERR_INVALID
;
1585 memset(&lsm
, 0, sizeof(lsm
));
1587 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1589 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1590 sizeof(lsm
.u
.disable
.channel_name
));
1592 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1594 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1595 sizeof(lsm
.session
.name
));
1597 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1601 * Lists all available tracepoints of domain.
1602 * Sets the contents of the events array.
1603 * Returns the number of lttng_event entries in events;
1604 * on error, returns a negative value.
1606 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1607 struct lttng_event
**events
)
1610 struct lttcomm_session_msg lsm
;
1612 if (handle
== NULL
) {
1613 return -LTTNG_ERR_INVALID
;
1616 memset(&lsm
, 0, sizeof(lsm
));
1617 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1618 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1620 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1625 return ret
/ sizeof(struct lttng_event
);
1629 * Lists all available tracepoint fields of domain.
1630 * Sets the contents of the event field array.
1631 * Returns the number of lttng_event_field entries in events;
1632 * on error, returns a negative value.
1634 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1635 struct lttng_event_field
**fields
)
1638 struct lttcomm_session_msg lsm
;
1640 if (handle
== NULL
) {
1641 return -LTTNG_ERR_INVALID
;
1644 memset(&lsm
, 0, sizeof(lsm
));
1645 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1646 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1648 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1653 return ret
/ sizeof(struct lttng_event_field
);
1657 * Lists all available kernel system calls. Allocates and sets the contents of
1660 * Returns the number of lttng_event entries in events; on error, returns a
1663 int lttng_list_syscalls(struct lttng_event
**events
)
1666 struct lttcomm_session_msg lsm
;
1669 return -LTTNG_ERR_INVALID
;
1672 memset(&lsm
, 0, sizeof(lsm
));
1673 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1674 /* Force kernel domain for system calls. */
1675 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1677 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1682 return ret
/ sizeof(struct lttng_event
);
1686 * Returns a human readable string describing
1687 * the error code (a negative value).
1689 const char *lttng_strerror(int code
)
1691 return error_get_str(code
);
1694 enum lttng_error_code
lttng_create_session_ext(
1695 struct lttng_session_descriptor
*session_descriptor
)
1697 enum lttng_error_code ret_code
;
1698 struct lttcomm_session_msg lsm
= {
1699 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1702 struct lttng_buffer_view reply_view
;
1704 bool sessiond_must_generate_ouput
;
1705 struct lttng_dynamic_buffer payload
;
1707 size_t descriptor_size
;
1708 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1710 lttng_dynamic_buffer_init(&payload
);
1711 if (!session_descriptor
) {
1712 ret_code
= LTTNG_ERR_INVALID
;
1716 sessiond_must_generate_ouput
=
1717 !lttng_session_descriptor_is_output_destination_initialized(
1718 session_descriptor
);
1719 if (sessiond_must_generate_ouput
) {
1720 const char *home_dir
= utils_get_home_dir();
1721 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1723 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1724 ret_code
= LTTNG_ERR_FATAL
;
1728 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1729 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1732 ret_code
= LTTNG_ERR_NOMEM
;
1737 descriptor_size
= payload
.size
;
1738 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1741 ret_code
= LTTNG_ERR_INVALID
;
1744 descriptor_size
= payload
.size
- descriptor_size
;
1745 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1747 /* Command returns a session descriptor on success. */
1748 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1749 payload
.size
, &reply
);
1750 if (reply_ret
< 0) {
1751 ret_code
= -reply_ret
;
1753 } else if (reply_ret
== 0) {
1754 /* Socket unexpectedly closed by the session daemon. */
1755 ret_code
= LTTNG_ERR_FATAL
;
1759 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1760 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1763 ret_code
= LTTNG_ERR_FATAL
;
1766 ret_code
= LTTNG_OK
;
1767 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1770 lttng_dynamic_buffer_reset(&payload
);
1771 lttng_session_descriptor_destroy(descriptor_reply
);
1776 * Create a new session using name and url for destination.
1778 * Return 0 on success else a negative LTTng error code.
1780 int lttng_create_session(const char *name
, const char *url
)
1784 struct lttng_uri
*uris
= NULL
;
1785 struct lttng_session_descriptor
*descriptor
= NULL
;
1786 enum lttng_error_code ret_code
;
1789 ret
= -LTTNG_ERR_INVALID
;
1793 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1795 ret
= -LTTNG_ERR_INVALID
;
1800 descriptor
= lttng_session_descriptor_create(name
);
1803 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1804 ret
= -LTTNG_ERR_INVALID
;
1807 descriptor
= lttng_session_descriptor_local_create(name
,
1811 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1815 ret
= -LTTNG_ERR_INVALID
;
1819 ret
= -LTTNG_ERR_INVALID
;
1822 ret_code
= lttng_create_session_ext(descriptor
);
1823 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1825 lttng_session_descriptor_destroy(descriptor
);
1831 * Create a session exclusively used for snapshot.
1833 * Return 0 on success else a negative LTTng error code.
1835 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1838 enum lttng_error_code ret_code
;
1840 struct lttng_uri
*uris
= NULL
;
1841 struct lttng_session_descriptor
*descriptor
= NULL
;
1844 ret
= -LTTNG_ERR_INVALID
;
1848 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1850 ret
= -LTTNG_ERR_INVALID
;
1854 * If the user does not specify a custom subdir, use the session name.
1856 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1857 strlen(uris
[0].subdir
) == 0) {
1858 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1861 PERROR("Failed to set session name as network destination sub-directory");
1862 ret
= -LTTNG_ERR_FATAL
;
1864 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1865 /* Truncated output. */
1866 ret
= -LTTNG_ERR_INVALID
;
1873 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1876 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1877 ret
= -LTTNG_ERR_INVALID
;
1880 descriptor
= lttng_session_descriptor_snapshot_local_create(
1885 descriptor
= lttng_session_descriptor_snapshot_network_create(
1891 ret
= -LTTNG_ERR_INVALID
;
1895 ret
= -LTTNG_ERR_INVALID
;
1898 ret_code
= lttng_create_session_ext(descriptor
);
1899 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1901 lttng_session_descriptor_destroy(descriptor
);
1907 * Create a session exclusively used for live.
1909 * Return 0 on success else a negative LTTng error code.
1911 int lttng_create_session_live(const char *name
, const char *url
,
1912 unsigned int timer_interval
)
1915 enum lttng_error_code ret_code
;
1916 struct lttng_session_descriptor
*descriptor
= NULL
;
1919 ret
= -LTTNG_ERR_INVALID
;
1924 descriptor
= lttng_session_descriptor_live_network_create(
1925 name
, url
, NULL
, timer_interval
);
1927 descriptor
= lttng_session_descriptor_live_create(
1928 name
, timer_interval
);
1931 ret
= -LTTNG_ERR_INVALID
;
1934 ret_code
= lttng_create_session_ext(descriptor
);
1935 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1937 lttng_session_descriptor_destroy(descriptor
);
1942 * Stop the session and wait for the data before destroying it
1944 * Return 0 on success else a negative LTTng error code.
1946 int lttng_destroy_session(const char *session_name
)
1949 enum lttng_error_code ret_code
;
1950 enum lttng_destruction_handle_status status
;
1951 struct lttng_destruction_handle
*handle
= NULL
;
1954 * Stop the tracing and wait for the data to be
1957 ret
= _lttng_stop_tracing(session_name
, 1);
1958 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1962 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
1963 if (ret_code
!= LTTNG_OK
) {
1964 ret
= (int) -ret_code
;
1969 /* Block until the completion of the destruction of the session. */
1970 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
1971 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
1972 ret
= -LTTNG_ERR_UNK
;
1976 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
1977 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
1978 ret
= -LTTNG_ERR_UNK
;
1981 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1983 lttng_destruction_handle_destroy(handle
);
1988 * Destroy the session without waiting for the data.
1990 int lttng_destroy_session_no_wait(const char *session_name
)
1992 enum lttng_error_code ret_code
;
1994 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
1995 return ret_code
== LTTNG_OK
? ret_code
: -ret_code
;
1999 * Ask the session daemon for all available sessions.
2000 * Sets the contents of the sessions array.
2001 * Returns the number of lttng_session entries in sessions;
2002 * on error, returns a negative value.
2004 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2007 struct lttcomm_session_msg lsm
;
2008 const size_t session_size
= sizeof(struct lttng_session
) +
2009 sizeof(struct lttng_session_extended
);
2010 size_t session_count
, i
;
2011 struct lttng_session_extended
*sessions_extended_begin
;
2012 struct lttng_session
*sessions
= NULL
;
2014 memset(&lsm
, 0, sizeof(lsm
));
2015 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2016 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2021 ret
= -LTTNG_ERR_FATAL
;
2025 if (ret
% session_size
) {
2026 ret
= -LTTNG_ERR_UNK
;
2028 *out_sessions
= NULL
;
2031 session_count
= (size_t) ret
/ session_size
;
2032 sessions_extended_begin
= (struct lttng_session_extended
*)
2033 (&sessions
[session_count
]);
2035 /* Set extended session info pointers. */
2036 for (i
= 0; i
< session_count
; i
++) {
2037 struct lttng_session
*session
= &sessions
[i
];
2038 struct lttng_session_extended
*extended
=
2039 &(sessions_extended_begin
[i
]);
2041 session
->extended
.ptr
= extended
;
2044 ret
= (int) session_count
;
2045 *out_sessions
= sessions
;
2050 enum lttng_error_code
lttng_session_get_creation_time(
2051 const struct lttng_session
*session
, uint64_t *creation_time
)
2053 enum lttng_error_code ret
= LTTNG_OK
;
2054 struct lttng_session_extended
*extended
;
2056 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2057 ret
= LTTNG_ERR_INVALID
;
2061 extended
= session
->extended
.ptr
;
2062 if (!extended
->creation_time
.is_set
) {
2063 /* Not created on the session daemon yet. */
2064 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2067 *creation_time
= extended
->creation_time
.value
;
2072 int lttng_set_session_shm_path(const char *session_name
,
2073 const char *shm_path
)
2075 struct lttcomm_session_msg lsm
;
2077 if (session_name
== NULL
) {
2078 return -LTTNG_ERR_INVALID
;
2081 memset(&lsm
, 0, sizeof(lsm
));
2082 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2084 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2085 sizeof(lsm
.session
.name
));
2086 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2087 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2089 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2093 * Ask the session daemon for all available domains of a session.
2094 * Sets the contents of the domains array.
2095 * Returns the number of lttng_domain entries in domains;
2096 * on error, returns a negative value.
2098 int lttng_list_domains(const char *session_name
,
2099 struct lttng_domain
**domains
)
2102 struct lttcomm_session_msg lsm
;
2104 if (session_name
== NULL
) {
2105 return -LTTNG_ERR_INVALID
;
2108 memset(&lsm
, 0, sizeof(lsm
));
2109 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2111 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2112 sizeof(lsm
.session
.name
));
2114 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2119 return ret
/ sizeof(struct lttng_domain
);
2123 * Ask the session daemon for all available channels of a session.
2124 * Sets the contents of the channels array.
2125 * Returns the number of lttng_channel entries in channels;
2126 * on error, returns a negative value.
2128 int lttng_list_channels(struct lttng_handle
*handle
,
2129 struct lttng_channel
**channels
)
2132 size_t channel_count
, i
;
2133 const size_t channel_size
= sizeof(struct lttng_channel
) +
2134 sizeof(struct lttng_channel_extended
);
2135 struct lttcomm_session_msg lsm
;
2138 if (handle
== NULL
) {
2139 ret
= -LTTNG_ERR_INVALID
;
2143 memset(&lsm
, 0, sizeof(lsm
));
2144 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2145 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2146 sizeof(lsm
.session
.name
));
2148 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2150 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2155 if (ret
% channel_size
) {
2156 ret
= -LTTNG_ERR_UNK
;
2161 channel_count
= (size_t) ret
/ channel_size
;
2163 /* Set extended info pointers */
2164 extended_at
= ((void *) *channels
) +
2165 channel_count
* sizeof(struct lttng_channel
);
2166 for (i
= 0; i
< channel_count
; i
++) {
2167 struct lttng_channel
*chan
= &(*channels
)[i
];
2169 chan
->attr
.extended
.ptr
= extended_at
;
2170 extended_at
+= sizeof(struct lttng_channel_extended
);
2173 ret
= (int) channel_count
;
2179 * Ask the session daemon for all available events of a session channel.
2180 * Sets the contents of the events array.
2181 * Returns the number of lttng_event entries in events;
2182 * on error, returns a negative value.
2184 int lttng_list_events(struct lttng_handle
*handle
,
2185 const char *channel_name
, struct lttng_event
**events
)
2188 struct lttcomm_session_msg lsm
;
2189 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2190 size_t cmd_header_len
;
2191 uint32_t nb_events
, i
;
2193 char *reception_buffer
= NULL
;
2194 struct lttng_dynamic_buffer listing
;
2197 /* Safety check. An handle and channel name are mandatory */
2198 if (handle
== NULL
|| channel_name
== NULL
) {
2199 return -LTTNG_ERR_INVALID
;
2202 memset(&lsm
, 0, sizeof(lsm
));
2203 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2204 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2205 sizeof(lsm
.session
.name
));
2206 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2207 sizeof(lsm
.u
.list
.channel_name
));
2208 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2210 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2211 (void **) &reception_buffer
, (void **) &cmd_header
,
2218 ret
= -LTTNG_ERR_UNK
;
2222 /* Set number of events and free command header */
2223 nb_events
= cmd_header
->nb_events
;
2224 if (nb_events
> INT_MAX
) {
2225 ret
= -LTTNG_ERR_OVERFLOW
;
2232 * The buffer that is returned must contain a "flat" version of
2233 * the events that are returned. In other words, all pointers
2234 * within an lttng_event must point to a location within the returned
2235 * buffer so that the user may free everything by simply calling free()
2236 * on the returned buffer. This is needed in order to maintain API
2239 * A first pass is performed to compute the size of the buffer that
2240 * must be allocated. A second pass is then performed to setup
2241 * the returned events so that their members always point within the
2244 * The layout of the returned buffer is as follows:
2245 * - struct lttng_event[nb_events],
2246 * - nb_events times the following:
2247 * - struct lttng_event_extended,
2248 * - flattened version of userspace_probe_location
2249 * - filter_expression
2251 * - padding to align to 64-bits
2253 comm_ext_at
= reception_buffer
+
2254 (nb_events
* sizeof(struct lttng_event
));
2255 storage_req
= nb_events
* sizeof(struct lttng_event
);
2257 for (i
= 0; i
< nb_events
; i
++) {
2258 struct lttcomm_event_extended_header
*ext_comm
=
2259 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2260 int probe_storage_req
= 0;
2262 comm_ext_at
+= sizeof(*ext_comm
);
2263 comm_ext_at
+= ext_comm
->filter_len
;
2265 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2267 if (ext_comm
->userspace_probe_location_len
) {
2268 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2269 struct lttng_buffer_view probe_location_view
;
2271 probe_location_view
= lttng_buffer_view_init(
2273 ext_comm
->userspace_probe_location_len
);
2276 * Create a temporary userspace probe location to
2277 * determine the size needed by a "flattened" version
2278 * of that same probe location.
2280 ret
= lttng_userspace_probe_location_create_from_buffer(
2281 &probe_location_view
, &probe_location
);
2283 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2287 ret
= lttng_userspace_probe_location_flatten(
2288 probe_location
, NULL
);
2289 lttng_userspace_probe_location_destroy(probe_location
);
2291 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2295 probe_storage_req
= ret
;
2296 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2299 storage_req
+= sizeof(struct lttng_event_extended
);
2300 storage_req
+= ext_comm
->filter_len
;
2301 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2302 /* Padding to ensure the flat probe is aligned. */
2303 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2304 storage_req
+= probe_storage_req
;
2307 lttng_dynamic_buffer_init(&listing
);
2309 * We must ensure that "listing" is never resized so as to preserve
2310 * the validity of the flattened objects.
2312 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2314 ret
= -LTTNG_ERR_NOMEM
;
2318 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2319 nb_events
* sizeof(struct lttng_event
));
2321 ret
= -LTTNG_ERR_NOMEM
;
2322 goto free_dynamic_buffer
;
2325 comm_ext_at
= reception_buffer
+
2326 (nb_events
* sizeof(struct lttng_event
));
2327 for (i
= 0; i
< nb_events
; i
++) {
2328 struct lttng_event
*event
= (struct lttng_event
*)
2329 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2330 struct lttcomm_event_extended_header
*ext_comm
=
2331 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2332 struct lttng_event_extended
*event_extended
=
2333 (struct lttng_event_extended
*)
2334 (listing
.data
+ listing
.size
);
2336 /* Insert struct lttng_event_extended. */
2337 ret
= lttng_dynamic_buffer_set_size(&listing
,
2338 listing
.size
+ sizeof(*event_extended
));
2340 ret
= -LTTNG_ERR_NOMEM
;
2341 goto free_dynamic_buffer
;
2343 event
->extended
.ptr
= event_extended
;
2345 comm_ext_at
+= sizeof(*ext_comm
);
2347 /* Insert filter expression. */
2348 if (ext_comm
->filter_len
) {
2349 event_extended
->filter_expression
= listing
.data
+
2351 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2352 ext_comm
->filter_len
);
2354 ret
= -LTTNG_ERR_NOMEM
;
2355 goto free_dynamic_buffer
;
2357 comm_ext_at
+= ext_comm
->filter_len
;
2360 /* Insert exclusions. */
2361 if (ext_comm
->nb_exclusions
) {
2362 event_extended
->exclusions
.count
=
2363 ext_comm
->nb_exclusions
;
2364 event_extended
->exclusions
.strings
=
2365 listing
.data
+ listing
.size
;
2367 ret
= lttng_dynamic_buffer_append(&listing
,
2369 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2371 ret
= -LTTNG_ERR_NOMEM
;
2372 goto free_dynamic_buffer
;
2374 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2377 /* Insert padding to align to 64-bits. */
2378 ret
= lttng_dynamic_buffer_set_size(&listing
,
2379 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2381 ret
= -LTTNG_ERR_NOMEM
;
2382 goto free_dynamic_buffer
;
2385 /* Insert flattened userspace probe location. */
2386 if (ext_comm
->userspace_probe_location_len
) {
2387 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2388 struct lttng_buffer_view probe_location_view
;
2390 probe_location_view
= lttng_buffer_view_init(
2392 ext_comm
->userspace_probe_location_len
);
2394 ret
= lttng_userspace_probe_location_create_from_buffer(
2395 &probe_location_view
, &probe_location
);
2397 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2398 goto free_dynamic_buffer
;
2401 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2402 (listing
.data
+ listing
.size
);
2403 ret
= lttng_userspace_probe_location_flatten(
2404 probe_location
, &listing
);
2405 lttng_userspace_probe_location_destroy(probe_location
);
2407 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2408 goto free_dynamic_buffer
;
2411 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2415 /* Don't reset listing buffer as we return its content. */
2416 *events
= (struct lttng_event
*) listing
.data
;
2417 lttng_dynamic_buffer_init(&listing
);
2418 ret
= (int) nb_events
;
2419 free_dynamic_buffer
:
2420 lttng_dynamic_buffer_reset(&listing
);
2423 free(reception_buffer
);
2428 * Sets the tracing_group variable with name.
2429 * This function allocates memory pointed to by tracing_group.
2430 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2432 int lttng_set_tracing_group(const char *name
)
2435 return -LTTNG_ERR_INVALID
;
2438 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2439 return -LTTNG_ERR_FATAL
;
2445 int lttng_calibrate(struct lttng_handle
*handle
,
2446 struct lttng_calibrate
*calibrate
)
2449 * This command was removed in LTTng 2.9.
2451 return -LTTNG_ERR_UND
;
2455 * Set default channel attributes.
2456 * If either or both of the arguments are null, attr content is zeroe'd.
2458 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2459 struct lttng_channel_attr
*attr
)
2461 struct lttng_channel_extended
*extended
;
2464 if (attr
== NULL
|| domain
== NULL
) {
2468 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2469 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2471 /* Same for all domains. */
2472 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2473 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2474 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2476 switch (domain
->type
) {
2477 case LTTNG_DOMAIN_KERNEL
:
2478 attr
->switch_timer_interval
=
2479 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2480 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2481 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2482 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2483 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2485 extended
->monitor_timer_interval
=
2486 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2487 extended
->blocking_timeout
=
2488 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2491 case LTTNG_DOMAIN_UST
:
2492 switch (domain
->buf_type
) {
2493 case LTTNG_BUFFER_PER_UID
:
2494 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2495 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2496 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2497 attr
->switch_timer_interval
=
2498 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2499 attr
->read_timer_interval
=
2500 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2502 extended
->monitor_timer_interval
=
2503 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2504 extended
->blocking_timeout
=
2505 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2508 case LTTNG_BUFFER_PER_PID
:
2510 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2511 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2512 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2513 attr
->switch_timer_interval
=
2514 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2515 attr
->read_timer_interval
=
2516 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2518 extended
->monitor_timer_interval
=
2519 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2520 extended
->blocking_timeout
=
2521 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2526 /* Default behavior: leave set to 0. */
2530 attr
->extended
.ptr
= extended
;
2533 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2534 uint64_t *discarded_events
)
2537 struct lttng_channel_extended
*chan_ext
;
2539 if (!channel
|| !discarded_events
) {
2540 ret
= -LTTNG_ERR_INVALID
;
2544 chan_ext
= channel
->attr
.extended
.ptr
;
2547 * This can happen since the lttng_channel structure is
2548 * used for other tasks where this pointer is never set.
2550 *discarded_events
= 0;
2554 *discarded_events
= chan_ext
->discarded_events
;
2559 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2560 uint64_t *lost_packets
)
2563 struct lttng_channel_extended
*chan_ext
;
2565 if (!channel
|| !lost_packets
) {
2566 ret
= -LTTNG_ERR_INVALID
;
2570 chan_ext
= channel
->attr
.extended
.ptr
;
2573 * This can happen since the lttng_channel structure is
2574 * used for other tasks where this pointer is never set.
2580 *lost_packets
= chan_ext
->lost_packets
;
2585 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2586 uint64_t *monitor_timer_interval
)
2590 if (!chan
|| !monitor_timer_interval
) {
2591 ret
= -LTTNG_ERR_INVALID
;
2595 if (!chan
->attr
.extended
.ptr
) {
2596 ret
= -LTTNG_ERR_INVALID
;
2600 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2601 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2606 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2607 uint64_t monitor_timer_interval
)
2611 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2612 ret
= -LTTNG_ERR_INVALID
;
2616 ((struct lttng_channel_extended
*)
2617 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2618 monitor_timer_interval
;
2623 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2624 int64_t *blocking_timeout
)
2628 if (!chan
|| !blocking_timeout
) {
2629 ret
= -LTTNG_ERR_INVALID
;
2633 if (!chan
->attr
.extended
.ptr
) {
2634 ret
= -LTTNG_ERR_INVALID
;
2638 *blocking_timeout
= ((struct lttng_channel_extended
*)
2639 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2644 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2645 int64_t blocking_timeout
)
2648 int64_t msec_timeout
;
2650 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2651 ret
= -LTTNG_ERR_INVALID
;
2655 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2656 ret
= -LTTNG_ERR_INVALID
;
2661 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2662 * us to accept a narrower range of values (msecs expressed as a signed
2665 msec_timeout
= blocking_timeout
/ 1000;
2666 if (msec_timeout
!= (int32_t) msec_timeout
) {
2667 ret
= -LTTNG_ERR_INVALID
;
2671 ((struct lttng_channel_extended
*)
2672 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2679 * Check if session daemon is alive.
2681 * Return 1 if alive or 0 if not.
2682 * On error returns a negative value.
2684 int lttng_session_daemon_alive(void)
2688 ret
= set_session_daemon_path();
2694 if (*sessiond_sock_path
== '\0') {
2696 * No socket path set. Weird error which means the constructor
2702 ret
= try_connect_sessiond(sessiond_sock_path
);
2713 * Set URL for a consumer for a session and domain.
2715 * Return 0 on success, else a negative value.
2717 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2718 const char *control_url
, const char *data_url
)
2722 struct lttcomm_session_msg lsm
;
2723 struct lttng_uri
*uris
= NULL
;
2725 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2726 return -LTTNG_ERR_INVALID
;
2729 memset(&lsm
, 0, sizeof(lsm
));
2731 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2733 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2734 sizeof(lsm
.session
.name
));
2735 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2737 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2739 return -LTTNG_ERR_INVALID
;
2742 lsm
.u
.uri
.size
= size
;
2744 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2745 sizeof(struct lttng_uri
) * size
, NULL
);
2754 int lttng_enable_consumer(struct lttng_handle
*handle
);
2755 int lttng_enable_consumer(struct lttng_handle
*handle
)
2763 int lttng_disable_consumer(struct lttng_handle
*handle
);
2764 int lttng_disable_consumer(struct lttng_handle
*handle
)
2772 int _lttng_create_session_ext(const char *name
, const char *url
,
2773 const char *datetime
);
2774 int _lttng_create_session_ext(const char *name
, const char *url
,
2775 const char *datetime
)
2781 * For a given session name, this call checks if the data is ready to be read
2782 * or is still being extracted by the consumer(s) hence not ready to be used by
2785 int lttng_data_pending(const char *session_name
)
2788 struct lttcomm_session_msg lsm
;
2789 uint8_t *pending
= NULL
;
2791 if (session_name
== NULL
) {
2792 return -LTTNG_ERR_INVALID
;
2795 memset(&lsm
, 0, sizeof(lsm
));
2796 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2798 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2799 sizeof(lsm
.session
.name
));
2801 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2804 } else if (ret
!= 1) {
2805 /* Unexpected payload size */
2806 ret
= -LTTNG_ERR_INVALID
;
2808 } else if (!pending
) {
2809 /* Internal error. */
2810 ret
= -LTTNG_ERR_UNK
;
2814 ret
= (int) *pending
;
2821 * List IDs in the tracker.
2823 * tracker_type is the type of tracker.
2824 * ids is set to an allocated array of IDs currently tracked. On
2825 * success, ids and contained ids must be freed/destroy by the caller.
2826 * nr_ids is set to the number of entries contained by the ids array.
2828 * Returns 0 on success, else a negative LTTng error code.
2830 int lttng_list_tracker_ids(struct lttng_handle
*handle
,
2831 enum lttng_tracker_type tracker_type
,
2832 struct lttng_tracker_ids
**_ids
)
2835 struct lttcomm_session_msg lsm
;
2836 struct lttcomm_tracker_command_header
*cmd_header
= NULL
;
2837 char *cmd_payload
= NULL
, *p
;
2838 size_t cmd_header_len
;
2840 struct lttng_tracker_ids
*ids
= NULL
;
2842 if (handle
== NULL
) {
2843 return -LTTNG_ERR_INVALID
;
2846 memset(&lsm
, 0, sizeof(lsm
));
2847 lsm
.cmd_type
= LTTNG_LIST_TRACKER_IDS
;
2848 lsm
.u
.id_tracker_list
.tracker_type
= tracker_type
;
2849 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2850 sizeof(lsm
.session
.name
));
2851 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2853 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2854 (void **) &cmd_payload
, (void **) &cmd_header
,
2860 /* Set number of tracker_id and free command header */
2861 nr_ids
= cmd_header
->nb_tracker_id
;
2862 if (nr_ids
> INT_MAX
) {
2863 ret
= -LTTNG_ERR_OVERFLOW
;
2869 ids
= lttng_tracker_ids_create(nr_ids
);
2871 ret
= -LTTNG_ERR_NOMEM
;
2876 for (i
= 0; i
< nr_ids
; i
++) {
2877 struct lttcomm_tracker_id_header
*tracker_id
;
2878 struct lttng_tracker_id
*id
;
2879 enum lttng_tracker_id_status status
;
2881 tracker_id
= (struct lttcomm_tracker_id_header
*) p
;
2882 p
+= sizeof(struct lttcomm_tracker_id_header
);
2883 id
= lttng_tracker_ids_get_pointer_of_index(ids
, i
);
2885 ret
= -LTTNG_ERR_INVALID
;
2889 switch (tracker_id
->type
) {
2891 status
= lttng_tracker_id_set_all(id
);
2893 case LTTNG_ID_VALUE
:
2894 id
->value
= tracker_id
->u
.value
;
2895 status
= lttng_tracker_id_set_value(
2896 id
, tracker_id
->u
.value
);
2898 case LTTNG_ID_STRING
:
2899 status
= lttng_tracker_id_set_string(id
, p
);
2900 p
+= tracker_id
->u
.var_data_len
;
2906 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
2907 ret
= -LTTNG_ERR_INVALID
;
2916 lttng_tracker_ids_destroy(ids
);
2923 * List PIDs in the tracker.
2925 * enabled is set to whether the PID tracker is enabled.
2926 * pids is set to an allocated array of PIDs currently tracked. On
2927 * success, pids must be freed by the caller.
2928 * nr_pids is set to the number of entries contained by the pids array.
2930 * Returns 0 on success, else a negative LTTng error code.
2932 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2933 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2935 struct lttng_tracker_ids
*ids
= NULL
;
2936 unsigned int nr_ids
= 0;
2939 enum lttng_tracker_id_status status
;
2940 const struct lttng_tracker_id
*id
;
2942 ret
= lttng_list_tracker_ids(handle
, LTTNG_TRACKER_PID
, &ids
);
2947 status
= lttng_tracker_ids_get_count(ids
, &nr_ids
);
2948 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
2949 ret
= -LTTNG_ERR_INVALID
;
2954 id
= lttng_tracker_ids_get_at_index(ids
, 0);
2955 if (id
&& lttng_tracker_id_get_type(id
) == LTTNG_ID_ALL
) {
2963 pids
= zmalloc(nr_ids
* sizeof(*pids
));
2965 ret
= -LTTNG_ERR_NOMEM
;
2968 for (i
= 0; i
< nr_ids
; i
++) {
2969 id
= lttng_tracker_ids_get_at_index(ids
, i
);
2970 status
= lttng_tracker_id_get_value(id
, &pids
[i
]);
2971 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
2972 ret
= -LTTNG_ERR_UNK
;
2979 lttng_tracker_ids_destroy(ids
);
2987 * Regenerate the metadata for a session.
2988 * Return 0 on success, a negative error code on error.
2990 int lttng_regenerate_metadata(const char *session_name
)
2993 struct lttcomm_session_msg lsm
;
2995 if (!session_name
) {
2996 ret
= -LTTNG_ERR_INVALID
;
3000 memset(&lsm
, 0, sizeof(lsm
));
3001 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3003 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3004 sizeof(lsm
.session
.name
));
3006 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3017 * Deprecated, replaced by lttng_regenerate_metadata.
3019 int lttng_metadata_regenerate(const char *session_name
)
3021 return lttng_regenerate_metadata(session_name
);
3025 * Regenerate the statedump of a session.
3026 * Return 0 on success, a negative error code on error.
3028 int lttng_regenerate_statedump(const char *session_name
)
3031 struct lttcomm_session_msg lsm
;
3033 if (!session_name
) {
3034 ret
= -LTTNG_ERR_INVALID
;
3038 memset(&lsm
, 0, sizeof(lsm
));
3039 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3041 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
3042 sizeof(lsm
.session
.name
));
3044 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3054 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3057 struct lttcomm_session_msg lsm
;
3058 struct lttng_dynamic_buffer buffer
;
3060 lttng_dynamic_buffer_init(&buffer
);
3062 ret
= -LTTNG_ERR_INVALID
;
3066 if (!lttng_trigger_validate(trigger
)) {
3067 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3071 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3073 ret
= -LTTNG_ERR_UNK
;
3077 memset(&lsm
, 0, sizeof(lsm
));
3078 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3079 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3080 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3083 lttng_dynamic_buffer_reset(&buffer
);
3087 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3090 struct lttcomm_session_msg lsm
;
3091 struct lttng_dynamic_buffer buffer
;
3093 lttng_dynamic_buffer_init(&buffer
);
3095 ret
= -LTTNG_ERR_INVALID
;
3099 if (!lttng_trigger_validate(trigger
)) {
3100 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3104 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3106 ret
= -LTTNG_ERR_UNK
;
3110 memset(&lsm
, 0, sizeof(lsm
));
3111 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3112 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3113 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3116 lttng_dynamic_buffer_reset(&buffer
);
3120 static int lttng_track_untrack_id(struct lttng_handle
*handle
,
3121 enum lttng_tracker_type tracker_type
,
3122 const struct lttng_tracker_id
*id
,
3123 enum lttcomm_sessiond_command cmd
)
3126 struct lttcomm_session_msg lsm
;
3127 const char *var_data
= NULL
;
3128 size_t var_data_len
= 0;
3130 enum lttng_tracker_id_status status
;
3132 /* NULL arguments are forbidden. No default values. */
3133 if (handle
== NULL
) {
3137 memset(&lsm
, 0, sizeof(lsm
));
3140 lsm
.u
.id_tracker
.tracker_type
= tracker_type
;
3141 lsm
.u
.id_tracker
.id_type
= lttng_tracker_id_get_type(id
);
3142 switch (lsm
.u
.id_tracker
.id_type
) {
3145 case LTTNG_ID_VALUE
:
3146 status
= lttng_tracker_id_get_value(id
, &value
);
3147 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
3150 lsm
.u
.id_tracker
.u
.value
= value
;
3152 case LTTNG_ID_STRING
:
3153 status
= lttng_tracker_id_get_string(id
, &var_data
);
3154 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
3157 var_data_len
= strlen(var_data
) + 1; /* Includes \0. */
3158 lsm
.u
.id_tracker
.u
.var_len
= var_data_len
;
3164 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
3166 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
3167 sizeof(lsm
.session
.name
));
3169 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
3170 &lsm
, (char *) var_data
, var_data_len
, NULL
);
3173 return -LTTNG_ERR_INVALID
;
3177 * Add ID to session tracker.
3178 * Return 0 on success else a negative LTTng error code.
3180 int lttng_track_id(struct lttng_handle
*handle
,
3181 enum lttng_tracker_type tracker_type
,
3182 const struct lttng_tracker_id
*id
)
3184 return lttng_track_untrack_id(handle
, tracker_type
, id
, LTTNG_TRACK_ID
);
3188 * Remove ID from session tracker.
3189 * Return 0 on success else a negative LTTng error code.
3191 int lttng_untrack_id(struct lttng_handle
*handle
,
3192 enum lttng_tracker_type tracker_type
,
3193 const struct lttng_tracker_id
*id
)
3195 return lttng_track_untrack_id(
3196 handle
, tracker_type
, id
, LTTNG_UNTRACK_ID
);
3200 * Add PID to session tracker.
3201 * Return 0 on success else a negative LTTng error code.
3203 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
3206 struct lttng_tracker_id
*id
= NULL
;
3207 enum lttng_tracker_id_status status
;
3209 id
= lttng_tracker_id_create();
3210 status
= lttng_tracker_id_set_value(id
, pid
);
3211 if (status
== LTTNG_TRACKER_ID_STATUS_INVALID
) {
3212 ret
= -LTTNG_ERR_INVALID
;
3216 ret
= lttng_track_id(handle
, LTTNG_TRACKER_PID
, id
);
3218 lttng_tracker_id_destroy(id
);
3223 * Remove PID from session tracker.
3224 * Return 0 on success else a negative LTTng error code.
3226 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
3229 struct lttng_tracker_id
*id
= NULL
;
3230 enum lttng_tracker_id_status status
;
3232 id
= lttng_tracker_id_create();
3233 status
= lttng_tracker_id_set_value(id
, pid
);
3234 if (status
== LTTNG_TRACKER_ID_STATUS_INVALID
) {
3235 ret
= -LTTNG_ERR_INVALID
;
3239 ret
= lttng_untrack_id(handle
, LTTNG_TRACKER_PID
, id
);
3241 lttng_tracker_id_destroy(id
);
3248 static void __attribute__((constructor
)) init(void)
3250 /* Set default session group */
3251 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3257 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3259 free(tracing_group
);