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>
38 #include "filter/filter-ast.h"
39 #include "filter/filter-parser.h"
40 #include "filter/filter-bytecode.h"
41 #include "filter/memstream.h"
42 #include "lttng-ctl-helper.h"
45 static const int print_xml
= 1;
46 #define dbg_printf(fmt, args...) \
47 printf("[debug liblttng-ctl] " fmt, ## args)
49 static const int print_xml
= 0;
50 #define dbg_printf(fmt, args...) \
52 /* do nothing but check printf format */ \
54 printf("[debug liblttnctl] " fmt, ## args); \
59 /* Socket to session daemon for communication */
60 static int sessiond_socket
;
61 static char sessiond_sock_path
[PATH_MAX
];
62 static char health_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
;
79 * Copy string from src to dst and enforce null terminated byte.
82 void lttng_ctl_copy_string(char *dst
, const char *src
, size_t len
)
85 strncpy(dst
, src
, len
);
86 /* Enforce the NULL terminated byte */
94 * Copy domain to lttcomm_session_msg domain.
96 * If domain is unknown, default domain will be the kernel.
99 void lttng_ctl_copy_lttng_domain(struct lttng_domain
*dst
,
100 struct lttng_domain
*src
)
104 case LTTNG_DOMAIN_KERNEL
:
105 case LTTNG_DOMAIN_UST
:
106 memcpy(dst
, src
, sizeof(struct lttng_domain
));
109 memset(dst
, 0, sizeof(struct lttng_domain
));
116 * Send lttcomm_session_msg to the session daemon.
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
121 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
126 ret
= -LTTNG_ERR_NO_SESSIOND
;
130 DBG("LSM cmd type : %d", lsm
->cmd_type
);
132 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
133 sizeof(struct lttcomm_session_msg
));
135 ret
= -LTTNG_ERR_FATAL
;
143 * Send var len data to the session daemon.
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
148 static int send_session_varlen(void *data
, size_t len
)
153 ret
= -LTTNG_ERR_NO_SESSIOND
;
162 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
164 ret
= -LTTNG_ERR_FATAL
;
172 * Receive data from the sessiond socket.
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
177 static int recv_data_sessiond(void *buf
, size_t len
)
182 ret
= -LTTNG_ERR_NO_SESSIOND
;
186 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
188 ret
= -LTTNG_ERR_FATAL
;
196 * Check if we are in the specified group.
198 * If yes return 1, else return -1.
200 static int check_tracing_group(const char *grp_name
)
202 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
204 int grp_list_size
, grp_id
, i
;
207 /* Get GID of group 'tracing' */
208 grp_tracing
= getgrnam(grp_name
);
210 /* If grp_tracing is NULL, the group does not exist. */
214 /* Get number of supplementary group IDs */
215 grp_list_size
= getgroups(0, NULL
);
216 if (grp_list_size
< 0) {
221 /* Alloc group list of the right size */
222 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
227 grp_id
= getgroups(grp_list_size
, grp_list
);
233 for (i
= 0; i
< grp_list_size
; i
++) {
234 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
248 * Try connect to session daemon with sock_path.
250 * Return 0 on success, else -1
252 static int try_connect_sessiond(const char *sock_path
)
256 /* If socket exist, we check if the daemon listens for connect. */
257 ret
= access(sock_path
, F_OK
);
263 ret
= lttcomm_connect_unix_sock(sock_path
);
269 ret
= lttcomm_close_unix_sock(ret
);
271 perror("lttcomm_close_unix_sock");
281 * Set sessiond socket path by putting it in the global sessiond_sock_path
284 * Returns 0 on success, negative value on failure (the sessiond socket path
285 * is somehow too long or ENOMEM).
287 static int set_session_daemon_path(void)
289 int in_tgroup
= 0; /* In tracing group */
295 /* Are we in the tracing group ? */
296 in_tgroup
= check_tracing_group(tracing_group
);
299 if ((uid
== 0) || in_tgroup
) {
300 lttng_ctl_copy_string(sessiond_sock_path
,
301 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
, sizeof(sessiond_sock_path
));
309 ret
= try_connect_sessiond(sessiond_sock_path
);
313 /* Global session daemon not available... */
315 /* ...or not in tracing group (and not root), default */
318 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
319 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
321 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
322 DEFAULT_HOME_CLIENT_UNIX_SOCK
, utils_get_home_dir());
323 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
335 * Connect to the LTTng session daemon.
337 * On success, return 0. On error, return -1.
339 static int connect_sessiond(void)
343 /* Don't try to connect if already connected. */
348 ret
= set_session_daemon_path();
353 /* Connect to the sesssion daemon */
354 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
359 sessiond_socket
= ret
;
369 * Clean disconnect from the session daemon.
370 * On success, return 0. On error, return -1.
372 static int disconnect_sessiond(void)
377 ret
= lttcomm_close_unix_sock(sessiond_socket
);
386 * Ask the session daemon a specific command and put the data into buf.
387 * Takes extra var. len. data as input to send to the session daemon.
389 * Return size of data (only payload, not header) or a negative error code.
392 int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
393 void *vardata
, size_t varlen
, void **buf
)
398 struct lttcomm_lttng_msg llm
;
400 ret
= connect_sessiond();
402 ret
= -LTTNG_ERR_NO_SESSIOND
;
406 /* Send command to session daemon */
407 ret
= send_session_msg(lsm
);
409 /* Ret value is a valid lttng error code. */
412 /* Send var len data */
413 ret
= send_session_varlen(vardata
, varlen
);
415 /* Ret value is a valid lttng error code. */
419 /* Get header from data transmission */
420 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
422 /* Ret value is a valid lttng error code. */
426 /* Check error code if OK */
427 if (llm
.ret_code
!= LTTNG_OK
) {
432 size
= llm
.data_size
;
434 /* If client free with size 0 */
442 data
= (void*) malloc(size
);
444 /* Get payload data */
445 ret
= recv_data_sessiond(data
, size
);
452 * Extra protection not to dereference a NULL pointer. If buf is NULL at
453 * this point, an error is returned and data is freed.
456 ret
= -LTTNG_ERR_INVALID
;
465 disconnect_sessiond();
470 * Create lttng handle and return pointer.
471 * The returned pointer will be NULL in case of malloc() error.
473 struct lttng_handle
*lttng_create_handle(const char *session_name
,
474 struct lttng_domain
*domain
)
476 struct lttng_handle
*handle
= NULL
;
478 if (domain
== NULL
) {
482 handle
= malloc(sizeof(struct lttng_handle
));
483 if (handle
== NULL
) {
484 PERROR("malloc handle");
488 /* Copy session name */
489 lttng_ctl_copy_string(handle
->session_name
, session_name
,
490 sizeof(handle
->session_name
));
492 /* Copy lttng domain */
493 lttng_ctl_copy_lttng_domain(&handle
->domain
, domain
);
500 * Destroy handle by free(3) the pointer.
502 void lttng_destroy_handle(struct lttng_handle
*handle
)
508 * Register an outside consumer.
509 * Returns size of returned session payload data or a negative error code.
511 int lttng_register_consumer(struct lttng_handle
*handle
,
512 const char *socket_path
)
514 struct lttcomm_session_msg lsm
;
516 if (handle
== NULL
|| socket_path
== NULL
) {
517 return -LTTNG_ERR_INVALID
;
520 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
521 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
522 sizeof(lsm
.session
.name
));
523 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
525 lttng_ctl_copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
527 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
531 * Start tracing for all traces of the session.
532 * Returns size of returned session payload data or a negative error code.
534 int lttng_start_tracing(const char *session_name
)
536 struct lttcomm_session_msg lsm
;
538 if (session_name
== NULL
) {
539 return -LTTNG_ERR_INVALID
;
542 lsm
.cmd_type
= LTTNG_START_TRACE
;
544 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
545 sizeof(lsm
.session
.name
));
547 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
551 * Stop tracing for all traces of the session.
553 static int _lttng_stop_tracing(const char *session_name
, int wait
)
556 struct lttcomm_session_msg lsm
;
558 if (session_name
== NULL
) {
559 return -LTTNG_ERR_INVALID
;
562 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
564 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
565 sizeof(lsm
.session
.name
));
567 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
568 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
576 _MSG("Waiting for data availability");
578 /* Check for data availability */
580 data_ret
= lttng_data_pending(session_name
);
582 /* Return the data available call error. */
588 * Data sleep time before retrying (in usec). Don't sleep if the call
589 * returned value indicates availability.
592 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
595 } while (data_ret
!= 0);
605 * Stop tracing and wait for data availability.
607 int lttng_stop_tracing(const char *session_name
)
609 return _lttng_stop_tracing(session_name
, 1);
613 * Stop tracing but _don't_ wait for data availability.
615 int lttng_stop_tracing_no_wait(const char *session_name
)
617 return _lttng_stop_tracing(session_name
, 0);
621 * Add context to a channel.
623 * If the given channel is NULL, add the contexts to all channels.
624 * The event_name param is ignored.
626 * Returns the size of the returned payload data or a negative error code.
628 int lttng_add_context(struct lttng_handle
*handle
,
629 struct lttng_event_context
*ctx
, const char *event_name
,
630 const char *channel_name
)
632 struct lttcomm_session_msg lsm
;
634 /* Safety check. Both are mandatory */
635 if (handle
== NULL
|| ctx
== NULL
) {
636 return -LTTNG_ERR_INVALID
;
639 memset(&lsm
, 0, sizeof(lsm
));
641 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
643 /* If no channel name, send empty string. */
644 if (channel_name
== NULL
) {
645 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, "",
646 sizeof(lsm
.u
.context
.channel_name
));
648 lttng_ctl_copy_string(lsm
.u
.context
.channel_name
, channel_name
,
649 sizeof(lsm
.u
.context
.channel_name
));
652 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
654 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
656 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
657 sizeof(lsm
.session
.name
));
659 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
663 * Enable event(s) for a channel.
664 * If no event name is specified, all events are enabled.
665 * If no channel name is specified, the default 'channel0' is used.
666 * Returns size of returned session payload data or a negative error code.
668 int lttng_enable_event(struct lttng_handle
*handle
,
669 struct lttng_event
*ev
, const char *channel_name
)
671 struct lttcomm_session_msg lsm
;
673 if (handle
== NULL
|| ev
== NULL
) {
674 return -LTTNG_ERR_INVALID
;
677 memset(&lsm
, 0, sizeof(lsm
));
679 /* If no channel name, send empty string. */
680 if (channel_name
== NULL
) {
681 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
682 sizeof(lsm
.u
.enable
.channel_name
));
684 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
685 sizeof(lsm
.u
.enable
.channel_name
));
688 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
690 if (ev
->name
[0] != '\0') {
691 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
693 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
695 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
697 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
698 sizeof(lsm
.session
.name
));
700 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
704 * Create or enable an event with a filter expression.
706 * Return negative error value on error.
707 * Return size of returned session payload data if OK.
709 int lttng_enable_event_with_filter(struct lttng_handle
*handle
,
710 struct lttng_event
*event
, const char *channel_name
,
711 const char *filter_expression
)
713 struct lttcomm_session_msg lsm
;
714 struct filter_parser_ctx
*ctx
;
718 if (!filter_expression
) {
720 * Fall back to normal event enabling if no filter
723 return lttng_enable_event(handle
, event
, channel_name
);
727 * Empty filter string will always be rejected by the parser
728 * anyway, so treat this corner-case early to eliminate
729 * lttng_fmemopen error for 0-byte allocation.
731 if (handle
== NULL
|| filter_expression
[0] == '\0') {
732 return -LTTNG_ERR_INVALID
;
736 * casting const to non-const, as the underlying function will
737 * use it in read-only mode.
739 fmem
= lttng_fmemopen((void *) filter_expression
,
740 strlen(filter_expression
), "r");
742 fprintf(stderr
, "Error opening memory as stream\n");
743 return -LTTNG_ERR_FILTER_NOMEM
;
745 ctx
= filter_parser_ctx_alloc(fmem
);
747 fprintf(stderr
, "Error allocating parser\n");
748 ret
= -LTTNG_ERR_FILTER_NOMEM
;
751 ret
= filter_parser_ctx_append_ast(ctx
);
753 fprintf(stderr
, "Parse error\n");
754 ret
= -LTTNG_ERR_FILTER_INVAL
;
757 ret
= filter_visitor_set_parent(ctx
);
759 fprintf(stderr
, "Set parent error\n");
760 ret
= -LTTNG_ERR_FILTER_INVAL
;
764 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
767 fprintf(stderr
, "XML print error\n");
768 ret
= -LTTNG_ERR_FILTER_INVAL
;
773 dbg_printf("Generating IR... ");
775 ret
= filter_visitor_ir_generate(ctx
);
777 fprintf(stderr
, "Generate IR error\n");
778 ret
= -LTTNG_ERR_FILTER_INVAL
;
781 dbg_printf("done\n");
783 dbg_printf("Validating IR... ");
785 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
787 ret
= -LTTNG_ERR_FILTER_INVAL
;
790 dbg_printf("done\n");
792 dbg_printf("Generating bytecode... ");
794 ret
= filter_visitor_bytecode_generate(ctx
);
796 fprintf(stderr
, "Generate bytecode error\n");
797 ret
= -LTTNG_ERR_FILTER_INVAL
;
800 dbg_printf("done\n");
801 dbg_printf("Size of bytecode generated: %u bytes.\n",
802 bytecode_get_len(&ctx
->bytecode
->b
));
804 memset(&lsm
, 0, sizeof(lsm
));
806 lsm
.cmd_type
= LTTNG_ENABLE_EVENT_WITH_FILTER
;
808 /* If no channel name, send empty string. */
809 if (channel_name
== NULL
) {
810 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, "",
811 sizeof(lsm
.u
.enable
.channel_name
));
813 lttng_ctl_copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
814 sizeof(lsm
.u
.enable
.channel_name
));
817 /* Copy event name */
819 memcpy(&lsm
.u
.enable
.event
, event
, sizeof(lsm
.u
.enable
.event
));
822 lsm
.u
.enable
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
823 + bytecode_get_len(&ctx
->bytecode
->b
);
825 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
827 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
828 sizeof(lsm
.session
.name
));
830 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
831 lsm
.u
.enable
.bytecode_len
, NULL
);
833 filter_bytecode_free(ctx
);
835 filter_parser_ctx_free(ctx
);
836 if (fclose(fmem
) != 0) {
842 filter_bytecode_free(ctx
);
844 filter_parser_ctx_free(ctx
);
846 if (fclose(fmem
) != 0) {
853 * Disable event(s) of a channel and domain.
854 * If no event name is specified, all events are disabled.
855 * If no channel name is specified, the default 'channel0' is used.
856 * Returns size of returned session payload data or a negative error code.
858 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
859 const char *channel_name
)
861 struct lttcomm_session_msg lsm
;
863 if (handle
== NULL
) {
864 return -LTTNG_ERR_INVALID
;
867 memset(&lsm
, 0, sizeof(lsm
));
869 /* If no channel name, send empty string. */
870 if (channel_name
== NULL
) {
871 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, "",
872 sizeof(lsm
.u
.disable
.channel_name
));
874 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
875 sizeof(lsm
.u
.disable
.channel_name
));
878 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
881 lttng_ctl_copy_string(lsm
.u
.disable
.name
, name
,
882 sizeof(lsm
.u
.disable
.name
));
883 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
885 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
888 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
889 sizeof(lsm
.session
.name
));
891 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
895 * Enable channel per domain
896 * Returns size of returned session payload data or a negative error code.
898 int lttng_enable_channel(struct lttng_handle
*handle
,
899 struct lttng_channel
*chan
)
901 struct lttcomm_session_msg lsm
;
904 * NULL arguments are forbidden. No default values.
906 if (handle
== NULL
|| chan
== NULL
) {
907 return -LTTNG_ERR_INVALID
;
910 memset(&lsm
, 0, sizeof(lsm
));
912 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
914 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
916 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
918 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
919 sizeof(lsm
.session
.name
));
921 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
925 * All tracing will be stopped for registered events of the channel.
926 * Returns size of returned session payload data or a negative error code.
928 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
930 struct lttcomm_session_msg lsm
;
932 /* Safety check. Both are mandatory */
933 if (handle
== NULL
|| name
== NULL
) {
934 return -LTTNG_ERR_INVALID
;
937 memset(&lsm
, 0, sizeof(lsm
));
939 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
941 lttng_ctl_copy_string(lsm
.u
.disable
.channel_name
, name
,
942 sizeof(lsm
.u
.disable
.channel_name
));
944 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
946 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
947 sizeof(lsm
.session
.name
));
949 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
953 * Lists all available tracepoints of domain.
954 * Sets the contents of the events array.
955 * Returns the number of lttng_event entries in events;
956 * on error, returns a negative value.
958 int lttng_list_tracepoints(struct lttng_handle
*handle
,
959 struct lttng_event
**events
)
962 struct lttcomm_session_msg lsm
;
964 if (handle
== NULL
) {
965 return -LTTNG_ERR_INVALID
;
968 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
969 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
971 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) events
);
976 return ret
/ sizeof(struct lttng_event
);
980 * Lists all available tracepoint fields of domain.
981 * Sets the contents of the event field array.
982 * Returns the number of lttng_event_field entries in events;
983 * on error, returns a negative value.
985 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
986 struct lttng_event_field
**fields
)
989 struct lttcomm_session_msg lsm
;
991 if (handle
== NULL
) {
992 return -LTTNG_ERR_INVALID
;
995 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
996 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
998 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) fields
);
1003 return ret
/ sizeof(struct lttng_event_field
);
1007 * Returns a human readable string describing
1008 * the error code (a negative value).
1010 const char *lttng_strerror(int code
)
1012 return error_get_str(code
);
1016 * Create a brand new session using name and url for destination.
1018 * Returns LTTNG_OK on success or a negative error code.
1020 int lttng_create_session(const char *name
, const char *url
)
1024 struct lttcomm_session_msg lsm
;
1025 struct lttng_uri
*uris
= NULL
;
1028 return -LTTNG_ERR_INVALID
;
1031 memset(&lsm
, 0, sizeof(lsm
));
1033 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1034 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1036 /* There should never be a data URL */
1037 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1039 return -LTTNG_ERR_INVALID
;
1042 lsm
.u
.uri
.size
= size
;
1044 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1045 sizeof(struct lttng_uri
) * size
, NULL
);
1052 * Destroy session using name.
1053 * Returns size of returned session payload data or a negative error code.
1055 int lttng_destroy_session(const char *session_name
)
1057 struct lttcomm_session_msg lsm
;
1059 if (session_name
== NULL
) {
1060 return -LTTNG_ERR_INVALID
;
1063 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1065 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1066 sizeof(lsm
.session
.name
));
1068 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1072 * Ask the session daemon for all available sessions.
1073 * Sets the contents of the sessions array.
1074 * Returns the number of lttng_session entries in sessions;
1075 * on error, returns a negative value.
1077 int lttng_list_sessions(struct lttng_session
**sessions
)
1080 struct lttcomm_session_msg lsm
;
1082 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1083 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) sessions
);
1088 return ret
/ sizeof(struct lttng_session
);
1092 * Ask the session daemon for all available domains of a session.
1093 * Sets the contents of the domains array.
1094 * Returns the number of lttng_domain entries in domains;
1095 * on error, returns a negative value.
1097 int lttng_list_domains(const char *session_name
,
1098 struct lttng_domain
**domains
)
1101 struct lttcomm_session_msg lsm
;
1103 if (session_name
== NULL
) {
1104 return -LTTNG_ERR_INVALID
;
1107 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1109 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1110 sizeof(lsm
.session
.name
));
1112 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) domains
);
1117 return ret
/ sizeof(struct lttng_domain
);
1121 * Ask the session daemon for all available channels of a session.
1122 * Sets the contents of the channels array.
1123 * Returns the number of lttng_channel entries in channels;
1124 * on error, returns a negative value.
1126 int lttng_list_channels(struct lttng_handle
*handle
,
1127 struct lttng_channel
**channels
)
1130 struct lttcomm_session_msg lsm
;
1132 if (handle
== NULL
) {
1133 return -LTTNG_ERR_INVALID
;
1136 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1137 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1138 sizeof(lsm
.session
.name
));
1140 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1142 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) channels
);
1147 return ret
/ sizeof(struct lttng_channel
);
1151 * Ask the session daemon for all available events of a session channel.
1152 * Sets the contents of the events array.
1153 * Returns the number of lttng_event entries in events;
1154 * on error, returns a negative value.
1156 int lttng_list_events(struct lttng_handle
*handle
,
1157 const char *channel_name
, struct lttng_event
**events
)
1160 struct lttcomm_session_msg lsm
;
1162 /* Safety check. An handle and channel name are mandatory */
1163 if (handle
== NULL
|| channel_name
== NULL
) {
1164 return -LTTNG_ERR_INVALID
;
1167 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1168 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1169 sizeof(lsm
.session
.name
));
1170 lttng_ctl_copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1171 sizeof(lsm
.u
.list
.channel_name
));
1173 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1175 ret
= lttng_ctl_ask_sessiond(&lsm
, (void**) events
);
1180 return ret
/ sizeof(struct lttng_event
);
1184 * Sets the tracing_group variable with name.
1185 * This function allocates memory pointed to by tracing_group.
1186 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1188 int lttng_set_tracing_group(const char *name
)
1191 return -LTTNG_ERR_INVALID
;
1194 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1195 return -LTTNG_ERR_FATAL
;
1202 * Returns size of returned session payload data or a negative error code.
1204 int lttng_calibrate(struct lttng_handle
*handle
,
1205 struct lttng_calibrate
*calibrate
)
1207 struct lttcomm_session_msg lsm
;
1209 /* Safety check. NULL pointer are forbidden */
1210 if (handle
== NULL
|| calibrate
== NULL
) {
1211 return -LTTNG_ERR_INVALID
;
1214 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1215 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1217 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1219 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
1223 * Set default channel attributes.
1224 * If either or both of the arguments are null, attr content is zeroe'd.
1226 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1227 struct lttng_channel_attr
*attr
)
1230 if (attr
== NULL
|| domain
== NULL
) {
1234 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1236 /* Same for all domains. */
1237 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1238 attr
->tracefile_size
= DEFAULT_CHANNEL_TRACEFILE_SIZE
;
1239 attr
->tracefile_count
= DEFAULT_CHANNEL_TRACEFILE_COUNT
;
1241 switch (domain
->type
) {
1242 case LTTNG_DOMAIN_KERNEL
:
1243 attr
->switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
1244 attr
->read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
1245 attr
->subbuf_size
= default_get_kernel_channel_subbuf_size();
1246 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1247 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1249 case LTTNG_DOMAIN_UST
:
1250 switch (domain
->buf_type
) {
1251 case LTTNG_BUFFER_PER_UID
:
1252 attr
->subbuf_size
= default_get_ust_uid_channel_subbuf_size();
1253 attr
->num_subbuf
= DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM
;
1254 attr
->output
= DEFAULT_UST_UID_CHANNEL_OUTPUT
;
1255 attr
->switch_timer_interval
= DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER
;
1256 attr
->read_timer_interval
= DEFAULT_UST_UID_CHANNEL_READ_TIMER
;
1258 case LTTNG_BUFFER_PER_PID
:
1260 attr
->subbuf_size
= default_get_ust_pid_channel_subbuf_size();
1261 attr
->num_subbuf
= DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM
;
1262 attr
->output
= DEFAULT_UST_PID_CHANNEL_OUTPUT
;
1263 attr
->switch_timer_interval
= DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER
;
1264 attr
->read_timer_interval
= DEFAULT_UST_PID_CHANNEL_READ_TIMER
;
1268 /* Default behavior: leave set to 0. */
1274 * Check if session daemon is alive.
1276 * Return 1 if alive or 0 if not.
1277 * On error returns a negative value.
1279 int lttng_session_daemon_alive(void)
1283 ret
= set_session_daemon_path();
1289 if (*sessiond_sock_path
== '\0') {
1291 * No socket path set. Weird error which means the constructor was not
1297 ret
= try_connect_sessiond(sessiond_sock_path
);
1308 * Set URL for a consumer for a session and domain.
1310 * Return 0 on success, else a negative value.
1312 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1313 const char *control_url
, const char *data_url
)
1317 struct lttcomm_session_msg lsm
;
1318 struct lttng_uri
*uris
= NULL
;
1320 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1321 return -LTTNG_ERR_INVALID
;
1324 memset(&lsm
, 0, sizeof(lsm
));
1326 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1328 lttng_ctl_copy_string(lsm
.session
.name
, handle
->session_name
,
1329 sizeof(lsm
.session
.name
));
1330 lttng_ctl_copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1332 size
= uri_parse_str_urls(control_url
, data_url
, &uris
);
1334 return -LTTNG_ERR_INVALID
;
1337 lsm
.u
.uri
.size
= size
;
1339 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1340 sizeof(struct lttng_uri
) * size
, NULL
);
1349 int lttng_enable_consumer(struct lttng_handle
*handle
)
1357 int lttng_disable_consumer(struct lttng_handle
*handle
)
1363 * Set health socket path by putting it in the global health_sock_path
1366 * Returns 0 on success or assert(0) on ENOMEM.
1368 static int set_health_socket_path(void)
1370 int in_tgroup
= 0; /* In tracing group */
1377 /* Are we in the tracing group ? */
1378 in_tgroup
= check_tracing_group(tracing_group
);
1381 if ((uid
== 0) || in_tgroup
) {
1382 lttng_ctl_copy_string(health_sock_path
,
1383 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
, sizeof(health_sock_path
));
1390 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1391 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1393 home
= utils_get_home_dir();
1395 /* Fallback in /tmp .. */
1399 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1400 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1401 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1402 /* ENOMEM at this point... just kill the control lib. */
1411 * Check session daemon health for a specific health component.
1413 * Return 0 if health is OK or else 1 if BAD.
1415 * Any other negative value is a lttng error code which can be translated with
1418 int lttng_health_check(enum lttng_health_component c
)
1421 struct lttcomm_health_msg msg
;
1422 struct lttcomm_health_data reply
;
1424 /* Connect to the sesssion daemon */
1425 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1427 ret
= -LTTNG_ERR_NO_SESSIOND
;
1431 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1434 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1436 ret
= -LTTNG_ERR_FATAL
;
1440 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1442 ret
= -LTTNG_ERR_FATAL
;
1446 ret
= reply
.ret_code
;
1452 closeret
= close(sock
);
1461 * This is an extension of create session that is ONLY and SHOULD only be used
1462 * by the lttng command line program. It exists to avoid using URI parsing in
1465 * We need the date and time for the trace path subdirectory for the case where
1466 * the user does NOT define one using either -o or -U. Using the normal
1467 * lttng_create_session API call, we have no clue on the session daemon side if
1468 * the URL was generated automatically by the client or define by the user.
1470 * So this function "wrapper" is hidden from the public API, takes the datetime
1471 * string and appends it if necessary to the URI subdirectory before sending it
1472 * to the session daemon.
1474 * With this extra function, the lttng_create_session call behavior is not
1475 * changed and the timestamp is appended to the URI on the session daemon side
1478 int _lttng_create_session_ext(const char *name
, const char *url
,
1479 const char *datetime
)
1483 struct lttcomm_session_msg lsm
;
1484 struct lttng_uri
*uris
= NULL
;
1486 if (name
== NULL
|| datetime
== NULL
) {
1487 return -LTTNG_ERR_INVALID
;
1490 memset(&lsm
, 0, sizeof(lsm
));
1492 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1493 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1495 /* There should never be a data URL */
1496 size
= uri_parse_str_urls(url
, NULL
, &uris
);
1498 ret
= -LTTNG_ERR_INVALID
;
1502 lsm
.u
.uri
.size
= size
;
1504 if (size
> 0 && uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1505 /* Don't append datetime if the name was automatically created. */
1506 if (strncmp(name
, DEFAULT_SESSION_NAME
"-",
1507 strlen(DEFAULT_SESSION_NAME
) + 1)) {
1508 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s-%s",
1511 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "%s", name
);
1514 PERROR("snprintf uri subdir");
1515 ret
= -LTTNG_ERR_FATAL
;
1520 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1521 sizeof(struct lttng_uri
) * size
, NULL
);
1529 * For a given session name, this call checks if the data is ready to be read
1530 * or is still being extracted by the consumer(s) hence not ready to be used by
1533 int lttng_data_pending(const char *session_name
)
1536 struct lttcomm_session_msg lsm
;
1538 if (session_name
== NULL
) {
1539 return -LTTNG_ERR_INVALID
;
1542 lsm
.cmd_type
= LTTNG_DATA_PENDING
;
1544 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
1545 sizeof(lsm
.session
.name
));
1547 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
1550 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1551 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1552 * that the data is available. Yes it is hackish but for now this is the
1563 * Create a session exclusively used for snapshot.
1565 * Returns LTTNG_OK on success or a negative error code.
1567 int lttng_create_session_snapshot(const char *name
, const char *snapshot_url
)
1571 struct lttcomm_session_msg lsm
;
1572 struct lttng_uri
*uris
= NULL
;
1575 return -LTTNG_ERR_INVALID
;
1578 memset(&lsm
, 0, sizeof(lsm
));
1580 lsm
.cmd_type
= LTTNG_CREATE_SESSION_SNAPSHOT
;
1581 lttng_ctl_copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1583 size
= uri_parse_str_urls(snapshot_url
, NULL
, &uris
);
1585 return -LTTNG_ERR_INVALID
;
1588 lsm
.u
.uri
.size
= size
;
1590 ret
= lttng_ctl_ask_sessiond_varlen(&lsm
, uris
,
1591 sizeof(struct lttng_uri
) * size
, NULL
);
1600 static void __attribute__((constructor
)) init()
1602 /* Set default session group */
1603 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1604 /* Set socket for health check */
1605 (void) set_health_socket_path();
1611 static void __attribute__((destructor
)) lttng_ctl_exit()
1613 free(tracing_group
);