4 * Linux Trace Toolkit Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/lttng.h>
37 #include <lttng/health-internal.h>
39 #include "filter/filter-ast.h"
40 #include "filter/filter-parser.h"
41 #include "filter/filter-bytecode.h"
42 #include "filter/memstream.h"
43 #include "lttng-ctl-helper.h"
46 static const int print_xml
= 1;
47 #define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
50 static const int print_xml
= 0;
51 #define dbg_printf(fmt, args...) \
53 /* do nothing but check printf format */ \
55 printf("[debug liblttnctl] " fmt, ## args); \
60 /* Socket to session daemon for communication */
61 static int sessiond_socket
;
62 static char sessiond_sock_path
[PATH_MAX
];
65 static char *tracing_group
;
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
76 int lttng_opt_verbose
;
80 * Copy string from src to dst and enforce null terminated byte.
83 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
86 strncpy(dst
, src
, len
);
87 /* Enforce the NULL terminated byte */
95 * Copy domain to lttcomm_session_msg domain.
97 * If domain is unknown, default domain will be the kernel.
100 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
101 struct lttng_domain
*src
)
105 case LTTNG_DOMAIN_KERNEL
:
106 case LTTNG_DOMAIN_UST
:
107 case LTTNG_DOMAIN_JUL
:
108 case LTTNG_DOMAIN_LOG4J
:
109 case LTTNG_DOMAIN_PYTHON
:
110 memcpy(dst
, src
, sizeof(struct lttng_domain
));
113 memset(dst
, 0, sizeof(struct lttng_domain
));
120 * Send lttcomm_session_msg to the session daemon.
122 * On success, returns the number of bytes sent (>=0)
123 * On error, returns -1
125 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
130 ret
= -LTTNG_ERR_NO_SESSIOND
;
134 DBG("LSM cmd type : %d", lsm
->cmd_type
);
136 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
137 sizeof(struct lttcomm_session_msg
));
139 ret
= -LTTNG_ERR_FATAL
;
147 * Send var len data to the session daemon.
149 * On success, returns the number of bytes sent (>=0)
150 * On error, returns -1
152 static int send_session_varlen(void *data
, size_t len
)
157 ret
= -LTTNG_ERR_NO_SESSIOND
;
166 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
168 ret
= -LTTNG_ERR_FATAL
;
176 * Receive data from the sessiond socket.
178 * On success, returns the number of bytes received (>=0)
179 * On error, returns -1 (recvmsg() error) or -ENOTCONN
181 static int recv_data_sessiond(void *buf
, size_t len
)
186 ret
= -LTTNG_ERR_NO_SESSIOND
;
190 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
192 ret
= -LTTNG_ERR_FATAL
;
200 * Check if we are in the specified group.
202 * If yes return 1, else return -1.
205 int lttng_check_tracing_group(void)
207 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
209 int grp_list_size
, grp_id
, i
;
211 const char *grp_name
= tracing_group
;
213 /* Get GID of group 'tracing' */
214 grp_tracing
= getgrnam(grp_name
);
216 /* If grp_tracing is NULL, the group does not exist. */
220 /* Get number of supplementary group IDs */
221 grp_list_size
= getgroups(0, NULL
);
222 if (grp_list_size
< 0) {
227 /* Alloc group list of the right size */
228 grp_list
= zmalloc(grp_list_size
* sizeof(gid_t
));
233 grp_id
= getgroups(grp_list_size
, grp_list
);
239 for (i
= 0; i
< grp_list_size
; i
++) {
240 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
254 * Try connect to session daemon with sock_path.
256 * Return 0 on success, else -1
258 static int try_connect_sessiond(const char *sock_path
)
262 /* If socket exist, we check if the daemon listens for connect. */
263 ret
= access(sock_path
, F_OK
);
269 ret
= lttcomm_connect_unix_sock(sock_path
);
275 ret
= lttcomm_close_unix_sock(ret
);
277 PERROR("lttcomm_close_unix_sock");
287 * Set sessiond socket path by putting it in the global sessiond_sock_path
290 * Returns 0 on success, negative value on failure (the sessiond socket path
291 * is somehow too long or ENOMEM).
293 static int set_session_daemon_path(void)
295 int in_tgroup
= 0; /* In tracing group. */
301 /* Are we in the tracing group ? */
302 in_tgroup
= lttng_check_tracing_group();
305 if ((uid
== 0) || in_tgroup
) {
306 lttng_ctl_copy_string(sessiond_sock_path
,
307 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
315 ret
= try_connect_sessiond(sessiond_sock_path
);
319 /* Global session daemon not available... */
321 /* ...or not in tracing group (and not root), default */
324 * With GNU C < 2.1, snprintf returns -1 if the target buffer
326 * With GNU C >= 2.1, snprintf returns the required size
327 * (excluding closing null)
329 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
330 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
331 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
343 * Connect to the LTTng session daemon.
345 * On success, return 0. On error, return -1.
347 static int connect_sessiond(void)
351 /* Don't try to connect if already connected. */
356 ret
= set_session_daemon_path();
361 /* Connect to the sesssion daemon. */
362 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
367 sessiond_socket
= ret
;
377 * Clean disconnect from the session daemon.
379 * On success, return 0. On error, return -1.
381 static int disconnect_sessiond(void)
386 ret
= lttcomm_close_unix_sock(sessiond_socket
);
395 * Ask the session daemon a specific command and put the data into buf.
396 * Takes extra var. len. data as input to send to the session daemon.
398 * Return size of data (only payload, not header) or a negative error code.
401 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
402 void *vardata
, size_t varlen
, void **buf
)
407 struct lttcomm_lttng_msg llm
;
409 ret
= connect_sessiond();
411 ret
= -LTTNG_ERR_NO_SESSIOND
;
415 /* Send command to session daemon */
416 ret
= send_session_msg(lsm
);
418 /* Ret value is a valid lttng error code. */
421 /* Send var len data */
422 ret
= send_session_varlen(vardata
, varlen
);
424 /* Ret value is a valid lttng error code. */
428 /* Get header from data transmission */
429 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
431 /* Ret value is a valid lttng error code. */
435 /* Check error code if OK */
436 if (llm
.ret_code
!= LTTNG_OK
) {
441 size
= llm
.data_size
;
443 /* If client free with size 0 */
451 data
= zmalloc(size
);
457 /* Get payload data */
458 ret
= recv_data_sessiond(data
, size
);
465 * Extra protection not to dereference a NULL pointer. If buf is NULL at
466 * this point, an error is returned and data is freed.
469 ret
= -LTTNG_ERR_INVALID
;
478 disconnect_sessiond();
483 * Create lttng handle and return pointer.
485 * The returned pointer will be NULL in case of malloc() error.
487 struct lttng_handle
*lttng_create_handle(const char *session_name
,
488 struct lttng_domain
*domain
)
490 struct lttng_handle
*handle
= NULL
;
492 if (domain
== NULL
) {
496 handle
= zmalloc(sizeof(struct lttng_handle
));
497 if (handle
== NULL
) {
498 PERROR("malloc handle");
502 /* Copy session name */
503 lttng_ctl_copy_string(handle
->session_name
, session_name
,
504 sizeof(handle
->session_name
));
506 /* Copy lttng domain */
507 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
514 * Destroy handle by free(3) the pointer.
516 void lttng_destroy_handle(struct lttng_handle
*handle
)
522 * Register an outside consumer.
524 * Returns size of returned session payload data or a negative error code.
526 int lttng_register_consumer(struct lttng_handle
*handle
,
527 const char *socket_path
)
529 struct lttcomm_session_msg lsm
;
531 if (handle
== NULL
|| socket_path
== NULL
) {
532 return -LTTNG_ERR_INVALID
;
535 memset(&lsm
, 0, sizeof(lsm
));
536 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
537 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
538 sizeof(lsm
.session
.name
));
539 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
541 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
,
542 sizeof(lsm
.u
.reg
.path
));
544 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
548 * Start tracing for all traces of the session.
550 * Returns size of returned session payload data or a negative error code.
552 int lttng_start_tracing(const char *session_name
)
554 struct lttcomm_session_msg lsm
;
556 if (session_name
== NULL
) {
557 return -LTTNG_ERR_INVALID
;
560 memset(&lsm
, 0, sizeof(lsm
));
561 lsm
.cmd_type
= LTTNG_START_TRACE
;
563 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
564 sizeof(lsm
.session
.name
));
566 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
570 * Stop tracing for all traces of the session.
572 static int _lttng_stop_tracing(const char *session_name
, int wait
)
575 struct lttcomm_session_msg lsm
;
577 if (session_name
== NULL
) {
578 return -LTTNG_ERR_INVALID
;
581 memset(&lsm
, 0, sizeof(lsm
));
582 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
584 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
585 sizeof(lsm
.session
.name
));
587 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
588 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
596 /* Check for data availability */
598 data_ret
= lttng_data_pending(session_name
);
600 /* Return the data available call error. */
606 * Data sleep time before retrying (in usec). Don't sleep if the
607 * call returned value indicates availability.
610 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
612 } while (data_ret
!= 0);
620 * Stop tracing and wait for data availability.
622 int lttng_stop_tracing(const char *session_name
)
624 return _lttng_stop_tracing(session_name
, 1);
628 * Stop tracing but _don't_ wait for data availability.
630 int lttng_stop_tracing_no_wait(const char *session_name
)
632 return _lttng_stop_tracing(session_name
, 0);
636 * Add context to a channel.
638 * If the given channel is NULL, add the contexts to all channels.
639 * The event_name param is ignored.
641 * Returns the size of the returned payload data or a negative error code.
643 int lttng_add_context(struct lttng_handle
*handle
,
644 struct lttng_event_context
*ctx
, const char *event_name
,
645 const char *channel_name
)
647 struct lttcomm_session_msg lsm
;
649 /* Safety check. Both are mandatory. */
650 if (handle
== NULL
|| ctx
== NULL
) {
651 return -LTTNG_ERR_INVALID
;
654 memset(&lsm
, 0, sizeof(lsm
));
656 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
658 /* If no channel name, send empty string. */
659 if (channel_name
== NULL
) {
660 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
661 sizeof(lsm
.u
.context
.channel_name
));
663 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
664 sizeof(lsm
.u
.context
.channel_name
));
667 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
669 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
671 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
672 sizeof(lsm
.session
.name
));
674 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
678 * Enable event(s) for a channel.
680 * If no event name is specified, all events are enabled.
681 * If no channel name is specified, the default 'channel0' is used.
683 * Returns size of returned session payload data or a negative error code.
685 int lttng_enable_event(struct lttng_handle
*handle
,
686 struct lttng_event
*ev
, const char *channel_name
)
688 return lttng_enable_event_with_exclusions(handle
, ev
, channel_name
,
693 * Create or enable an event with a filter expression.
695 * Return negative error value on error.
696 * Return size of returned session payload data if OK.
698 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
699 struct lttng_event
*event
, const char *channel_name
,
700 const char *filter_expression
)
702 return lttng_enable_event_with_exclusions(handle
, event
, channel_name
,
703 filter_expression
, 0, NULL
);
707 * Depending on the event, return a newly allocated agent filter expression or
708 * NULL if not applicable.
710 * An event with NO loglevel and the name is * will return NULL.
712 static char *set_agent_filter(const char *filter
, struct lttng_event
*ev
)
715 char *agent_filter
= NULL
;
719 /* Don't add filter for the '*' event. */
720 if (ev
->name
[0] != '*') {
722 err
= asprintf(&agent_filter
, "(%s) && (logger_name == \"%s\")", filter
,
725 err
= asprintf(&agent_filter
, "logger_name == \"%s\"", ev
->name
);
733 /* Add loglevel filtering if any for the JUL domain. */
734 if (ev
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
737 if (ev
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_RANGE
) {
743 if (filter
|| agent_filter
) {
746 err
= asprintf(&new_filter
, "(%s) && (int_loglevel %s %d)",
747 agent_filter
? agent_filter
: filter
, op
,
752 agent_filter
= new_filter
;
754 err
= asprintf(&agent_filter
, "int_loglevel %s %d", op
,
770 * Generate the filter bytecode from a given filter expression string. Put the
771 * newly allocated parser context in ctxp and populate the lsm object with the
774 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
776 static int generate_filter(char *filter_expression
,
777 struct lttcomm_session_msg
*lsm
, struct filter_parser_ctx
**ctxp
)
780 struct filter_parser_ctx
*ctx
= NULL
;
783 assert(filter_expression
);
788 * Casting const to non-const, as the underlying function will use it in
791 fmem
= lttng_fmemopen((void *) filter_expression
,
792 strlen(filter_expression
), "r");
794 fprintf(stderr
, "Error opening memory as stream\n");
795 ret
= -LTTNG_ERR_FILTER_NOMEM
;
798 ctx
= filter_parser_ctx_alloc(fmem
);
800 fprintf(stderr
, "Error allocating parser\n");
801 ret
= -LTTNG_ERR_FILTER_NOMEM
;
802 goto filter_alloc_error
;
804 ret
= filter_parser_ctx_append_ast(ctx
);
806 fprintf(stderr
, "Parse error\n");
807 ret
= -LTTNG_ERR_FILTER_INVAL
;
810 ret
= filter_visitor_set_parent(ctx
);
812 fprintf(stderr
, "Set parent error\n");
813 ret
= -LTTNG_ERR_FILTER_INVAL
;
817 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
820 fprintf(stderr
, "XML print error\n");
821 ret
= -LTTNG_ERR_FILTER_INVAL
;
826 dbg_printf("Generating IR... ");
828 ret
= filter_visitor_ir_generate(ctx
);
830 fprintf(stderr
, "Generate IR error\n");
831 ret
= -LTTNG_ERR_FILTER_INVAL
;
834 dbg_printf("done\n");
836 dbg_printf("Validating IR... ");
838 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
840 ret
= -LTTNG_ERR_FILTER_INVAL
;
843 /* Validate strings used as literals in the expression. */
844 ret
= filter_visitor_ir_validate_string(ctx
);
846 ret
= -LTTNG_ERR_FILTER_INVAL
;
849 dbg_printf("done\n");
851 dbg_printf("Generating bytecode... ");
853 ret
= filter_visitor_bytecode_generate(ctx
);
855 fprintf(stderr
, "Generate bytecode error\n");
856 ret
= -LTTNG_ERR_FILTER_INVAL
;
859 dbg_printf("done\n");
860 dbg_printf("Size of bytecode generated: %u bytes.\n",
861 bytecode_get_len(&ctx
->bytecode
->b
));
863 lsm
->u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
864 + bytecode_get_len(&ctx
->bytecode
->b
);
865 lsm
->u
.enable
.expression_len
= strlen(filter_expression
) + 1;
867 /* No need to keep the memory stream. */
868 if (fclose(fmem
) != 0) {
877 filter_parser_ctx_free(ctx
);
879 if (fclose(fmem
) != 0) {
887 * Enable event(s) for a channel, possibly with exclusions and a filter.
888 * If no event name is specified, all events are enabled.
889 * If no channel name is specified, the default name is used.
890 * If filter expression is not NULL, the filter is set for the event.
891 * If exclusion count is not zero, the exclusions are set for the event.
892 * Returns size of returned session payload data or a negative error code.
894 int lttng_enable_event_with_exclusions(struct lttng_handle
*handle
,
895 struct lttng_event
*ev
, const char *channel_name
,
896 const char *original_filter_expression
,
897 int exclusion_count
, char **exclusion_list
)
899 struct lttcomm_session_msg lsm
;
902 unsigned int free_filter_expression
= 0;
903 struct filter_parser_ctx
*ctx
= NULL
;
905 * Cast as non-const since we may replace the filter expression
906 * by a dynamically allocated string. Otherwise, the original
907 * string is not modified.
909 char *filter_expression
= (char *) original_filter_expression
;
911 if (handle
== NULL
|| ev
== NULL
) {
912 ret
= -LTTNG_ERR_INVALID
;
917 * Empty filter string will always be rejected by the parser
918 * anyway, so treat this corner-case early to eliminate
919 * lttng_fmemopen error for 0-byte allocation.
921 if (filter_expression
&& filter_expression
[0] == '\0') {
922 ret
= -LTTNG_ERR_INVALID
;
926 memset(&lsm
, 0, sizeof(lsm
));
928 /* If no channel name, send empty string. */
929 if (channel_name
== NULL
) {
930 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
931 sizeof(lsm
.u
.enable
.channel_name
));
933 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
934 sizeof(lsm
.u
.enable
.channel_name
));
937 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
938 if (ev
->name
[0] == '\0') {
939 /* Enable all events */
940 lttng_ctl_copy_string(ev
->name
, "*", sizeof(ev
->name
));
943 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
944 /* FIXME: copying non-packed struct to packed struct. */
945 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
947 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
948 sizeof(lsm
.session
.name
));
949 lsm
.u
.enable
.exclusion_count
= exclusion_count
;
950 lsm
.u
.enable
.bytecode_len
= 0;
953 * For the JUL domain, a filter is enforced except for the enable all
954 * event. This is done to avoid having the event in all sessions thus
955 * filtering by logger name.
957 if (exclusion_count
== 0 && filter_expression
== NULL
&&
958 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
959 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
960 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
965 * We have either a filter or some exclusions, so we need to set up
966 * a variable-length memory block from where to send the data.
969 /* Parse filter expression. */
970 if (filter_expression
!= NULL
|| handle
->domain
.type
== LTTNG_DOMAIN_JUL
971 || handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
972 || handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
973 if (handle
->domain
.type
== LTTNG_DOMAIN_JUL
||
974 handle
->domain
.type
== LTTNG_DOMAIN_LOG4J
||
975 handle
->domain
.type
== LTTNG_DOMAIN_PYTHON
) {
978 /* Setup JUL filter if needed. */
979 agent_filter
= set_agent_filter(filter_expression
, ev
);
981 if (!filter_expression
) {
983 * No JUL and no filter, just skip
990 * With an agent filter, the original filter has
991 * been added to it thus replace the filter
994 filter_expression
= agent_filter
;
995 free_filter_expression
= 1;
999 ret
= generate_filter(filter_expression
, &lsm
, &ctx
);
1005 varlen_data
= zmalloc(lsm
.u
.enable
.bytecode_len
1006 + lsm
.u
.enable
.expression_len
1007 + LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
1009 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1013 /* Put exclusion names first in the data. */
1014 while (exclusion_count
--) {
1015 strncpy(varlen_data
+ LTTNG_SYMBOL_NAME_LEN
* exclusion_count
,
1016 *(exclusion_list
+ exclusion_count
),
1017 LTTNG_SYMBOL_NAME_LEN
- 1);
1019 /* Add filter expression next. */
1020 if (lsm
.u
.enable
.expression_len
!= 0) {
1022 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
,
1024 lsm
.u
.enable
.expression_len
);
1026 /* Add filter bytecode next. */
1027 if (ctx
&& lsm
.u
.enable
.bytecode_len
!= 0) {
1029 + LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
1030 + lsm
.u
.enable
.expression_len
,
1032 lsm
.u
.enable
.bytecode_len
);
1035 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1036 (LTTNG_SYMBOL_NAME_LEN
* lsm
.u
.enable
.exclusion_count
) +
1037 lsm
.u
.enable
.bytecode_len
+ lsm
.u
.enable
.expression_len
,
1042 if (filter_expression
&& ctx
) {
1043 filter_bytecode_free(ctx
);
1044 filter_ir_free(ctx
);
1045 filter_parser_ctx_free(ctx
);
1048 if (free_filter_expression
) {
1050 * The filter expression has been replaced and must be freed as
1051 * it is not the original filter expression received as a
1054 free(filter_expression
);
1058 * Return directly to the caller and don't ask the sessiond since
1059 * something went wrong in the parsing of data above.
1064 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1068 int lttng_disable_event_ext(struct lttng_handle
*handle
,
1069 struct lttng_event
*ev
, const char *channel_name
,
1070 const char *original_filter_expression
)
1072 struct lttcomm_session_msg lsm
;
1075 unsigned int free_filter_expression
= 0;
1076 struct filter_parser_ctx
*ctx
= NULL
;
1078 * Cast as non-const since we may replace the filter expression
1079 * by a dynamically allocated string. Otherwise, the original
1080 * string is not modified.
1082 char *filter_expression
= (char *) original_filter_expression
;
1084 if (handle
== NULL
|| ev
== NULL
) {
1085 ret
= -LTTNG_ERR_INVALID
;
1090 * Empty filter string will always be rejected by the parser
1091 * anyway, so treat this corner-case early to eliminate
1092 * lttng_fmemopen error for 0-byte allocation.
1094 if (filter_expression
&& filter_expression
[0] == '\0') {
1095 ret
= -LTTNG_ERR_INVALID
;
1099 memset(&lsm
, 0, sizeof(lsm
));
1101 /* If no channel name, send empty string. */
1102 if (channel_name
== NULL
) {
1103 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
1104 sizeof(lsm
.u
.disable
.channel_name
));
1106 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
1107 sizeof(lsm
.u
.disable
.channel_name
));
1110 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
1112 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1113 /* FIXME: copying non-packed struct to packed struct. */
1114 memcpy(&lsm
.u
.disable
.event
, ev
, sizeof(lsm
.u
.disable
.event
));
1116 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1117 sizeof(lsm
.session
.name
));
1118 lsm
.u
.disable
.bytecode_len
= 0;
1121 * For the JUL domain, a filter is enforced except for the
1122 * disable all event. This is done to avoid having the event in
1123 * all sessions thus filtering by logger name.
1125 if (filter_expression
== NULL
&&
1126 (handle
->domain
.type
!= LTTNG_DOMAIN_JUL
&&
1127 handle
->domain
.type
!= LTTNG_DOMAIN_LOG4J
&&
1128 handle
->domain
.type
!= LTTNG_DOMAIN_PYTHON
)) {
1133 * We have a filter, so we need to set up a variable-length
1134 * memory block from where to send the data.
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 a JUL 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 varlen_data
= zmalloc(lsm
.u
.disable
.bytecode_len
1174 + lsm
.u
.disable
.expression_len
);
1176 ret
= -LTTNG_ERR_EXCLUSION_NOMEM
;
1180 /* Add filter expression. */
1181 if (lsm
.u
.disable
.expression_len
!= 0) {
1184 lsm
.u
.disable
.expression_len
);
1186 /* Add filter bytecode next. */
1187 if (ctx
&& lsm
.u
.disable
.bytecode_len
!= 0) {
1189 + lsm
.u
.disable
.expression_len
,
1191 lsm
.u
.disable
.bytecode_len
);
1194 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, varlen_data
,
1195 lsm
.u
.disable
.bytecode_len
+ lsm
.u
.disable
.expression_len
, NULL
);
1199 if (filter_expression
&& ctx
) {
1200 filter_bytecode_free(ctx
);
1201 filter_ir_free(ctx
);
1202 filter_parser_ctx_free(ctx
);
1205 if (free_filter_expression
) {
1207 * The filter expression has been replaced and must be freed as
1208 * it is not the original filter expression received as a
1211 free(filter_expression
);
1215 * Return directly to the caller and don't ask the sessiond since
1216 * something went wrong in the parsing of data above.
1221 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1226 * Disable event(s) of a channel and domain.
1227 * If no event name is specified, all events are disabled.
1228 * If no channel name is specified, the default 'channel0' is used.
1229 * Returns size of returned session payload data or a negative error code.
1231 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
1232 const char *channel_name
)
1234 struct lttng_event ev
;
1236 memset(&ev
, 0, sizeof(ev
));
1238 ev
.type
= LTTNG_EVENT_ALL
;
1239 lttng_ctl_copy_string(ev
.name
, name
, sizeof(ev
.name
));
1240 return lttng_disable_event_ext(handle
, &ev
, channel_name
, NULL
);
1244 * Enable channel per domain
1245 * Returns size of returned session payload data or a negative error code.
1247 int lttng_enable_channel(struct lttng_handle
*handle
,
1248 struct lttng_channel
*chan
)
1250 struct lttcomm_session_msg lsm
;
1252 /* NULL arguments are forbidden. No default values. */
1253 if (handle
== NULL
|| chan
== NULL
) {
1254 return -LTTNG_ERR_INVALID
;
1257 memset(&lsm
, 0, sizeof(lsm
));
1259 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
1261 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
1263 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1265 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1266 sizeof(lsm
.session
.name
));
1268 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1272 * All tracing will be stopped for registered events of the channel.
1273 * Returns size of returned session payload data or a negative error code.
1275 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
1277 struct lttcomm_session_msg lsm
;
1279 /* Safety check. Both are mandatory. */
1280 if (handle
== NULL
|| name
== NULL
) {
1281 return -LTTNG_ERR_INVALID
;
1284 memset(&lsm
, 0, sizeof(lsm
));
1286 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
1288 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
1289 sizeof(lsm
.u
.disable
.channel_name
));
1291 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1293 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1294 sizeof(lsm
.session
.name
));
1296 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1300 * Add PID to session tracker.
1301 * Return 0 on success else a negative LTTng error code.
1303 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
1305 struct lttcomm_session_msg lsm
;
1307 /* NULL arguments are forbidden. No default values. */
1308 if (handle
== NULL
) {
1309 return -LTTNG_ERR_INVALID
;
1312 memset(&lsm
, 0, sizeof(lsm
));
1314 lsm
.cmd_type
= LTTNG_TRACK_PID
;
1315 lsm
.u
.pid_tracker
.pid
= pid
;
1317 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1319 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1320 sizeof(lsm
.session
.name
));
1322 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1326 * Remove PID from session tracker.
1327 * Return 0 on success else a negative LTTng error code.
1329 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
1331 struct lttcomm_session_msg lsm
;
1333 /* NULL arguments are forbidden. No default values. */
1334 if (handle
== NULL
) {
1335 return -LTTNG_ERR_INVALID
;
1338 memset(&lsm
, 0, sizeof(lsm
));
1340 lsm
.cmd_type
= LTTNG_UNTRACK_PID
;
1341 lsm
.u
.pid_tracker
.pid
= pid
;
1343 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1345 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1346 sizeof(lsm
.session
.name
));
1348 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1352 * Lists all available tracepoints of domain.
1353 * Sets the contents of the events array.
1354 * Returns the number of lttng_event entries in events;
1355 * on error, returns a negative value.
1357 int lttng_list_tracepoints(struct lttng_handle
*handle
,
1358 struct lttng_event
**events
)
1361 struct lttcomm_session_msg lsm
;
1363 if (handle
== NULL
) {
1364 return -LTTNG_ERR_INVALID
;
1367 memset(&lsm
, 0, sizeof(lsm
));
1368 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1369 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1371 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1376 return ret
/ sizeof(struct lttng_event
);
1380 * Lists all available tracepoint fields of domain.
1381 * Sets the contents of the event field array.
1382 * Returns the number of lttng_event_field entries in events;
1383 * on error, returns a negative value.
1385 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1386 struct lttng_event_field
**fields
)
1389 struct lttcomm_session_msg lsm
;
1391 if (handle
== NULL
) {
1392 return -LTTNG_ERR_INVALID
;
1395 memset(&lsm
, 0, sizeof(lsm
));
1396 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1397 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1399 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1404 return ret
/ sizeof(struct lttng_event_field
);
1408 * Lists all available kernel system calls. Allocates and sets the contents of
1411 * Returns the number of lttng_event entries in events; on error, returns a
1414 int lttng_list_syscalls(struct lttng_event
**events
)
1417 struct lttcomm_session_msg lsm
;
1420 return -LTTNG_ERR_INVALID
;
1423 memset(&lsm
, 0, sizeof(lsm
));
1424 lsm
.cmd_type
= LTTNG_LIST_SYSCALLS
;
1425 /* Force kernel domain for system calls. */
1426 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
1428 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
1433 return ret
/ sizeof(struct lttng_event
);
1437 * Returns a human readable string describing
1438 * the error code (a negative value).
1440 const char *lttng_strerror(int code
)
1442 return error_get_str(code
);
1446 * Create a brand new session using name and url for destination.
1448 * Returns LTTNG_OK on success or a negative error code.
1450 int lttng_create_session(const char *name
, const char *url
)
1454 struct lttcomm_session_msg lsm
;
1455 struct lttng_uri
*uris
= NULL
;
1458 return -LTTNG_ERR_INVALID
;
1461 memset(&lsm
, 0, sizeof(lsm
));
1463 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1464 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1466 /* There should never be a data URL */
1467 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1469 return -LTTNG_ERR_INVALID
;
1472 lsm
.u
.uri
.size
= size
;
1474 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1475 sizeof(struct lttng_uri
) * size
, NULL
);
1482 * Destroy session using name.
1483 * Returns size of returned session payload data or a negative error code.
1485 int lttng_destroy_session(const char *session_name
)
1487 struct lttcomm_session_msg lsm
;
1489 if (session_name
== NULL
) {
1490 return -LTTNG_ERR_INVALID
;
1493 memset(&lsm
, 0, sizeof(lsm
));
1494 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1496 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1497 sizeof(lsm
.session
.name
));
1499 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1503 * Ask the session daemon for all available sessions.
1504 * Sets the contents of the sessions array.
1505 * Returns the number of lttng_session entries in sessions;
1506 * on error, returns a negative value.
1508 int lttng_list_sessions(struct lttng_session
**sessions
)
1511 struct lttcomm_session_msg lsm
;
1513 memset(&lsm
, 0, sizeof(lsm
));
1514 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1515 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1520 return ret
/ sizeof(struct lttng_session
);
1523 int lttng_set_session_shm_path(const char *session_name
,
1524 const char *shm_path
)
1526 struct lttcomm_session_msg lsm
;
1528 if (session_name
== NULL
) {
1529 return -LTTNG_ERR_INVALID
;
1532 memset(&lsm
, 0, sizeof(lsm
));
1533 lsm
.cmd_type
= LTTNG_SET_SESSION_SHM_PATH
;
1535 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1536 sizeof(lsm
.session
.name
));
1537 lttng_ctl_copy_string(lsm
.u
.set_shm_path
.shm_path
, shm_path
,
1538 sizeof(lsm
.u
.set_shm_path
.shm_path
));
1540 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1544 * Ask the session daemon for all available domains of a session.
1545 * Sets the contents of the domains array.
1546 * Returns the number of lttng_domain entries in domains;
1547 * on error, returns a negative value.
1549 int lttng_list_domains(const char *session_name
,
1550 struct lttng_domain
**domains
)
1553 struct lttcomm_session_msg lsm
;
1555 if (session_name
== NULL
) {
1556 return -LTTNG_ERR_INVALID
;
1559 memset(&lsm
, 0, sizeof(lsm
));
1560 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1562 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1563 sizeof(lsm
.session
.name
));
1565 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1570 return ret
/ sizeof(struct lttng_domain
);
1574 * Ask the session daemon for all available channels of a session.
1575 * Sets the contents of the channels array.
1576 * Returns the number of lttng_channel entries in channels;
1577 * on error, returns a negative value.
1579 int lttng_list_channels(struct lttng_handle
*handle
,
1580 struct lttng_channel
**channels
)
1583 struct lttcomm_session_msg lsm
;
1585 if (handle
== NULL
) {
1586 return -LTTNG_ERR_INVALID
;
1589 memset(&lsm
, 0, sizeof(lsm
));
1590 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1591 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1592 sizeof(lsm
.session
.name
));
1594 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1596 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1601 return ret
/ sizeof(struct lttng_channel
);
1605 * Ask the session daemon for all available events of a session channel.
1606 * Sets the contents of the events array.
1607 * Returns the number of lttng_event entries in events;
1608 * on error, returns a negative value.
1610 int lttng_list_events(struct lttng_handle
*handle
,
1611 const char *channel_name
, struct lttng_event
**events
)
1614 struct lttcomm_session_msg lsm
;
1616 /* Safety check. An handle and channel name are mandatory */
1617 if (handle
== NULL
|| channel_name
== NULL
) {
1618 return -LTTNG_ERR_INVALID
;
1621 memset(&lsm
, 0, sizeof(lsm
));
1622 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1623 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1624 sizeof(lsm
.session
.name
));
1625 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1626 sizeof(lsm
.u
.list
.channel_name
));
1628 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1630 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1635 return ret
/ sizeof(struct lttng_event
);
1639 * Sets the tracing_group variable with name.
1640 * This function allocates memory pointed to by tracing_group.
1641 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1643 int lttng_set_tracing_group(const char *name
)
1646 return -LTTNG_ERR_INVALID
;
1649 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1650 return -LTTNG_ERR_FATAL
;
1657 * Returns size of returned session payload data or a negative error code.
1659 int lttng_calibrate(struct lttng_handle
*handle
,
1660 struct lttng_calibrate
*calibrate
)
1662 struct lttcomm_session_msg lsm
;
1664 /* Safety check. NULL pointer are forbidden */
1665 if (handle
== NULL
|| calibrate
== NULL
) {
1666 return -LTTNG_ERR_INVALID
;
1669 memset(&lsm
, 0, sizeof(lsm
));
1670 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1671 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1673 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1675 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1679 * Set default channel attributes.
1680 * If either or both of the arguments are null, attr content is zeroe'd.
1682 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1683 struct lttng_channel_attr
*attr
)
1686 if (attr
== NULL
|| domain
== NULL
) {
1690 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1692 /* Same for all domains. */
1693 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1694 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1695 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1697 switch (domain
->type
) {
1698 case LTTNG_DOMAIN_KERNEL
:
1699 attr
->switch_timer_interval
=
1700 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1701 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1702 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1703 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1704 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1706 case LTTNG_DOMAIN_UST
:
1707 switch (domain
->buf_type
) {
1708 case LTTNG_BUFFER_PER_UID
:
1709 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1710 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1711 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1712 attr
->switch_timer_interval
=
1713 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1714 attr
->read_timer_interval
=
1715 DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1717 case LTTNG_BUFFER_PER_PID
:
1719 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1720 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1721 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1722 attr
->switch_timer_interval
=
1723 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1724 attr
->read_timer_interval
=
1725 DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1729 /* Default behavior: leave set to 0. */
1735 * Check if session daemon is alive.
1737 * Return 1 if alive or 0 if not.
1738 * On error returns a negative value.
1740 int lttng_session_daemon_alive(void)
1744 ret
= set_session_daemon_path();
1750 if (*sessiond_sock_path
== '\0') {
1752 * No socket path set. Weird error which means the constructor
1758 ret
= try_connect_sessiond(sessiond_sock_path
);
1769 * Set URL for a consumer for a session and domain.
1771 * Return 0 on success, else a negative value.
1773 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1774 const char *control_url
, const char *data_url
)
1778 struct lttcomm_session_msg lsm
;
1779 struct lttng_uri
*uris
= NULL
;
1781 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1782 return -LTTNG_ERR_INVALID
;
1785 memset(&lsm
, 0, sizeof(lsm
));
1787 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1789 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1790 sizeof(lsm
.session
.name
));
1791 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1793 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1795 return -LTTNG_ERR_INVALID
;
1798 lsm
.u
.uri
.size
= size
;
1800 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1801 sizeof(struct lttng_uri
) * size
, NULL
);
1810 int lttng_enable_consumer(struct lttng_handle
*handle
)
1818 int lttng_disable_consumer(struct lttng_handle
*handle
)
1824 * This is an extension of create session that is ONLY and SHOULD only be used
1825 * by the lttng command line program. It exists to avoid using URI parsing in
1828 * We need the date and time for the trace path subdirectory for the case where
1829 * the user does NOT define one using either -o or -U. Using the normal
1830 * lttng_create_session API call, we have no clue on the session daemon side if
1831 * the URL was generated automatically by the client or define by the user.
1833 * So this function "wrapper" is hidden from the public API, takes the datetime
1834 * string and appends it if necessary to the URI subdirectory before sending it
1835 * to the session daemon.
1837 * With this extra function, the lttng_create_session call behavior is not
1838 * changed and the timestamp is appended to the URI on the session daemon side
1841 int _lttng_create_session_ext(const char *name
, const char *url
,
1842 const char *datetime
)
1846 struct lttcomm_session_msg lsm
;
1847 struct lttng_uri
*uris
= NULL
;
1849 if (name
== NULL
|| datetime
== NULL
) {
1850 return -LTTNG_ERR_INVALID
;
1853 memset(&lsm
, 0, sizeof(lsm
));
1855 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1856 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1858 /* There should never be a data URL. */
1859 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1861 ret
= -LTTNG_ERR_INVALID
;
1865 lsm
.u
.uri
.size
= size
;
1867 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1868 /* Don't append datetime if the name was automatically created. */
1869 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1870 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1871 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1874 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1877 PERROR("snprintf uri subdir");
1878 ret
= -LTTNG_ERR_FATAL
;
1883 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1884 sizeof(struct lttng_uri
) * size
, NULL
);
1892 * For a given session name, this call checks if the data is ready to be read
1893 * or is still being extracted by the consumer(s) hence not ready to be used by
1896 int lttng_data_pending(const char *session_name
)
1899 struct lttcomm_session_msg lsm
;
1900 uint8_t *pending
= NULL
;
1902 if (session_name
== NULL
) {
1903 return -LTTNG_ERR_INVALID
;
1906 memset(&lsm
, 0, sizeof(lsm
));
1907 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1909 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1910 sizeof(lsm
.session
.name
));
1912 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pending
);
1915 } else if (ret
!= 1) {
1916 /* Unexpected payload size */
1917 ret
= -LTTNG_ERR_INVALID
;
1921 ret
= (int) *pending
;
1928 * Create a session exclusively used for snapshot.
1930 * Returns LTTNG_OK on success or a negative error code.
1932 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1936 struct lttcomm_session_msg lsm
;
1937 struct lttng_uri
*uris
= NULL
;
1940 return -LTTNG_ERR_INVALID
;
1943 memset(&lsm
, 0, sizeof(lsm
));
1945 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1946 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1948 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1950 return -LTTNG_ERR_INVALID
;
1953 lsm
.u
.uri
.size
= size
;
1955 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1956 sizeof(struct lttng_uri
) * size
, NULL
);
1963 * Create a session exclusively used for live.
1965 * Returns LTTNG_OK on success or a negative error code.
1967 int lttng_create_session_live(const char *name
, const char *url
,
1968 unsigned int timer_interval
)
1972 struct lttcomm_session_msg lsm
;
1973 struct lttng_uri
*uris
= NULL
;
1975 if (name
== NULL
|| timer_interval
== 0) {
1976 return -LTTNG_ERR_INVALID
;
1979 memset(&lsm
, 0, sizeof(lsm
));
1981 lsm
.cmd_type
= LTTNG_CREATE_SESSION_LIVE
;
1982 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1985 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1987 ret
= -LTTNG_ERR_INVALID
;
1991 /* file:// is not accepted for live session. */
1992 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
1993 ret
= -LTTNG_ERR_INVALID
;
2000 lsm
.u
.session_live
.nb_uri
= size
;
2001 lsm
.u
.session_live
.timer_interval
= timer_interval
;
2003 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
2004 sizeof(struct lttng_uri
) * size
, NULL
);
2012 * List PIDs in the tracker.
2014 * enabled is set to whether the PID tracker is enabled.
2015 * pids is set to an allocated array of PIDs currently tracked. On
2016 * success, pids must be freed by the caller.
2017 * nr_pids is set to the number of entries contained by the pids array.
2019 * Returns 0 on success, else a negative LTTng error code.
2021 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
2022 int *_enabled
, int32_t **_pids
, size_t *_nr_pids
)
2026 struct lttcomm_session_msg lsm
;
2030 if (handle
== NULL
) {
2031 return -LTTNG_ERR_INVALID
;
2034 memset(&lsm
, 0, sizeof(lsm
));
2035 lsm
.cmd_type
= LTTNG_LIST_TRACKER_PIDS
;
2036 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
2037 sizeof(lsm
.session
.name
));
2038 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
2040 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &pids
);
2044 nr_pids
= ret
/ sizeof(int32_t);
2045 if (nr_pids
== 1 && pids
[0] == -1) {
2051 *_enabled
= enabled
;
2053 *_nr_pids
= nr_pids
;
2060 static void __attribute__((constructor
)) init()
2062 /* Set default session group */
2063 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
2069 static void __attribute__((destructor
)) lttng_ctl_exit()
2071 free(tracing_group
);