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>
47 #include "filter/filter-ast.h"
48 #include "filter/filter-parser.h"
49 #include "filter/filter-bytecode.h"
50 #include "filter/memstream.h"
51 #include "lttng-ctl-helper.h"
54 static const int print_xml
= 1;
55 #define dbg_printf(fmt, args...) \
56 printf("[debug liblttng-ctl] " fmt, ## args)
58 static const int print_xml
= 0;
59 #define dbg_printf(fmt, args...) \
61 /* do nothing but check printf format */ \
63 printf("[debug liblttnctl] " fmt, ## args); \
68 /* Socket to session daemon for communication */
69 static int sessiond_socket
;
70 static char sessiond_sock_path
[PATH_MAX
];
73 static char *tracing_group
;
79 * Those two variables are used by error.h to silent or control the verbosity of
80 * error message. They are global to the library so application linking with it
81 * are able to compile correctly and also control verbosity of the library.
84 int lttng_opt_verbose
;
88 * Copy string from src to dst and enforce null terminated byte.
91 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
94 strncpy(dst
, src
, len
);
95 /* Enforce the NULL terminated byte */
103 * Copy domain to lttcomm_session_msg domain.
105 * If domain is unknown, default domain will be the kernel.
108 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
109 struct lttng_domain
*src
)
113 case LTTNG_DOMAIN_KERNEL
:
114 case LTTNG_DOMAIN_UST
:
115 case LTTNG_DOMAIN_JUL
:
116 case LTTNG_DOMAIN_LOG4J
:
117 case LTTNG_DOMAIN_PYTHON
:
118 memcpy(dst
, src
, sizeof(struct lttng_domain
));
121 memset(dst
, 0, sizeof(struct lttng_domain
));
128 * Send lttcomm_session_msg to the session daemon.
130 * On success, returns the number of bytes sent (>=0)
131 * On error, returns -1
133 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
138 ret
= -LTTNG_ERR_NO_SESSIOND
;
142 DBG("LSM cmd type : %d", lsm
->cmd_type
);
144 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
145 sizeof(struct lttcomm_session_msg
));
147 ret
= -LTTNG_ERR_FATAL
;
155 * Send var len data to the session daemon.
157 * On success, returns the number of bytes sent (>=0)
158 * On error, returns -1
160 static int send_session_varlen(const void *data
, size_t len
)
165 ret
= -LTTNG_ERR_NO_SESSIOND
;
174 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
176 ret
= -LTTNG_ERR_FATAL
;
184 * Send file descriptors to the session daemon.
186 * On success, returns the number of bytes sent (>=0)
187 * On error, returns -1
189 static int send_session_fds(const int *fds
, size_t nb_fd
)
194 ret
= -LTTNG_ERR_NO_SESSIOND
;
198 if (!fds
|| !nb_fd
) {
203 ret
= lttcomm_send_fds_unix_sock(sessiond_socket
, fds
, nb_fd
);
205 ret
= -LTTNG_ERR_FATAL
;
213 * Receive data from the sessiond socket.
215 * On success, returns the number of bytes received (>=0)
216 * On error, returns -1 (recvmsg() error) or -ENOTCONN
218 static int recv_data_sessiond(void *buf
, size_t len
)
223 ret
= -LTTNG_ERR_NO_SESSIOND
;
227 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
229 ret
= -LTTNG_ERR_FATAL
;
237 * Check if we are in the specified group.
239 * If yes return 1, else return -1.
242 int lttng_check_tracing_group(void)
244 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
246 int grp_list_size
, grp_id
, i
;
248 const char *grp_name
= tracing_group
;
250 /* Get GID of group 'tracing' */
251 grp_tracing
= getgrnam(grp_name
);
253 /* If grp_tracing is NULL, the group does not exist. */
257 /* Get number of supplementary group IDs */
258 grp_list_size
= getgroups(0, NULL
);
259 if (grp_list_size
< 0) {
264 /* Alloc group list of the right size */
265 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
270 grp_id
= getgroups(grp_list_size
, grp_list
);
276 for (i
= 0; i
< grp_list_size
; i
++) {
277 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
290 static int check_enough_available_memory(size_t num_bytes_requested_per_cpu
)
294 size_t best_mem_info
;
295 size_t num_bytes_requested_total
;
298 * Get the number of CPU currently online to compute the amount of
299 * memory needed to create a buffer for every CPU.
301 num_cpu
= sysconf(_SC_NPROCESSORS_ONLN
);
306 num_bytes_requested_total
= num_bytes_requested_per_cpu
* num_cpu
;
309 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
310 * reliable estimate we can get but it is only exposed by the kernel
311 * since 3.14. (See Linux kernel commit:
312 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
314 ret
= utils_get_memory_available(&best_mem_info
);
320 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
321 * is a sanity check for obvious user error.
323 ret
= utils_get_memory_total(&best_mem_info
);
331 return best_mem_info
>= num_bytes_requested_total
;
335 * Try connect to session daemon with sock_path.
337 * Return 0 on success, else -1
339 static int try_connect_sessiond(const char *sock_path
)
343 /* If socket exist, we check if the daemon listens for connect. */
344 ret
= access(sock_path
, F_OK
);
350 ret
= lttcomm_connect_unix_sock(sock_path
);
356 ret
= lttcomm_close_unix_sock(ret
);
358 PERROR("lttcomm_close_unix_sock");
368 * Set sessiond socket path by putting it in the global sessiond_sock_path
371 * Returns 0 on success, negative value on failure (the sessiond socket path
372 * is somehow too long or ENOMEM).
374 static int set_session_daemon_path(void)
376 int in_tgroup
= 0; /* In tracing group. */
382 /* Are we in the tracing group ? */
383 in_tgroup
= lttng_check_tracing_group();
386 if ((uid
== 0) || in_tgroup
) {
387 lttng_ctl_copy_string(sessiond_sock_path
,
388 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
396 ret
= try_connect_sessiond(sessiond_sock_path
);
400 /* Global session daemon not available... */
402 /* ...or not in tracing group (and not root), default */
405 * With GNU C < 2.1, snprintf returns -1 if the target buffer
407 * With GNU C >= 2.1, snprintf returns the required size
408 * (excluding closing null)
410 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
411 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
412 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
424 * Connect to the LTTng session daemon.
426 * On success, return 0. On error, return -1.
428 static int connect_sessiond(void)
432 /* Don't try to connect if already connected. */
437 ret
= set_session_daemon_path();
442 /* Connect to the sesssion daemon. */
443 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
448 sessiond_socket
= ret
;
458 * Clean disconnect from the session daemon.
460 * On success, return 0. On error, return -1.
462 static int disconnect_sessiond(void)
467 ret
= lttcomm_close_unix_sock(sessiond_socket
);
475 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
483 ret
= -LTTNG_ERR_INVALID
;
493 ret
= recv_data_sessiond(buf
, len
);
499 ret
= -LTTNG_ERR_INVALID
;
503 /* Move ownership of command header buffer to user. */
508 /* No command header. */
524 * Ask the session daemon a specific command and put the data into buf.
525 * Takes extra var. len. data and file descriptors as input to send to the
528 * Return size of data (only payload, not header) or a negative error code.
531 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg
*lsm
,
532 const int *fds
, size_t nb_fd
, const void *vardata
,
533 size_t vardata_len
, void **user_payload_buf
,
534 void **user_cmd_header_buf
, size_t *user_cmd_header_len
)
538 struct lttcomm_lttng_msg llm
;
540 ret
= connect_sessiond();
542 ret
= -LTTNG_ERR_NO_SESSIOND
;
546 /* Send command to session daemon */
547 ret
= send_session_msg(lsm
);
549 /* Ret value is a valid lttng error code. */
552 /* Send var len data */
553 ret
= send_session_varlen(vardata
, vardata_len
);
555 /* Ret value is a valid lttng error code. */
560 ret
= send_session_fds(fds
, nb_fd
);
562 /* Ret value is a valid lttng error code. */
566 /* Get header from data transmission */
567 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
569 /* Ret value is a valid lttng error code. */
573 /* Check error code if OK */
574 if (llm
.ret_code
!= LTTNG_OK
) {
579 /* Get command header from data transmission */
580 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
581 user_cmd_header_buf
, user_cmd_header_len
);
586 /* Get payload from data transmission */
587 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
596 disconnect_sessiond();
601 * Create lttng handle and return pointer.
603 * The returned pointer will be NULL in case of malloc() error.
605 struct lttng_handle
*lttng_create_handle(const char *session_name
,
606 struct lttng_domain
*domain
)
608 struct lttng_handle
*handle
= NULL
;
610 handle
= zmalloc(sizeof(struct lttng_handle
));
611 if (handle
== NULL
) {
612 PERROR("malloc handle");
616 /* Copy session name */
617 lttng_ctl_copy_string(handle
->session_name
, session_name
,
618 sizeof(handle
->session_name
));
620 /* Copy lttng domain or leave initialized to 0. */
622 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
630 * Destroy handle by free(3) the pointer.
632 void lttng_destroy_handle(struct lttng_handle
*handle
)
638 * Register an outside consumer.
640 * Returns size of returned session payload data or a negative error code.
642 int lttng_register_consumer(struct lttng_handle
*handle
,
643 const char *socket_path
)
645 struct lttcomm_session_msg lsm
;
647 if (handle
== NULL
|| socket_path
== NULL
) {
648 return -LTTNG_ERR_INVALID
;
651 memset(&lsm
, 0, sizeof(lsm
));
652 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
653 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
654 sizeof(lsm
.session
.name
));
655 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
657 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
658 sizeof(lsm
.u
.reg
.path
));
660 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
664 * Start tracing for all traces of the session.
666 * Returns size of returned session payload data or a negative error code.
668 int lttng_start_tracing(const char *session_name
)
670 struct lttcomm_session_msg lsm
;
672 if (session_name
== NULL
) {
673 return -LTTNG_ERR_INVALID
;
676 memset(&lsm
, 0, sizeof(lsm
));
677 lsm
.cmd_type
= LTTNG_START_TRACE
;
679 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
680 sizeof(lsm
.session
.name
));
682 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
686 * Stop tracing for all traces of the session.
688 static int _lttng_stop_tracing(const char *session_name
, int wait
)
691 struct lttcomm_session_msg lsm
;
693 if (session_name
== NULL
) {
694 return -LTTNG_ERR_INVALID
;
697 memset(&lsm
, 0, sizeof(lsm
));
698 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
700 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
701 sizeof(lsm
.session
.name
));
703 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
704 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
712 /* Check for data availability */
714 data_ret
= lttng_data_pending(session_name
);
716 /* Return the data available call error. */
722 * Data sleep time before retrying (in usec). Don't sleep if the
723 * call returned value indicates availability.
726 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
728 } while (data_ret
!= 0);
736 * Stop tracing and wait for data availability.
738 int lttng_stop_tracing(const char *session_name
)
740 return _lttng_stop_tracing(session_name
, 1);
744 * Stop tracing but _don't_ wait for data availability.
746 int lttng_stop_tracing_no_wait(const char *session_name
)
748 return _lttng_stop_tracing(session_name
, 0);
752 * Add context to a channel.
754 * If the given channel is NULL, add the contexts to all channels.
755 * The event_name param is ignored.
757 * Returns the size of the returned payload data or a negative error code.
759 int lttng_add_context(struct lttng_handle
*handle
,
760 struct lttng_event_context
*ctx
, const char *event_name
,
761 const char *channel_name
)
766 struct lttcomm_session_msg lsm
;
768 /* Safety check. Both are mandatory. */
769 if (handle
== NULL
|| ctx
== NULL
) {
770 ret
= -LTTNG_ERR_INVALID
;
774 memset(&lsm
, 0, sizeof(lsm
));
775 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
777 /* If no channel name, send empty string. */
778 if (channel_name
== NULL
) {
779 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
780 sizeof(lsm
.u
.context
.channel_name
));
782 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
783 sizeof(lsm
.u
.context
.channel_name
));
786 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
787 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
788 sizeof(lsm
.session
.name
));
790 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
791 size_t provider_len
, ctx_len
;
792 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
793 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
795 if (!provider_name
|| !ctx_name
) {
796 ret
= -LTTNG_ERR_INVALID
;
800 provider_len
= strlen(provider_name
);
801 if (provider_len
== 0) {
802 ret
= -LTTNG_ERR_INVALID
;
805 lsm
.u
.context
.provider_name_len
= provider_len
;
807 ctx_len
= strlen(ctx_name
);
809 ret
= -LTTNG_ERR_INVALID
;
812 lsm
.u
.context
.context_name_len
= ctx_len
;
814 len
= provider_len
+ ctx_len
;
817 ret
= -LTTNG_ERR_NOMEM
;
821 memcpy(buf
, provider_name
, provider_len
);
822 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
824 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
826 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
828 * Don't leak application addresses to the sessiond.
829 * This is only necessary when ctx is for an app ctx otherwise
830 * the values inside the union (type & config) are overwritten.
832 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
833 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
836 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
843 * Enable event(s) for a channel.
845 * If no event name is specified, all events are enabled.
846 * If no channel name is specified, the default 'channel0' is used.
848 * Returns size of returned session payload data or a negative error code.
850 int lttng_enable_event(struct lttng_handle
*handle
,
851 struct lttng_event
*ev
, const char *channel_name
)
853 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
858 * Create or enable an event with a filter expression.
860 * Return negative error value on error.
861 * Return size of returned session payload data if OK.
863 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
864 struct lttng_event
*event
, const char *channel_name
,
865 const char *filter_expression
)
867 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
868 filter_expression
, 0, NULL
);
872 * Depending on the event, return a newly allocated agent filter expression or
873 * NULL if not applicable.
875 * An event with NO loglevel and the name is * will return NULL.
877 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
880 char *agent_filter
= NULL
;
884 /* Don't add filter for the '*' event. */
885 if (strcmp(ev
->name
, "*") != 0) {
887 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
890 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
898 /* Add loglevel filtering if any for the JUL domain. */
899 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
902 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
908 if (filter
|| agent_filter
) {
911 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
912 agent_filter
? agent_filter
: filter
, op
,
917 agent_filter
= new_filter
;
919 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
935 * Generate the filter bytecode from a given filter expression string. Put the
936 * newly allocated parser context in ctxp and populate the lsm object with the
939 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
941 static int generate_filter(char *filter_expression
,
942 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
945 struct filter_parser_ctx
*ctx
= NULL
;
948 assert(filter_expression
);
953 * Casting const to non-const, as the underlying function will use it in
956 fmem
= lttng_fmemopen((void *) filter_expression
,
957 strlen(filter_expression
), "r");
959 fprintf(stderr
, "Error opening memory as stream\n");
960 ret
= -LTTNG_ERR_FILTER_NOMEM
;
963 ctx
= filter_parser_ctx_alloc(fmem
);
965 fprintf(stderr
, "Error allocating parser\n");
966 ret
= -LTTNG_ERR_FILTER_NOMEM
;
967 goto filter_alloc_error
;
969 ret
= filter_parser_ctx_append_ast(ctx
);
971 fprintf(stderr
, "Parse error\n");
972 ret
= -LTTNG_ERR_FILTER_INVAL
;
976 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
979 fprintf(stderr
, "XML print error\n");
980 ret
= -LTTNG_ERR_FILTER_INVAL
;
985 dbg_printf("Generating IR... ");
987 ret
= filter_visitor_ir_generate(ctx
);
989 fprintf(stderr
, "Generate IR error\n");
990 ret
= -LTTNG_ERR_FILTER_INVAL
;
993 dbg_printf("done\n");
995 dbg_printf("Validating IR... ");
997 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
999 ret
= -LTTNG_ERR_FILTER_INVAL
;
1003 /* Normalize globbing patterns in the expression. */
1004 ret
= filter_visitor_ir_normalize_glob_patterns(ctx
);
1006 ret
= -LTTNG_ERR_FILTER_INVAL
;
1010 /* Validate strings used as literals in the expression. */
1011 ret
= filter_visitor_ir_validate_string(ctx
);
1013 ret
= -LTTNG_ERR_FILTER_INVAL
;
1017 /* Validate globbing patterns in the expression. */
1018 ret
= filter_visitor_ir_validate_globbing(ctx
);
1020 ret
= -LTTNG_ERR_FILTER_INVAL
;
1024 dbg_printf("done\n");
1026 dbg_printf("Generating bytecode... ");
1028 ret
= filter_visitor_bytecode_generate(ctx
);
1030 fprintf(stderr
, "Generate bytecode error\n");
1031 ret
= -LTTNG_ERR_FILTER_INVAL
;
1034 dbg_printf("done\n");
1035 dbg_printf("Size of bytecode generated: %u bytes.\n",
1036 bytecode_get_len(&ctx
->bytecode
->b
));
1038 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
1039 + bytecode_get_len(&ctx
->bytecode
->b
);
1040 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
1042 /* No need to keep the memory stream. */
1043 if (fclose(fmem
) != 0) {
1051 filter_ir_free(ctx
);
1052 filter_parser_ctx_free(ctx
);
1054 if (fclose(fmem
) != 0) {
1062 * Enable event(s) for a channel, possibly with exclusions and a filter.
1063 * If no event name is specified, all events are enabled.
1064 * If no channel name is specified, the default name is used.
1065 * If filter expression is not NULL, the filter is set for the event.
1066 * If exclusion count is not zero, the exclusions are set for the event.
1067 * Returns size of returned session payload data or a negative error code.
1069 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
1070 struct lttng_event
*ev
, const char *channel_name
,
1071 const char *original_filter_expression
,
1072 int exclusion_count
, char **exclusion_list
)
1074 struct lttcomm_session_msg lsm
;
1075 struct lttng_dynamic_buffer send_buffer
;
1076 int ret
= 0, i
, fd_to_send
= -1;
1077 bool send_fd
= false;
1078 unsigned int free_filter_expression
= 0;
1079 struct filter_parser_ctx
*ctx
= NULL
;
1081 memset(&send_buffer
, 0, sizeof(send_buffer
));
1084 * Cast as non-const since we may replace the filter expression
1085 * by a dynamically allocated string. Otherwise, the original
1086 * string is not modified.
1088 char *filter_expression
= (char *) original_filter_expression
;
1090 if (handle
== NULL
|| ev
== NULL
) {
1091 ret
= -LTTNG_ERR_INVALID
;
1096 * Empty filter string will always be rejected by the parser
1097 * anyway, so treat this corner-case early to eliminate
1098 * lttng_fmemopen error for 0-byte allocation.
1100 if (filter_expression
&& filter_expression
[0] == '\0') {
1101 ret
= -LTTNG_ERR_INVALID
;
1105 memset(&lsm
, 0, sizeof(lsm
));
1107 /* If no channel name, send empty string. */
1108 if (channel_name
== NULL
) {
1109 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1110 sizeof(lsm
.u
.enable
.channel_name
));
1112 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1113 sizeof(lsm
.u
.enable
.channel_name
));
1116 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1117 if (ev
->name
[0] == '\0') {
1118 /* Enable all events */
1119 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1122 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1123 /* FIXME: copying non-packed struct to packed struct. */
1124 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1126 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1127 sizeof(lsm
.session
.name
));
1128 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1129 lsm
.u
.enable
.bytecode_len
= 0;
1132 * We have either a filter or some exclusions, so we need to set up
1133 * a variable-length memory block from where to send the data.
1135 lttng_dynamic_buffer_init(&send_buffer
);
1137 /* Parse filter expression. */
1138 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1139 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1140 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1141 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1142 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1143 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1146 /* Setup JUL filter if needed. */
1147 agent_filter
= set_agent_filter(filter_expression
, ev
);
1148 if (!agent_filter
) {
1149 if (!filter_expression
) {
1151 * No JUL and no filter, just skip
1158 * With an agent filter, the original filter has
1159 * been added to it thus replace the filter
1162 filter_expression
= agent_filter
;
1163 free_filter_expression
= 1;
1167 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1173 ret
= lttng_dynamic_buffer_set_capacity(&send_buffer
,
1174 lsm
.u
.enable
.bytecode_len
1175 + lsm
.u
.enable
.expression_len
1176 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1178 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1182 /* Put exclusion names first in the data. */
1183 for (i
= 0; i
< exclusion_count
; i
++) {
1184 size_t exclusion_len
;
1186 exclusion_len
= lttng_strnlen(*(exclusion_list
+ i
),
1187 LTTNG_SYMBOL_NAME_LEN
);
1188 if (exclusion_len
== LTTNG_SYMBOL_NAME_LEN
) {
1189 /* Exclusion is not NULL-terminated. */
1190 ret
= -LTTNG_ERR_INVALID
;
1194 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1195 *(exclusion_list
+ i
),
1196 LTTNG_SYMBOL_NAME_LEN
);
1202 /* Add filter expression next. */
1203 if (filter_expression
) {
1204 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1205 filter_expression
, lsm
.u
.enable
.expression_len
);
1210 /* Add filter bytecode next. */
1211 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1212 ret
= lttng_dynamic_buffer_append(&send_buffer
,
1213 &ctx
->bytecode
->b
, lsm
.u
.enable
.bytecode_len
);
1218 if (ev
->extended
.ptr
) {
1219 struct lttng_event_extended
*ev_ext
=
1220 (struct lttng_event_extended
*) ev
->extended
.ptr
;
1222 if (ev_ext
->probe_location
) {
1224 * lttng_userspace_probe_location_serialize returns the
1225 * number of bytes that was appended to the buffer.
1227 ret
= lttng_userspace_probe_location_serialize(
1228 ev_ext
->probe_location
, &send_buffer
,
1234 send_fd
= fd_to_send
>= 0;
1236 * Set the size of the userspace probe location element
1237 * of the buffer so that the receiving side knows where
1240 lsm
.u
.enable
.userspace_probe_location_len
= ret
;
1244 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
,
1245 send_fd
? &fd_to_send
: NULL
,
1247 send_buffer
.size
? send_buffer
.data
: NULL
,
1248 send_buffer
.size
, NULL
, NULL
, 0);
1251 if (filter_expression
&& ctx
) {
1252 filter_bytecode_free(ctx
);
1253 filter_ir_free(ctx
);
1254 filter_parser_ctx_free(ctx
);
1257 if (free_filter_expression
) {
1259 * The filter expression has been replaced and must be freed as
1260 * it is not the original filter expression received as a
1263 free(filter_expression
);
1267 * Return directly to the caller and don't ask the sessiond since
1268 * something went wrong in the parsing of data above.
1270 lttng_dynamic_buffer_reset(&send_buffer
);
1274 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1278 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1279 struct lttng_event
*ev
, const char *channel_name
,
1280 const char *original_filter_expression
)
1282 struct lttcomm_session_msg lsm
;
1285 unsigned int free_filter_expression
= 0;
1286 struct filter_parser_ctx
*ctx
= NULL
;
1288 * Cast as non-const since we may replace the filter expression
1289 * by a dynamically allocated string. Otherwise, the original
1290 * string is not modified.
1292 char *filter_expression
= (char *) original_filter_expression
;
1294 if (handle
== NULL
|| ev
== NULL
) {
1295 ret
= -LTTNG_ERR_INVALID
;
1300 * Empty filter string will always be rejected by the parser
1301 * anyway, so treat this corner-case early to eliminate
1302 * lttng_fmemopen error for 0-byte allocation.
1304 if (filter_expression
&& filter_expression
[0] == '\0') {
1305 ret
= -LTTNG_ERR_INVALID
;
1309 memset(&lsm
, 0, sizeof(lsm
));
1311 /* If no channel name, send empty string. */
1312 if (channel_name
== NULL
) {
1313 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1314 sizeof(lsm
.u
.disable
.channel_name
));
1316 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1317 sizeof(lsm
.u
.disable
.channel_name
));
1320 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1322 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1323 /* FIXME: copying non-packed struct to packed struct. */
1324 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1326 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1327 sizeof(lsm
.session
.name
));
1328 lsm
.u
.disable
.bytecode_len
= 0;
1331 * For the JUL domain, a filter is enforced except for the
1332 * disable all event. This is done to avoid having the event in
1333 * all sessions thus filtering by logger name.
1335 if (filter_expression
== NULL
&&
1336 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1337 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1338 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1343 * We have a filter, so we need to set up a variable-length
1344 * memory block from where to send the data.
1347 /* Parse filter expression */
1348 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1349 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1350 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1351 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1352 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1353 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1356 /* Setup JUL filter if needed. */
1357 agent_filter
= set_agent_filter(filter_expression
, ev
);
1358 if (!agent_filter
) {
1359 if (!filter_expression
) {
1361 * No JUL and no filter, just skip
1368 * With a JUL filter, the original filter has
1369 * been added to it thus replace the filter
1372 filter_expression
= agent_filter
;
1373 free_filter_expression
= 1;
1377 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1383 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1384 + lsm
.u
.disable
.expression_len
);
1386 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1390 /* Add filter expression. */
1391 if (lsm
.u
.disable
.expression_len
!= 0) {
1394 lsm
.u
.disable
.expression_len
);
1396 /* Add filter bytecode next. */
1397 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1399 + lsm
.u
.disable
.expression_len
,
1401 lsm
.u
.disable
.bytecode_len
);
1404 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1405 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1409 if (filter_expression
&& ctx
) {
1410 filter_bytecode_free(ctx
);
1411 filter_ir_free(ctx
);
1412 filter_parser_ctx_free(ctx
);
1415 if (free_filter_expression
) {
1417 * The filter expression has been replaced and must be freed as
1418 * it is not the original filter expression received as a
1421 free(filter_expression
);
1425 * Return directly to the caller and don't ask the sessiond since
1426 * something went wrong in the parsing of data above.
1431 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1436 * Disable event(s) of a channel and domain.
1437 * If no event name is specified, all events are disabled.
1438 * If no channel name is specified, the default 'channel0' is used.
1439 * Returns size of returned session payload data or a negative error code.
1441 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1442 const char *channel_name
)
1444 struct lttng_event ev
;
1446 memset(&ev
, 0, sizeof(ev
));
1448 ev
.type
= LTTNG_EVENT_ALL
;
1449 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1450 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1453 struct lttng_channel
*lttng_channel_create(struct lttng_domain
*domain
)
1455 struct lttng_channel
*channel
= NULL
;
1456 struct lttng_channel_extended
*extended
= NULL
;
1462 /* Validate domain. */
1463 switch (domain
->type
) {
1464 case LTTNG_DOMAIN_UST
:
1465 switch (domain
->buf_type
) {
1466 case LTTNG_BUFFER_PER_UID
:
1467 case LTTNG_BUFFER_PER_PID
:
1473 case LTTNG_DOMAIN_KERNEL
:
1474 if (domain
->buf_type
!= LTTNG_BUFFER_GLOBAL
) {
1482 channel
= zmalloc(sizeof(*channel
));
1487 extended
= zmalloc(sizeof(*extended
));
1492 channel
->attr
.extended
.ptr
= extended
;
1494 lttng_channel_set_default_attr(domain
, &channel
->attr
);
1502 void lttng_channel_destroy(struct lttng_channel
*channel
)
1508 if (channel
->attr
.extended
.ptr
) {
1509 free(channel
->attr
.extended
.ptr
);
1515 * Enable channel per domain
1516 * Returns size of returned session payload data or a negative error code.
1518 int lttng_enable_channel(struct lttng_handle
*handle
,
1519 struct lttng_channel
*in_chan
)
1521 struct lttcomm_session_msg lsm
;
1522 size_t total_buffer_size_needed_per_cpu
= 0;
1524 /* NULL arguments are forbidden. No default values. */
1525 if (handle
== NULL
|| in_chan
== NULL
) {
1526 return -LTTNG_ERR_INVALID
;
1529 memset(&lsm
, 0, sizeof(lsm
));
1530 memcpy(&lsm
.u
.channel
.chan
, in_chan
, sizeof(lsm
.u
.channel
.chan
));
1531 lsm
.u
.channel
.chan
.attr
.extended
.ptr
= NULL
;
1533 if (!in_chan
->attr
.extended
.ptr
) {
1534 struct lttng_channel
*channel
;
1535 struct lttng_channel_extended
*extended
;
1537 channel
= lttng_channel_create(&handle
->domain
);
1539 return -LTTNG_ERR_NOMEM
;
1543 * Create a new channel in order to use default extended
1546 extended
= (struct lttng_channel_extended
*)
1547 channel
->attr
.extended
.ptr
;
1548 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1549 lttng_channel_destroy(channel
);
1551 struct lttng_channel_extended
*extended
;
1553 extended
= (struct lttng_channel_extended
*)
1554 in_chan
->attr
.extended
.ptr
;
1555 memcpy(&lsm
.u
.channel
.extended
, extended
, sizeof(*extended
));
1559 * Verify that the amount of memory required to create the requested
1560 * buffer is available on the system at the moment.
1562 total_buffer_size_needed_per_cpu
= lsm
.u
.channel
.chan
.attr
.num_subbuf
*
1563 lsm
.u
.channel
.chan
.attr
.subbuf_size
;
1564 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu
)) {
1565 return -LTTNG_ERR_NOMEM
;
1568 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1569 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1571 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1572 sizeof(lsm
.session
.name
));
1574 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1578 * All tracing will be stopped for registered events of the channel.
1579 * Returns size of returned session payload data or a negative error code.
1581 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1583 struct lttcomm_session_msg lsm
;
1585 /* Safety check. Both are mandatory. */
1586 if (handle
== NULL
|| name
== NULL
) {
1587 return -LTTNG_ERR_INVALID
;
1590 memset(&lsm
, 0, sizeof(lsm
));
1592 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1594 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1595 sizeof(lsm
.u
.disable
.channel_name
));
1597 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1599 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1600 sizeof(lsm
.session
.name
));
1602 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1606 * Add PID to session tracker.
1607 * Return 0 on success else a negative LTTng error code.
1609 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1611 struct lttcomm_session_msg lsm
;
1613 /* NULL arguments are forbidden. No default values. */
1614 if (handle
== NULL
) {
1615 return -LTTNG_ERR_INVALID
;
1618 memset(&lsm
, 0, sizeof(lsm
));
1620 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1621 lsm
.u
.pid_tracker
.pid
= pid
;
1623 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1625 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1626 sizeof(lsm
.session
.name
));
1628 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1632 * Remove PID from session tracker.
1633 * Return 0 on success else a negative LTTng error code.
1635 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1637 struct lttcomm_session_msg lsm
;
1639 /* NULL arguments are forbidden. No default values. */
1640 if (handle
== NULL
) {
1641 return -LTTNG_ERR_INVALID
;
1644 memset(&lsm
, 0, sizeof(lsm
));
1646 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1647 lsm
.u
.pid_tracker
.pid
= pid
;
1649 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1651 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1652 sizeof(lsm
.session
.name
));
1654 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1658 * Lists all available tracepoints of domain.
1659 * Sets the contents of the events array.
1660 * Returns the number of lttng_event entries in events;
1661 * on error, returns a negative value.
1663 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1664 struct lttng_event
**events
)
1667 struct lttcomm_session_msg lsm
;
1669 if (handle
== NULL
) {
1670 return -LTTNG_ERR_INVALID
;
1673 memset(&lsm
, 0, sizeof(lsm
));
1674 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1675 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1677 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1682 return ret
/ sizeof(struct lttng_event
);
1686 * Lists all available tracepoint fields of domain.
1687 * Sets the contents of the event field array.
1688 * Returns the number of lttng_event_field entries in events;
1689 * on error, returns a negative value.
1691 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1692 struct lttng_event_field
**fields
)
1695 struct lttcomm_session_msg lsm
;
1697 if (handle
== NULL
) {
1698 return -LTTNG_ERR_INVALID
;
1701 memset(&lsm
, 0, sizeof(lsm
));
1702 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1703 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1705 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1710 return ret
/ sizeof(struct lttng_event_field
);
1714 * Lists all available kernel system calls. Allocates and sets the contents of
1717 * Returns the number of lttng_event entries in events; on error, returns a
1720 int lttng_list_syscalls(struct lttng_event
**events
)
1723 struct lttcomm_session_msg lsm
;
1726 return -LTTNG_ERR_INVALID
;
1729 memset(&lsm
, 0, sizeof(lsm
));
1730 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1731 /* Force kernel domain for system calls. */
1732 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1734 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1739 return ret
/ sizeof(struct lttng_event
);
1743 * Returns a human readable string describing
1744 * the error code (a negative value).
1746 const char *lttng_strerror(int code
)
1748 return error_get_str(code
);
1752 * Create a brand new session using name and url for destination.
1754 * Returns LTTNG_OK on success or a negative error code.
1756 int lttng_create_session(const char *name
, const char *url
)
1760 struct lttcomm_session_msg lsm
;
1761 struct lttng_uri
*uris
= NULL
;
1764 return -LTTNG_ERR_INVALID
;
1767 memset(&lsm
, 0, sizeof(lsm
));
1769 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1770 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1772 /* There should never be a data URL */
1773 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1775 return -LTTNG_ERR_INVALID
;
1778 lsm
.u
.uri
.size
= size
;
1780 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1781 sizeof(struct lttng_uri
) * size
, NULL
);
1788 * Destroy session using name.
1789 * Returns size of returned session payload data or a negative error code.
1792 int _lttng_destroy_session(const char *session_name
)
1794 struct lttcomm_session_msg lsm
;
1796 if (session_name
== NULL
) {
1797 return -LTTNG_ERR_INVALID
;
1800 memset(&lsm
, 0, sizeof(lsm
));
1801 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1803 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1804 sizeof(lsm
.session
.name
));
1806 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1810 * Stop the session and wait for the data before destroying it
1812 int lttng_destroy_session(const char *session_name
)
1817 * Stop the tracing and wait for the data.
1819 ret
= _lttng_stop_tracing(session_name
, 1);
1820 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1824 ret
= _lttng_destroy_session(session_name
);
1830 * Destroy the session without waiting for the data.
1832 int lttng_destroy_session_no_wait(const char *session_name
)
1837 * Stop the tracing without waiting for the data.
1838 * The session might already have been stopped, so just
1841 ret
= _lttng_stop_tracing(session_name
, 0);
1842 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1846 ret
= _lttng_destroy_session(session_name
);
1852 * Ask the session daemon for all available sessions.
1853 * Sets the contents of the sessions array.
1854 * Returns the number of lttng_session entries in sessions;
1855 * on error, returns a negative value.
1857 int lttng_list_sessions(struct lttng_session
**sessions
)
1860 struct lttcomm_session_msg lsm
;
1862 memset(&lsm
, 0, sizeof(lsm
));
1863 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1864 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1869 return ret
/ sizeof(struct lttng_session
);
1872 int lttng_set_session_shm_path(const char *session_name
,
1873 const char *shm_path
)
1875 struct lttcomm_session_msg lsm
;
1877 if (session_name
== NULL
) {
1878 return -LTTNG_ERR_INVALID
;
1881 memset(&lsm
, 0, sizeof(lsm
));
1882 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1884 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1885 sizeof(lsm
.session
.name
));
1886 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1887 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1889 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1893 * Ask the session daemon for all available domains of a session.
1894 * Sets the contents of the domains array.
1895 * Returns the number of lttng_domain entries in domains;
1896 * on error, returns a negative value.
1898 int lttng_list_domains(const char *session_name
,
1899 struct lttng_domain
**domains
)
1902 struct lttcomm_session_msg lsm
;
1904 if (session_name
== NULL
) {
1905 return -LTTNG_ERR_INVALID
;
1908 memset(&lsm
, 0, sizeof(lsm
));
1909 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1911 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1912 sizeof(lsm
.session
.name
));
1914 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1919 return ret
/ sizeof(struct lttng_domain
);
1923 * Ask the session daemon for all available channels of a session.
1924 * Sets the contents of the channels array.
1925 * Returns the number of lttng_channel entries in channels;
1926 * on error, returns a negative value.
1928 int lttng_list_channels(struct lttng_handle
*handle
,
1929 struct lttng_channel
**channels
)
1932 size_t channel_count
, i
;
1933 const size_t channel_size
= sizeof(struct lttng_channel
) +
1934 sizeof(struct lttng_channel_extended
);
1935 struct lttcomm_session_msg lsm
;
1938 if (handle
== NULL
) {
1939 ret
= -LTTNG_ERR_INVALID
;
1943 memset(&lsm
, 0, sizeof(lsm
));
1944 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1945 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1946 sizeof(lsm
.session
.name
));
1948 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1950 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1955 if (ret
% channel_size
) {
1956 ret
= -LTTNG_ERR_UNK
;
1961 channel_count
= (size_t) ret
/ channel_size
;
1963 /* Set extended info pointers */
1964 extended_at
= ((void *) *channels
) +
1965 channel_count
* sizeof(struct lttng_channel
);
1966 for (i
= 0; i
< channel_count
; i
++) {
1967 struct lttng_channel
*chan
= &(*channels
)[i
];
1969 chan
->attr
.extended
.ptr
= extended_at
;
1970 extended_at
+= sizeof(struct lttng_channel_extended
);
1973 ret
= (int) channel_count
;
1979 * Ask the session daemon for all available events of a session channel.
1980 * Sets the contents of the events array.
1981 * Returns the number of lttng_event entries in events;
1982 * on error, returns a negative value.
1984 int lttng_list_events(struct lttng_handle
*handle
,
1985 const char *channel_name
, struct lttng_event
**events
)
1988 struct lttcomm_session_msg lsm
;
1989 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1990 size_t cmd_header_len
;
1991 uint32_t nb_events
, i
;
1993 char *reception_buffer
= NULL
;
1994 struct lttng_dynamic_buffer listing
;
1997 /* Safety check. An handle and channel name are mandatory */
1998 if (handle
== NULL
|| channel_name
== NULL
) {
1999 return -LTTNG_ERR_INVALID
;
2002 memset(&lsm
, 0, sizeof(lsm
));
2003 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
2004 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2005 sizeof(lsm
.session
.name
));
2006 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
2007 sizeof(lsm
.u
.list
.channel_name
));
2008 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2010 ret
= lttng_ctl_ask_sessiond_fds_varlen(&lsm
, NULL
, 0, NULL
, 0,
2011 (void **) &reception_buffer
, (void **) &cmd_header
,
2018 ret
= -LTTNG_ERR_UNK
;
2022 /* Set number of events and free command header */
2023 nb_events
= cmd_header
->nb_events
;
2024 if (nb_events
> INT_MAX
) {
2032 * The buffer that is returned must contain a "flat" version of
2033 * the events that are returned. In other words, all pointers
2034 * within an lttng_event must point to a location within the returned
2035 * buffer so that the user may free everything by simply calling free()
2036 * on the returned buffer. This is needed in order to maintain API
2039 * A first pass is performed to compute the size of the buffer that
2040 * must be allocated. A second pass is then performed to setup
2041 * the returned events so that their members always point within the
2044 * The layout of the returned buffer is as follows:
2045 * - struct lttng_event[nb_events],
2046 * - nb_events times the following:
2047 * - struct lttng_event_extended,
2048 * - flattened version of userspace_probe_location
2049 * - filter_expression
2051 * - padding to align to 64-bits
2053 comm_ext_at
= reception_buffer
+
2054 (nb_events
* sizeof(struct lttng_event
));
2055 storage_req
= nb_events
* sizeof(struct lttng_event
);
2057 for (i
= 0; i
< nb_events
; i
++) {
2058 struct lttcomm_event_extended_header
*ext_comm
=
2059 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2060 int probe_storage_req
= 0;
2062 comm_ext_at
+= sizeof(*ext_comm
);
2063 comm_ext_at
+= ext_comm
->filter_len
;
2065 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2067 if (ext_comm
->userspace_probe_location_len
) {
2068 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2069 struct lttng_buffer_view probe_location_view
;
2071 probe_location_view
= lttng_buffer_view_init(
2073 ext_comm
->userspace_probe_location_len
);
2076 * Create a temporary userspace probe location to
2077 * determine the size needed by a "flattened" version
2078 * of that same probe location.
2080 ret
= lttng_userspace_probe_location_create_from_buffer(
2081 &probe_location_view
, &probe_location
);
2083 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2087 ret
= lttng_userspace_probe_location_flatten(
2088 probe_location
, NULL
);
2089 lttng_userspace_probe_location_destroy(probe_location
);
2091 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2095 probe_storage_req
= ret
;
2096 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2099 storage_req
+= sizeof(struct lttng_event_extended
);
2100 storage_req
+= ext_comm
->filter_len
;
2101 storage_req
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2102 /* Padding to ensure the flat probe is aligned. */
2103 storage_req
= ALIGN_TO(storage_req
, sizeof(uint64_t));
2104 storage_req
+= probe_storage_req
;
2107 lttng_dynamic_buffer_init(&listing
);
2109 * We must ensure that "listing" is never resized so as to preserve
2110 * the validity of the flattened objects.
2112 ret
= lttng_dynamic_buffer_set_capacity(&listing
, storage_req
);
2114 ret
= -LTTNG_ERR_NOMEM
;
2118 ret
= lttng_dynamic_buffer_append(&listing
, reception_buffer
,
2119 nb_events
* sizeof(struct lttng_event
));
2121 ret
= -LTTNG_ERR_NOMEM
;
2122 goto free_dynamic_buffer
;
2125 comm_ext_at
= reception_buffer
+
2126 (nb_events
* sizeof(struct lttng_event
));
2127 for (i
= 0; i
< nb_events
; i
++) {
2128 struct lttng_event
*event
= (struct lttng_event
*)
2129 (listing
.data
+ (sizeof(struct lttng_event
) * i
));
2130 struct lttcomm_event_extended_header
*ext_comm
=
2131 (struct lttcomm_event_extended_header
*) comm_ext_at
;
2132 struct lttng_event_extended
*event_extended
=
2133 (struct lttng_event_extended
*)
2134 (listing
.data
+ listing
.size
);
2136 /* Insert struct lttng_event_extended. */
2137 ret
= lttng_dynamic_buffer_set_size(&listing
,
2138 listing
.size
+ sizeof(*event_extended
));
2140 ret
= -LTTNG_ERR_NOMEM
;
2141 goto free_dynamic_buffer
;
2143 event
->extended
.ptr
= event_extended
;
2145 comm_ext_at
+= sizeof(*ext_comm
);
2147 /* Insert filter expression. */
2148 if (ext_comm
->filter_len
) {
2149 event_extended
->filter_expression
= listing
.data
+
2151 ret
= lttng_dynamic_buffer_append(&listing
, comm_ext_at
,
2152 ext_comm
->filter_len
);
2154 ret
= -LTTNG_ERR_NOMEM
;
2155 goto free_dynamic_buffer
;
2157 comm_ext_at
+= ext_comm
->filter_len
;
2160 /* Insert exclusions. */
2161 if (ext_comm
->nb_exclusions
) {
2162 event_extended
->exclusions
.count
=
2163 ext_comm
->nb_exclusions
;
2164 event_extended
->exclusions
.strings
=
2165 listing
.data
+ listing
.size
;
2167 ret
= lttng_dynamic_buffer_append(&listing
,
2169 ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
);
2171 ret
= -LTTNG_ERR_NOMEM
;
2172 goto free_dynamic_buffer
;
2174 comm_ext_at
+= ext_comm
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
2177 /* Insert padding to align to 64-bits. */
2178 ret
= lttng_dynamic_buffer_set_size(&listing
,
2179 ALIGN_TO(listing
.size
, sizeof(uint64_t)));
2181 ret
= -LTTNG_ERR_NOMEM
;
2182 goto free_dynamic_buffer
;
2185 /* Insert flattened userspace probe location. */
2186 if (ext_comm
->userspace_probe_location_len
) {
2187 struct lttng_userspace_probe_location
*probe_location
= NULL
;
2188 struct lttng_buffer_view probe_location_view
;
2190 probe_location_view
= lttng_buffer_view_init(
2192 ext_comm
->userspace_probe_location_len
);
2194 ret
= lttng_userspace_probe_location_create_from_buffer(
2195 &probe_location_view
, &probe_location
);
2197 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2198 goto free_dynamic_buffer
;
2201 event_extended
->probe_location
= (struct lttng_userspace_probe_location
*)
2202 (listing
.data
+ listing
.size
);
2203 ret
= lttng_userspace_probe_location_flatten(
2204 probe_location
, &listing
);
2205 lttng_userspace_probe_location_destroy(probe_location
);
2207 ret
= -LTTNG_ERR_PROBE_LOCATION_INVAL
;
2208 goto free_dynamic_buffer
;
2211 comm_ext_at
+= ext_comm
->userspace_probe_location_len
;
2215 /* Don't reset listing buffer as we return its content. */
2216 *events
= (struct lttng_event
*) listing
.data
;
2217 lttng_dynamic_buffer_init(&listing
);
2218 ret
= (int) nb_events
;
2219 free_dynamic_buffer
:
2220 lttng_dynamic_buffer_reset(&listing
);
2223 free(reception_buffer
);
2228 * Sets the tracing_group variable with name.
2229 * This function allocates memory pointed to by tracing_group.
2230 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2232 int lttng_set_tracing_group(const char *name
)
2235 return -LTTNG_ERR_INVALID
;
2238 if (asprintf(&tracing_group
, "%s", name
) < 0) {
2239 return -LTTNG_ERR_FATAL
;
2245 int lttng_calibrate(struct lttng_handle
*handle
,
2246 struct lttng_calibrate
*calibrate
)
2249 * This command was removed in LTTng 2.9.
2251 return -LTTNG_ERR_UND
;
2255 * Set default channel attributes.
2256 * If either or both of the arguments are null, attr content is zeroe'd.
2258 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
2259 struct lttng_channel_attr
*attr
)
2261 struct lttng_channel_extended
*extended
;
2264 if (attr
== NULL
|| domain
== NULL
) {
2268 extended
= (struct lttng_channel_extended
*) attr
->extended
.ptr
;
2269 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
2271 /* Same for all domains. */
2272 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2273 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
2274 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2276 switch (domain
->type
) {
2277 case LTTNG_DOMAIN_KERNEL
:
2278 attr
->switch_timer_interval
=
2279 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2280 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2281 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2282 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2283 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2285 extended
->monitor_timer_interval
=
2286 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER
;
2287 extended
->blocking_timeout
=
2288 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT
;
2291 case LTTNG_DOMAIN_UST
:
2292 switch (domain
->buf_type
) {
2293 case LTTNG_BUFFER_PER_UID
:
2294 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2295 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2296 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2297 attr
->switch_timer_interval
=
2298 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2299 attr
->read_timer_interval
=
2300 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2302 extended
->monitor_timer_interval
=
2303 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER
;
2304 extended
->blocking_timeout
=
2305 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT
;
2308 case LTTNG_BUFFER_PER_PID
:
2310 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2311 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2312 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2313 attr
->switch_timer_interval
=
2314 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2315 attr
->read_timer_interval
=
2316 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2318 extended
->monitor_timer_interval
=
2319 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER
;
2320 extended
->blocking_timeout
=
2321 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT
;
2326 /* Default behavior: leave set to 0. */
2330 attr
->extended
.ptr
= extended
;
2333 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2334 uint64_t *discarded_events
)
2337 struct lttng_channel_extended
*chan_ext
;
2339 if (!channel
|| !discarded_events
) {
2340 ret
= -LTTNG_ERR_INVALID
;
2344 chan_ext
= channel
->attr
.extended
.ptr
;
2347 * This can happen since the lttng_channel structure is
2348 * used for other tasks where this pointer is never set.
2350 *discarded_events
= 0;
2354 *discarded_events
= chan_ext
->discarded_events
;
2359 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2360 uint64_t *lost_packets
)
2363 struct lttng_channel_extended
*chan_ext
;
2365 if (!channel
|| !lost_packets
) {
2366 ret
= -LTTNG_ERR_INVALID
;
2370 chan_ext
= channel
->attr
.extended
.ptr
;
2373 * This can happen since the lttng_channel structure is
2374 * used for other tasks where this pointer is never set.
2380 *lost_packets
= chan_ext
->lost_packets
;
2385 int lttng_channel_get_monitor_timer_interval(struct lttng_channel
*chan
,
2386 uint64_t *monitor_timer_interval
)
2390 if (!chan
|| !monitor_timer_interval
) {
2391 ret
= -LTTNG_ERR_INVALID
;
2395 if (!chan
->attr
.extended
.ptr
) {
2396 ret
= -LTTNG_ERR_INVALID
;
2400 *monitor_timer_interval
= ((struct lttng_channel_extended
*)
2401 chan
->attr
.extended
.ptr
)->monitor_timer_interval
;
2406 int lttng_channel_set_monitor_timer_interval(struct lttng_channel
*chan
,
2407 uint64_t monitor_timer_interval
)
2411 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2412 ret
= -LTTNG_ERR_INVALID
;
2416 ((struct lttng_channel_extended
*)
2417 chan
->attr
.extended
.ptr
)->monitor_timer_interval
=
2418 monitor_timer_interval
;
2423 int lttng_channel_get_blocking_timeout(struct lttng_channel
*chan
,
2424 int64_t *blocking_timeout
)
2428 if (!chan
|| !blocking_timeout
) {
2429 ret
= -LTTNG_ERR_INVALID
;
2433 if (!chan
->attr
.extended
.ptr
) {
2434 ret
= -LTTNG_ERR_INVALID
;
2438 *blocking_timeout
= ((struct lttng_channel_extended
*)
2439 chan
->attr
.extended
.ptr
)->blocking_timeout
;
2444 int lttng_channel_set_blocking_timeout(struct lttng_channel
*chan
,
2445 int64_t blocking_timeout
)
2448 int64_t msec_timeout
;
2450 if (!chan
|| !chan
->attr
.extended
.ptr
) {
2451 ret
= -LTTNG_ERR_INVALID
;
2455 if (blocking_timeout
< 0 && blocking_timeout
!= -1) {
2456 ret
= -LTTNG_ERR_INVALID
;
2461 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2462 * us to accept a narrower range of values (msecs expressed as a signed
2465 msec_timeout
= blocking_timeout
/ 1000;
2466 if (msec_timeout
!= (int32_t) msec_timeout
) {
2467 ret
= -LTTNG_ERR_INVALID
;
2471 ((struct lttng_channel_extended
*)
2472 chan
->attr
.extended
.ptr
)->blocking_timeout
=
2479 * Check if session daemon is alive.
2481 * Return 1 if alive or 0 if not.
2482 * On error returns a negative value.
2484 int lttng_session_daemon_alive(void)
2488 ret
= set_session_daemon_path();
2494 if (*sessiond_sock_path
== '\0') {
2496 * No socket path set. Weird error which means the constructor
2502 ret
= try_connect_sessiond(sessiond_sock_path
);
2513 * Set URL for a consumer for a session and domain.
2515 * Return 0 on success, else a negative value.
2517 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2518 const char *control_url
, const char *data_url
)
2522 struct lttcomm_session_msg lsm
;
2523 struct lttng_uri
*uris
= NULL
;
2525 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2526 return -LTTNG_ERR_INVALID
;
2529 memset(&lsm
, 0, sizeof(lsm
));
2531 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2533 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2534 sizeof(lsm
.session
.name
));
2535 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2537 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2539 return -LTTNG_ERR_INVALID
;
2542 lsm
.u
.uri
.size
= size
;
2544 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2545 sizeof(struct lttng_uri
) * size
, NULL
);
2554 int lttng_enable_consumer(struct lttng_handle
*handle
)
2562 int lttng_disable_consumer(struct lttng_handle
*handle
)
2568 * This is an extension of create session that is ONLY and SHOULD only be used
2569 * by the lttng command line program. It exists to avoid using URI parsing in
2572 * We need the date and time for the trace path subdirectory for the case where
2573 * the user does NOT define one using either -o or -U. Using the normal
2574 * lttng_create_session API call, we have no clue on the session daemon side if
2575 * the URL was generated automatically by the client or define by the user.
2577 * So this function "wrapper" is hidden from the public API, takes the datetime
2578 * string and appends it if necessary to the URI subdirectory before sending it
2579 * to the session daemon.
2581 * With this extra function, the lttng_create_session call behavior is not
2582 * changed and the timestamp is appended to the URI on the session daemon side
2585 int _lttng_create_session_ext(const char *name
, const char *url
,
2586 const char *datetime
)
2590 struct lttcomm_session_msg lsm
;
2591 struct lttng_uri
*uris
= NULL
;
2593 if (name
== NULL
|| datetime
== NULL
) {
2594 return -LTTNG_ERR_INVALID
;
2597 memset(&lsm
, 0, sizeof(lsm
));
2599 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2600 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2602 /* There should never be a data URL. */
2603 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2605 ret
= -LTTNG_ERR_INVALID
;
2609 lsm
.u
.uri
.size
= size
;
2611 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2612 /* Don't append datetime if the name was automatically created. */
2613 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2614 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2615 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2618 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2621 PERROR("snprintf uri subdir");
2622 ret
= -LTTNG_ERR_FATAL
;
2627 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2628 sizeof(struct lttng_uri
) * size
, NULL
);
2636 * For a given session name, this call checks if the data is ready to be read
2637 * or is still being extracted by the consumer(s) hence not ready to be used by
2640 int lttng_data_pending(const char *session_name
)
2643 struct lttcomm_session_msg lsm
;
2644 uint8_t *pending
= NULL
;
2646 if (session_name
== NULL
) {
2647 return -LTTNG_ERR_INVALID
;
2650 memset(&lsm
, 0, sizeof(lsm
));
2651 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2653 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2654 sizeof(lsm
.session
.name
));
2656 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2659 } else if (ret
!= 1) {
2660 /* Unexpected payload size */
2661 ret
= -LTTNG_ERR_INVALID
;
2665 ret
= (int) *pending
;
2672 * Create a session exclusively used for snapshot.
2674 * Returns LTTNG_OK on success or a negative error code.
2676 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2680 struct lttcomm_session_msg lsm
;
2681 struct lttng_uri
*uris
= NULL
;
2684 return -LTTNG_ERR_INVALID
;
2687 memset(&lsm
, 0, sizeof(lsm
));
2689 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2690 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2692 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2694 return -LTTNG_ERR_INVALID
;
2697 lsm
.u
.uri
.size
= size
;
2700 * If the user does not specify a custom subdir, use the session name.
2702 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2703 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2705 PERROR("snprintf uri subdir");
2706 ret
= -LTTNG_ERR_FATAL
;
2711 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2712 sizeof(struct lttng_uri
) * size
, NULL
);
2720 * Create a session exclusively used for live.
2722 * Returns LTTNG_OK on success or a negative error code.
2724 int lttng_create_session_live(const char *name
, const char *url
,
2725 unsigned int timer_interval
)
2729 struct lttcomm_session_msg lsm
;
2730 struct lttng_uri
*uris
= NULL
;
2732 if (name
== NULL
|| timer_interval
== 0) {
2733 return -LTTNG_ERR_INVALID
;
2736 memset(&lsm
, 0, sizeof(lsm
));
2738 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2739 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2742 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2744 ret
= -LTTNG_ERR_INVALID
;
2748 /* file:// is not accepted for live session. */
2749 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2750 ret
= -LTTNG_ERR_INVALID
;
2757 lsm
.u
.session_live
.nb_uri
= size
;
2758 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2760 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2761 sizeof(struct lttng_uri
) * size
, NULL
);
2769 * List PIDs in the tracker.
2771 * enabled is set to whether the PID tracker is enabled.
2772 * pids is set to an allocated array of PIDs currently tracked. On
2773 * success, pids must be freed by the caller.
2774 * nr_pids is set to the number of entries contained by the pids array.
2776 * Returns 0 on success, else a negative LTTng error code.
2778 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2779 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2783 struct lttcomm_session_msg lsm
;
2785 int32_t *pids
= NULL
;
2787 if (handle
== NULL
) {
2788 return -LTTNG_ERR_INVALID
;
2791 memset(&lsm
, 0, sizeof(lsm
));
2792 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2793 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2794 sizeof(lsm
.session
.name
));
2795 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2797 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2801 nr_pids
= ret
/ sizeof(int32_t);
2802 if (nr_pids
> 0 && !pids
) {
2803 return -LTTNG_ERR_UNK
;
2805 if (nr_pids
== 1 && pids
[0] == -1) {
2811 *_enabled
= enabled
;
2813 *_nr_pids
= nr_pids
;
2818 * Regenerate the metadata for a session.
2819 * Return 0 on success, a negative error code on error.
2821 int lttng_regenerate_metadata(const char *session_name
)
2824 struct lttcomm_session_msg lsm
;
2826 if (!session_name
) {
2827 ret
= -LTTNG_ERR_INVALID
;
2831 memset(&lsm
, 0, sizeof(lsm
));
2832 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2834 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2835 sizeof(lsm
.session
.name
));
2837 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2848 * Deprecated, replaced by lttng_regenerate_metadata.
2850 int lttng_metadata_regenerate(const char *session_name
)
2852 return lttng_regenerate_metadata(session_name
);
2856 * Regenerate the statedump of a session.
2857 * Return 0 on success, a negative error code on error.
2859 int lttng_regenerate_statedump(const char *session_name
)
2862 struct lttcomm_session_msg lsm
;
2864 if (!session_name
) {
2865 ret
= -LTTNG_ERR_INVALID
;
2869 memset(&lsm
, 0, sizeof(lsm
));
2870 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2872 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2873 sizeof(lsm
.session
.name
));
2875 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2885 int lttng_register_trigger(struct lttng_trigger
*trigger
)
2888 struct lttcomm_session_msg lsm
;
2889 struct lttng_dynamic_buffer buffer
;
2891 lttng_dynamic_buffer_init(&buffer
);
2893 ret
= -LTTNG_ERR_INVALID
;
2897 if (!lttng_trigger_validate(trigger
)) {
2898 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2902 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2904 ret
= -LTTNG_ERR_UNK
;
2908 memset(&lsm
, 0, sizeof(lsm
));
2909 lsm
.cmd_type
= LTTNG_REGISTER_TRIGGER
;
2910 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2911 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2914 lttng_dynamic_buffer_reset(&buffer
);
2918 int lttng_unregister_trigger(struct lttng_trigger
*trigger
)
2921 struct lttcomm_session_msg lsm
;
2922 struct lttng_dynamic_buffer buffer
;
2924 lttng_dynamic_buffer_init(&buffer
);
2926 ret
= -LTTNG_ERR_INVALID
;
2930 if (!lttng_trigger_validate(trigger
)) {
2931 ret
= -LTTNG_ERR_INVALID_TRIGGER
;
2935 ret
= lttng_trigger_serialize(trigger
, &buffer
);
2937 ret
= -LTTNG_ERR_UNK
;
2941 memset(&lsm
, 0, sizeof(lsm
));
2942 lsm
.cmd_type
= LTTNG_UNREGISTER_TRIGGER
;
2943 lsm
.u
.trigger
.length
= (uint32_t) buffer
.size
;
2944 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buffer
.data
,
2947 lttng_dynamic_buffer_reset(&buffer
);
2954 static void __attribute__((constructor
)) init(void)
2956 /* Set default session group */
2957 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2963 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2965 free(tracing_group
);