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/defaults.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/uri.h>
36 #include <common/utils.h>
37 #include <lttng/lttng.h>
38 #include <lttng/health-internal.h>
40 #include "filter/filter-ast.h"
41 #include "filter/filter-parser.h"
42 #include "filter/filter-bytecode.h"
43 #include "filter/memstream.h"
44 #include "lttng-ctl-helper.h"
47 static const int print_xml
= 1;
48 #define dbg_printf(fmt, args...) \
49 printf("[debug liblttng-ctl] " fmt, ## args)
51 static const int print_xml
= 0;
52 #define dbg_printf(fmt, args...) \
54 /* do nothing but check printf format */ \
56 printf("[debug liblttnctl] " fmt, ## args); \
61 /* Socket to session daemon for communication */
62 static int sessiond_socket
;
63 static char sessiond_sock_path
[PATH_MAX
];
66 static char *tracing_group
;
72 * Those two variables are used by error.h to silent or control the verbosity of
73 * error message. They are global to the library so application linking with it
74 * are able to compile correctly and also control verbosity of the library.
77 int lttng_opt_verbose
;
81 * Copy string from src to dst and enforce null terminated byte.
84 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
87 strncpy(dst
, src
, len
);
88 /* Enforce the NULL terminated byte */
96 * Copy domain to lttcomm_session_msg domain.
98 * If domain is unknown, default domain will be the kernel.
101 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
102 struct lttng_domain
*src
)
106 case LTTNG_DOMAIN_KERNEL
:
107 case LTTNG_DOMAIN_UST
:
108 case LTTNG_DOMAIN_JUL
:
109 case LTTNG_DOMAIN_LOG4J
:
110 case LTTNG_DOMAIN_PYTHON
:
111 memcpy(dst
, src
, sizeof(struct lttng_domain
));
114 memset(dst
, 0, sizeof(struct lttng_domain
));
121 * Send lttcomm_session_msg to the session daemon.
123 * On success, returns the number of bytes sent (>=0)
124 * On error, returns -1
126 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
131 ret
= -LTTNG_ERR_NO_SESSIOND
;
135 DBG("LSM cmd type : %d", lsm
->cmd_type
);
137 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
138 sizeof(struct lttcomm_session_msg
));
140 ret
= -LTTNG_ERR_FATAL
;
148 * Send var len data to the session daemon.
150 * On success, returns the number of bytes sent (>=0)
151 * On error, returns -1
153 static int send_session_varlen(const void *data
, size_t len
)
158 ret
= -LTTNG_ERR_NO_SESSIOND
;
167 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
169 ret
= -LTTNG_ERR_FATAL
;
177 * Receive data from the sessiond socket.
179 * On success, returns the number of bytes received (>=0)
180 * On error, returns -1 (recvmsg() error) or -ENOTCONN
182 static int recv_data_sessiond(void *buf
, size_t len
)
187 ret
= -LTTNG_ERR_NO_SESSIOND
;
191 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
193 ret
= -LTTNG_ERR_FATAL
;
201 * Check if we are in the specified group.
203 * If yes return 1, else return -1.
206 int lttng_check_tracing_group(void)
208 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
210 int grp_list_size
, grp_id
, i
;
212 const char *grp_name
= tracing_group
;
214 /* Get GID of group 'tracing' */
215 grp_tracing
= getgrnam(grp_name
);
217 /* If grp_tracing is NULL, the group does not exist. */
221 /* Get number of supplementary group IDs */
222 grp_list_size
= getgroups(0, NULL
);
223 if (grp_list_size
< 0) {
228 /* Alloc group list of the right size */
229 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
234 grp_id
= getgroups(grp_list_size
, grp_list
);
240 for (i
= 0; i
< grp_list_size
; i
++) {
241 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
255 * Try connect to session daemon with sock_path.
257 * Return 0 on success, else -1
259 static int try_connect_sessiond(const char *sock_path
)
263 /* If socket exist, we check if the daemon listens for connect. */
264 ret
= access(sock_path
, F_OK
);
270 ret
= lttcomm_connect_unix_sock(sock_path
);
276 ret
= lttcomm_close_unix_sock(ret
);
278 PERROR("lttcomm_close_unix_sock");
288 * Set sessiond socket path by putting it in the global sessiond_sock_path
291 * Returns 0 on success, negative value on failure (the sessiond socket path
292 * is somehow too long or ENOMEM).
294 static int set_session_daemon_path(void)
296 int in_tgroup
= 0; /* In tracing group. */
302 /* Are we in the tracing group ? */
303 in_tgroup
= lttng_check_tracing_group();
306 if ((uid
== 0) || in_tgroup
) {
307 lttng_ctl_copy_string(sessiond_sock_path
,
308 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
316 ret
= try_connect_sessiond(sessiond_sock_path
);
320 /* Global session daemon not available... */
322 /* ...or not in tracing group (and not root), default */
325 * With GNU C < 2.1, snprintf returns -1 if the target buffer
327 * With GNU C >= 2.1, snprintf returns the required size
328 * (excluding closing null)
330 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
331 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
332 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
344 * Connect to the LTTng session daemon.
346 * On success, return 0. On error, return -1.
348 static int connect_sessiond(void)
352 /* Don't try to connect if already connected. */
357 ret
= set_session_daemon_path();
362 /* Connect to the sesssion daemon. */
363 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
368 sessiond_socket
= ret
;
378 * Clean disconnect from the session daemon.
380 * On success, return 0. On error, return -1.
382 static int disconnect_sessiond(void)
387 ret
= lttcomm_close_unix_sock(sessiond_socket
);
395 static int recv_sessiond_optional_data(size_t len
, void **user_buf
,
403 ret
= -LTTNG_ERR_INVALID
;
413 ret
= recv_data_sessiond(buf
, len
);
419 ret
= -LTTNG_ERR_INVALID
;
423 /* Move ownership of command header buffer to user. */
428 /* No command header. */
444 * Ask the session daemon a specific command and put the data into buf.
445 * Takes extra var. len. data as input to send to the session daemon.
447 * Return size of data (only payload, not header) or a negative error code.
450 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
451 const void *vardata
, size_t vardata_len
,
452 void **user_payload_buf
, void **user_cmd_header_buf
,
453 size_t *user_cmd_header_len
)
457 struct lttcomm_lttng_msg llm
;
459 ret
= connect_sessiond();
461 ret
= -LTTNG_ERR_NO_SESSIOND
;
465 /* Send command to session daemon */
466 ret
= send_session_msg(lsm
);
468 /* Ret value is a valid lttng error code. */
471 /* Send var len data */
472 ret
= send_session_varlen(vardata
, vardata_len
);
474 /* Ret value is a valid lttng error code. */
478 /* Get header from data transmission */
479 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
481 /* Ret value is a valid lttng error code. */
485 /* Check error code if OK */
486 if (llm
.ret_code
!= LTTNG_OK
) {
491 /* Get command header from data transmission */
492 ret
= recv_sessiond_optional_data(llm
.cmd_header_size
,
493 user_cmd_header_buf
, user_cmd_header_len
);
498 /* Get payload from data transmission */
499 ret
= recv_sessiond_optional_data(llm
.data_size
, user_payload_buf
,
508 disconnect_sessiond();
513 * Create lttng handle and return pointer.
515 * The returned pointer will be NULL in case of malloc() error.
517 struct lttng_handle
*lttng_create_handle(const char *session_name
,
518 struct lttng_domain
*domain
)
520 struct lttng_handle
*handle
= NULL
;
522 handle
= zmalloc(sizeof(struct lttng_handle
));
523 if (handle
== NULL
) {
524 PERROR("malloc handle");
528 /* Copy session name */
529 lttng_ctl_copy_string(handle
->session_name
, session_name
,
530 sizeof(handle
->session_name
));
532 /* Copy lttng domain or leave initialized to 0. */
534 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
542 * Destroy handle by free(3) the pointer.
544 void lttng_destroy_handle(struct lttng_handle
*handle
)
550 * Register an outside consumer.
552 * Returns size of returned session payload data or a negative error code.
554 int lttng_register_consumer(struct lttng_handle
*handle
,
555 const char *socket_path
)
557 struct lttcomm_session_msg lsm
;
559 if (handle
== NULL
|| socket_path
== NULL
) {
560 return -LTTNG_ERR_INVALID
;
563 memset(&lsm
, 0, sizeof(lsm
));
564 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
565 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
566 sizeof(lsm
.session
.name
));
567 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
569 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
570 sizeof(lsm
.u
.reg
.path
));
572 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
576 * Start tracing for all traces of the session.
578 * Returns size of returned session payload data or a negative error code.
580 int lttng_start_tracing(const char *session_name
)
582 struct lttcomm_session_msg lsm
;
584 if (session_name
== NULL
) {
585 return -LTTNG_ERR_INVALID
;
588 memset(&lsm
, 0, sizeof(lsm
));
589 lsm
.cmd_type
= LTTNG_START_TRACE
;
591 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
592 sizeof(lsm
.session
.name
));
594 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
598 * Stop tracing for all traces of the session.
600 static int _lttng_stop_tracing(const char *session_name
, int wait
)
603 struct lttcomm_session_msg lsm
;
605 if (session_name
== NULL
) {
606 return -LTTNG_ERR_INVALID
;
609 memset(&lsm
, 0, sizeof(lsm
));
610 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
612 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
613 sizeof(lsm
.session
.name
));
615 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
616 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
624 /* Check for data availability */
626 data_ret
= lttng_data_pending(session_name
);
628 /* Return the data available call error. */
634 * Data sleep time before retrying (in usec). Don't sleep if the
635 * call returned value indicates availability.
638 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
640 } while (data_ret
!= 0);
648 * Stop tracing and wait for data availability.
650 int lttng_stop_tracing(const char *session_name
)
652 return _lttng_stop_tracing(session_name
, 1);
656 * Stop tracing but _don't_ wait for data availability.
658 int lttng_stop_tracing_no_wait(const char *session_name
)
660 return _lttng_stop_tracing(session_name
, 0);
664 * Add context to a channel.
666 * If the given channel is NULL, add the contexts to all channels.
667 * The event_name param is ignored.
669 * Returns the size of the returned payload data or a negative error code.
671 int lttng_add_context(struct lttng_handle
*handle
,
672 struct lttng_event_context
*ctx
, const char *event_name
,
673 const char *channel_name
)
678 struct lttcomm_session_msg lsm
;
680 /* Safety check. Both are mandatory. */
681 if (handle
== NULL
|| ctx
== NULL
) {
682 ret
= -LTTNG_ERR_INVALID
;
686 memset(&lsm
, 0, sizeof(lsm
));
687 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
689 /* If no channel name, send empty string. */
690 if (channel_name
== NULL
) {
691 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
692 sizeof(lsm
.u
.context
.channel_name
));
694 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
695 sizeof(lsm
.u
.context
.channel_name
));
698 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
699 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
700 sizeof(lsm
.session
.name
));
702 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
703 size_t provider_len
, ctx_len
;
704 const char *provider_name
= ctx
->u
.app_ctx
.provider_name
;
705 const char *ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
707 if (!provider_name
|| !ctx_name
) {
708 ret
= -LTTNG_ERR_INVALID
;
712 provider_len
= strlen(provider_name
);
713 if (provider_len
== 0) {
714 ret
= -LTTNG_ERR_INVALID
;
717 lsm
.u
.context
.provider_name_len
= provider_len
;
719 ctx_len
= strlen(ctx_name
);
721 ret
= -LTTNG_ERR_INVALID
;
724 lsm
.u
.context
.context_name_len
= ctx_len
;
726 len
= provider_len
+ ctx_len
;
729 ret
= -LTTNG_ERR_NOMEM
;
733 memcpy(buf
, provider_name
, provider_len
);
734 memcpy(buf
+ provider_len
, ctx_name
, ctx_len
);
736 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
738 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
740 * Don't leak application addresses to the sessiond.
741 * This is only necessary when ctx is for an app ctx otherwise
742 * the values inside the union (type & config) are overwritten.
744 lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
745 lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
748 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, buf
, len
, NULL
);
755 * Enable event(s) for a channel.
757 * If no event name is specified, all events are enabled.
758 * If no channel name is specified, the default 'channel0' is used.
760 * Returns size of returned session payload data or a negative error code.
762 int lttng_enable_event(struct lttng_handle
*handle
,
763 struct lttng_event
*ev
, const char *channel_name
)
765 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
770 * Create or enable an event with a filter expression.
772 * Return negative error value on error.
773 * Return size of returned session payload data if OK.
775 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
776 struct lttng_event
*event
, const char *channel_name
,
777 const char *filter_expression
)
779 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
780 filter_expression
, 0, NULL
);
784 * Depending on the event, return a newly allocated agent filter expression or
785 * NULL if not applicable.
787 * An event with NO loglevel and the name is * will return NULL.
789 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
792 char *agent_filter
= NULL
;
796 /* Don't add filter for the '*' event. */
797 if (ev
->name
[0] != '*') {
799 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
802 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
810 /* Add loglevel filtering if any for the JUL domain. */
811 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
814 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
820 if (filter
|| agent_filter
) {
823 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
824 agent_filter
? agent_filter
: filter
, op
,
829 agent_filter
= new_filter
;
831 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
847 * Generate the filter bytecode from a given filter expression string. Put the
848 * newly allocated parser context in ctxp and populate the lsm object with the
851 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
853 static int generate_filter(char *filter_expression
,
854 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
857 struct filter_parser_ctx
*ctx
= NULL
;
860 assert(filter_expression
);
865 * Casting const to non-const, as the underlying function will use it in
868 fmem
= lttng_fmemopen((void *) filter_expression
,
869 strlen(filter_expression
), "r");
871 fprintf(stderr
, "Error opening memory as stream\n");
872 ret
= -LTTNG_ERR_FILTER_NOMEM
;
875 ctx
= filter_parser_ctx_alloc(fmem
);
877 fprintf(stderr
, "Error allocating parser\n");
878 ret
= -LTTNG_ERR_FILTER_NOMEM
;
879 goto filter_alloc_error
;
881 ret
= filter_parser_ctx_append_ast(ctx
);
883 fprintf(stderr
, "Parse error\n");
884 ret
= -LTTNG_ERR_FILTER_INVAL
;
887 ret
= filter_visitor_set_parent(ctx
);
889 fprintf(stderr
, "Set parent error\n");
890 ret
= -LTTNG_ERR_FILTER_INVAL
;
894 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
897 fprintf(stderr
, "XML print error\n");
898 ret
= -LTTNG_ERR_FILTER_INVAL
;
903 dbg_printf("Generating IR... ");
905 ret
= filter_visitor_ir_generate(ctx
);
907 fprintf(stderr
, "Generate IR error\n");
908 ret
= -LTTNG_ERR_FILTER_INVAL
;
911 dbg_printf("done\n");
913 dbg_printf("Validating IR... ");
915 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
917 ret
= -LTTNG_ERR_FILTER_INVAL
;
920 /* Validate strings used as literals in the expression. */
921 ret
= filter_visitor_ir_validate_string(ctx
);
923 ret
= -LTTNG_ERR_FILTER_INVAL
;
926 dbg_printf("done\n");
928 dbg_printf("Generating bytecode... ");
930 ret
= filter_visitor_bytecode_generate(ctx
);
932 fprintf(stderr
, "Generate bytecode error\n");
933 ret
= -LTTNG_ERR_FILTER_INVAL
;
936 dbg_printf("done\n");
937 dbg_printf("Size of bytecode generated: %u bytes.\n",
938 bytecode_get_len(&ctx
->bytecode
->b
));
940 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
941 + bytecode_get_len(&ctx
->bytecode
->b
);
942 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
944 /* No need to keep the memory stream. */
945 if (fclose(fmem
) != 0) {
954 filter_parser_ctx_free(ctx
);
956 if (fclose(fmem
) != 0) {
964 * Enable event(s) for a channel, possibly with exclusions and a filter.
965 * If no event name is specified, all events are enabled.
966 * If no channel name is specified, the default name is used.
967 * If filter expression is not NULL, the filter is set for the event.
968 * If exclusion count is not zero, the exclusions are set for the event.
969 * Returns size of returned session payload data or a negative error code.
971 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
972 struct lttng_event
*ev
, const char *channel_name
,
973 const char *original_filter_expression
,
974 int exclusion_count
, char **exclusion_list
)
976 struct lttcomm_session_msg lsm
;
979 unsigned int free_filter_expression
= 0;
980 struct filter_parser_ctx
*ctx
= NULL
;
982 * Cast as non-const since we may replace the filter expression
983 * by a dynamically allocated string. Otherwise, the original
984 * string is not modified.
986 char *filter_expression
= (char *) original_filter_expression
;
988 if (handle
== NULL
|| ev
== NULL
) {
989 ret
= -LTTNG_ERR_INVALID
;
994 * Empty filter string will always be rejected by the parser
995 * anyway, so treat this corner-case early to eliminate
996 * lttng_fmemopen error for 0-byte allocation.
998 if (filter_expression
&& filter_expression
[0] == '\0') {
999 ret
= -LTTNG_ERR_INVALID
;
1003 memset(&lsm
, 0, sizeof(lsm
));
1005 /* If no channel name, send empty string. */
1006 if (channel_name
== NULL
) {
1007 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
1008 sizeof(lsm
.u
.enable
.channel_name
));
1010 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
1011 sizeof(lsm
.u
.enable
.channel_name
));
1014 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
1015 if (ev
->name
[0] == '\0') {
1016 /* Enable all events */
1017 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
1020 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1021 /* FIXME: copying non-packed struct to packed struct. */
1022 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
1024 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1025 sizeof(lsm
.session
.name
));
1026 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
1027 lsm
.u
.enable
.bytecode_len
= 0;
1030 * For the JUL domain, a filter is enforced except for the enable all
1031 * event. This is done to avoid having the event in all sessions thus
1032 * filtering by logger name.
1034 if (exclusion_count
== 0 && filter_expression
== NULL
&&
1035 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1036 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1037 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1042 * We have either a filter or some exclusions, so we need to set up
1043 * a variable-length memory block from where to send the data.
1046 /* Parse filter expression. */
1047 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1048 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1049 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1050 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1051 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1052 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1055 /* Setup JUL filter if needed. */
1056 agent_filter
= set_agent_filter(filter_expression
, ev
);
1057 if (!agent_filter
) {
1058 if (!filter_expression
) {
1060 * No JUL and no filter, just skip
1067 * With an agent filter, the original filter has
1068 * been added to it thus replace the filter
1071 filter_expression
= agent_filter
;
1072 free_filter_expression
= 1;
1076 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1082 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1083 + lsm
.u
.enable
.expression_len
1084 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1086 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1090 /* Put exclusion names first in the data. */
1091 while (exclusion_count
--) {
1092 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1093 *(exclusion_list
+ exclusion_count
),
1094 LTTNG_SYMBOL_NAME_LEN
- 1);
1096 /* Add filter expression next. */
1097 if (lsm
.u
.enable
.expression_len
!= 0) {
1099 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1101 lsm
.u
.enable
.expression_len
);
1103 /* Add filter bytecode next. */
1104 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1106 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1107 + lsm
.u
.enable
.expression_len
,
1109 lsm
.u
.enable
.bytecode_len
);
1112 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1113 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1114 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1119 if (filter_expression
&& ctx
) {
1120 filter_bytecode_free(ctx
);
1121 filter_ir_free(ctx
);
1122 filter_parser_ctx_free(ctx
);
1125 if (free_filter_expression
) {
1127 * The filter expression has been replaced and must be freed as
1128 * it is not the original filter expression received as a
1131 free(filter_expression
);
1135 * Return directly to the caller and don't ask the sessiond since
1136 * something went wrong in the parsing of data above.
1141 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1145 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1146 struct lttng_event
*ev
, const char *channel_name
,
1147 const char *original_filter_expression
)
1149 struct lttcomm_session_msg lsm
;
1152 unsigned int free_filter_expression
= 0;
1153 struct filter_parser_ctx
*ctx
= NULL
;
1155 * Cast as non-const since we may replace the filter expression
1156 * by a dynamically allocated string. Otherwise, the original
1157 * string is not modified.
1159 char *filter_expression
= (char *) original_filter_expression
;
1161 if (handle
== NULL
|| ev
== NULL
) {
1162 ret
= -LTTNG_ERR_INVALID
;
1167 * Empty filter string will always be rejected by the parser
1168 * anyway, so treat this corner-case early to eliminate
1169 * lttng_fmemopen error for 0-byte allocation.
1171 if (filter_expression
&& filter_expression
[0] == '\0') {
1172 ret
= -LTTNG_ERR_INVALID
;
1176 memset(&lsm
, 0, sizeof(lsm
));
1178 /* If no channel name, send empty string. */
1179 if (channel_name
== NULL
) {
1180 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1181 sizeof(lsm
.u
.disable
.channel_name
));
1183 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1184 sizeof(lsm
.u
.disable
.channel_name
));
1187 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1189 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1190 /* FIXME: copying non-packed struct to packed struct. */
1191 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1193 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1194 sizeof(lsm
.session
.name
));
1195 lsm
.u
.disable
.bytecode_len
= 0;
1198 * For the JUL domain, a filter is enforced except for the
1199 * disable all event. This is done to avoid having the event in
1200 * all sessions thus filtering by logger name.
1202 if (filter_expression
== NULL
&&
1203 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1204 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1205 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1210 * We have a filter, so we need to set up a variable-length
1211 * memory block from where to send the data.
1214 /* Parse filter expression */
1215 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
1216 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
1217 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1218 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
1219 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
1220 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
1223 /* Setup JUL filter if needed. */
1224 agent_filter
= set_agent_filter(filter_expression
, ev
);
1225 if (!agent_filter
) {
1226 if (!filter_expression
) {
1228 * No JUL and no filter, just skip
1235 * With a JUL filter, the original filter has
1236 * been added to it thus replace the filter
1239 filter_expression
= agent_filter
;
1240 free_filter_expression
= 1;
1244 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1250 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1251 + lsm
.u
.disable
.expression_len
);
1253 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1257 /* Add filter expression. */
1258 if (lsm
.u
.disable
.expression_len
!= 0) {
1261 lsm
.u
.disable
.expression_len
);
1263 /* Add filter bytecode next. */
1264 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1266 + lsm
.u
.disable
.expression_len
,
1268 lsm
.u
.disable
.bytecode_len
);
1271 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, varlen_data
,
1272 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1276 if (filter_expression
&& ctx
) {
1277 filter_bytecode_free(ctx
);
1278 filter_ir_free(ctx
);
1279 filter_parser_ctx_free(ctx
);
1282 if (free_filter_expression
) {
1284 * The filter expression has been replaced and must be freed as
1285 * it is not the original filter expression received as a
1288 free(filter_expression
);
1292 * Return directly to the caller and don't ask the sessiond since
1293 * something went wrong in the parsing of data above.
1298 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1303 * Disable event(s) of a channel and domain.
1304 * If no event name is specified, all events are disabled.
1305 * If no channel name is specified, the default 'channel0' is used.
1306 * Returns size of returned session payload data or a negative error code.
1308 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1309 const char *channel_name
)
1311 struct lttng_event ev
;
1313 memset(&ev
, 0, sizeof(ev
));
1315 ev
.type
= LTTNG_EVENT_ALL
;
1316 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1317 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1321 * Enable channel per domain
1322 * Returns size of returned session payload data or a negative error code.
1324 int lttng_enable_channel(struct lttng_handle
*handle
,
1325 struct lttng_channel
*chan
)
1327 struct lttcomm_session_msg lsm
;
1329 /* NULL arguments are forbidden. No default values. */
1330 if (handle
== NULL
|| chan
== NULL
) {
1331 return -LTTNG_ERR_INVALID
;
1334 memset(&lsm
, 0, sizeof(lsm
));
1336 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1338 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1340 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1342 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1343 sizeof(lsm
.session
.name
));
1345 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1349 * All tracing will be stopped for registered events of the channel.
1350 * Returns size of returned session payload data or a negative error code.
1352 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1354 struct lttcomm_session_msg lsm
;
1356 /* Safety check. Both are mandatory. */
1357 if (handle
== NULL
|| name
== NULL
) {
1358 return -LTTNG_ERR_INVALID
;
1361 memset(&lsm
, 0, sizeof(lsm
));
1363 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1365 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1366 sizeof(lsm
.u
.disable
.channel_name
));
1368 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1370 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1371 sizeof(lsm
.session
.name
));
1373 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1377 int lttng_track_untrack_id(struct lttng_handle
*handle
,
1378 enum lttng_tracker_type tracker_type
,
1379 struct lttng_tracker_id
*id
,
1380 enum lttcomm_sessiond_command cmd
)
1382 struct lttcomm_session_msg lsm
;
1383 char *var_data
= NULL
;
1384 size_t var_data_len
= 0;
1386 /* NULL arguments are forbidden. No default values. */
1387 if (handle
== NULL
) {
1388 return -LTTNG_ERR_INVALID
;
1391 memset(&lsm
, 0, sizeof(lsm
));
1394 lsm
.u
.id_tracker
.tracker_type
= tracker_type
;
1395 lsm
.u
.id_tracker
.id_type
= id
->type
;
1399 case LTTNG_ID_VALUE
:
1400 lsm
.u
.id_tracker
.u
.value
= id
->value
;
1402 case LTTNG_ID_STRING
:
1403 var_data
= id
->string
;
1404 var_data_len
= strlen(var_data
) + 1; /* Includes \0. */
1405 lsm
.u
.id_tracker
.u
.var_len
= var_data_len
;
1408 return -LTTNG_ERR_INVALID
;
1411 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1413 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1414 sizeof(lsm
.session
.name
));
1416 return lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
,
1417 var_data
, var_data_len
, NULL
);
1422 * Add ID to session tracker.
1423 * Return 0 on success else a negative LTTng error code.
1425 int lttng_track_id(struct lttng_handle
*handle
,
1426 enum lttng_tracker_type tracker_type
,
1427 struct lttng_tracker_id
*id
)
1429 return lttng_track_untrack_id(handle
, tracker_type
,
1430 id
, LTTNG_TRACK_ID
);
1434 * Remove ID from session tracker.
1435 * Return 0 on success else a negative LTTng error code.
1437 int lttng_untrack_id(struct lttng_handle
*handle
,
1438 enum lttng_tracker_type tracker_type
,
1439 struct lttng_tracker_id
*id
)
1441 return lttng_track_untrack_id(handle
, tracker_type
,
1442 id
, LTTNG_UNTRACK_ID
);
1446 * Add PID to session tracker.
1447 * Return 0 on success else a negative LTTng error code.
1449 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1451 struct lttng_tracker_id id
;
1453 id
.type
= LTTNG_TRACKER_PID
;
1455 return lttng_track_id(handle
, LTTNG_TRACKER_PID
, &id
);
1459 * Remove PID from session tracker.
1460 * Return 0 on success else a negative LTTng error code.
1462 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1464 struct lttng_tracker_id id
;
1466 id
.type
= LTTNG_TRACKER_PID
;
1468 return lttng_untrack_id(handle
, LTTNG_TRACKER_PID
, &id
);
1472 * Lists all available tracepoints of domain.
1473 * Sets the contents of the events array.
1474 * Returns the number of lttng_event entries in events;
1475 * on error, returns a negative value.
1477 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1478 struct lttng_event
**events
)
1481 struct lttcomm_session_msg lsm
;
1483 if (handle
== NULL
) {
1484 return -LTTNG_ERR_INVALID
;
1487 memset(&lsm
, 0, sizeof(lsm
));
1488 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1489 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1491 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1496 return ret
/ sizeof(struct lttng_event
);
1500 * Lists all available tracepoint fields of domain.
1501 * Sets the contents of the event field array.
1502 * Returns the number of lttng_event_field entries in events;
1503 * on error, returns a negative value.
1505 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1506 struct lttng_event_field
**fields
)
1509 struct lttcomm_session_msg lsm
;
1511 if (handle
== NULL
) {
1512 return -LTTNG_ERR_INVALID
;
1515 memset(&lsm
, 0, sizeof(lsm
));
1516 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1517 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1519 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1524 return ret
/ sizeof(struct lttng_event_field
);
1528 * Lists all available kernel system calls. Allocates and sets the contents of
1531 * Returns the number of lttng_event entries in events; on error, returns a
1534 int lttng_list_syscalls(struct lttng_event
**events
)
1537 struct lttcomm_session_msg lsm
;
1540 return -LTTNG_ERR_INVALID
;
1543 memset(&lsm
, 0, sizeof(lsm
));
1544 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1545 /* Force kernel domain for system calls. */
1546 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1548 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1553 return ret
/ sizeof(struct lttng_event
);
1557 * Returns a human readable string describing
1558 * the error code (a negative value).
1560 const char *lttng_strerror(int code
)
1562 return error_get_str(code
);
1566 * Create a brand new session using name and url for destination.
1568 * Returns LTTNG_OK on success or a negative error code.
1570 int lttng_create_session(const char *name
, const char *url
)
1574 struct lttcomm_session_msg lsm
;
1575 struct lttng_uri
*uris
= NULL
;
1578 return -LTTNG_ERR_INVALID
;
1581 memset(&lsm
, 0, sizeof(lsm
));
1583 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1584 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1586 /* There should never be a data URL */
1587 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1589 return -LTTNG_ERR_INVALID
;
1592 lsm
.u
.uri
.size
= size
;
1594 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
1595 sizeof(struct lttng_uri
) * size
, NULL
);
1602 * Destroy session using name.
1603 * Returns size of returned session payload data or a negative error code.
1606 int _lttng_destroy_session(const char *session_name
)
1608 struct lttcomm_session_msg lsm
;
1610 if (session_name
== NULL
) {
1611 return -LTTNG_ERR_INVALID
;
1614 memset(&lsm
, 0, sizeof(lsm
));
1615 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1617 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1618 sizeof(lsm
.session
.name
));
1620 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1624 * Stop the session and wait for the data before destroying it
1626 int lttng_destroy_session(const char *session_name
)
1631 * Stop the tracing and wait for the data.
1633 ret
= _lttng_stop_tracing(session_name
, 1);
1634 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1638 ret
= _lttng_destroy_session(session_name
);
1644 * Destroy the session without waiting for the data.
1646 int lttng_destroy_session_no_wait(const char *session_name
)
1651 * Stop the tracing without waiting for the data.
1652 * The session might already have been stopped, so just
1655 ret
= _lttng_stop_tracing(session_name
, 0);
1656 if (ret
&& ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
1660 ret
= _lttng_destroy_session(session_name
);
1666 * Ask the session daemon for all available sessions.
1667 * Sets the contents of the sessions array.
1668 * Returns the number of lttng_session entries in sessions;
1669 * on error, returns a negative value.
1671 int lttng_list_sessions(struct lttng_session
**sessions
)
1674 struct lttcomm_session_msg lsm
;
1676 memset(&lsm
, 0, sizeof(lsm
));
1677 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1678 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1683 return ret
/ sizeof(struct lttng_session
);
1686 int lttng_set_session_shm_path(const char *session_name
,
1687 const char *shm_path
)
1689 struct lttcomm_session_msg lsm
;
1691 if (session_name
== NULL
) {
1692 return -LTTNG_ERR_INVALID
;
1695 memset(&lsm
, 0, sizeof(lsm
));
1696 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1698 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1699 sizeof(lsm
.session
.name
));
1700 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1701 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1703 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1707 * Ask the session daemon for all available domains of a session.
1708 * Sets the contents of the domains array.
1709 * Returns the number of lttng_domain entries in domains;
1710 * on error, returns a negative value.
1712 int lttng_list_domains(const char *session_name
,
1713 struct lttng_domain
**domains
)
1716 struct lttcomm_session_msg lsm
;
1718 if (session_name
== NULL
) {
1719 return -LTTNG_ERR_INVALID
;
1722 memset(&lsm
, 0, sizeof(lsm
));
1723 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1725 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1726 sizeof(lsm
.session
.name
));
1728 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1733 return ret
/ sizeof(struct lttng_domain
);
1737 * Ask the session daemon for all available channels of a session.
1738 * Sets the contents of the channels array.
1739 * Returns the number of lttng_channel entries in channels;
1740 * on error, returns a negative value.
1742 int lttng_list_channels(struct lttng_handle
*handle
,
1743 struct lttng_channel
**channels
)
1746 size_t channel_count
, i
;
1747 const size_t channel_size
= sizeof(struct lttng_channel
) +
1748 sizeof(struct lttcomm_channel_extended
);
1749 struct lttcomm_session_msg lsm
;
1752 if (handle
== NULL
) {
1753 ret
= -LTTNG_ERR_INVALID
;
1757 memset(&lsm
, 0, sizeof(lsm
));
1758 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1759 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1760 sizeof(lsm
.session
.name
));
1762 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1764 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1769 if (ret
% channel_size
) {
1770 ret
= -LTTNG_ERR_UNK
;
1775 channel_count
= (size_t) ret
/ channel_size
;
1777 /* Set extended info pointers */
1778 extended_at
= ((void *) *channels
) +
1779 channel_count
* sizeof(struct lttng_channel
);
1780 for (i
= 0; i
< channel_count
; i
++) {
1781 struct lttng_channel
*chan
= &(*channels
)[i
];
1783 chan
->attr
.extended
.ptr
= extended_at
;
1784 extended_at
+= sizeof(struct lttcomm_channel_extended
);
1787 ret
= (int) channel_count
;
1793 * Ask the session daemon for all available events of a session channel.
1794 * Sets the contents of the events array.
1795 * Returns the number of lttng_event entries in events;
1796 * on error, returns a negative value.
1798 int lttng_list_events(struct lttng_handle
*handle
,
1799 const char *channel_name
, struct lttng_event
**events
)
1802 struct lttcomm_session_msg lsm
;
1803 struct lttcomm_event_command_header
*cmd_header
= NULL
;
1804 size_t cmd_header_len
;
1805 uint32_t nb_events
, i
;
1808 /* Safety check. An handle and channel name are mandatory */
1809 if (handle
== NULL
|| channel_name
== NULL
) {
1810 return -LTTNG_ERR_INVALID
;
1813 memset(&lsm
, 0, sizeof(lsm
));
1814 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1815 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1816 sizeof(lsm
.session
.name
));
1817 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1818 sizeof(lsm
.u
.list
.channel_name
));
1819 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1821 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) events
,
1822 (void **) &cmd_header
, &cmd_header_len
);
1827 /* Set number of events and free command header */
1828 nb_events
= cmd_header
->nb_events
;
1829 if (nb_events
> INT_MAX
) {
1830 ret
= -LTTNG_ERR_OVERFLOW
;
1833 ret
= (int) nb_events
;
1837 /* Set extended info pointers */
1838 extended_at
= ((void*) (*events
)) +
1839 nb_events
* sizeof(struct lttng_event
);
1841 for (i
= 0; i
< nb_events
; i
++) {
1842 struct lttcomm_event_extended_header
*ext_header
;
1843 struct lttng_event
*event
= &(*events
)[i
];
1845 event
->extended
.ptr
= extended_at
;
1847 (struct lttcomm_event_extended_header
*) extended_at
;
1848 extended_at
+= sizeof(*ext_header
);
1849 extended_at
+= ext_header
->filter_len
;
1851 ext_header
->nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
1861 int lttng_event_get_filter_expression(struct lttng_event
*event
,
1862 const char **filter_expression
)
1865 struct lttcomm_event_extended_header
*ext_header
;
1867 if (!event
|| !filter_expression
) {
1868 ret
= -LTTNG_ERR_INVALID
;
1872 ext_header
= event
->extended
.ptr
;
1876 * This can happen since the lttng_event structure is
1877 * used for other tasks where this pointer is never set.
1879 *filter_expression
= NULL
;
1883 if (ext_header
->filter_len
) {
1884 *filter_expression
= ((const char *) (ext_header
)) +
1885 sizeof(*ext_header
);
1887 *filter_expression
= NULL
;
1894 int lttng_event_get_exclusion_name_count(struct lttng_event
*event
)
1897 struct lttcomm_event_extended_header
*ext_header
;
1900 ret
= -LTTNG_ERR_INVALID
;
1904 ext_header
= event
->extended
.ptr
;
1907 * This can happen since the lttng_event structure is
1908 * used for other tasks where this pointer is never set.
1914 if (ext_header
->nb_exclusions
> INT_MAX
) {
1915 ret
= -LTTNG_ERR_OVERFLOW
;
1918 ret
= (int) ext_header
->nb_exclusions
;
1923 int lttng_event_get_exclusion_name(struct lttng_event
*event
,
1924 size_t index
, const char **exclusion_name
)
1927 struct lttcomm_event_extended_header
*ext_header
;
1930 if (!event
|| !exclusion_name
) {
1931 ret
= -LTTNG_ERR_INVALID
;
1935 ext_header
= event
->extended
.ptr
;
1937 ret
= -LTTNG_ERR_INVALID
;
1941 if (index
>= ext_header
->nb_exclusions
) {
1942 ret
= -LTTNG_ERR_INVALID
;
1946 at
= (void *) ext_header
+ sizeof(*ext_header
);
1947 at
+= ext_header
->filter_len
;
1948 at
+= index
* LTTNG_SYMBOL_NAME_LEN
;
1949 *exclusion_name
= at
;
1956 * Sets the tracing_group variable with name.
1957 * This function allocates memory pointed to by tracing_group.
1958 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1960 int lttng_set_tracing_group(const char *name
)
1963 return -LTTNG_ERR_INVALID
;
1966 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1967 return -LTTNG_ERR_FATAL
;
1973 int lttng_calibrate(struct lttng_handle
*handle
,
1974 struct lttng_calibrate
*calibrate
)
1977 * This command was removed in LTTng 2.9.
1979 return -LTTNG_ERR_UND
;
1983 * Set default channel attributes.
1984 * If either or both of the arguments are null, attr content is zeroe'd.
1986 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1987 struct lttng_channel_attr
*attr
)
1990 if (attr
== NULL
|| domain
== NULL
) {
1994 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1996 /* Same for all domains. */
1997 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1998 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1999 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
2001 switch (domain
->type
) {
2002 case LTTNG_DOMAIN_KERNEL
:
2003 attr
->switch_timer_interval
=
2004 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
2005 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
2006 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
2007 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
2008 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
2010 case LTTNG_DOMAIN_UST
:
2011 switch (domain
->buf_type
) {
2012 case LTTNG_BUFFER_PER_UID
:
2013 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
2014 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
2015 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
2016 attr
->switch_timer_interval
=
2017 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
2018 attr
->read_timer_interval
=
2019 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
2021 case LTTNG_BUFFER_PER_PID
:
2023 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
2024 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
2025 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
2026 attr
->switch_timer_interval
=
2027 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
2028 attr
->read_timer_interval
=
2029 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
2033 /* Default behavior: leave set to 0. */
2038 int lttng_channel_get_discarded_event_count(struct lttng_channel
*channel
,
2039 uint64_t *discarded_events
)
2042 struct lttcomm_channel_extended
*chan_ext
;
2044 if (!channel
|| !discarded_events
) {
2045 ret
= -LTTNG_ERR_INVALID
;
2049 chan_ext
= channel
->attr
.extended
.ptr
;
2052 * This can happen since the lttng_channel structure is
2053 * used for other tasks where this pointer is never set.
2055 *discarded_events
= 0;
2059 *discarded_events
= chan_ext
->discarded_events
;
2064 int lttng_channel_get_lost_packet_count(struct lttng_channel
*channel
,
2065 uint64_t *lost_packets
)
2068 struct lttcomm_channel_extended
*chan_ext
;
2070 if (!channel
|| !lost_packets
) {
2071 ret
= -LTTNG_ERR_INVALID
;
2075 chan_ext
= channel
->attr
.extended
.ptr
;
2078 * This can happen since the lttng_channel structure is
2079 * used for other tasks where this pointer is never set.
2085 *lost_packets
= chan_ext
->lost_packets
;
2091 * Check if session daemon is alive.
2093 * Return 1 if alive or 0 if not.
2094 * On error returns a negative value.
2096 int lttng_session_daemon_alive(void)
2100 ret
= set_session_daemon_path();
2106 if (*sessiond_sock_path
== '\0') {
2108 * No socket path set. Weird error which means the constructor
2114 ret
= try_connect_sessiond(sessiond_sock_path
);
2125 * Set URL for a consumer for a session and domain.
2127 * Return 0 on success, else a negative value.
2129 int lttng_set_consumer_url(struct lttng_handle
*handle
,
2130 const char *control_url
, const char *data_url
)
2134 struct lttcomm_session_msg lsm
;
2135 struct lttng_uri
*uris
= NULL
;
2137 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
2138 return -LTTNG_ERR_INVALID
;
2141 memset(&lsm
, 0, sizeof(lsm
));
2143 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
2145 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2146 sizeof(lsm
.session
.name
));
2147 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2149 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
2151 return -LTTNG_ERR_INVALID
;
2154 lsm
.u
.uri
.size
= size
;
2156 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2157 sizeof(struct lttng_uri
) * size
, NULL
);
2166 int lttng_enable_consumer(struct lttng_handle
*handle
)
2174 int lttng_disable_consumer(struct lttng_handle
*handle
)
2180 * This is an extension of create session that is ONLY and SHOULD only be used
2181 * by the lttng command line program. It exists to avoid using URI parsing in
2184 * We need the date and time for the trace path subdirectory for the case where
2185 * the user does NOT define one using either -o or -U. Using the normal
2186 * lttng_create_session API call, we have no clue on the session daemon side if
2187 * the URL was generated automatically by the client or define by the user.
2189 * So this function "wrapper" is hidden from the public API, takes the datetime
2190 * string and appends it if necessary to the URI subdirectory before sending it
2191 * to the session daemon.
2193 * With this extra function, the lttng_create_session call behavior is not
2194 * changed and the timestamp is appended to the URI on the session daemon side
2197 int _lttng_create_session_ext(const char *name
, const char *url
,
2198 const char *datetime
)
2202 struct lttcomm_session_msg lsm
;
2203 struct lttng_uri
*uris
= NULL
;
2205 if (name
== NULL
|| datetime
== NULL
) {
2206 return -LTTNG_ERR_INVALID
;
2209 memset(&lsm
, 0, sizeof(lsm
));
2211 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
2212 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2214 /* There should never be a data URL. */
2215 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2217 ret
= -LTTNG_ERR_INVALID
;
2221 lsm
.u
.uri
.size
= size
;
2223 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
2224 /* Don't append datetime if the name was automatically created. */
2225 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
2226 strlen(DEFAULT_SESSION_NAME
) + 1)) {
2227 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
2230 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
2233 PERROR("snprintf uri subdir");
2234 ret
= -LTTNG_ERR_FATAL
;
2239 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2240 sizeof(struct lttng_uri
) * size
, NULL
);
2248 * For a given session name, this call checks if the data is ready to be read
2249 * or is still being extracted by the consumer(s) hence not ready to be used by
2252 int lttng_data_pending(const char *session_name
)
2255 struct lttcomm_session_msg lsm
;
2256 uint8_t *pending
= NULL
;
2258 if (session_name
== NULL
) {
2259 return -LTTNG_ERR_INVALID
;
2262 memset(&lsm
, 0, sizeof(lsm
));
2263 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
2265 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2266 sizeof(lsm
.session
.name
));
2268 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
2271 } else if (ret
!= 1) {
2272 /* Unexpected payload size */
2273 ret
= -LTTNG_ERR_INVALID
;
2277 ret
= (int) *pending
;
2284 * Create a session exclusively used for snapshot.
2286 * Returns LTTNG_OK on success or a negative error code.
2288 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
2292 struct lttcomm_session_msg lsm
;
2293 struct lttng_uri
*uris
= NULL
;
2296 return -LTTNG_ERR_INVALID
;
2299 memset(&lsm
, 0, sizeof(lsm
));
2301 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
2302 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2304 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
2306 return -LTTNG_ERR_INVALID
;
2309 lsm
.u
.uri
.size
= size
;
2311 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2312 sizeof(struct lttng_uri
) * size
, NULL
);
2319 * Create a session exclusively used for live.
2321 * Returns LTTNG_OK on success or a negative error code.
2323 int lttng_create_session_live(const char *name
, const char *url
,
2324 unsigned int timer_interval
)
2328 struct lttcomm_session_msg lsm
;
2329 struct lttng_uri
*uris
= NULL
;
2331 if (name
== NULL
|| timer_interval
== 0) {
2332 return -LTTNG_ERR_INVALID
;
2335 memset(&lsm
, 0, sizeof(lsm
));
2337 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
2338 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
2341 size
= uri_parse_str_urls(url
, NULL
, &uris
);
2343 ret
= -LTTNG_ERR_INVALID
;
2347 /* file:// is not accepted for live session. */
2348 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
2349 ret
= -LTTNG_ERR_INVALID
;
2356 lsm
.u
.session_live
.nb_uri
= size
;
2357 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2359 ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm
, uris
,
2360 sizeof(struct lttng_uri
) * size
, NULL
);
2368 * List IDs in the tracker.
2370 * tracker_type is the type of tracker.
2371 * ids is set to an allocated array of IDs currently tracked. On
2372 * success, ids and all the strings it contains must be freed by the caller.
2373 * nr_ids is set to the number of entries contained by the ids array.
2375 * Returns 0 on success, else a negative LTTng error code.
2377 int lttng_list_tracker_ids(struct lttng_handle
*handle
,
2378 enum lttng_tracker_type tracker_type
,
2379 struct lttng_tracker_id
**_ids
, size_t *_nr_ids
)
2382 struct lttcomm_session_msg lsm
;
2383 struct lttcomm_tracker_command_header
*cmd_header
= NULL
;
2384 char *cmd_payload
= NULL
, *p
;
2385 size_t cmd_header_len
;
2387 struct lttng_tracker_id
*ids
= NULL
;
2389 if (handle
== NULL
) {
2390 return -LTTNG_ERR_INVALID
;
2393 memset(&lsm
, 0, sizeof(lsm
));
2394 lsm
.cmd_type
= LTTNG_LIST_TRACKER_IDS
;
2395 lsm
.u
.id_tracker_list
.tracker_type
= tracker_type
;
2396 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2397 sizeof(lsm
.session
.name
));
2398 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2400 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, NULL
, 0, (void **) &cmd_payload
,
2401 (void **) &cmd_header
, &cmd_header_len
);
2406 /* Set number of tracker_id and free command header */
2407 nr_ids
= cmd_header
->nb_tracker_id
;
2408 if (nr_ids
> INT_MAX
) {
2409 ret
= -LTTNG_ERR_OVERFLOW
;
2415 ids
= zmalloc(sizeof(*ids
) * nr_ids
);
2417 ret
= -LTTNG_ERR_NOMEM
;
2422 for (i
= 0; i
< nr_ids
; i
++) {
2423 struct lttcomm_tracker_id_header
*tracker_id
;
2424 struct lttng_tracker_id
*id
;
2426 tracker_id
= (struct lttcomm_tracker_id_header
*) p
;
2427 p
+= sizeof(struct lttcomm_tracker_id_header
);
2430 id
->type
= tracker_id
->type
;
2431 switch (tracker_id
->type
) {
2434 case LTTNG_ID_VALUE
:
2435 id
->value
= tracker_id
->u
.value
;
2437 case LTTNG_ID_STRING
:
2438 id
->string
= strdup(p
);
2440 ret
= -LTTNG_ERR_NOMEM
;
2443 p
+= tracker_id
->u
.var_data_len
;
2456 for (i
= 0; i
< nr_ids
; i
++) {
2457 free(ids
[i
].string
);
2467 * List PIDs in the tracker.
2469 * enabled is set to whether the PID tracker is enabled.
2470 * pids is set to an allocated array of PIDs currently tracked. On
2471 * success, pids must be freed by the caller.
2472 * nr_pids is set to the number of entries contained by the pids array.
2474 * Returns 0 on success, else a negative LTTng error code.
2476 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2477 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2479 struct lttng_tracker_id
*ids
= NULL
;
2484 ret
= lttng_list_tracker_ids(handle
, LTTNG_TRACKER_PID
,
2489 if (nr_ids
== 1 && ids
[0].type
== LTTNG_ID_ALL
) {
2495 pids
= zmalloc(nr_ids
* sizeof(*pids
));
2497 ret
= -LTTNG_ERR_NOMEM
;
2500 for (i
= 0; i
< nr_ids
; i
++) {
2501 struct lttng_tracker_id
*id
= &ids
[i
];
2503 if (id
->type
!= LTTNG_ID_VALUE
) {
2504 ret
= -LTTNG_ERR_UNK
;
2507 pids
[i
] = id
->value
;
2512 for (i
= 0; i
< nr_ids
; i
++) {
2513 free(ids
[i
].string
);
2523 * Regenerate the metadata for a session.
2524 * Return 0 on success, a negative error code on error.
2526 int lttng_regenerate_metadata(const char *session_name
)
2529 struct lttcomm_session_msg lsm
;
2531 if (!session_name
) {
2532 ret
= -LTTNG_ERR_INVALID
;
2536 memset(&lsm
, 0, sizeof(lsm
));
2537 lsm
.cmd_type
= LTTNG_REGENERATE_METADATA
;
2539 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2540 sizeof(lsm
.session
.name
));
2542 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2553 * Deprecated, replaced by lttng_regenerate_metadata.
2555 int lttng_metadata_regenerate(const char *session_name
)
2557 return lttng_regenerate_metadata(session_name
);
2561 * Regenerate the statedump of a session.
2562 * Return 0 on success, a negative error code on error.
2564 int lttng_regenerate_statedump(const char *session_name
)
2567 struct lttcomm_session_msg lsm
;
2569 if (!session_name
) {
2570 ret
= -LTTNG_ERR_INVALID
;
2574 memset(&lsm
, 0, sizeof(lsm
));
2575 lsm
.cmd_type
= LTTNG_REGENERATE_STATEDUMP
;
2577 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
2578 sizeof(lsm
.session
.name
));
2580 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
2593 static void __attribute__((constructor
)) init(void)
2595 /* Set default session group */
2596 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2602 static void __attribute__((destructor
)) lttng_ctl_exit(void)
2604 free(tracing_group
);