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>
48 #include <lttng/destruction-handle.h>
50 #include "filter/filter-ast.h"
51 #include "filter/filter-parser.h"
52 #include "filter/filter-bytecode.h"
53 #include "filter/memstream.h"
54 #include "lttng-ctl-helper.h"
57 static const int print_xml
= 1;
58 #define dbg_printf(fmt, args...) \
59 printf("[debug liblttng-ctl] " fmt, ## args)
61 static const int print_xml
= 0;
62 #define dbg_printf(fmt, args...) \
64 /* do nothing but check printf format */ \
66 printf("[debug liblttnctl] " fmt, ## args); \
70 #define COPY_DOMAIN_PACKED(dst, src) \
72 struct lttng_domain _tmp_domain; \
74 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
78 /* Socket to session daemon for communication */
79 static int sessiond_socket
= -1;
80 static char sessiond_sock_path
[PATH_MAX
];
83 static char *tracing_group
;
89 * Those two variables are used by error.h to silent or control the verbosity of
90 * error message. They are global to the library so application linking with it
91 * are able to compile correctly and also control verbosity of the library.
94 int lttng_opt_verbose
;
98 * Copy domain to lttcomm_session_msg domain.
100 * If domain is unknown, default domain will be the kernel.
103 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
104 struct lttng_domain
*src
)
108 case LTTNG_DOMAIN_KERNEL
:
109 case LTTNG_DOMAIN_UST
:
110 case LTTNG_DOMAIN_JUL
:
111 case LTTNG_DOMAIN_LOG4J
:
112 case LTTNG_DOMAIN_PYTHON
:
113 memcpy(dst
, src
, sizeof(struct lttng_domain
));
116 memset(dst
, 0, sizeof(struct lttng_domain
));
123 * Send lttcomm_session_msg to the session daemon.
125 * On success, returns the number of bytes sent (>=0)
126 * On error, returns -1
128 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
133 ret
= -LTTNG_ERR_NO_SESSIOND
;
137 DBG("LSM cmd type : %d", lsm
->cmd_type
);
139 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
140 sizeof(struct lttcomm_session_msg
));
142 ret
= -LTTNG_ERR_FATAL
;
150 * Send var len data to the session daemon.
152 * On success, returns the number of bytes sent (>=0)
153 * On error, returns -1
155 static int send_session_varlen(const void *data
, size_t len
)
160 ret
= -LTTNG_ERR_NO_SESSIOND
;
169 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
171 ret
= -LTTNG_ERR_FATAL
;
179 * Send file descriptors to the session daemon.
181 * On success, returns the number of bytes sent (>=0)
182 * On error, returns -1
184 static int send_session_fds(const int *fds
, size_t nb_fd
)
189 ret
= -LTTNG_ERR_NO_SESSIOND
;
193 if (!fds
|| !nb_fd
) {
198 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
200 ret
= -LTTNG_ERR_FATAL
;
208 * Receive data from the sessiond socket.
210 * On success, returns the number of bytes received (>=0)
211 * On error, returns -1 (recvmsg() error) or -ENOTCONN
213 static int recv_data_sessiond(void *buf
, size_t len
)
218 ret
= -LTTNG_ERR_NO_SESSIOND
;
222 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
224 ret
= -LTTNG_ERR_FATAL
;
232 * Check if we are in the specified group.
234 * If yes return 1, else return -1.
237 int lttng_check_tracing_group(void)
239 gid_t
*grp_list
, tracing_gid
;
240 int grp_list_size
, grp_id
, i
;
242 const char *grp_name
= tracing_group
;
244 /* Get GID of group 'tracing' */
245 if (utils_get_group_id(grp_name
, false, &tracing_gid
)) {
246 /* If grp_tracing is NULL, the group does not exist. */
250 /* Get number of supplementary group IDs */
251 grp_list_size
= getgroups(0, NULL
);
252 if (grp_list_size
< 0) {
257 /* Alloc group list of the right size */
258 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
263 grp_id
= getgroups(grp_list_size
, grp_list
);
269 for (i
= 0; i
< grp_list_size
; i
++) {
270 if (grp_list
[i
] == tracing_gid
) {
283 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
287 size_t best_mem_info
;
288 size_t num_bytes_requested_total
;
291 * Get the number of CPU currently online to compute the amount of
292 * memory needed to create a buffer for every CPU.
294 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
299 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
302 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
303 * reliable estimate we can get but it is only exposed by the kernel
304 * since 3.14. (See Linux kernel commit:
305 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
307 ret
= utils_get_memory_available(&best_mem_info
);
313 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
314 * is a sanity check for obvious user error.
316 ret
= utils_get_memory_total(&best_mem_info
);
324 return best_mem_info
>= num_bytes_requested_total
;
328 * Try connect to session daemon with sock_path.
330 * Return 0 on success, else -1
332 static int try_connect_sessiond(const char *sock_path
)
336 /* If socket exist, we check if the daemon listens for connect. */
337 ret
= access(sock_path
, F_OK
);
343 ret
= lttcomm_connect_unix_sock(sock_path
);
349 ret
= lttcomm_close_unix_sock(ret
);
351 PERROR("lttcomm_close_unix_sock");
361 * Set sessiond socket path by putting it in the global sessiond_sock_path
364 * Returns 0 on success, negative value on failure (the sessiond socket path
365 * is somehow too long or ENOMEM).
367 static int set_session_daemon_path(void)
369 int in_tgroup
= 0; /* In tracing group. */
375 /* Are we in the tracing group ? */
376 in_tgroup
= lttng_check_tracing_group();
379 if ((uid
== 0) || in_tgroup
) {
380 const int ret
= lttng_strncpy(sessiond_sock_path
,
381 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
382 sizeof(sessiond_sock_path
));
394 ret
= try_connect_sessiond(sessiond_sock_path
);
398 /* Global session daemon not available... */
400 /* ...or not in tracing group (and not root), default */
403 * With GNU C < 2.1, snprintf returns -1 if the target buffer
405 * With GNU C >= 2.1, snprintf returns the required size
406 * (excluding closing null)
408 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
409 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
410 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
422 * Connect to the LTTng session daemon.
424 * On success, return the socket's file descriptor. On error, return -1.
426 LTTNG_HIDDEN
int connect_sessiond(void)
430 ret
= set_session_daemon_path();
435 /* Connect to the sesssion daemon. */
436 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
447 static void reset_global_sessiond_connection_state(void)
449 sessiond_socket
= -1;
454 * Clean disconnect from the session daemon.
456 * On success, return 0. On error, return -1.
458 static int disconnect_sessiond(void)
463 ret
= lttcomm_close_unix_sock(sessiond_socket
);
464 reset_global_sessiond_connection_state();
470 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
478 ret
= -LTTNG_ERR_INVALID
;
488 ret
= recv_data_sessiond(buf
, len
);
494 ret
= -LTTNG_ERR_INVALID
;
498 /* Move ownership of command header buffer to user. */
503 /* No command header. */
519 * Ask the session daemon a specific command and put the data into buf.
520 * Takes extra var. len. data and file descriptors as input to send to the
523 * Return size of data (only payload, not header) or a negative error code.
526 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
527 const int *fds
, size_t nb_fd
, const void *vardata
,
528 size_t vardata_len
, void **user_payload_buf
,
529 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
533 struct lttcomm_lttng_msg llm
;
535 ret
= connect_sessiond();
537 ret
= -LTTNG_ERR_NO_SESSIOND
;
540 sessiond_socket
= ret
;
544 /* Send command to session daemon */
545 ret
= send_session_msg(lsm
);
547 /* Ret value is a valid lttng error code. */
550 /* Send var len data */
551 ret
= send_session_varlen(vardata
, vardata_len
);
553 /* Ret value is a valid lttng error code. */
558 ret
= send_session_fds(fds
, nb_fd
);
560 /* Ret value is a valid lttng error code. */
564 /* Get header from data transmission */
565 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
567 /* Ret value is a valid lttng error code. */
571 /* Check error code if OK */
572 if (llm
.ret_code
!= LTTNG_OK
) {
577 /* Get command header from data transmission */
578 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
579 user_cmd_header_buf
, user_cmd_header_len
);
584 /* Get payload from data transmission */
585 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
594 disconnect_sessiond();
599 * Create lttng handle and return pointer.
601 * The returned pointer will be NULL in case of malloc() error.
603 struct lttng_handle
*lttng_create_handle(const char *session_name
,
604 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 ret
= lttng_strncpy(handle
->session_name
, session_name
? : "",
617 sizeof(handle
->session_name
));
622 /* Copy lttng domain or leave initialized to 0. */
624 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
635 * Destroy handle by free(3) the pointer.
637 void lttng_destroy_handle(struct lttng_handle
*handle
)
643 * Register an outside consumer.
645 * Returns size of returned session payload data or a negative error code.
647 int lttng_register_consumer(struct lttng_handle
*handle
,
648 const char *socket_path
)
651 struct lttcomm_session_msg lsm
;
653 if (handle
== NULL
|| socket_path
== NULL
) {
654 ret
= -LTTNG_ERR_INVALID
;
658 memset(&lsm
, 0, sizeof(lsm
));
659 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
660 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
661 sizeof(lsm
.session
.name
));
663 ret
= -LTTNG_ERR_INVALID
;
667 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
669 ret
= lttng_strncpy(lsm
.u
.reg
.path
, socket_path
,
670 sizeof(lsm
.u
.reg
.path
));
672 ret
= -LTTNG_ERR_INVALID
;
676 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
682 * Start tracing for all traces of the session.
684 * Returns size of returned session payload data or a negative error code.
686 int lttng_start_tracing(const char *session_name
)
689 struct lttcomm_session_msg lsm
;
691 if (session_name
== NULL
) {
692 ret
= -LTTNG_ERR_INVALID
;
696 memset(&lsm
, 0, sizeof(lsm
));
697 lsm
.cmd_type
= LTTNG_START_TRACE
;
699 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
700 sizeof(lsm
.session
.name
));
702 ret
= -LTTNG_ERR_INVALID
;
706 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
712 * Stop tracing for all traces of the session.
714 static int _lttng_stop_tracing(const char *session_name
, int wait
)
717 struct lttcomm_session_msg lsm
;
719 if (session_name
== NULL
) {
720 ret
= -LTTNG_ERR_INVALID
;
724 memset(&lsm
, 0, sizeof(lsm
));
725 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
727 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
728 sizeof(lsm
.session
.name
));
730 ret
= -LTTNG_ERR_INVALID
;
734 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
735 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
743 /* Check for data availability */
745 data_ret
= lttng_data_pending(session_name
);
747 /* Return the data available call error. */
753 * Data sleep time before retrying (in usec). Don't sleep if the
754 * call returned value indicates availability.
757 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
759 } while (data_ret
!= 0);
767 * Stop tracing and wait for data availability.
769 int lttng_stop_tracing(const char *session_name
)
771 return _lttng_stop_tracing(session_name
, 1);
775 * Stop tracing but _don't_ wait for data availability.
777 int lttng_stop_tracing_no_wait(const char *session_name
)
779 return _lttng_stop_tracing(session_name
, 0);
783 * Add context to a channel.
785 * If the given channel is NULL, add the contexts to all channels.
786 * The event_name param is ignored.
788 * Returns the size of the returned payload data or a negative error code.
790 int lttng_add_context(struct lttng_handle
*handle
,
791 struct lttng_event_context
*ctx
, const char *event_name
,
792 const char *channel_name
)
797 struct lttcomm_session_msg lsm
;
799 /* Safety check. Both are mandatory. */
800 if (handle
== NULL
|| ctx
== NULL
) {
801 ret
= -LTTNG_ERR_INVALID
;
805 memset(&lsm
, 0, sizeof(lsm
));
806 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
808 /* If no channel name, send empty string. */
809 ret
= lttng_strncpy(lsm
.u
.context
.channel_name
, channel_name
?: "",
810 sizeof(lsm
.u
.context
.channel_name
));
812 ret
= -LTTNG_ERR_INVALID
;
816 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
817 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
818 sizeof(lsm
.session
.name
));
820 ret
= -LTTNG_ERR_INVALID
;
824 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
825 size_t provider_len
, ctx_len
;
826 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
827 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
829 if (!provider_name
|| !ctx_name
) {
830 ret
= -LTTNG_ERR_INVALID
;
834 provider_len
= strlen(provider_name
);
835 if (provider_len
== 0) {
836 ret
= -LTTNG_ERR_INVALID
;
839 lsm
.u
.context
.provider_name_len
= provider_len
;
841 ctx_len
= strlen(ctx_name
);
843 ret
= -LTTNG_ERR_INVALID
;
846 lsm
.u
.context
.context_name_len
= ctx_len
;
848 len
= provider_len
+ ctx_len
;
851 ret
= -LTTNG_ERR_NOMEM
;
855 memcpy(buf
, provider_name
, provider_len
);
856 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
858 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
860 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
862 * Don't leak application addresses to the sessiond.
863 * This is only necessary when ctx is for an app ctx otherwise
864 * the values inside the union (type & config) are overwritten.
866 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
867 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
870 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
877 * Enable event(s) for a channel.
879 * If no event name is specified, all events are enabled.
880 * If no channel name is specified, the default 'channel0' is used.
882 * Returns size of returned session payload data or a negative error code.
884 int lttng_enable_event(struct lttng_handle
*handle
,
885 struct lttng_event
*ev
, const char *channel_name
)
887 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
892 * Create or enable an event with a filter expression.
894 * Return negative error value on error.
895 * Return size of returned session payload data if OK.
897 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
898 struct lttng_event
*event
, const char *channel_name
,
899 const char *filter_expression
)
901 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
902 filter_expression
, 0, NULL
);
906 * Depending on the event, return a newly allocated agent filter expression or
907 * NULL if not applicable.
909 * An event with NO loglevel and the name is * will return NULL.
911 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
914 char *agent_filter
= NULL
;
918 /* Don't add filter for the '*' event. */
919 if (strcmp(ev
->name
, "*") != 0) {
921 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
924 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
932 /* Add loglevel filtering if any for the JUL domain. */
933 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
936 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
942 if (filter
|| agent_filter
) {
945 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
946 agent_filter
? agent_filter
: filter
, op
,
951 agent_filter
= new_filter
;
953 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
969 * Generate the filter bytecode from a given filter expression string. Put the
970 * newly allocated parser context in ctxp and populate the lsm object with the
973 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
975 static int generate_filter(char *filter_expression
,
976 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
979 struct filter_parser_ctx
*ctx
= NULL
;
982 assert(filter_expression
);
987 * Casting const to non-const, as the underlying function will use it in
990 fmem
= lttng_fmemopen((void *) filter_expression
,
991 strlen(filter_expression
), "r");
993 fprintf(stderr
, "Error opening memory as stream\n");
994 ret
= -LTTNG_ERR_FILTER_NOMEM
;
997 ctx
= filter_parser_ctx_alloc(fmem
);
999 fprintf(stderr
, "Error allocating parser\n");
1000 ret
= -LTTNG_ERR_FILTER_NOMEM
;
1001 goto filter_alloc_error
;
1003 ret
= filter_parser_ctx_append_ast(ctx
);
1005 fprintf(stderr
, "Parse error\n");
1006 ret
= -LTTNG_ERR_FILTER_INVAL
;
1010 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
1013 fprintf(stderr
, "XML print error\n");
1014 ret
= -LTTNG_ERR_FILTER_INVAL
;
1019 dbg_printf("Generating IR... ");
1021 ret
= filter_visitor_ir_generate(ctx
);
1023 fprintf(stderr
, "Generate IR error\n");
1024 ret
= -LTTNG_ERR_FILTER_INVAL
;
1027 dbg_printf("done\n");
1029 dbg_printf("Validating IR... ");
1031 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
1033 ret
= -LTTNG_ERR_FILTER_INVAL
;
1037 /* Normalize globbing patterns in the expression. */
1038 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1040 ret
= -LTTNG_ERR_FILTER_INVAL
;
1044 /* Validate strings used as literals in the expression. */
1045 ret
= filter_visitor_ir_validate_string(ctx
);
1047 ret
= -LTTNG_ERR_FILTER_INVAL
;
1051 /* Validate globbing patterns in the expression. */
1052 ret
= filter_visitor_ir_validate_globbing(ctx
);
1054 ret
= -LTTNG_ERR_FILTER_INVAL
;
1058 dbg_printf("done\n");
1060 dbg_printf("Generating bytecode... ");
1062 ret
= filter_visitor_bytecode_generate(ctx
);
1064 fprintf(stderr
, "Generate bytecode error\n");
1065 ret
= -LTTNG_ERR_FILTER_INVAL
;
1068 dbg_printf("done\n");
1069 dbg_printf("Size of bytecode generated: %u bytes.\n",
1070 bytecode_get_len(&ctx
->bytecode
->b
));
1072 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1073 + bytecode_get_len(&ctx
->bytecode
->b
);
1074 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1076 /* No need to keep the memory stream. */
1077 if (fclose(fmem
) != 0) {
1085 filter_ir_free(ctx
);
1086 filter_parser_ctx_free(ctx
);
1088 if (fclose(fmem
) != 0) {
1096 * Enable event(s) for a channel, possibly with exclusions and a filter.
1097 * If no event name is specified, all events are enabled.
1098 * If no channel name is specified, the default name is used.
1099 * If filter expression is not NULL, the filter is set for the event.
1100 * If exclusion count is not zero, the exclusions are set for the event.
1101 * Returns size of returned session payload data or a negative error code.
1103 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1104 struct lttng_event
*ev
, const char *channel_name
,
1105 const char *original_filter_expression
,
1106 int exclusion_count
, char **exclusion_list
)
1108 struct lttcomm_session_msg lsm
;
1109 struct lttng_dynamic_buffer send_buffer
;
1110 int ret
= 0, i
, fd_to_send
= -1;
1111 bool send_fd
= false;
1112 unsigned int free_filter_expression
= 0;
1113 struct filter_parser_ctx
*ctx
= NULL
;
1116 * We have either a filter or some exclusions, so we need to set up
1117 * a variable-length memory block from where to send the data.
1119 lttng_dynamic_buffer_init(&send_buffer
);
1122 * Cast as non-const since we may replace the filter expression
1123 * by a dynamically allocated string. Otherwise, the original
1124 * string is not modified.
1126 char *filter_expression
= (char *) original_filter_expression
;
1128 if (handle
== NULL
|| ev
== NULL
) {
1129 ret
= -LTTNG_ERR_INVALID
;
1134 * Empty filter string will always be rejected by the parser
1135 * anyway, so treat this corner-case early to eliminate
1136 * lttng_fmemopen error for 0-byte allocation.
1138 if (filter_expression
&& filter_expression
[0] == '\0') {
1139 ret
= -LTTNG_ERR_INVALID
;
1143 memset(&lsm
, 0, sizeof(lsm
));
1145 /* If no channel name, send empty string. */
1146 ret
= lttng_strncpy(lsm
.u
.enable
.channel_name
, channel_name
?: "",
1147 sizeof(lsm
.u
.enable
.channel_name
));
1149 ret
= -LTTNG_ERR_INVALID
;
1153 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1154 if (ev
->name
[0] == '\0') {
1155 /* Enable all events. */
1156 ret
= lttng_strncpy(ev
->name
, "*", sizeof(ev
->name
));
1160 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1161 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1163 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1164 sizeof(lsm
.session
.name
));
1166 ret
= -LTTNG_ERR_INVALID
;
1170 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1171 lsm
.u
.enable
.bytecode_len
= 0;
1173 /* Parse filter expression. */
1174 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1175 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1176 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1177 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1178 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1179 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1182 /* Setup JUL filter if needed. */
1183 agent_filter
= set_agent_filter(filter_expression
, ev
);
1184 if (!agent_filter
) {
1185 if (!filter_expression
) {
1187 * No JUL and no filter, just skip
1194 * With an agent filter, the original filter has
1195 * been added to it thus replace the filter
1198 filter_expression
= agent_filter
;
1199 free_filter_expression
= 1;
1203 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1209 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1210 lsm
.u
.enable
.bytecode_len
1211 + lsm
.u
.enable
.expression_len
1212 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1214 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1218 /* Put exclusion names first in the data. */
1219 for (i
= 0; i
< exclusion_count
; i
++) {
1220 size_t exclusion_len
;
1222 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1223 LTTNG_SYMBOL_NAME_LEN
);
1224 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1225 /* Exclusion is not NULL-terminated. */
1226 ret
= -LTTNG_ERR_INVALID
;
1230 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1231 *(exclusion_list
+ i
),
1232 LTTNG_SYMBOL_NAME_LEN
);
1238 /* Add filter expression next. */
1239 if (filter_expression
) {
1240 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1241 filter_expression
, lsm
.u
.enable
.expression_len
);
1246 /* Add filter bytecode next. */
1247 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1248 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1249 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1254 if (ev
->extended
.ptr
) {
1255 struct lttng_event_extended
*ev_ext
=
1256 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1258 if (ev_ext
->probe_location
) {
1260 * lttng_userspace_probe_location_serialize returns the
1261 * number of bytes that was appended to the buffer.
1263 ret
= lttng_userspace_probe_location_serialize(
1264 ev_ext
->probe_location
, &send_buffer
,
1270 send_fd
= fd_to_send
>= 0;
1272 * Set the size of the userspace probe location element
1273 * of the buffer so that the receiving side knows where
1276 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1280 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1281 send_fd
? &fd_to_send
: NULL
,
1283 send_buffer
.size
? send_buffer
.data
: NULL
,
1284 send_buffer
.size
, NULL
, NULL
, 0);
1287 if (filter_expression
&& ctx
) {
1288 filter_bytecode_free(ctx
);
1289 filter_ir_free(ctx
);
1290 filter_parser_ctx_free(ctx
);
1293 if (free_filter_expression
) {
1295 * The filter expression has been replaced and must be freed as
1296 * it is not the original filter expression received as a
1299 free(filter_expression
);
1303 * Return directly to the caller and don't ask the sessiond since
1304 * something went wrong in the parsing of data above.
1306 lttng_dynamic_buffer_reset(&send_buffer
);
1310 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1314 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1315 struct lttng_event
*ev
, const char *channel_name
,
1316 const char *original_filter_expression
)
1318 struct lttcomm_session_msg lsm
;
1321 unsigned int free_filter_expression
= 0;
1322 struct filter_parser_ctx
*ctx
= NULL
;
1324 * Cast as non-const since we may replace the filter expression
1325 * by a dynamically allocated string. Otherwise, the original
1326 * string is not modified.
1328 char *filter_expression
= (char *) original_filter_expression
;
1330 if (handle
== NULL
|| ev
== NULL
) {
1331 ret
= -LTTNG_ERR_INVALID
;
1336 * Empty filter string will always be rejected by the parser
1337 * anyway, so treat this corner-case early to eliminate
1338 * lttng_fmemopen error for 0-byte allocation.
1340 if (filter_expression
&& filter_expression
[0] == '\0') {
1341 ret
= -LTTNG_ERR_INVALID
;
1345 memset(&lsm
, 0, sizeof(lsm
));
1347 /* If no channel name, send empty string. */
1348 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, channel_name
?: "",
1349 sizeof(lsm
.u
.disable
.channel_name
));
1351 ret
= -LTTNG_ERR_INVALID
;
1355 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1357 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1358 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1360 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1361 sizeof(lsm
.session
.name
));
1363 ret
= -LTTNG_ERR_INVALID
;
1367 lsm
.u
.disable
.bytecode_len
= 0;
1370 * For the JUL domain, a filter is enforced except for the
1371 * disable all event. This is done to avoid having the event in
1372 * all sessions thus filtering by logger name.
1374 if (filter_expression
== NULL
&&
1375 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1376 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1377 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1382 * We have a filter, so we need to set up a variable-length
1383 * memory block from where to send the data.
1386 /* Parse filter expression */
1387 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1388 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1389 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1390 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1391 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1392 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1395 /* Setup JUL filter if needed. */
1396 agent_filter
= set_agent_filter(filter_expression
, ev
);
1397 if (!agent_filter
) {
1398 if (!filter_expression
) {
1400 * No JUL and no filter, just skip
1407 * With a JUL filter, the original filter has
1408 * been added to it thus replace the filter
1411 filter_expression
= agent_filter
;
1412 free_filter_expression
= 1;
1416 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1422 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1423 + lsm
.u
.disable
.expression_len
);
1425 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1429 /* Add filter expression. */
1430 if (lsm
.u
.disable
.expression_len
!= 0) {
1433 lsm
.u
.disable
.expression_len
);
1435 /* Add filter bytecode next. */
1436 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1438 + lsm
.u
.disable
.expression_len
,
1440 lsm
.u
.disable
.bytecode_len
);
1443 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1444 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1448 if (filter_expression
&& ctx
) {
1449 filter_bytecode_free(ctx
);
1450 filter_ir_free(ctx
);
1451 filter_parser_ctx_free(ctx
);
1454 if (free_filter_expression
) {
1456 * The filter expression has been replaced and must be freed as
1457 * it is not the original filter expression received as a
1460 free(filter_expression
);
1464 * Return directly to the caller and don't ask the sessiond since
1465 * something went wrong in the parsing of data above.
1470 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1475 * Disable event(s) of a channel and domain.
1476 * If no event name is specified, all events are disabled.
1477 * If no channel name is specified, the default 'channel0' is used.
1478 * Returns size of returned session payload data or a negative error code.
1480 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1481 const char *channel_name
)
1484 struct lttng_event ev
;
1486 memset(&ev
, 0, sizeof(ev
));
1488 ev
.type
= LTTNG_EVENT_ALL
;
1489 ret
= lttng_strncpy(ev
.name
, name
?: "", sizeof(ev
.name
));
1491 ret
= -LTTNG_ERR_INVALID
;
1495 ret
= lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1500 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1502 struct lttng_channel
*channel
= NULL
;
1503 struct lttng_channel_extended
*extended
= NULL
;
1509 /* Validate domain. */
1510 switch (domain
->type
) {
1511 case LTTNG_DOMAIN_UST
:
1512 switch (domain
->buf_type
) {
1513 case LTTNG_BUFFER_PER_UID
:
1514 case LTTNG_BUFFER_PER_PID
:
1520 case LTTNG_DOMAIN_KERNEL
:
1521 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1529 channel
= zmalloc(sizeof(*channel
));
1534 extended
= zmalloc(sizeof(*extended
));
1539 channel
->attr
.extended
.ptr
= extended
;
1541 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1549 void lttng_channel_destroy(struct lttng_channel
*channel
)
1555 if (channel
->attr
.extended
.ptr
) {
1556 free(channel
->attr
.extended
.ptr
);
1562 * Enable channel per domain
1563 * Returns size of returned session payload data or a negative error code.
1565 int lttng_enable_channel(struct lttng_handle
*handle
,
1566 struct lttng_channel
*in_chan
)
1569 struct lttcomm_session_msg lsm
;
1570 size_t total_buffer_size_needed_per_cpu
= 0;
1572 /* NULL arguments are forbidden. No default values. */
1573 if (handle
== NULL
|| in_chan
== NULL
) {
1574 return -LTTNG_ERR_INVALID
;
1577 memset(&lsm
, 0, sizeof(lsm
));
1578 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1579 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1581 if (!in_chan
->attr
.extended
.ptr
) {
1582 struct lttng_channel
*channel
;
1583 struct lttng_channel_extended
*extended
;
1585 channel
= lttng_channel_create(&handle
->domain
);
1587 return -LTTNG_ERR_NOMEM
;
1591 * Create a new channel in order to use default extended
1594 extended
= (struct lttng_channel_extended
*)
1595 channel
->attr
.extended
.ptr
;
1596 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1597 lttng_channel_destroy(channel
);
1599 struct lttng_channel_extended
*extended
;
1601 extended
= (struct lttng_channel_extended
*)
1602 in_chan
->attr
.extended
.ptr
;
1603 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1607 * Verify that the amount of memory required to create the requested
1608 * buffer is available on the system at the moment.
1610 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1611 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1612 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1613 return -LTTNG_ERR_NOMEM
;
1616 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1617 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1619 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1620 sizeof(lsm
.session
.name
));
1622 ret
= -LTTNG_ERR_INVALID
;
1626 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1632 * All tracing will be stopped for registered events of the channel.
1633 * Returns size of returned session payload data or a negative error code.
1635 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1638 struct lttcomm_session_msg lsm
;
1640 /* Safety check. Both are mandatory. */
1641 if (handle
== NULL
|| name
== NULL
) {
1642 return -LTTNG_ERR_INVALID
;
1645 memset(&lsm
, 0, sizeof(lsm
));
1647 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1649 ret
= lttng_strncpy(lsm
.u
.disable
.channel_name
, name
,
1650 sizeof(lsm
.u
.disable
.channel_name
));
1652 ret
= -LTTNG_ERR_INVALID
;
1656 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1658 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1659 sizeof(lsm
.session
.name
));
1661 ret
= -LTTNG_ERR_INVALID
;
1665 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1671 * Add PID to session tracker.
1672 * Return 0 on success else a negative LTTng error code.
1674 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1677 struct lttcomm_session_msg lsm
;
1679 /* NULL arguments are forbidden. No default values. */
1680 if (handle
== NULL
) {
1681 return -LTTNG_ERR_INVALID
;
1684 memset(&lsm
, 0, sizeof(lsm
));
1686 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1687 lsm
.u
.pid_tracker
.pid
= pid
;
1689 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1691 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1692 sizeof(lsm
.session
.name
));
1694 ret
= -LTTNG_ERR_INVALID
;
1698 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1704 * Remove PID from session tracker.
1705 * Return 0 on success else a negative LTTng error code.
1707 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1710 struct lttcomm_session_msg lsm
;
1712 /* NULL arguments are forbidden. No default values. */
1713 if (handle
== NULL
) {
1714 return -LTTNG_ERR_INVALID
;
1717 memset(&lsm
, 0, sizeof(lsm
));
1719 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1720 lsm
.u
.pid_tracker
.pid
= pid
;
1722 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1724 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
1725 sizeof(lsm
.session
.name
));
1727 ret
= -LTTNG_ERR_INVALID
;
1731 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1737 * Lists all available tracepoints of domain.
1738 * Sets the contents of the events array.
1739 * Returns the number of lttng_event entries in events;
1740 * on error, returns a negative value.
1742 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1743 struct lttng_event
**events
)
1746 struct lttcomm_session_msg lsm
;
1748 if (handle
== NULL
) {
1749 return -LTTNG_ERR_INVALID
;
1752 memset(&lsm
, 0, sizeof(lsm
));
1753 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1754 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1756 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1761 return ret
/ sizeof(struct lttng_event
);
1765 * Lists all available tracepoint fields of domain.
1766 * Sets the contents of the event field array.
1767 * Returns the number of lttng_event_field entries in events;
1768 * on error, returns a negative value.
1770 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1771 struct lttng_event_field
**fields
)
1774 struct lttcomm_session_msg lsm
;
1776 if (handle
== NULL
) {
1777 return -LTTNG_ERR_INVALID
;
1780 memset(&lsm
, 0, sizeof(lsm
));
1781 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1782 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
1784 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1789 return ret
/ sizeof(struct lttng_event_field
);
1793 * Lists all available kernel system calls. Allocates and sets the contents of
1796 * Returns the number of lttng_event entries in events; on error, returns a
1799 int lttng_list_syscalls(struct lttng_event
**events
)
1802 struct lttcomm_session_msg lsm
;
1805 return -LTTNG_ERR_INVALID
;
1808 memset(&lsm
, 0, sizeof(lsm
));
1809 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1810 /* Force kernel domain for system calls. */
1811 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1813 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1818 return ret
/ sizeof(struct lttng_event
);
1822 * Returns a human readable string describing
1823 * the error code (a negative value).
1825 const char *lttng_strerror(int code
)
1827 return error_get_str(code
);
1830 enum lttng_error_code
lttng_create_session_ext(
1831 struct lttng_session_descriptor
*session_descriptor
)
1833 enum lttng_error_code ret_code
;
1834 struct lttcomm_session_msg lsm
= {
1835 .cmd_type
= LTTNG_CREATE_SESSION_EXT
,
1838 struct lttng_buffer_view reply_view
;
1840 bool sessiond_must_generate_ouput
;
1841 struct lttng_dynamic_buffer payload
;
1843 size_t descriptor_size
;
1844 struct lttng_session_descriptor
*descriptor_reply
= NULL
;
1846 lttng_dynamic_buffer_init(&payload
);
1847 if (!session_descriptor
) {
1848 ret_code
= LTTNG_ERR_INVALID
;
1852 sessiond_must_generate_ouput
=
1853 !lttng_session_descriptor_is_output_destination_initialized(
1854 session_descriptor
);
1855 if (sessiond_must_generate_ouput
) {
1856 const char *home_dir
= utils_get_home_dir();
1857 size_t home_dir_len
= home_dir
? strlen(home_dir
) + 1 : 0;
1859 if (!home_dir
|| home_dir_len
> LTTNG_PATH_MAX
) {
1860 ret_code
= LTTNG_ERR_FATAL
;
1864 lsm
.u
.create_session
.home_dir_size
= (uint16_t) home_dir_len
;
1865 ret
= lttng_dynamic_buffer_append(&payload
, home_dir
,
1868 ret_code
= LTTNG_ERR_NOMEM
;
1873 descriptor_size
= payload
.size
;
1874 ret
= lttng_session_descriptor_serialize(session_descriptor
,
1877 ret_code
= LTTNG_ERR_INVALID
;
1880 descriptor_size
= payload
.size
- descriptor_size
;
1881 lsm
.u
.create_session
.session_descriptor_size
= descriptor_size
;
1883 /* Command returns a session descriptor on success. */
1884 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, payload
.data
,
1885 payload
.size
, &reply
);
1886 if (reply_ret
< 0) {
1887 ret_code
= -reply_ret
;
1889 } else if (reply_ret
== 0) {
1890 /* Socket unexpectedly closed by the session daemon. */
1891 ret_code
= LTTNG_ERR_FATAL
;
1895 reply_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
1896 ret
= lttng_session_descriptor_create_from_buffer(&reply_view
,
1899 ret_code
= LTTNG_ERR_FATAL
;
1902 ret_code
= LTTNG_OK
;
1903 lttng_session_descriptor_assign(session_descriptor
, descriptor_reply
);
1906 lttng_dynamic_buffer_reset(&payload
);
1907 lttng_session_descriptor_destroy(descriptor_reply
);
1912 * Create a new session using name and url for destination.
1914 * Return 0 on success else a negative LTTng error code.
1916 int lttng_create_session(const char *name
, const char *url
)
1920 struct lttng_uri
*uris
= NULL
;
1921 struct lttng_session_descriptor
*descriptor
= NULL
;
1922 enum lttng_error_code ret_code
;
1925 ret
= -LTTNG_ERR_INVALID
;
1929 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1931 ret
= -LTTNG_ERR_INVALID
;
1936 descriptor
= lttng_session_descriptor_create(name
);
1939 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
1940 ret
= -LTTNG_ERR_INVALID
;
1943 descriptor
= lttng_session_descriptor_local_create(name
,
1947 descriptor
= lttng_session_descriptor_network_create(name
, url
,
1951 ret
= -LTTNG_ERR_INVALID
;
1955 ret
= -LTTNG_ERR_INVALID
;
1958 ret_code
= lttng_create_session_ext(descriptor
);
1959 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
1961 lttng_session_descriptor_destroy(descriptor
);
1967 * Create a session exclusively used for snapshot.
1969 * Return 0 on success else a negative LTTng error code.
1971 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1974 enum lttng_error_code ret_code
;
1976 struct lttng_uri
*uris
= NULL
;
1977 struct lttng_session_descriptor
*descriptor
= NULL
;
1980 ret
= -LTTNG_ERR_INVALID
;
1984 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1986 ret
= -LTTNG_ERR_INVALID
;
1990 * If the user does not specify a custom subdir, use the session name.
1992 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&&
1993 strlen(uris
[0].subdir
) == 0) {
1994 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s",
1997 PERROR("Failed to set session name as network destination sub-directory");
1998 ret
= -LTTNG_ERR_FATAL
;
2000 } else if (ret
>= sizeof(uris
[0].subdir
)) {
2001 /* Truncated output. */
2002 ret
= -LTTNG_ERR_INVALID
;
2009 descriptor
= lttng_session_descriptor_snapshot_create(name
);
2012 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
2013 ret
= -LTTNG_ERR_INVALID
;
2016 descriptor
= lttng_session_descriptor_snapshot_local_create(
2021 descriptor
= lttng_session_descriptor_snapshot_network_create(
2027 ret
= -LTTNG_ERR_INVALID
;
2031 ret
= -LTTNG_ERR_INVALID
;
2034 ret_code
= lttng_create_session_ext(descriptor
);
2035 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2037 lttng_session_descriptor_destroy(descriptor
);
2043 * Create a session exclusively used for live.
2045 * Return 0 on success else a negative LTTng error code.
2047 int lttng_create_session_live(const char *name
, const char *url
,
2048 unsigned int timer_interval
)
2051 enum lttng_error_code ret_code
;
2052 struct lttng_session_descriptor
*descriptor
= NULL
;
2055 ret
= -LTTNG_ERR_INVALID
;
2060 descriptor
= lttng_session_descriptor_live_network_create(
2061 name
, url
, NULL
, timer_interval
);
2063 descriptor
= lttng_session_descriptor_live_create(
2064 name
, timer_interval
);
2067 ret
= -LTTNG_ERR_INVALID
;
2070 ret_code
= lttng_create_session_ext(descriptor
);
2071 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2073 lttng_session_descriptor_destroy(descriptor
);
2078 * Stop the session and wait for the data before destroying it
2080 * Return 0 on success else a negative LTTng error code.
2082 int lttng_destroy_session(const char *session_name
)
2085 enum lttng_error_code ret_code
;
2086 enum lttng_destruction_handle_status status
;
2087 struct lttng_destruction_handle
*handle
= NULL
;
2090 * Stop the tracing and wait for the data to be
2093 ret
= _lttng_stop_tracing(session_name
, 1);
2094 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
2098 ret_code
= lttng_destroy_session_ext(session_name
, &handle
);
2099 if (ret_code
!= LTTNG_OK
) {
2100 ret
= (int) -ret_code
;
2105 /* Block until the completion of the destruction of the session. */
2106 status
= lttng_destruction_handle_wait_for_completion(handle
, -1);
2107 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
) {
2108 ret
= -LTTNG_ERR_UNK
;
2112 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
2113 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
2114 ret
= -LTTNG_ERR_UNK
;
2117 ret
= ret_code
== LTTNG_OK
? 0 : -ret_code
;
2119 lttng_destruction_handle_destroy(handle
);
2124 * Destroy the session without waiting for the data.
2126 int lttng_destroy_session_no_wait(const char *session_name
)
2128 enum lttng_error_code ret_code
;
2130 ret_code
= lttng_destroy_session_ext(session_name
, NULL
);
2131 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
2135 * Ask the session daemon for all available sessions.
2136 * Sets the contents of the sessions array.
2137 * Returns the number of lttng_session entries in sessions;
2138 * on error, returns a negative value.
2140 int lttng_list_sessions(struct lttng_session
**out_sessions
)
2143 struct lttcomm_session_msg lsm
;
2144 const size_t session_size
= sizeof(struct lttng_session
) +
2145 sizeof(struct lttng_session_extended
);
2146 size_t session_count
, i
;
2147 struct lttng_session_extended
*sessions_extended_begin
;
2148 struct lttng_session
*sessions
= NULL
;
2150 memset(&lsm
, 0, sizeof(lsm
));
2151 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
2152 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) &sessions
);
2157 ret
= -LTTNG_ERR_FATAL
;
2161 if (ret
% session_size
) {
2162 ret
= -LTTNG_ERR_UNK
;
2164 *out_sessions
= NULL
;
2167 session_count
= (size_t) ret
/ session_size
;
2168 sessions_extended_begin
= (struct lttng_session_extended
*)
2169 (&sessions
[session_count
]);
2171 /* Set extended session info pointers. */
2172 for (i
= 0; i
< session_count
; i
++) {
2173 struct lttng_session
*session
= &sessions
[i
];
2174 struct lttng_session_extended
*extended
=
2175 &(sessions_extended_begin
[i
]);
2177 session
->extended
.ptr
= extended
;
2180 ret
= (int) session_count
;
2181 *out_sessions
= sessions
;
2186 enum lttng_error_code
lttng_session_get_creation_time(
2187 const struct lttng_session
*session
, uint64_t *creation_time
)
2189 enum lttng_error_code ret
= LTTNG_OK
;
2190 struct lttng_session_extended
*extended
;
2192 if (!session
|| !creation_time
|| !session
->extended
.ptr
) {
2193 ret
= LTTNG_ERR_INVALID
;
2197 extended
= session
->extended
.ptr
;
2198 if (!extended
->creation_time
.is_set
) {
2199 /* Not created on the session daemon yet. */
2200 ret
= LTTNG_ERR_SESSION_NOT_EXIST
;
2203 *creation_time
= extended
->creation_time
.value
;
2208 int lttng_set_session_shm_path(const char *session_name
,
2209 const char *shm_path
)
2212 struct lttcomm_session_msg lsm
;
2214 if (session_name
== NULL
) {
2215 return -LTTNG_ERR_INVALID
;
2218 memset(&lsm
, 0, sizeof(lsm
));
2219 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
2221 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2222 sizeof(lsm
.session
.name
));
2224 ret
= -LTTNG_ERR_INVALID
;
2228 ret
= lttng_strncpy(lsm
.u
.set_shm_path
.shm_path
, shm_path
?: "",
2229 sizeof(lsm
.u
.set_shm_path
.shm_path
));
2231 ret
= -LTTNG_ERR_INVALID
;
2235 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2241 * Ask the session daemon for all available domains of a session.
2242 * Sets the contents of the domains array.
2243 * Returns the number of lttng_domain entries in domains;
2244 * on error, returns a negative value.
2246 int lttng_list_domains(const char *session_name
,
2247 struct lttng_domain
**domains
)
2250 struct lttcomm_session_msg lsm
;
2252 if (session_name
== NULL
) {
2253 ret
= -LTTNG_ERR_INVALID
;
2257 memset(&lsm
, 0, sizeof(lsm
));
2258 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
2260 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2261 sizeof(lsm
.session
.name
));
2263 ret
= -LTTNG_ERR_INVALID
;
2267 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
2272 return ret
/ sizeof(struct lttng_domain
);
2278 * Ask the session daemon for all available channels of a session.
2279 * Sets the contents of the channels array.
2280 * Returns the number of lttng_channel entries in channels;
2281 * on error, returns a negative value.
2283 int lttng_list_channels(struct lttng_handle
*handle
,
2284 struct lttng_channel
**channels
)
2287 size_t channel_count
, i
;
2288 const size_t channel_size
= sizeof(struct lttng_channel
) +
2289 sizeof(struct lttng_channel_extended
);
2290 struct lttcomm_session_msg lsm
;
2293 if (handle
== NULL
) {
2294 ret
= -LTTNG_ERR_INVALID
;
2298 memset(&lsm
, 0, sizeof(lsm
));
2299 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
2300 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2301 sizeof(lsm
.session
.name
));
2303 ret
= -LTTNG_ERR_INVALID
;
2307 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2309 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
2314 if (ret
% channel_size
) {
2315 ret
= -LTTNG_ERR_UNK
;
2320 channel_count
= (size_t) ret
/ channel_size
;
2322 /* Set extended info pointers */
2323 extended_at
= ((void *) *channels
) +
2324 channel_count
* sizeof(struct lttng_channel
);
2325 for (i
= 0; i
< channel_count
; i
++) {
2326 struct lttng_channel
*chan
= &(*channels
)[i
];
2328 chan
->attr
.extended
.ptr
= extended_at
;
2329 extended_at
+= sizeof(struct lttng_channel_extended
);
2332 ret
= (int) channel_count
;
2338 * Ask the session daemon for all available events of a session channel.
2339 * Sets the contents of the events array.
2340 * Returns the number of lttng_event entries in events;
2341 * on error, returns a negative value.
2343 int lttng_list_events(struct lttng_handle
*handle
,
2344 const char *channel_name
, struct lttng_event
**events
)
2347 struct lttcomm_session_msg lsm
;
2348 struct lttcomm_event_command_header
*cmd_header
= NULL
;
2349 size_t cmd_header_len
;
2350 uint32_t nb_events
, i
;
2352 char *reception_buffer
= NULL
;
2353 struct lttng_dynamic_buffer listing
;
2356 /* Safety check. An handle and channel name are mandatory */
2357 if (handle
== NULL
|| channel_name
== NULL
) {
2358 return -LTTNG_ERR_INVALID
;
2361 memset(&lsm
, 0, sizeof(lsm
));
2362 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2363 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2364 sizeof(lsm
.session
.name
));
2366 ret
= -LTTNG_ERR_INVALID
;
2370 ret
= lttng_strncpy(lsm
.u
.list
.channel_name
, channel_name
,
2371 sizeof(lsm
.u
.list
.channel_name
));
2373 ret
= -LTTNG_ERR_INVALID
;
2377 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2379 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2380 (void **) &reception_buffer
, (void **) &cmd_header
,
2387 ret
= -LTTNG_ERR_UNK
;
2391 /* Set number of events and free command header */
2392 nb_events
= cmd_header
->nb_events
;
2393 if (nb_events
> INT_MAX
) {
2401 * The buffer that is returned must contain a "flat" version of
2402 * the events that are returned. In other words, all pointers
2403 * within an lttng_event must point to a location within the returned
2404 * buffer so that the user may free everything by simply calling free()
2405 * on the returned buffer. This is needed in order to maintain API
2408 * A first pass is performed to compute the size of the buffer that
2409 * must be allocated. A second pass is then performed to setup
2410 * the returned events so that their members always point within the
2413 * The layout of the returned buffer is as follows:
2414 * - struct lttng_event[nb_events],
2415 * - nb_events times the following:
2416 * - struct lttng_event_extended,
2417 * - flattened version of userspace_probe_location
2418 * - filter_expression
2420 * - padding to align to 64-bits
2422 comm_ext_at
= reception_buffer
+
2423 (nb_events
* sizeof(struct lttng_event
));
2424 storage_req
= nb_events
* sizeof(struct lttng_event
);
2426 for (i
= 0; i
< nb_events
; i
++) {
2427 struct lttcomm_event_extended_header
*ext_comm
=
2428 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2429 int probe_storage_req
= 0;
2431 comm_ext_at
+= sizeof(*ext_comm
);
2432 comm_ext_at
+= ext_comm
->filter_len
;
2434 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2436 if (ext_comm
->userspace_probe_location_len
) {
2437 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2438 struct lttng_buffer_view probe_location_view
;
2440 probe_location_view
= lttng_buffer_view_init(
2442 ext_comm
->userspace_probe_location_len
);
2445 * Create a temporary userspace probe location to
2446 * determine the size needed by a "flattened" version
2447 * of that same probe location.
2449 ret
= lttng_userspace_probe_location_create_from_buffer(
2450 &probe_location_view
, &probe_location
);
2452 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2456 ret
= lttng_userspace_probe_location_flatten(
2457 probe_location
, NULL
);
2458 lttng_userspace_probe_location_destroy(probe_location
);
2460 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2464 probe_storage_req
= ret
;
2465 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2468 storage_req
+= sizeof(struct lttng_event_extended
);
2469 storage_req
+= ext_comm
->filter_len
;
2470 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2471 /* Padding to ensure the flat probe is aligned. */
2472 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2473 storage_req
+= probe_storage_req
;
2476 lttng_dynamic_buffer_init(&listing
);
2478 * We must ensure that "listing" is never resized so as to preserve
2479 * the validity of the flattened objects.
2481 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2483 ret
= -LTTNG_ERR_NOMEM
;
2487 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2488 nb_events
* sizeof(struct lttng_event
));
2490 ret
= -LTTNG_ERR_NOMEM
;
2491 goto free_dynamic_buffer
;
2494 comm_ext_at
= reception_buffer
+
2495 (nb_events
* sizeof(struct lttng_event
));
2496 for (i
= 0; i
< nb_events
; i
++) {
2497 struct lttng_event
*event
= (struct lttng_event
*)
2498 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2499 struct lttcomm_event_extended_header
*ext_comm
=
2500 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2501 struct lttng_event_extended
*event_extended
=
2502 (struct lttng_event_extended
*)
2503 (listing
.data
+ listing
.size
);
2505 /* Insert struct lttng_event_extended. */
2506 ret
= lttng_dynamic_buffer_set_size(&listing
,
2507 listing
.size
+ sizeof(*event_extended
));
2509 ret
= -LTTNG_ERR_NOMEM
;
2510 goto free_dynamic_buffer
;
2512 event
->extended
.ptr
= event_extended
;
2514 comm_ext_at
+= sizeof(*ext_comm
);
2516 /* Insert filter expression. */
2517 if (ext_comm
->filter_len
) {
2518 event_extended
->filter_expression
= listing
.data
+
2520 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2521 ext_comm
->filter_len
);
2523 ret
= -LTTNG_ERR_NOMEM
;
2524 goto free_dynamic_buffer
;
2526 comm_ext_at
+= ext_comm
->filter_len
;
2529 /* Insert exclusions. */
2530 if (ext_comm
->nb_exclusions
) {
2531 event_extended
->exclusions
.count
=
2532 ext_comm
->nb_exclusions
;
2533 event_extended
->exclusions
.strings
=
2534 listing
.data
+ listing
.size
;
2536 ret
= lttng_dynamic_buffer_append(&listing
,
2538 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2540 ret
= -LTTNG_ERR_NOMEM
;
2541 goto free_dynamic_buffer
;
2543 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2546 /* Insert padding to align to 64-bits. */
2547 ret
= lttng_dynamic_buffer_set_size(&listing
,
2548 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2550 ret
= -LTTNG_ERR_NOMEM
;
2551 goto free_dynamic_buffer
;
2554 /* Insert flattened userspace probe location. */
2555 if (ext_comm
->userspace_probe_location_len
) {
2556 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2557 struct lttng_buffer_view probe_location_view
;
2559 probe_location_view
= lttng_buffer_view_init(
2561 ext_comm
->userspace_probe_location_len
);
2563 ret
= lttng_userspace_probe_location_create_from_buffer(
2564 &probe_location_view
, &probe_location
);
2566 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2567 goto free_dynamic_buffer
;
2570 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2571 (listing
.data
+ listing
.size
);
2572 ret
= lttng_userspace_probe_location_flatten(
2573 probe_location
, &listing
);
2574 lttng_userspace_probe_location_destroy(probe_location
);
2576 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2577 goto free_dynamic_buffer
;
2580 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2584 /* Don't reset listing buffer as we return its content. */
2585 *events
= (struct lttng_event
*) listing
.data
;
2586 lttng_dynamic_buffer_init(&listing
);
2587 ret
= (int) nb_events
;
2588 free_dynamic_buffer
:
2589 lttng_dynamic_buffer_reset(&listing
);
2592 free(reception_buffer
);
2597 * Sets the tracing_group variable with name.
2598 * This function allocates memory pointed to by tracing_group.
2599 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2601 int lttng_set_tracing_group(const char *name
)
2604 return -LTTNG_ERR_INVALID
;
2607 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2608 return -LTTNG_ERR_FATAL
;
2614 int lttng_calibrate(struct lttng_handle
*handle
,
2615 struct lttng_calibrate
*calibrate
)
2618 * This command was removed in LTTng 2.9.
2620 return -LTTNG_ERR_UND
;
2624 * Set default channel attributes.
2625 * If either or both of the arguments are null, attr content is zeroe'd.
2627 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2628 struct lttng_channel_attr
*attr
)
2630 struct lttng_channel_extended
*extended
;
2633 if (attr
== NULL
|| domain
== NULL
) {
2637 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2638 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2640 /* Same for all domains. */
2641 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2642 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2643 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2645 switch (domain
->type
) {
2646 case LTTNG_DOMAIN_KERNEL
:
2647 attr
->switch_timer_interval
=
2648 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2649 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2650 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2651 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2652 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2654 extended
->monitor_timer_interval
=
2655 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2656 extended
->blocking_timeout
=
2657 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2660 case LTTNG_DOMAIN_UST
:
2661 switch (domain
->buf_type
) {
2662 case LTTNG_BUFFER_PER_UID
:
2663 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2664 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2665 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2666 attr
->switch_timer_interval
=
2667 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2668 attr
->read_timer_interval
=
2669 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2671 extended
->monitor_timer_interval
=
2672 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2673 extended
->blocking_timeout
=
2674 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2677 case LTTNG_BUFFER_PER_PID
:
2679 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2680 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2681 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2682 attr
->switch_timer_interval
=
2683 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2684 attr
->read_timer_interval
=
2685 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2687 extended
->monitor_timer_interval
=
2688 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2689 extended
->blocking_timeout
=
2690 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2695 /* Default behavior: leave set to 0. */
2699 attr
->extended
.ptr
= extended
;
2702 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2703 uint64_t *discarded_events
)
2706 struct lttng_channel_extended
*chan_ext
;
2708 if (!channel
|| !discarded_events
) {
2709 ret
= -LTTNG_ERR_INVALID
;
2713 chan_ext
= channel
->attr
.extended
.ptr
;
2716 * This can happen since the lttng_channel structure is
2717 * used for other tasks where this pointer is never set.
2719 *discarded_events
= 0;
2723 *discarded_events
= chan_ext
->discarded_events
;
2728 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2729 uint64_t *lost_packets
)
2732 struct lttng_channel_extended
*chan_ext
;
2734 if (!channel
|| !lost_packets
) {
2735 ret
= -LTTNG_ERR_INVALID
;
2739 chan_ext
= channel
->attr
.extended
.ptr
;
2742 * This can happen since the lttng_channel structure is
2743 * used for other tasks where this pointer is never set.
2749 *lost_packets
= chan_ext
->lost_packets
;
2754 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2755 uint64_t *monitor_timer_interval
)
2759 if (!chan
|| !monitor_timer_interval
) {
2760 ret
= -LTTNG_ERR_INVALID
;
2764 if (!chan
->attr
.extended
.ptr
) {
2765 ret
= -LTTNG_ERR_INVALID
;
2769 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2770 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2775 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2776 uint64_t monitor_timer_interval
)
2780 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2781 ret
= -LTTNG_ERR_INVALID
;
2785 ((struct lttng_channel_extended
*)
2786 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2787 monitor_timer_interval
;
2792 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2793 int64_t *blocking_timeout
)
2797 if (!chan
|| !blocking_timeout
) {
2798 ret
= -LTTNG_ERR_INVALID
;
2802 if (!chan
->attr
.extended
.ptr
) {
2803 ret
= -LTTNG_ERR_INVALID
;
2807 *blocking_timeout
= ((struct lttng_channel_extended
*)
2808 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2813 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2814 int64_t blocking_timeout
)
2817 int64_t msec_timeout
;
2819 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2820 ret
= -LTTNG_ERR_INVALID
;
2824 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2825 ret
= -LTTNG_ERR_INVALID
;
2830 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2831 * us to accept a narrower range of values (msecs expressed as a signed
2834 msec_timeout
= blocking_timeout
/ 1000;
2835 if (msec_timeout
!= (int32_t) msec_timeout
) {
2836 ret
= -LTTNG_ERR_INVALID
;
2840 ((struct lttng_channel_extended
*)
2841 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2848 * Check if session daemon is alive.
2850 * Return 1 if alive or 0 if not.
2851 * On error returns a negative value.
2853 int lttng_session_daemon_alive(void)
2857 ret
= set_session_daemon_path();
2863 if (*sessiond_sock_path
== '\0') {
2865 * No socket path set. Weird error which means the constructor
2871 ret
= try_connect_sessiond(sessiond_sock_path
);
2882 * Set URL for a consumer for a session and domain.
2884 * Return 0 on success, else a negative value.
2886 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2887 const char *control_url
, const char *data_url
)
2891 struct lttcomm_session_msg lsm
;
2892 struct lttng_uri
*uris
= NULL
;
2894 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2895 ret
= -LTTNG_ERR_INVALID
;
2899 memset(&lsm
, 0, sizeof(lsm
));
2901 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2903 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
2904 sizeof(lsm
.session
.name
));
2906 ret
= -LTTNG_ERR_INVALID
;
2910 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
2912 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2914 ret
= -LTTNG_ERR_INVALID
;
2918 lsm
.u
.uri
.size
= size
;
2920 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2921 sizeof(struct lttng_uri
) * size
, NULL
);
2931 int lttng_enable_consumer(struct lttng_handle
*handle
)
2939 int lttng_disable_consumer(struct lttng_handle
*handle
)
2947 int _lttng_create_session_ext(const char *name
, const char *url
,
2948 const char *datetime
)
2954 * For a given session name, this call checks if the data is ready to be read
2955 * or is still being extracted by the consumer(s) hence not ready to be used by
2958 int lttng_data_pending(const char *session_name
)
2961 struct lttcomm_session_msg lsm
;
2962 uint8_t *pending
= NULL
;
2964 if (session_name
== NULL
) {
2965 return -LTTNG_ERR_INVALID
;
2968 memset(&lsm
, 0, sizeof(lsm
));
2969 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2971 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
2972 sizeof(lsm
.session
.name
));
2974 ret
= -LTTNG_ERR_INVALID
;
2978 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2981 } else if (ret
!= 1) {
2982 /* Unexpected payload size */
2983 ret
= -LTTNG_ERR_INVALID
;
2985 } else if (!pending
) {
2986 /* Internal error. */
2987 ret
= -LTTNG_ERR_UNK
;
2991 ret
= (int) *pending
;
2998 * List PIDs in the tracker.
3000 * enabled is set to whether the PID tracker is enabled.
3001 * pids is set to an allocated array of PIDs currently tracked. On
3002 * success, pids must be freed by the caller.
3003 * nr_pids is set to the number of entries contained by the pids array.
3005 * Returns 0 on success, else a negative LTTng error code.
3007 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
3008 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
3012 struct lttcomm_session_msg lsm
;
3014 int32_t *pids
= NULL
;
3016 if (handle
== NULL
) {
3017 return -LTTNG_ERR_INVALID
;
3020 memset(&lsm
, 0, sizeof(lsm
));
3021 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
3022 ret
= lttng_strncpy(lsm
.session
.name
, handle
->session_name
,
3023 sizeof(lsm
.session
.name
));
3025 return -LTTNG_ERR_INVALID
;
3027 COPY_DOMAIN_PACKED(lsm
.domain
, handle
->domain
);
3029 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
3033 nr_pids
= ret
/ sizeof(int32_t);
3034 if (nr_pids
> 0 && !pids
) {
3035 return -LTTNG_ERR_UNK
;
3037 if (nr_pids
== 1 && pids
[0] == -1) {
3043 *_enabled
= enabled
;
3045 *_nr_pids
= nr_pids
;
3050 * Regenerate the metadata for a session.
3051 * Return 0 on success, a negative error code on error.
3053 int lttng_regenerate_metadata(const char *session_name
)
3056 struct lttcomm_session_msg lsm
;
3058 if (!session_name
) {
3059 ret
= -LTTNG_ERR_INVALID
;
3063 memset(&lsm
, 0, sizeof(lsm
));
3064 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
3066 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3067 sizeof(lsm
.session
.name
));
3069 ret
= -LTTNG_ERR_INVALID
;
3073 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3084 * Deprecated, replaced by lttng_regenerate_metadata.
3086 int lttng_metadata_regenerate(const char *session_name
)
3088 return lttng_regenerate_metadata(session_name
);
3092 * Regenerate the statedump of a session.
3093 * Return 0 on success, a negative error code on error.
3095 int lttng_regenerate_statedump(const char *session_name
)
3098 struct lttcomm_session_msg lsm
;
3100 if (!session_name
) {
3101 ret
= -LTTNG_ERR_INVALID
;
3105 memset(&lsm
, 0, sizeof(lsm
));
3106 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
3108 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
3109 sizeof(lsm
.session
.name
));
3111 ret
= -LTTNG_ERR_INVALID
;
3115 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
3125 int lttng_register_trigger(struct lttng_trigger
*trigger
)
3128 struct lttcomm_session_msg lsm
;
3129 struct lttng_dynamic_buffer buffer
;
3131 lttng_dynamic_buffer_init(&buffer
);
3133 ret
= -LTTNG_ERR_INVALID
;
3137 if (!lttng_trigger_validate(trigger
)) {
3138 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3142 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3144 ret
= -LTTNG_ERR_UNK
;
3148 memset(&lsm
, 0, sizeof(lsm
));
3149 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
3150 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3151 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3154 lttng_dynamic_buffer_reset(&buffer
);
3158 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
3161 struct lttcomm_session_msg lsm
;
3162 struct lttng_dynamic_buffer buffer
;
3164 lttng_dynamic_buffer_init(&buffer
);
3166 ret
= -LTTNG_ERR_INVALID
;
3170 if (!lttng_trigger_validate(trigger
)) {
3171 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
3175 ret
= lttng_trigger_serialize(trigger
, &buffer
);
3177 ret
= -LTTNG_ERR_UNK
;
3181 memset(&lsm
, 0, sizeof(lsm
));
3182 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
3183 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
3184 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
3187 lttng_dynamic_buffer_reset(&buffer
);
3194 static void __attribute__((constructor
)) init(void)
3196 /* Set default session group */
3197 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
3203 static void __attribute__((destructor
)) lttng_ctl_exit(void)
3205 free(tracing_group
);