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 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <common/common.h>
33 #include <common/compat/string.h>
34 #include <common/defaults.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/uri.h>
37 #include <common/utils.h>
38 #include <common/dynamic-buffer.h>
39 #include <lttng/lttng.h>
40 #include <lttng/health-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <lttng/endpoint.h>
43 #include <lttng/channel-internal.h>
44 #include <lttng/event-internal.h>
45 #include <lttng/userspace-probe-internal.h>
46 #include <lttng/session-internal.h>
47 #include <lttng/session-descriptor-internal.h>
49 #include "filter/filter-ast.h"
50 #include "filter/filter-parser.h"
51 #include "filter/filter-bytecode.h"
52 #include "filter/memstream.h"
53 #include "lttng-ctl-helper.h"
56 static const int print_xml
= 1;
57 #define dbg_printf(fmt, args...) \
58 printf("[debug liblttng-ctl] " fmt, ## args)
60 static const int print_xml
= 0;
61 #define dbg_printf(fmt, args...) \
63 /* do nothing but check printf format */ \
65 printf("[debug liblttnctl] " fmt, ## args); \
70 /* Socket to session daemon for communication */
71 static int sessiond_socket
;
72 static char sessiond_sock_path
[PATH_MAX
];
75 static char *tracing_group
;
81 * Those two variables are used by error.h to silent or control the verbosity of
82 * error message. They are global to the library so application linking with it
83 * are able to compile correctly and also control verbosity of the library.
86 int lttng_opt_verbose
;
90 * Copy string from src to dst and enforce null terminated byte.
93 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
96 strncpy(dst
, src
, len
);
97 /* Enforce the NULL terminated byte */
105 * Copy domain to lttcomm_session_msg domain.
107 * If domain is unknown, default domain will be the kernel.
110 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
111 struct lttng_domain
*src
)
115 case LTTNG_DOMAIN_KERNEL
:
116 case LTTNG_DOMAIN_UST
:
117 case LTTNG_DOMAIN_JUL
:
118 case LTTNG_DOMAIN_LOG4J
:
119 case LTTNG_DOMAIN_PYTHON
:
120 memcpy(dst
, src
, sizeof(struct lttng_domain
));
123 memset(dst
, 0, sizeof(struct lttng_domain
));
130 * Send lttcomm_session_msg to the session daemon.
132 * On success, returns the number of bytes sent (>=0)
133 * On error, returns -1
135 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
140 ret
= -LTTNG_ERR_NO_SESSIOND
;
144 DBG("LSM cmd type : %d", lsm
->cmd_type
);
146 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
147 sizeof(struct lttcomm_session_msg
));
149 ret
= -LTTNG_ERR_FATAL
;
157 * Send var len data to the session daemon.
159 * On success, returns the number of bytes sent (>=0)
160 * On error, returns -1
162 static int send_session_varlen(const void *data
, size_t len
)
167 ret
= -LTTNG_ERR_NO_SESSIOND
;
176 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
178 ret
= -LTTNG_ERR_FATAL
;
186 * Send file descriptors to the session daemon.
188 * On success, returns the number of bytes sent (>=0)
189 * On error, returns -1
191 static int send_session_fds(const int *fds
, size_t nb_fd
)
196 ret
= -LTTNG_ERR_NO_SESSIOND
;
200 if (!fds
|| !nb_fd
) {
205 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
207 ret
= -LTTNG_ERR_FATAL
;
215 * Receive data from the sessiond socket.
217 * On success, returns the number of bytes received (>=0)
218 * On error, returns -1 (recvmsg() error) or -ENOTCONN
220 static int recv_data_sessiond(void *buf
, size_t len
)
225 ret
= -LTTNG_ERR_NO_SESSIOND
;
229 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
231 ret
= -LTTNG_ERR_FATAL
;
239 * Check if we are in the specified group.
241 * If yes return 1, else return -1.
244 int lttng_check_tracing_group(void)
246 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
248 int grp_list_size
, grp_id
, i
;
250 const char *grp_name
= tracing_group
;
252 /* Get GID of group 'tracing' */
253 grp_tracing
= getgrnam(grp_name
);
255 /* If grp_tracing is NULL, the group does not exist. */
259 /* Get number of supplementary group IDs */
260 grp_list_size
= getgroups(0, NULL
);
261 if (grp_list_size
< 0) {
266 /* Alloc group list of the right size */
267 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
272 grp_id
= getgroups(grp_list_size
, grp_list
);
278 for (i
= 0; i
< grp_list_size
; i
++) {
279 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
292 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
296 size_t best_mem_info
;
297 size_t num_bytes_requested_total
;
300 * Get the number of CPU currently online to compute the amount of
301 * memory needed to create a buffer for every CPU.
303 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
308 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
311 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
312 * reliable estimate we can get but it is only exposed by the kernel
313 * since 3.14. (See Linux kernel commit:
314 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
316 ret
= utils_get_memory_available(&best_mem_info
);
322 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
323 * is a sanity check for obvious user error.
325 ret
= utils_get_memory_total(&best_mem_info
);
333 return best_mem_info
>= num_bytes_requested_total
;
337 * Try connect to session daemon with sock_path.
339 * Return 0 on success, else -1
341 static int try_connect_sessiond(const char *sock_path
)
345 /* If socket exist, we check if the daemon listens for connect. */
346 ret
= access(sock_path
, F_OK
);
352 ret
= lttcomm_connect_unix_sock(sock_path
);
358 ret
= lttcomm_close_unix_sock(ret
);
360 PERROR("lttcomm_close_unix_sock");
370 * Set sessiond socket path by putting it in the global sessiond_sock_path
373 * Returns 0 on success, negative value on failure (the sessiond socket path
374 * is somehow too long or ENOMEM).
376 static int set_session_daemon_path(void)
378 int in_tgroup
= 0; /* In tracing group. */
384 /* Are we in the tracing group ? */
385 in_tgroup
= lttng_check_tracing_group();
388 if ((uid
== 0) || in_tgroup
) {
389 lttng_ctl_copy_string(sessiond_sock_path
,
390 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
398 ret
= try_connect_sessiond(sessiond_sock_path
);
402 /* Global session daemon not available... */
404 /* ...or not in tracing group (and not root), default */
407 * With GNU C < 2.1, snprintf returns -1 if the target buffer
409 * With GNU C >= 2.1, snprintf returns the required size
410 * (excluding closing null)
412 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
413 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
414 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
426 * Connect to the LTTng session daemon.
428 * On success, return 0. On error, return -1.
430 static int connect_sessiond(void)
434 /* Don't try to connect if already connected. */
439 ret
= set_session_daemon_path();
444 /* Connect to the sesssion daemon. */
445 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
450 sessiond_socket
= ret
;
460 * Clean disconnect from the session daemon.
462 * On success, return 0. On error, return -1.
464 static int disconnect_sessiond(void)
469 ret
= lttcomm_close_unix_sock(sessiond_socket
);
477 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
485 ret
= -LTTNG_ERR_INVALID
;
495 ret
= recv_data_sessiond(buf
, len
);
501 ret
= -LTTNG_ERR_INVALID
;
505 /* Move ownership of command header buffer to user. */
510 /* No command header. */
526 * Ask the session daemon a specific command and put the data into buf.
527 * Takes extra var. len. data and file descriptors as input to send to the
530 * Return size of data (only payload, not header) or a negative error code.
533 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
534 const int *fds
, size_t nb_fd
, const void *vardata
,
535 size_t vardata_len
, void **user_payload_buf
,
536 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
540 struct lttcomm_lttng_msg llm
;
542 ret
= connect_sessiond();
544 ret
= -LTTNG_ERR_NO_SESSIOND
;
548 /* Send command to session daemon */
549 ret
= send_session_msg(lsm
);
551 /* Ret value is a valid lttng error code. */
554 /* Send var len data */
555 ret
= send_session_varlen(vardata
, vardata_len
);
557 /* Ret value is a valid lttng error code. */
562 ret
= send_session_fds(fds
, nb_fd
);
564 /* Ret value is a valid lttng error code. */
568 /* Get header from data transmission */
569 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
571 /* Ret value is a valid lttng error code. */
575 /* Check error code if OK */
576 if (llm
.ret_code
!= LTTNG_OK
) {
581 /* Get command header from data transmission */
582 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
583 user_cmd_header_buf
, user_cmd_header_len
);
588 /* Get payload from data transmission */
589 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
598 disconnect_sessiond();
603 * Create lttng handle and return pointer.
605 * The returned pointer will be NULL in case of malloc() error.
607 struct lttng_handle
*lttng_create_handle(const char *session_name
,
608 struct lttng_domain
*domain
)
610 struct lttng_handle
*handle
= NULL
;
612 handle
= zmalloc(sizeof(struct lttng_handle
));
613 if (handle
== NULL
) {
614 PERROR("malloc handle");
618 /* Copy session name */
619 lttng_ctl_copy_string(handle
->session_name
, session_name
,
620 sizeof(handle
->session_name
));
622 /* Copy lttng domain or leave initialized to 0. */
624 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
632 * Destroy handle by free(3) the pointer.
634 void lttng_destroy_handle(struct lttng_handle
*handle
)
640 * Register an outside consumer.
642 * Returns size of returned session payload data or a negative error code.
644 int lttng_register_consumer(struct lttng_handle
*handle
,
645 const char *socket_path
)
647 struct lttcomm_session_msg lsm
;
649 if (handle
== NULL
|| socket_path
== NULL
) {
650 return -LTTNG_ERR_INVALID
;
653 memset(&lsm
, 0, sizeof(lsm
));
654 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
655 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
656 sizeof(lsm
.session
.name
));
657 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
659 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
660 sizeof(lsm
.u
.reg
.path
));
662 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
666 * Start tracing for all traces of the session.
668 * Returns size of returned session payload data or a negative error code.
670 int lttng_start_tracing(const char *session_name
)
672 struct lttcomm_session_msg lsm
;
674 if (session_name
== NULL
) {
675 return -LTTNG_ERR_INVALID
;
678 memset(&lsm
, 0, sizeof(lsm
));
679 lsm
.cmd_type
= LTTNG_START_TRACE
;
681 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
682 sizeof(lsm
.session
.name
));
684 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
688 * Stop tracing for all traces of the session.
690 static int _lttng_stop_tracing(const char *session_name
, int wait
)
693 struct lttcomm_session_msg lsm
;
695 if (session_name
== NULL
) {
696 return -LTTNG_ERR_INVALID
;
699 memset(&lsm
, 0, sizeof(lsm
));
700 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
702 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
703 sizeof(lsm
.session
.name
));
705 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
706 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
714 /* Check for data availability */
716 data_ret
= lttng_data_pending(session_name
);
718 /* Return the data available call error. */
724 * Data sleep time before retrying (in usec). Don't sleep if the
725 * call returned value indicates availability.
728 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
730 } while (data_ret
!= 0);
738 * Stop tracing and wait for data availability.
740 int lttng_stop_tracing(const char *session_name
)
742 return _lttng_stop_tracing(session_name
, 1);
746 * Stop tracing but _don't_ wait for data availability.
748 int lttng_stop_tracing_no_wait(const char *session_name
)
750 return _lttng_stop_tracing(session_name
, 0);
754 * Add context to a channel.
756 * If the given channel is NULL, add the contexts to all channels.
757 * The event_name param is ignored.
759 * Returns the size of the returned payload data or a negative error code.
761 int lttng_add_context(struct lttng_handle
*handle
,
762 struct lttng_event_context
*ctx
, const char *event_name
,
763 const char *channel_name
)
768 struct lttcomm_session_msg lsm
;
770 /* Safety check. Both are mandatory. */
771 if (handle
== NULL
|| ctx
== NULL
) {
772 ret
= -LTTNG_ERR_INVALID
;
776 memset(&lsm
, 0, sizeof(lsm
));
777 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
779 /* If no channel name, send empty string. */
780 if (channel_name
== NULL
) {
781 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
782 sizeof(lsm
.u
.context
.channel_name
));
784 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
785 sizeof(lsm
.u
.context
.channel_name
));
788 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
789 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
790 sizeof(lsm
.session
.name
));
792 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
793 size_t provider_len
, ctx_len
;
794 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
795 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
797 if (!provider_name
|| !ctx_name
) {
798 ret
= -LTTNG_ERR_INVALID
;
802 provider_len
= strlen(provider_name
);
803 if (provider_len
== 0) {
804 ret
= -LTTNG_ERR_INVALID
;
807 lsm
.u
.context
.provider_name_len
= provider_len
;
809 ctx_len
= strlen(ctx_name
);
811 ret
= -LTTNG_ERR_INVALID
;
814 lsm
.u
.context
.context_name_len
= ctx_len
;
816 len
= provider_len
+ ctx_len
;
819 ret
= -LTTNG_ERR_NOMEM
;
823 memcpy(buf
, provider_name
, provider_len
);
824 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
826 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
828 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
830 * Don't leak application addresses to the sessiond.
831 * This is only necessary when ctx is for an app ctx otherwise
832 * the values inside the union (type & config) are overwritten.
834 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
835 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
838 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
845 * Enable event(s) for a channel.
847 * If no event name is specified, all events are enabled.
848 * If no channel name is specified, the default 'channel0' is used.
850 * Returns size of returned session payload data or a negative error code.
852 int lttng_enable_event(struct lttng_handle
*handle
,
853 struct lttng_event
*ev
, const char *channel_name
)
855 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
860 * Create or enable an event with a filter expression.
862 * Return negative error value on error.
863 * Return size of returned session payload data if OK.
865 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
866 struct lttng_event
*event
, const char *channel_name
,
867 const char *filter_expression
)
869 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
870 filter_expression
, 0, NULL
);
874 * Depending on the event, return a newly allocated agent filter expression or
875 * NULL if not applicable.
877 * An event with NO loglevel and the name is * will return NULL.
879 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
882 char *agent_filter
= NULL
;
886 /* Don't add filter for the '*' event. */
887 if (strcmp(ev
->name
, "*") != 0) {
889 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
892 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
900 /* Add loglevel filtering if any for the JUL domain. */
901 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
904 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
910 if (filter
|| agent_filter
) {
913 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
914 agent_filter
? agent_filter
: filter
, op
,
919 agent_filter
= new_filter
;
921 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
937 * Generate the filter bytecode from a given filter expression string. Put the
938 * newly allocated parser context in ctxp and populate the lsm object with the
941 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
943 static int generate_filter(char *filter_expression
,
944 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
947 struct filter_parser_ctx
*ctx
= NULL
;
950 assert(filter_expression
);
955 * Casting const to non-const, as the underlying function will use it in
958 fmem
= lttng_fmemopen((void *) filter_expression
,
959 strlen(filter_expression
), "r");
961 fprintf(stderr
, "Error opening memory as stream\n");
962 ret
= -LTTNG_ERR_FILTER_NOMEM
;
965 ctx
= filter_parser_ctx_alloc(fmem
);
967 fprintf(stderr
, "Error allocating parser\n");
968 ret
= -LTTNG_ERR_FILTER_NOMEM
;
969 goto filter_alloc_error
;
971 ret
= filter_parser_ctx_append_ast(ctx
);
973 fprintf(stderr
, "Parse error\n");
974 ret
= -LTTNG_ERR_FILTER_INVAL
;
978 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
981 fprintf(stderr
, "XML print error\n");
982 ret
= -LTTNG_ERR_FILTER_INVAL
;
987 dbg_printf("Generating IR... ");
989 ret
= filter_visitor_ir_generate(ctx
);
991 fprintf(stderr
, "Generate IR error\n");
992 ret
= -LTTNG_ERR_FILTER_INVAL
;
995 dbg_printf("done\n");
997 dbg_printf("Validating IR... ");
999 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1001 ret
= -LTTNG_ERR_FILTER_INVAL
;
1005 /* Normalize globbing patterns in the expression. */
1006 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1008 ret
= -LTTNG_ERR_FILTER_INVAL
;
1012 /* Validate strings used as literals in the expression. */
1013 ret
= filter_visitor_ir_validate_string(ctx
);
1015 ret
= -LTTNG_ERR_FILTER_INVAL
;
1019 /* Validate globbing patterns in the expression. */
1020 ret
= filter_visitor_ir_validate_globbing(ctx
);
1022 ret
= -LTTNG_ERR_FILTER_INVAL
;
1026 dbg_printf("done\n");
1028 dbg_printf("Generating bytecode... ");
1030 ret
= filter_visitor_bytecode_generate(ctx
);
1032 fprintf(stderr
, "Generate bytecode error\n");
1033 ret
= -LTTNG_ERR_FILTER_INVAL
;
1036 dbg_printf("done\n");
1037 dbg_printf("Size of bytecode generated: %u bytes.\n",
1038 bytecode_get_len(&ctx
->bytecode
->b
));
1040 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1041 + bytecode_get_len(&ctx
->bytecode
->b
);
1042 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1044 /* No need to keep the memory stream. */
1045 if (fclose(fmem
) != 0) {
1053 filter_ir_free(ctx
);
1054 filter_parser_ctx_free(ctx
);
1056 if (fclose(fmem
) != 0) {
1064 * Enable event(s) for a channel, possibly with exclusions and a filter.
1065 * If no event name is specified, all events are enabled.
1066 * If no channel name is specified, the default name is used.
1067 * If filter expression is not NULL, the filter is set for the event.
1068 * If exclusion count is not zero, the exclusions are set for the event.
1069 * Returns size of returned session payload data or a negative error code.
1071 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1072 struct lttng_event
*ev
, const char *channel_name
,
1073 const char *original_filter_expression
,
1074 int exclusion_count
, char **exclusion_list
)
1076 struct lttcomm_session_msg lsm
;
1077 struct lttng_dynamic_buffer send_buffer
;
1078 int ret
= 0, i
, fd_to_send
= -1;
1079 bool send_fd
= false;
1080 unsigned int free_filter_expression
= 0;
1081 struct filter_parser_ctx
*ctx
= NULL
;
1083 memset(&send_buffer
, 0, sizeof(send_buffer
));
1086 * Cast as non-const since we may replace the filter expression
1087 * by a dynamically allocated string. Otherwise, the original
1088 * string is not modified.
1090 char *filter_expression
= (char *) original_filter_expression
;
1092 if (handle
== NULL
|| ev
== NULL
) {
1093 ret
= -LTTNG_ERR_INVALID
;
1098 * Empty filter string will always be rejected by the parser
1099 * anyway, so treat this corner-case early to eliminate
1100 * lttng_fmemopen error for 0-byte allocation.
1102 if (filter_expression
&& filter_expression
[0] == '\0') {
1103 ret
= -LTTNG_ERR_INVALID
;
1107 memset(&lsm
, 0, sizeof(lsm
));
1109 /* If no channel name, send empty string. */
1110 if (channel_name
== NULL
) {
1111 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1112 sizeof(lsm
.u
.enable
.channel_name
));
1114 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1115 sizeof(lsm
.u
.enable
.channel_name
));
1118 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1119 if (ev
->name
[0] == '\0') {
1120 /* Enable all events */
1121 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1124 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1125 /* FIXME: copying non-packed struct to packed struct. */
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;
1134 * We have either a filter or some exclusions, so we need to set up
1135 * a variable-length memory block from where to send the data.
1137 lttng_dynamic_buffer_init(&send_buffer
);
1139 /* Parse filter expression. */
1140 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1141 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1142 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1143 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1144 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1145 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1148 /* Setup JUL filter if needed. */
1149 agent_filter
= set_agent_filter(filter_expression
, ev
);
1150 if (!agent_filter
) {
1151 if (!filter_expression
) {
1153 * No JUL and no filter, just skip
1160 * With an agent filter, the original filter has
1161 * been added to it thus replace the filter
1164 filter_expression
= agent_filter
;
1165 free_filter_expression
= 1;
1169 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1175 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1176 lsm
.u
.enable
.bytecode_len
1177 + lsm
.u
.enable
.expression_len
1178 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1180 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1184 /* Put exclusion names first in the data. */
1185 for (i
= 0; i
< exclusion_count
; i
++) {
1186 size_t exclusion_len
;
1188 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1189 LTTNG_SYMBOL_NAME_LEN
);
1190 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1191 /* Exclusion is not NULL-terminated. */
1192 ret
= -LTTNG_ERR_INVALID
;
1196 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1197 *(exclusion_list
+ i
),
1198 LTTNG_SYMBOL_NAME_LEN
);
1204 /* Add filter expression next. */
1205 if (filter_expression
) {
1206 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1207 filter_expression
, lsm
.u
.enable
.expression_len
);
1212 /* Add filter bytecode next. */
1213 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1214 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1215 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1220 if (ev
->extended
.ptr
) {
1221 struct lttng_event_extended
*ev_ext
=
1222 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1224 if (ev_ext
->probe_location
) {
1226 * lttng_userspace_probe_location_serialize returns the
1227 * number of bytes that was appended to the buffer.
1229 ret
= lttng_userspace_probe_location_serialize(
1230 ev_ext
->probe_location
, &send_buffer
,
1236 send_fd
= fd_to_send
>= 0;
1238 * Set the size of the userspace probe location element
1239 * of the buffer so that the receiving side knows where
1242 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1246 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1247 send_fd
? &fd_to_send
: NULL
,
1249 send_buffer
.size
? send_buffer
.data
: NULL
,
1250 send_buffer
.size
, NULL
, NULL
, 0);
1253 if (filter_expression
&& ctx
) {
1254 filter_bytecode_free(ctx
);
1255 filter_ir_free(ctx
);
1256 filter_parser_ctx_free(ctx
);
1259 if (free_filter_expression
) {
1261 * The filter expression has been replaced and must be freed as
1262 * it is not the original filter expression received as a
1265 free(filter_expression
);
1269 * Return directly to the caller and don't ask the sessiond since
1270 * something went wrong in the parsing of data above.
1272 lttng_dynamic_buffer_reset(&send_buffer
);
1276 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1280 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1281 struct lttng_event
*ev
, const char *channel_name
,
1282 const char *original_filter_expression
)
1284 struct lttcomm_session_msg lsm
;
1287 unsigned int free_filter_expression
= 0;
1288 struct filter_parser_ctx
*ctx
= NULL
;
1290 * Cast as non-const since we may replace the filter expression
1291 * by a dynamically allocated string. Otherwise, the original
1292 * string is not modified.
1294 char *filter_expression
= (char *) original_filter_expression
;
1296 if (handle
== NULL
|| ev
== NULL
) {
1297 ret
= -LTTNG_ERR_INVALID
;
1302 * Empty filter string will always be rejected by the parser
1303 * anyway, so treat this corner-case early to eliminate
1304 * lttng_fmemopen error for 0-byte allocation.
1306 if (filter_expression
&& filter_expression
[0] == '\0') {
1307 ret
= -LTTNG_ERR_INVALID
;
1311 memset(&lsm
, 0, sizeof(lsm
));
1313 /* If no channel name, send empty string. */
1314 if (channel_name
== NULL
) {
1315 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1316 sizeof(lsm
.u
.disable
.channel_name
));
1318 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1319 sizeof(lsm
.u
.disable
.channel_name
));
1322 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1324 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1325 /* FIXME: copying non-packed struct to packed struct. */
1326 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1328 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1329 sizeof(lsm
.session
.name
));
1330 lsm
.u
.disable
.bytecode_len
= 0;
1333 * For the JUL domain, a filter is enforced except for the
1334 * disable all event. This is done to avoid having the event in
1335 * all sessions thus filtering by logger name.
1337 if (filter_expression
== NULL
&&
1338 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1339 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1340 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1345 * We have a filter, so we need to set up a variable-length
1346 * memory block from where to send the data.
1349 /* Parse filter expression */
1350 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1351 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1352 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1353 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1354 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1355 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1358 /* Setup JUL filter if needed. */
1359 agent_filter
= set_agent_filter(filter_expression
, ev
);
1360 if (!agent_filter
) {
1361 if (!filter_expression
) {
1363 * No JUL and no filter, just skip
1370 * With a JUL filter, the original filter has
1371 * been added to it thus replace the filter
1374 filter_expression
= agent_filter
;
1375 free_filter_expression
= 1;
1379 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1385 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1386 + lsm
.u
.disable
.expression_len
);
1388 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1392 /* Add filter expression. */
1393 if (lsm
.u
.disable
.expression_len
!= 0) {
1396 lsm
.u
.disable
.expression_len
);
1398 /* Add filter bytecode next. */
1399 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1401 + lsm
.u
.disable
.expression_len
,
1403 lsm
.u
.disable
.bytecode_len
);
1406 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1407 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1411 if (filter_expression
&& ctx
) {
1412 filter_bytecode_free(ctx
);
1413 filter_ir_free(ctx
);
1414 filter_parser_ctx_free(ctx
);
1417 if (free_filter_expression
) {
1419 * The filter expression has been replaced and must be freed as
1420 * it is not the original filter expression received as a
1423 free(filter_expression
);
1427 * Return directly to the caller and don't ask the sessiond since
1428 * something went wrong in the parsing of data above.
1433 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1438 * Disable event(s) of a channel and domain.
1439 * If no event name is specified, all events are disabled.
1440 * If no channel name is specified, the default 'channel0' is used.
1441 * Returns size of returned session payload data or a negative error code.
1443 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1444 const char *channel_name
)
1446 struct lttng_event ev
;
1448 memset(&ev
, 0, sizeof(ev
));
1450 ev
.type
= LTTNG_EVENT_ALL
;
1451 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1452 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1455 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1457 struct lttng_channel
*channel
= NULL
;
1458 struct lttng_channel_extended
*extended
= NULL
;
1464 /* Validate domain. */
1465 switch (domain
->type
) {
1466 case LTTNG_DOMAIN_UST
:
1467 switch (domain
->buf_type
) {
1468 case LTTNG_BUFFER_PER_UID
:
1469 case LTTNG_BUFFER_PER_PID
:
1475 case LTTNG_DOMAIN_KERNEL
:
1476 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1484 channel
= zmalloc(sizeof(*channel
));
1489 extended
= zmalloc(sizeof(*extended
));
1494 channel
->attr
.extended
.ptr
= extended
;
1496 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1504 void lttng_channel_destroy(struct lttng_channel
*channel
)
1510 if (channel
->attr
.extended
.ptr
) {
1511 free(channel
->attr
.extended
.ptr
);
1517 * Enable channel per domain
1518 * Returns size of returned session payload data or a negative error code.
1520 int lttng_enable_channel(struct lttng_handle
*handle
,
1521 struct lttng_channel
*in_chan
)
1523 struct lttcomm_session_msg lsm
;
1524 size_t total_buffer_size_needed_per_cpu
= 0;
1526 /* NULL arguments are forbidden. No default values. */
1527 if (handle
== NULL
|| in_chan
== NULL
) {
1528 return -LTTNG_ERR_INVALID
;
1531 memset(&lsm
, 0, sizeof(lsm
));
1532 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1533 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1535 if (!in_chan
->attr
.extended
.ptr
) {
1536 struct lttng_channel
*channel
;
1537 struct lttng_channel_extended
*extended
;
1539 channel
= lttng_channel_create(&handle
->domain
);
1541 return -LTTNG_ERR_NOMEM
;
1545 * Create a new channel in order to use default extended
1548 extended
= (struct lttng_channel_extended
*)
1549 channel
->attr
.extended
.ptr
;
1550 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1551 lttng_channel_destroy(channel
);
1553 struct lttng_channel_extended
*extended
;
1555 extended
= (struct lttng_channel_extended
*)
1556 in_chan
->attr
.extended
.ptr
;
1557 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1561 * Verify that the amount of memory required to create the requested
1562 * buffer is available on the system at the moment.
1564 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1565 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1566 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1567 return -LTTNG_ERR_NOMEM
;
1570 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1571 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1573 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1574 sizeof(lsm
.session
.name
));
1576 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1580 * All tracing will be stopped for registered events of the channel.
1581 * Returns size of returned session payload data or a negative error code.
1583 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1585 struct lttcomm_session_msg lsm
;
1587 /* Safety check. Both are mandatory. */
1588 if (handle
== NULL
|| name
== NULL
) {
1589 return -LTTNG_ERR_INVALID
;
1592 memset(&lsm
, 0, sizeof(lsm
));
1594 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1596 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1597 sizeof(lsm
.u
.disable
.channel_name
));
1599 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1601 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1602 sizeof(lsm
.session
.name
));
1604 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1608 * Add PID to session tracker.
1609 * Return 0 on success else a negative LTTng error code.
1611 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1613 struct lttcomm_session_msg lsm
;
1615 /* NULL arguments are forbidden. No default values. */
1616 if (handle
== NULL
) {
1617 return -LTTNG_ERR_INVALID
;
1620 memset(&lsm
, 0, sizeof(lsm
));
1622 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1623 lsm
.u
.pid_tracker
.pid
= pid
;
1625 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1627 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1628 sizeof(lsm
.session
.name
));
1630 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1634 * Remove PID from session tracker.
1635 * Return 0 on success else a negative LTTng error code.
1637 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1639 struct lttcomm_session_msg lsm
;
1641 /* NULL arguments are forbidden. No default values. */
1642 if (handle
== NULL
) {
1643 return -LTTNG_ERR_INVALID
;
1646 memset(&lsm
, 0, sizeof(lsm
));
1648 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1649 lsm
.u
.pid_tracker
.pid
= pid
;
1651 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1653 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1654 sizeof(lsm
.session
.name
));
1656 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1660 * Lists all available tracepoints of domain.
1661 * Sets the contents of the events array.
1662 * Returns the number of lttng_event entries in events;
1663 * on error, returns a negative value.
1665 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1666 struct lttng_event
**events
)
1669 struct lttcomm_session_msg lsm
;
1671 if (handle
== NULL
) {
1672 return -LTTNG_ERR_INVALID
;
1675 memset(&lsm
, 0, sizeof(lsm
));
1676 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1677 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1679 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1684 return ret
/ sizeof(struct lttng_event
);
1688 * Lists all available tracepoint fields of domain.
1689 * Sets the contents of the event field array.
1690 * Returns the number of lttng_event_field entries in events;
1691 * on error, returns a negative value.
1693 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1694 struct lttng_event_field
**fields
)
1697 struct lttcomm_session_msg lsm
;
1699 if (handle
== NULL
) {
1700 return -LTTNG_ERR_INVALID
;
1703 memset(&lsm
, 0, sizeof(lsm
));
1704 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1705 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1707 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1712 return ret
/ sizeof(struct lttng_event_field
);
1716 * Lists all available kernel system calls. Allocates and sets the contents of
1719 * Returns the number of lttng_event entries in events; on error, returns a
1722 int lttng_list_syscalls(struct lttng_event
**events
)
1725 struct lttcomm_session_msg lsm
;
1728 return -LTTNG_ERR_INVALID
;
1731 memset(&lsm
, 0, sizeof(lsm
));
1732 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1733 /* Force kernel domain for system calls. */
1734 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1736 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1741 return ret
/ sizeof(struct lttng_event
);
1745 * Returns a human readable string describing
1746 * the error code (a negative value).
1748 const char *lttng_strerror(int code
)
1750 return error_get_str(code
);
1753 enum lttng_error_code
lttng_create_session_ext(
1754 struct lttng_session_descriptor
*session_descriptor
)
1756 enum lttng_error_code ret_code
;
1757 struct lttcomm_session_msg lsm
= {
1758 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1761 struct lttng_buffer_view reply_view
;
1763 bool sessiond_must_generate_ouput
;
1764 struct lttng_dynamic_buffer payload
;
1766 size_t descriptor_size
;
1767 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1769 lttng_dynamic_buffer_init(&payload
);
1770 if (!session_descriptor
) {
1771 ret_code
= LTTNG_ERR_INVALID
;
1775 sessiond_must_generate_ouput
=
1776 !lttng_session_descriptor_is_output_destination_initialized(
1777 session_descriptor
);
1778 if (sessiond_must_generate_ouput
) {
1779 const char *home_dir
= utils_get_home_dir();
1780 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1782 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1783 ret_code
= LTTNG_ERR_FATAL
;
1787 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1788 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1791 ret_code
= LTTNG_ERR_NOMEM
;
1796 descriptor_size
= payload
.size
;
1797 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1800 ret_code
= LTTNG_ERR_INVALID
;
1803 descriptor_size
= payload
.size
- descriptor_size
;
1804 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1806 /* Command returns a session descriptor on success. */
1807 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1808 payload
.size
, &reply
);
1809 if (reply_ret
< 0) {
1810 ret_code
= -reply_ret
;
1812 } else if (reply_ret
== 0) {
1813 /* Socket unexpectedly closed by the session daemon. */
1814 ret_code
= LTTNG_ERR_FATAL
;
1818 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1819 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1822 ret_code
= LTTNG_ERR_FATAL
;
1825 ret_code
= LTTNG_OK
;
1826 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1829 lttng_dynamic_buffer_reset(&payload
);
1830 lttng_session_descriptor_destroy(descriptor_reply
);
1835 * Create a new session using name and url for destination.
1837 * Returns LTTNG_OK on success or a negative error code.
1839 int lttng_create_session(const char *name
, const char *url
)
1843 struct lttng_uri
*uris
= NULL
;
1844 struct lttng_session_descriptor
*descriptor
= NULL
;
1845 enum lttng_error_code ret_code
;
1848 ret
= -LTTNG_ERR_INVALID
;
1852 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1854 ret
= -LTTNG_ERR_INVALID
;
1859 descriptor
= lttng_session_descriptor_create(name
);
1862 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1863 ret
= -LTTNG_ERR_INVALID
;
1866 descriptor
= lttng_session_descriptor_local_create(name
,
1870 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1874 ret
= -LTTNG_ERR_INVALID
;
1878 ret
= -LTTNG_ERR_INVALID
;
1881 ret_code
= lttng_create_session_ext(descriptor
);
1882 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1884 lttng_session_descriptor_destroy(descriptor
);
1890 * Create a session exclusively used for snapshot.
1892 * Returns LTTNG_OK on success or a negative error code.
1894 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1897 enum lttng_error_code ret_code
;
1899 struct lttng_uri
*uris
= NULL
;
1900 struct lttng_session_descriptor
*descriptor
= NULL
;
1903 ret
= -LTTNG_ERR_INVALID
;
1907 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1909 ret
= -LTTNG_ERR_INVALID
;
1913 * If the user does not specify a custom subdir, use the session name.
1915 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1916 strlen(uris
[0].subdir
) == 0) {
1917 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1920 PERROR("Failed to set session name as network destination sub-directory");
1921 ret
= -LTTNG_ERR_FATAL
;
1923 } else if (ret
>= sizeof(uris
[0].subdir
)) {
1924 /* Truncated output. */
1925 ret
= -LTTNG_ERR_INVALID
;
1932 descriptor
= lttng_session_descriptor_snapshot_create(name
);
1935 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1936 ret
= -LTTNG_ERR_INVALID
;
1939 descriptor
= lttng_session_descriptor_snapshot_local_create(
1944 descriptor
= lttng_session_descriptor_snapshot_network_create(
1950 ret
= -LTTNG_ERR_INVALID
;
1954 ret
= -LTTNG_ERR_INVALID
;
1957 ret_code
= lttng_create_session_ext(descriptor
);
1958 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1960 lttng_session_descriptor_destroy(descriptor
);
1966 * Create a session exclusively used for live.
1968 * Returns LTTNG_OK on success or a negative error code.
1970 int lttng_create_session_live(const char *name
, const char *url
,
1971 unsigned int timer_interval
)
1974 enum lttng_error_code ret_code
;
1975 struct lttng_session_descriptor
*descriptor
= NULL
;
1978 ret
= -LTTNG_ERR_INVALID
;
1983 descriptor
= lttng_session_descriptor_live_network_create(
1984 name
, url
, NULL
, timer_interval
);
1986 descriptor
= lttng_session_descriptor_live_create(
1987 name
, timer_interval
);
1990 ret
= -LTTNG_ERR_INVALID
;
1993 ret_code
= lttng_create_session_ext(descriptor
);
1994 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1996 lttng_session_descriptor_destroy(descriptor
);
2001 * Destroy session using name.
2002 * Returns size of returned session payload data or a negative error code.
2005 int _lttng_destroy_session(const char *session_name
)
2007 struct lttcomm_session_msg lsm
;
2009 if (session_name
== NULL
) {
2010 return -LTTNG_ERR_INVALID
;
2013 memset(&lsm
, 0, sizeof(lsm
));
2014 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
2016 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2017 sizeof(lsm
.session
.name
));
2019 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2023 * Stop the session and wait for the data before destroying it
2025 int lttng_destroy_session(const char *session_name
)
2030 * Stop the tracing and wait for the data.
2032 ret
= _lttng_stop_tracing(session_name
, 1);
2033 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2037 ret
= _lttng_destroy_session(session_name
);
2043 * Destroy the session without waiting for the data.
2045 int lttng_destroy_session_no_wait(const char *session_name
)
2050 * Stop the tracing without waiting for the data.
2051 * The session might already have been stopped, so just
2054 ret
= _lttng_stop_tracing(session_name
, 0);
2055 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2059 ret
= _lttng_destroy_session(session_name
);
2065 * Ask the session daemon for all available sessions.
2066 * Sets the contents of the sessions array.
2067 * Returns the number of lttng_session entries in sessions;
2068 * on error, returns a negative value.
2070 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2073 struct lttcomm_session_msg lsm
;
2074 const size_t session_size
= sizeof(struct lttng_session
) +
2075 sizeof(struct lttng_session_extended
);
2076 size_t session_count
, i
;
2077 struct lttng_session_extended
*sessions_extended_begin
;
2078 struct lttng_session
*sessions
= NULL
;
2080 memset(&lsm
, 0, sizeof(lsm
));
2081 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2082 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2084 ret
= ret
== 0 ? -LTTNG_ERR_FATAL
: ret
;
2088 ret
= -LTTNG_ERR_FATAL
;
2092 if (ret
% session_size
) {
2093 ret
= -LTTNG_ERR_UNK
;
2095 *out_sessions
= NULL
;
2098 session_count
= (size_t) ret
/ session_size
;
2099 sessions_extended_begin
= (struct lttng_session_extended
*)
2100 (&sessions
[session_count
]);
2102 /* Set extended session info pointers. */
2103 for (i
= 0; i
< session_count
; i
++) {
2104 struct lttng_session
*session
= &sessions
[i
];
2105 struct lttng_session_extended
*extended
=
2106 &(sessions_extended_begin
[i
]);
2108 session
->extended
.ptr
= extended
;
2111 ret
= (int) session_count
;
2112 *out_sessions
= sessions
;
2117 enum lttng_error_code
lttng_session_get_creation_time(
2118 const struct lttng_session
*session
, uint64_t *creation_time
)
2120 enum lttng_error_code ret
= LTTNG_OK
;
2121 struct lttng_session_extended
*extended
;
2123 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2124 ret
= LTTNG_ERR_INVALID
;
2128 extended
= session
->extended
.ptr
;
2129 if (!extended
->creation_time
.is_set
) {
2130 /* Not created on the session daemon yet. */
2131 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2134 *creation_time
= extended
->creation_time
.value
;
2139 int lttng_set_session_shm_path(const char *session_name
,
2140 const char *shm_path
)
2142 struct lttcomm_session_msg lsm
;
2144 if (session_name
== NULL
) {
2145 return -LTTNG_ERR_INVALID
;
2148 memset(&lsm
, 0, sizeof(lsm
));
2149 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2151 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2152 sizeof(lsm
.session
.name
));
2153 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
2154 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2156 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
2160 * Ask the session daemon for all available domains of a session.
2161 * Sets the contents of the domains array.
2162 * Returns the number of lttng_domain entries in domains;
2163 * on error, returns a negative value.
2165 int lttng_list_domains(const char *session_name
,
2166 struct lttng_domain
**domains
)
2169 struct lttcomm_session_msg lsm
;
2171 if (session_name
== NULL
) {
2172 return -LTTNG_ERR_INVALID
;
2175 memset(&lsm
, 0, sizeof(lsm
));
2176 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2178 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2179 sizeof(lsm
.session
.name
));
2181 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2186 return ret
/ sizeof(struct lttng_domain
);
2190 * Ask the session daemon for all available channels of a session.
2191 * Sets the contents of the channels array.
2192 * Returns the number of lttng_channel entries in channels;
2193 * on error, returns a negative value.
2195 int lttng_list_channels(struct lttng_handle
*handle
,
2196 struct lttng_channel
**channels
)
2199 size_t channel_count
, i
;
2200 const size_t channel_size
= sizeof(struct lttng_channel
) +
2201 sizeof(struct lttng_channel_extended
);
2202 struct lttcomm_session_msg lsm
;
2205 if (handle
== NULL
) {
2206 ret
= -LTTNG_ERR_INVALID
;
2210 memset(&lsm
, 0, sizeof(lsm
));
2211 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2212 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2213 sizeof(lsm
.session
.name
));
2215 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2217 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2222 if (ret
% channel_size
) {
2223 ret
= -LTTNG_ERR_UNK
;
2228 channel_count
= (size_t) ret
/ channel_size
;
2230 /* Set extended info pointers */
2231 extended_at
= ((void *) *channels
) +
2232 channel_count
* sizeof(struct lttng_channel
);
2233 for (i
= 0; i
< channel_count
; i
++) {
2234 struct lttng_channel
*chan
= &(*channels
)[i
];
2236 chan
->attr
.extended
.ptr
= extended_at
;
2237 extended_at
+= sizeof(struct lttng_channel_extended
);
2240 ret
= (int) channel_count
;
2246 * Ask the session daemon for all available events of a session channel.
2247 * Sets the contents of the events array.
2248 * Returns the number of lttng_event entries in events;
2249 * on error, returns a negative value.
2251 int lttng_list_events(struct lttng_handle
*handle
,
2252 const char *channel_name
, struct lttng_event
**events
)
2255 struct lttcomm_session_msg lsm
;
2256 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2257 size_t cmd_header_len
;
2258 uint32_t nb_events
, i
;
2260 char *reception_buffer
= NULL
;
2261 struct lttng_dynamic_buffer listing
;
2264 /* Safety check. An handle and channel name are mandatory */
2265 if (handle
== NULL
|| channel_name
== NULL
) {
2266 return -LTTNG_ERR_INVALID
;
2269 memset(&lsm
, 0, sizeof(lsm
));
2270 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2271 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2272 sizeof(lsm
.session
.name
));
2273 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2274 sizeof(lsm
.u
.list
.channel_name
));
2275 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2277 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2278 (void **) &reception_buffer
, (void **) &cmd_header
,
2285 ret
= -LTTNG_ERR_UNK
;
2289 /* Set number of events and free command header */
2290 nb_events
= cmd_header
->nb_events
;
2291 if (nb_events
> INT_MAX
) {
2299 * The buffer that is returned must contain a "flat" version of
2300 * the events that are returned. In other words, all pointers
2301 * within an lttng_event must point to a location within the returned
2302 * buffer so that the user may free everything by simply calling free()
2303 * on the returned buffer. This is needed in order to maintain API
2306 * A first pass is performed to compute the size of the buffer that
2307 * must be allocated. A second pass is then performed to setup
2308 * the returned events so that their members always point within the
2311 * The layout of the returned buffer is as follows:
2312 * - struct lttng_event[nb_events],
2313 * - nb_events times the following:
2314 * - struct lttng_event_extended,
2315 * - flattened version of userspace_probe_location
2316 * - filter_expression
2318 * - padding to align to 64-bits
2320 comm_ext_at
= reception_buffer
+
2321 (nb_events
* sizeof(struct lttng_event
));
2322 storage_req
= nb_events
* sizeof(struct lttng_event
);
2324 for (i
= 0; i
< nb_events
; i
++) {
2325 struct lttcomm_event_extended_header
*ext_comm
=
2326 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2327 int probe_storage_req
= 0;
2329 comm_ext_at
+= sizeof(*ext_comm
);
2330 comm_ext_at
+= ext_comm
->filter_len
;
2332 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2334 if (ext_comm
->userspace_probe_location_len
) {
2335 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2336 struct lttng_buffer_view probe_location_view
;
2338 probe_location_view
= lttng_buffer_view_init(
2340 ext_comm
->userspace_probe_location_len
);
2343 * Create a temporary userspace probe location to
2344 * determine the size needed by a "flattened" version
2345 * of that same probe location.
2347 ret
= lttng_userspace_probe_location_create_from_buffer(
2348 &probe_location_view
, &probe_location
);
2350 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2354 ret
= lttng_userspace_probe_location_flatten(
2355 probe_location
, NULL
);
2356 lttng_userspace_probe_location_destroy(probe_location
);
2358 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2362 probe_storage_req
= ret
;
2363 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2366 storage_req
+= sizeof(struct lttng_event_extended
);
2367 storage_req
+= ext_comm
->filter_len
;
2368 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2369 /* Padding to ensure the flat probe is aligned. */
2370 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2371 storage_req
+= probe_storage_req
;
2374 lttng_dynamic_buffer_init(&listing
);
2376 * We must ensure that "listing" is never resized so as to preserve
2377 * the validity of the flattened objects.
2379 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2381 ret
= -LTTNG_ERR_NOMEM
;
2385 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2386 nb_events
* sizeof(struct lttng_event
));
2388 ret
= -LTTNG_ERR_NOMEM
;
2389 goto free_dynamic_buffer
;
2392 comm_ext_at
= reception_buffer
+
2393 (nb_events
* sizeof(struct lttng_event
));
2394 for (i
= 0; i
< nb_events
; i
++) {
2395 struct lttng_event
*event
= (struct lttng_event
*)
2396 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2397 struct lttcomm_event_extended_header
*ext_comm
=
2398 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2399 struct lttng_event_extended
*event_extended
=
2400 (struct lttng_event_extended
*)
2401 (listing
.data
+ listing
.size
);
2403 /* Insert struct lttng_event_extended. */
2404 ret
= lttng_dynamic_buffer_set_size(&listing
,
2405 listing
.size
+ sizeof(*event_extended
));
2407 ret
= -LTTNG_ERR_NOMEM
;
2408 goto free_dynamic_buffer
;
2410 event
->extended
.ptr
= event_extended
;
2412 comm_ext_at
+= sizeof(*ext_comm
);
2414 /* Insert filter expression. */
2415 if (ext_comm
->filter_len
) {
2416 event_extended
->filter_expression
= listing
.data
+
2418 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2419 ext_comm
->filter_len
);
2421 ret
= -LTTNG_ERR_NOMEM
;
2422 goto free_dynamic_buffer
;
2424 comm_ext_at
+= ext_comm
->filter_len
;
2427 /* Insert exclusions. */
2428 if (ext_comm
->nb_exclusions
) {
2429 event_extended
->exclusions
.count
=
2430 ext_comm
->nb_exclusions
;
2431 event_extended
->exclusions
.strings
=
2432 listing
.data
+ listing
.size
;
2434 ret
= lttng_dynamic_buffer_append(&listing
,
2436 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2438 ret
= -LTTNG_ERR_NOMEM
;
2439 goto free_dynamic_buffer
;
2441 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2444 /* Insert padding to align to 64-bits. */
2445 ret
= lttng_dynamic_buffer_set_size(&listing
,
2446 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2448 ret
= -LTTNG_ERR_NOMEM
;
2449 goto free_dynamic_buffer
;
2452 /* Insert flattened userspace probe location. */
2453 if (ext_comm
->userspace_probe_location_len
) {
2454 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2455 struct lttng_buffer_view probe_location_view
;
2457 probe_location_view
= lttng_buffer_view_init(
2459 ext_comm
->userspace_probe_location_len
);
2461 ret
= lttng_userspace_probe_location_create_from_buffer(
2462 &probe_location_view
, &probe_location
);
2464 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2465 goto free_dynamic_buffer
;
2468 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2469 (listing
.data
+ listing
.size
);
2470 ret
= lttng_userspace_probe_location_flatten(
2471 probe_location
, &listing
);
2472 lttng_userspace_probe_location_destroy(probe_location
);
2474 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2475 goto free_dynamic_buffer
;
2478 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2482 /* Don't reset listing buffer as we return its content. */
2483 *events
= (struct lttng_event
*) listing
.data
;
2484 lttng_dynamic_buffer_init(&listing
);
2485 ret
= (int) nb_events
;
2486 free_dynamic_buffer
:
2487 lttng_dynamic_buffer_reset(&listing
);
2490 free(reception_buffer
);
2495 * Sets the tracing_group variable with name.
2496 * This function allocates memory pointed to by tracing_group.
2497 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2499 int lttng_set_tracing_group(const char *name
)
2502 return -LTTNG_ERR_INVALID
;
2505 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2506 return -LTTNG_ERR_FATAL
;
2512 int lttng_calibrate(struct lttng_handle
*handle
,
2513 struct lttng_calibrate
*calibrate
)
2516 * This command was removed in LTTng 2.9.
2518 return -LTTNG_ERR_UND
;
2522 * Set default channel attributes.
2523 * If either or both of the arguments are null, attr content is zeroe'd.
2525 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2526 struct lttng_channel_attr
*attr
)
2528 struct lttng_channel_extended
*extended
;
2531 if (attr
== NULL
|| domain
== NULL
) {
2535 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2536 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2538 /* Same for all domains. */
2539 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2540 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2541 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2543 switch (domain
->type
) {
2544 case LTTNG_DOMAIN_KERNEL
:
2545 attr
->switch_timer_interval
=
2546 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2547 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2548 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2549 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2550 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2552 extended
->monitor_timer_interval
=
2553 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2554 extended
->blocking_timeout
=
2555 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2558 case LTTNG_DOMAIN_UST
:
2559 switch (domain
->buf_type
) {
2560 case LTTNG_BUFFER_PER_UID
:
2561 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2562 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2563 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2564 attr
->switch_timer_interval
=
2565 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2566 attr
->read_timer_interval
=
2567 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2569 extended
->monitor_timer_interval
=
2570 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2571 extended
->blocking_timeout
=
2572 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2575 case LTTNG_BUFFER_PER_PID
:
2577 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2578 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2579 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2580 attr
->switch_timer_interval
=
2581 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2582 attr
->read_timer_interval
=
2583 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2585 extended
->monitor_timer_interval
=
2586 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2587 extended
->blocking_timeout
=
2588 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2593 /* Default behavior: leave set to 0. */
2597 attr
->extended
.ptr
= extended
;
2600 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2601 uint64_t *discarded_events
)
2604 struct lttng_channel_extended
*chan_ext
;
2606 if (!channel
|| !discarded_events
) {
2607 ret
= -LTTNG_ERR_INVALID
;
2611 chan_ext
= channel
->attr
.extended
.ptr
;
2614 * This can happen since the lttng_channel structure is
2615 * used for other tasks where this pointer is never set.
2617 *discarded_events
= 0;
2621 *discarded_events
= chan_ext
->discarded_events
;
2626 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2627 uint64_t *lost_packets
)
2630 struct lttng_channel_extended
*chan_ext
;
2632 if (!channel
|| !lost_packets
) {
2633 ret
= -LTTNG_ERR_INVALID
;
2637 chan_ext
= channel
->attr
.extended
.ptr
;
2640 * This can happen since the lttng_channel structure is
2641 * used for other tasks where this pointer is never set.
2647 *lost_packets
= chan_ext
->lost_packets
;
2652 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2653 uint64_t *monitor_timer_interval
)
2657 if (!chan
|| !monitor_timer_interval
) {
2658 ret
= -LTTNG_ERR_INVALID
;
2662 if (!chan
->attr
.extended
.ptr
) {
2663 ret
= -LTTNG_ERR_INVALID
;
2667 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2668 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2673 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2674 uint64_t monitor_timer_interval
)
2678 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2679 ret
= -LTTNG_ERR_INVALID
;
2683 ((struct lttng_channel_extended
*)
2684 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2685 monitor_timer_interval
;
2690 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2691 int64_t *blocking_timeout
)
2695 if (!chan
|| !blocking_timeout
) {
2696 ret
= -LTTNG_ERR_INVALID
;
2700 if (!chan
->attr
.extended
.ptr
) {
2701 ret
= -LTTNG_ERR_INVALID
;
2705 *blocking_timeout
= ((struct lttng_channel_extended
*)
2706 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2711 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2712 int64_t blocking_timeout
)
2715 int64_t msec_timeout
;
2717 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2718 ret
= -LTTNG_ERR_INVALID
;
2722 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2723 ret
= -LTTNG_ERR_INVALID
;
2728 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2729 * us to accept a narrower range of values (msecs expressed as a signed
2732 msec_timeout
= blocking_timeout
/ 1000;
2733 if (msec_timeout
!= (int32_t) msec_timeout
) {
2734 ret
= -LTTNG_ERR_INVALID
;
2738 ((struct lttng_channel_extended
*)
2739 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2746 * Check if session daemon is alive.
2748 * Return 1 if alive or 0 if not.
2749 * On error returns a negative value.
2751 int lttng_session_daemon_alive(void)
2755 ret
= set_session_daemon_path();
2761 if (*sessiond_sock_path
== '\0') {
2763 * No socket path set. Weird error which means the constructor
2769 ret
= try_connect_sessiond(sessiond_sock_path
);
2780 * Set URL for a consumer for a session and domain.
2782 * Return 0 on success, else a negative value.
2784 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2785 const char *control_url
, const char *data_url
)
2789 struct lttcomm_session_msg lsm
;
2790 struct lttng_uri
*uris
= NULL
;
2792 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2793 return -LTTNG_ERR_INVALID
;
2796 memset(&lsm
, 0, sizeof(lsm
));
2798 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2800 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2801 sizeof(lsm
.session
.name
));
2802 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2804 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2806 return -LTTNG_ERR_INVALID
;
2809 lsm
.u
.uri
.size
= size
;
2811 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2812 sizeof(struct lttng_uri
) * size
, NULL
);
2821 int lttng_enable_consumer(struct lttng_handle
*handle
)
2829 int lttng_disable_consumer(struct lttng_handle
*handle
)
2837 int _lttng_create_session_ext(const char *name
, const char *url
,
2838 const char *datetime
)
2844 * For a given session name, this call checks if the data is ready to be read
2845 * or is still being extracted by the consumer(s) hence not ready to be used by
2848 int lttng_data_pending(const char *session_name
)
2851 struct lttcomm_session_msg lsm
;
2852 uint8_t *pending
= NULL
;
2854 if (session_name
== NULL
) {
2855 return -LTTNG_ERR_INVALID
;
2858 memset(&lsm
, 0, sizeof(lsm
));
2859 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2861 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2862 sizeof(lsm
.session
.name
));
2864 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2867 } else if (ret
!= 1) {
2868 /* Unexpected payload size */
2869 ret
= -LTTNG_ERR_INVALID
;
2873 ret
= (int) *pending
;
2880 * List PIDs in the tracker.
2882 * enabled is set to whether the PID tracker is enabled.
2883 * pids is set to an allocated array of PIDs currently tracked. On
2884 * success, pids must be freed by the caller.
2885 * nr_pids is set to the number of entries contained by the pids array.
2887 * Returns 0 on success, else a negative LTTng error code.
2889 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2890 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2894 struct lttcomm_session_msg lsm
;
2896 int32_t *pids
= NULL
;
2898 if (handle
== NULL
) {
2899 return -LTTNG_ERR_INVALID
;
2902 memset(&lsm
, 0, sizeof(lsm
));
2903 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2904 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2905 sizeof(lsm
.session
.name
));
2906 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2908 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2912 nr_pids
= ret
/ sizeof(int32_t);
2913 if (nr_pids
> 0 && !pids
) {
2914 return -LTTNG_ERR_UNK
;
2916 if (nr_pids
== 1 && pids
[0] == -1) {
2922 *_enabled
= enabled
;
2924 *_nr_pids
= nr_pids
;
2929 * Regenerate the metadata for a session.
2930 * Return 0 on success, a negative error code on error.
2932 int lttng_regenerate_metadata(const char *session_name
)
2935 struct lttcomm_session_msg lsm
;
2937 if (!session_name
) {
2938 ret
= -LTTNG_ERR_INVALID
;
2942 memset(&lsm
, 0, sizeof(lsm
));
2943 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2945 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2946 sizeof(lsm
.session
.name
));
2948 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2959 * Deprecated, replaced by lttng_regenerate_metadata.
2961 int lttng_metadata_regenerate(const char *session_name
)
2963 return lttng_regenerate_metadata(session_name
);
2967 * Regenerate the statedump of a session.
2968 * Return 0 on success, a negative error code on error.
2970 int lttng_regenerate_statedump(const char *session_name
)
2973 struct lttcomm_session_msg lsm
;
2975 if (!session_name
) {
2976 ret
= -LTTNG_ERR_INVALID
;
2980 memset(&lsm
, 0, sizeof(lsm
));
2981 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2983 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2984 sizeof(lsm
.session
.name
));
2986 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2996 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2999 struct lttcomm_session_msg lsm
;
3000 struct lttng_dynamic_buffer buffer
;
3002 lttng_dynamic_buffer_init(&buffer
);
3004 ret
= -LTTNG_ERR_INVALID
;
3008 if (!lttng_trigger_validate(trigger
)) {
3009 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3013 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3015 ret
= -LTTNG_ERR_UNK
;
3019 memset(&lsm
, 0, sizeof(lsm
));
3020 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3021 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3022 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3025 lttng_dynamic_buffer_reset(&buffer
);
3029 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3032 struct lttcomm_session_msg lsm
;
3033 struct lttng_dynamic_buffer buffer
;
3035 lttng_dynamic_buffer_init(&buffer
);
3037 ret
= -LTTNG_ERR_INVALID
;
3041 if (!lttng_trigger_validate(trigger
)) {
3042 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3046 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3048 ret
= -LTTNG_ERR_UNK
;
3052 memset(&lsm
, 0, sizeof(lsm
));
3053 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3054 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3055 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3058 lttng_dynamic_buffer_reset(&buffer
);
3065 static void __attribute__((constructor
)) init(void)
3067 /* Set default session group */
3068 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3074 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3076 free(tracing_group
);