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 <lttng/lttng.h>
37 #include "filter-ast.h"
38 #include "filter-parser.h"
39 #include "filter-bytecode.h"
40 #include "memstream.h"
43 const int print_xml
= 1;
44 #define dbg_printf(fmt, args...) \
45 printf("[debug liblttng-ctl] " fmt, ## args)
47 const int print_xml
= 0;
48 #define dbg_printf(fmt, args...) \
50 /* do nothing but check printf format */ \
52 printf("[debug liblttnctl] " fmt, ## args); \
57 /* Socket to session daemon for communication */
58 static int sessiond_socket
;
59 static char sessiond_sock_path
[PATH_MAX
];
60 static char health_sock_path
[PATH_MAX
];
63 static char *tracing_group
;
69 * Those two variables are used by error.h to silent or control the verbosity of
70 * error message. They are global to the library so application linking with it
71 * are able to compile correctly and also control verbosity of the library.
73 * Note that it is *not* possible to silent ERR() and PERROR() macros.
76 int lttng_opt_verbose
;
78 static void set_default_url_attr(struct lttng_uri
*uri
,
79 enum lttng_stream_type stype
)
82 if (uri
->dtype
!= LTTNG_DST_PATH
&& uri
->port
== 0) {
83 uri
->port
= (stype
== LTTNG_STREAM_CONTROL
) ?
84 DEFAULT_NETWORK_CONTROL_PORT
: DEFAULT_NETWORK_DATA_PORT
;
89 * Parse a string URL and creates URI(s) returning the size of the populated
92 static ssize_t
parse_str_urls_to_uri(const char *ctrl_url
, const char *data_url
,
93 struct lttng_uri
**uris
)
96 unsigned int equal
= 1, idx
= 0;
97 /* Add the "file://" size to the URL maximum size */
98 char url
[PATH_MAX
+ 7];
99 ssize_t size_ctrl
= 0, size_data
= 0, size
;
100 struct lttng_uri
*ctrl_uris
= NULL
, *data_uris
= NULL
;
101 struct lttng_uri
*tmp_uris
= NULL
;
103 /* No URL(s) is allowed. This means that the consumer will be disabled. */
104 if (ctrl_url
== NULL
&& data_url
== NULL
) {
108 /* Check if URLs are equal and if so, only use the control URL */
109 if (ctrl_url
&& data_url
) {
110 equal
= !strcmp(ctrl_url
, data_url
);
114 * Since we allow the str_url to be a full local filesystem path, we are
115 * going to create a valid file:// URL if it's the case.
117 * Check if first character is a '/' or else reject the URL.
119 if (ctrl_url
&& ctrl_url
[0] == '/') {
120 ret
= snprintf(url
, sizeof(url
), "file://%s", ctrl_url
);
122 PERROR("snprintf file url");
128 /* Parse the control URL if there is one */
130 size_ctrl
= uri_parse(ctrl_url
, &ctrl_uris
);
132 ERR("Unable to parse the URL %s", ctrl_url
);
136 /* At this point, we know there is at least one URI in the array */
137 set_default_url_attr(&ctrl_uris
[0], LTTNG_STREAM_CONTROL
);
139 if (ctrl_uris
[0].dtype
== LTTNG_DST_PATH
&& data_url
) {
140 ERR("Can not have a data URL when destination is file://");
144 /* URL are not equal but the control URL uses a net:// protocol */
145 if (size_ctrl
== 2) {
147 ERR("Control URL uses the net:// protocol and the data URL is "
148 "different. Not allowed.");
151 set_default_url_attr(&ctrl_uris
[1], LTTNG_STREAM_DATA
);
153 * The data_url and ctrl_url are equal and the ctrl_url
154 * contains a net:// protocol so we just skip the data part.
162 /* We have to parse the data URL in this case */
163 size_data
= uri_parse(data_url
, &data_uris
);
165 ERR("Unable to parse the URL %s", data_url
);
167 } else if (size_data
== 2) {
168 ERR("Data URL can not be set with the net[4|6]:// protocol");
172 set_default_url_attr(&data_uris
[0], LTTNG_STREAM_DATA
);
175 /* Compute total size */
176 size
= size_ctrl
+ size_data
;
178 tmp_uris
= zmalloc(sizeof(struct lttng_uri
) * size
);
179 if (tmp_uris
== NULL
) {
180 PERROR("zmalloc uris");
185 /* It's possible the control URIs array contains more than one URI */
186 memcpy(tmp_uris
, ctrl_uris
, sizeof(struct lttng_uri
) * size_ctrl
);
191 memcpy(&tmp_uris
[idx
], data_uris
, sizeof(struct lttng_uri
));
207 * Copy string from src to dst and enforce null terminated byte.
209 static void copy_string(char *dst
, const char *src
, size_t len
)
212 strncpy(dst
, src
, len
);
213 /* Enforce the NULL terminated byte */
221 * Copy domain to lttcomm_session_msg domain.
223 * If domain is unknown, default domain will be the kernel.
225 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
229 case LTTNG_DOMAIN_KERNEL
:
230 case LTTNG_DOMAIN_UST
:
232 case LTTNG_DOMAIN_UST_EXEC_NAME:
233 case LTTNG_DOMAIN_UST_PID:
234 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
236 memcpy(dst
, src
, sizeof(struct lttng_domain
));
239 memset(dst
, 0, sizeof(struct lttng_domain
));
246 * Send lttcomm_session_msg to the session daemon.
248 * On success, returns the number of bytes sent (>=0)
249 * On error, returns -1
251 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
260 DBG("LSM cmd type : %d", lsm
->cmd_type
);
262 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
263 sizeof(struct lttcomm_session_msg
));
270 * Send var len data to the session daemon.
272 * On success, returns the number of bytes sent (>=0)
273 * On error, returns -1
275 static int send_session_varlen(void *data
, size_t len
)
289 ret
= lttcomm_send_unix_sock(sessiond_socket
, data
, len
);
296 * Receive data from the sessiond socket.
298 * On success, returns the number of bytes received (>=0)
299 * On error, returns -1 (recvmsg() error) or -ENOTCONN
301 static int recv_data_sessiond(void *buf
, size_t len
)
310 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
317 * Check if we are in the specified group.
319 * If yes return 1, else return -1.
321 static int check_tracing_group(const char *grp_name
)
323 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
325 int grp_list_size
, grp_id
, i
;
328 /* Get GID of group 'tracing' */
329 grp_tracing
= getgrnam(grp_name
);
331 /* If grp_tracing is NULL, the group does not exist. */
335 /* Get number of supplementary group IDs */
336 grp_list_size
= getgroups(0, NULL
);
337 if (grp_list_size
< 0) {
342 /* Alloc group list of the right size */
343 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
348 grp_id
= getgroups(grp_list_size
, grp_list
);
354 for (i
= 0; i
< grp_list_size
; i
++) {
355 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
369 * Try connect to session daemon with sock_path.
371 * Return 0 on success, else -1
373 static int try_connect_sessiond(const char *sock_path
)
377 /* If socket exist, we check if the daemon listens for connect. */
378 ret
= access(sock_path
, F_OK
);
384 ret
= lttcomm_connect_unix_sock(sock_path
);
390 ret
= lttcomm_close_unix_sock(ret
);
392 perror("lttcomm_close_unix_sock");
399 * Set sessiond socket path by putting it in the global
400 * sessiond_sock_path variable.
401 * Returns 0 on success,
402 * -ENOMEM on failure (the sessiond socket path is somehow too long)
404 static int set_session_daemon_path(void)
407 int in_tgroup
= 0; /* In tracing group */
413 /* Are we in the tracing group ? */
414 in_tgroup
= check_tracing_group(tracing_group
);
417 if ((uid
== 0) || in_tgroup
) {
418 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
419 sizeof(sessiond_sock_path
));
425 ret
= try_connect_sessiond(sessiond_sock_path
);
429 /* Global session daemon not available... */
431 /* ...or not in tracing group (and not root), default */
434 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
435 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
437 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
439 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
448 * Connect to the LTTng session daemon.
450 * On success, return 0. On error, return -1.
452 static int connect_sessiond(void)
456 ret
= set_session_daemon_path();
458 return -1; /* set_session_daemon_path() returns -ENOMEM */
461 /* Connect to the sesssion daemon */
462 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
467 sessiond_socket
= ret
;
474 * Clean disconnect from the session daemon.
475 * On success, return 0. On error, return -1.
477 static int disconnect_sessiond(void)
482 ret
= lttcomm_close_unix_sock(sessiond_socket
);
491 * Ask the session daemon a specific command and put the data into buf.
492 * Takes extra var. len. data as input to send to the session daemon.
494 * Return size of data (only payload, not header) or a negative error code.
496 static int ask_sessiond_varlen(struct lttcomm_session_msg
*lsm
,
497 void *vardata
, size_t varlen
, void **buf
)
502 struct lttcomm_lttng_msg llm
;
504 ret
= connect_sessiond();
509 /* Send command to session daemon */
510 ret
= send_session_msg(lsm
);
514 /* Send var len data */
515 ret
= send_session_varlen(vardata
, varlen
);
520 /* Get header from data transmission */
521 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
526 /* Check error code if OK */
527 if (llm
.ret_code
!= LTTCOMM_OK
) {
532 size
= llm
.data_size
;
534 /* If client free with size 0 */
542 data
= (void*) malloc(size
);
544 /* Get payload data */
545 ret
= recv_data_sessiond(data
, size
);
552 * Extra protection not to dereference a NULL pointer. If buf is NULL at
553 * this point, an error is returned and data is freed.
565 disconnect_sessiond();
570 * Ask the session daemon a specific command and put the data into buf.
572 * Return size of data (only payload, not header) or a negative error code.
574 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
576 return ask_sessiond_varlen(lsm
, NULL
, 0, buf
);
580 * Create lttng handle and return pointer.
581 * The returned pointer will be NULL in case of malloc() error.
583 struct lttng_handle
*lttng_create_handle(const char *session_name
,
584 struct lttng_domain
*domain
)
586 struct lttng_handle
*handle
;
588 handle
= malloc(sizeof(struct lttng_handle
));
589 if (handle
== NULL
) {
590 perror("malloc handle");
594 /* Copy session name */
595 copy_string(handle
->session_name
, session_name
,
596 sizeof(handle
->session_name
));
598 /* Copy lttng domain */
599 copy_lttng_domain(&handle
->domain
, domain
);
606 * Destroy handle by free(3) the pointer.
608 void lttng_destroy_handle(struct lttng_handle
*handle
)
616 * Register an outside consumer.
617 * Returns size of returned session payload data or a negative error code.
619 int lttng_register_consumer(struct lttng_handle
*handle
,
620 const char *socket_path
)
622 struct lttcomm_session_msg lsm
;
624 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
625 copy_string(lsm
.session
.name
, handle
->session_name
,
626 sizeof(lsm
.session
.name
));
627 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
629 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
631 return ask_sessiond(&lsm
, NULL
);
635 * Start tracing for all traces of the session.
636 * Returns size of returned session payload data or a negative error code.
638 int lttng_start_tracing(const char *session_name
)
640 struct lttcomm_session_msg lsm
;
642 if (session_name
== NULL
) {
646 lsm
.cmd_type
= LTTNG_START_TRACE
;
648 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
650 return ask_sessiond(&lsm
, NULL
);
654 * Stop tracing for all traces of the session.
655 * Returns size of returned session payload data or a negative error code.
657 int lttng_stop_tracing(const char *session_name
)
659 struct lttcomm_session_msg lsm
;
661 if (session_name
== NULL
) {
665 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
667 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
669 return ask_sessiond(&lsm
, NULL
);
673 * Add context to event and/or channel.
674 * If event_name is NULL, the context is applied to all events of the channel.
675 * If channel_name is NULL, a lookup of the event's channel is done.
676 * If both are NULL, the context is applied to all events of all channels.
678 * Returns the size of the returned payload data or a negative error code.
680 int lttng_add_context(struct lttng_handle
*handle
,
681 struct lttng_event_context
*ctx
, const char *event_name
,
682 const char *channel_name
)
684 struct lttcomm_session_msg lsm
;
686 /* Safety check. Both are mandatory */
687 if (handle
== NULL
|| ctx
== NULL
) {
691 memset(&lsm
, 0, sizeof(lsm
));
693 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
695 /* Copy channel name */
696 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
697 sizeof(lsm
.u
.context
.channel_name
));
698 /* Copy event name */
699 copy_string(lsm
.u
.context
.event_name
, event_name
,
700 sizeof(lsm
.u
.context
.event_name
));
702 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
704 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
706 copy_string(lsm
.session
.name
, handle
->session_name
,
707 sizeof(lsm
.session
.name
));
709 return ask_sessiond(&lsm
, NULL
);
713 * Enable event(s) for a channel.
714 * If no event name is specified, all events are enabled.
715 * If no channel name is specified, the default 'channel0' is used.
716 * Returns size of returned session payload data or a negative error code.
718 int lttng_enable_event(struct lttng_handle
*handle
,
719 struct lttng_event
*ev
, const char *channel_name
)
721 struct lttcomm_session_msg lsm
;
723 if (handle
== NULL
|| ev
== NULL
) {
727 memset(&lsm
, 0, sizeof(lsm
));
729 /* If no channel name, we put the default name */
730 if (channel_name
== NULL
) {
731 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
732 sizeof(lsm
.u
.enable
.channel_name
));
734 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
735 sizeof(lsm
.u
.enable
.channel_name
));
738 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
740 if (ev
->name
[0] != '\0') {
741 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
743 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
745 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
747 copy_string(lsm
.session
.name
, handle
->session_name
,
748 sizeof(lsm
.session
.name
));
750 return ask_sessiond(&lsm
, NULL
);
754 * set filter for an event
755 * Return negative error value on error.
756 * Return size of returned session payload data if OK.
759 int lttng_set_event_filter(struct lttng_handle
*handle
,
760 const char *event_name
, const char *channel_name
,
761 const char *filter_expression
)
763 struct lttcomm_session_msg lsm
;
764 struct filter_parser_ctx
*ctx
;
769 if (handle
== NULL
) {
773 if (!filter_expression
) {
778 * casting const to non-const, as the underlying function will
779 * use it in read-only mode.
781 fmem
= lttng_fmemopen((void *) filter_expression
,
782 strlen(filter_expression
), "r");
784 fprintf(stderr
, "Error opening memory as stream\n");
787 ctx
= filter_parser_ctx_alloc(fmem
);
789 fprintf(stderr
, "Error allocating parser\n");
793 ret
= filter_parser_ctx_append_ast(ctx
);
795 fprintf(stderr
, "Parse error\n");
799 ret
= filter_visitor_set_parent(ctx
);
801 fprintf(stderr
, "Set parent error\n");
806 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
809 fprintf(stderr
, "XML print error\n");
815 dbg_printf("Generating IR... ");
817 ret
= filter_visitor_ir_generate(ctx
);
819 fprintf(stderr
, "Generate IR error\n");
823 dbg_printf("done\n");
825 dbg_printf("Validating IR... ");
827 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
832 dbg_printf("done\n");
834 dbg_printf("Generating bytecode... ");
836 ret
= filter_visitor_bytecode_generate(ctx
);
838 fprintf(stderr
, "Generate bytecode error\n");
842 dbg_printf("done\n");
843 dbg_printf("Size of bytecode generated: %u bytes.\n",
844 bytecode_get_len(&ctx
->bytecode
->b
));
846 memset(&lsm
, 0, sizeof(lsm
));
848 lsm
.cmd_type
= LTTNG_SET_FILTER
;
850 /* Copy channel name */
851 copy_string(lsm
.u
.filter
.channel_name
, channel_name
,
852 sizeof(lsm
.u
.filter
.channel_name
));
853 /* Copy event name */
854 copy_string(lsm
.u
.filter
.event_name
, event_name
,
855 sizeof(lsm
.u
.filter
.event_name
));
856 lsm
.u
.filter
.bytecode_len
= sizeof(ctx
->bytecode
->b
)
857 + bytecode_get_len(&ctx
->bytecode
->b
);
859 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
861 copy_string(lsm
.session
.name
, handle
->session_name
,
862 sizeof(lsm
.session
.name
));
864 ret
= ask_sessiond_varlen(&lsm
, &ctx
->bytecode
->b
,
865 lsm
.u
.filter
.bytecode_len
, NULL
);
867 filter_bytecode_free(ctx
);
869 filter_parser_ctx_free(ctx
);
870 if (fclose(fmem
) != 0) {
876 filter_bytecode_free(ctx
);
878 filter_parser_ctx_free(ctx
);
880 if (fclose(fmem
) != 0) {
887 * Disable event(s) of a channel and domain.
888 * If no event name is specified, all events are disabled.
889 * If no channel name is specified, the default 'channel0' is used.
890 * Returns size of returned session payload data or a negative error code.
892 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
893 const char *channel_name
)
895 struct lttcomm_session_msg lsm
;
897 if (handle
== NULL
) {
901 memset(&lsm
, 0, sizeof(lsm
));
904 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
905 sizeof(lsm
.u
.disable
.channel_name
));
907 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
908 sizeof(lsm
.u
.disable
.channel_name
));
911 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
914 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
915 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
917 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
920 copy_string(lsm
.session
.name
, handle
->session_name
,
921 sizeof(lsm
.session
.name
));
923 return ask_sessiond(&lsm
, NULL
);
927 * Enable channel per domain
928 * Returns size of returned session payload data or a negative error code.
930 int lttng_enable_channel(struct lttng_handle
*handle
,
931 struct lttng_channel
*chan
)
933 struct lttcomm_session_msg lsm
;
936 * NULL arguments are forbidden. No default values.
938 if (handle
== NULL
|| chan
== NULL
) {
942 memset(&lsm
, 0, sizeof(lsm
));
944 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
946 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
948 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
950 copy_string(lsm
.session
.name
, handle
->session_name
,
951 sizeof(lsm
.session
.name
));
953 return ask_sessiond(&lsm
, NULL
);
957 * All tracing will be stopped for registered events of the channel.
958 * Returns size of returned session payload data or a negative error code.
960 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
962 struct lttcomm_session_msg lsm
;
964 /* Safety check. Both are mandatory */
965 if (handle
== NULL
|| name
== NULL
) {
969 memset(&lsm
, 0, sizeof(lsm
));
971 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
973 copy_string(lsm
.u
.disable
.channel_name
, name
,
974 sizeof(lsm
.u
.disable
.channel_name
));
976 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
978 copy_string(lsm
.session
.name
, handle
->session_name
,
979 sizeof(lsm
.session
.name
));
981 return ask_sessiond(&lsm
, NULL
);
985 * Lists all available tracepoints of domain.
986 * Sets the contents of the events array.
987 * Returns the number of lttng_event entries in events;
988 * on error, returns a negative value.
990 int lttng_list_tracepoints(struct lttng_handle
*handle
,
991 struct lttng_event
**events
)
994 struct lttcomm_session_msg lsm
;
996 if (handle
== NULL
) {
1000 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
1001 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1003 ret
= ask_sessiond(&lsm
, (void **) events
);
1008 return ret
/ sizeof(struct lttng_event
);
1012 * Lists all available tracepoint fields of domain.
1013 * Sets the contents of the event field array.
1014 * Returns the number of lttng_event_field entries in events;
1015 * on error, returns a negative value.
1017 int lttng_list_tracepoint_fields(struct lttng_handle
*handle
,
1018 struct lttng_event_field
**fields
)
1021 struct lttcomm_session_msg lsm
;
1023 if (handle
== NULL
) {
1027 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINT_FIELDS
;
1028 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1030 ret
= ask_sessiond(&lsm
, (void **) fields
);
1035 return ret
/ sizeof(struct lttng_event_field
);
1039 * Returns a human readable string describing
1040 * the error code (a negative value).
1042 const char *lttng_strerror(int code
)
1044 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
1045 if (code
> -LTTCOMM_OK
) {
1046 return "Ended with errors";
1049 return lttcomm_get_readable_code(code
);
1053 * Create a brand new session using name and url for destination.
1055 * Returns LTTCOMM_OK on success or a negative error code.
1057 int lttng_create_session(const char *name
, const char *url
)
1060 struct lttcomm_session_msg lsm
;
1061 struct lttng_uri
*uris
= NULL
;
1067 memset(&lsm
, 0, sizeof(lsm
));
1069 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1070 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1072 /* There should never be a data URL */
1073 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1075 return LTTCOMM_INVALID
;
1078 lsm
.u
.uri
.size
= size
;
1080 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1085 * Destroy session using name.
1086 * Returns size of returned session payload data or a negative error code.
1088 int lttng_destroy_session(const char *session_name
)
1090 struct lttcomm_session_msg lsm
;
1092 if (session_name
== NULL
) {
1096 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
1098 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1100 return ask_sessiond(&lsm
, NULL
);
1104 * Ask the session daemon for all available sessions.
1105 * Sets the contents of the sessions array.
1106 * Returns the number of lttng_session entries in sessions;
1107 * on error, returns a negative value.
1109 int lttng_list_sessions(struct lttng_session
**sessions
)
1112 struct lttcomm_session_msg lsm
;
1114 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
1115 ret
= ask_sessiond(&lsm
, (void**) sessions
);
1120 return ret
/ sizeof(struct lttng_session
);
1124 * Ask the session daemon for all available domains of a session.
1125 * Sets the contents of the domains array.
1126 * Returns the number of lttng_domain entries in domains;
1127 * on error, returns a negative value.
1129 int lttng_list_domains(const char *session_name
,
1130 struct lttng_domain
**domains
)
1133 struct lttcomm_session_msg lsm
;
1135 if (session_name
== NULL
) {
1139 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
1141 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
1143 ret
= ask_sessiond(&lsm
, (void**) domains
);
1148 return ret
/ sizeof(struct lttng_domain
);
1152 * Ask the session daemon for all available channels of a session.
1153 * Sets the contents of the channels array.
1154 * Returns the number of lttng_channel entries in channels;
1155 * on error, returns a negative value.
1157 int lttng_list_channels(struct lttng_handle
*handle
,
1158 struct lttng_channel
**channels
)
1161 struct lttcomm_session_msg lsm
;
1163 if (handle
== NULL
) {
1167 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
1168 copy_string(lsm
.session
.name
, handle
->session_name
,
1169 sizeof(lsm
.session
.name
));
1171 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1173 ret
= ask_sessiond(&lsm
, (void**) channels
);
1178 return ret
/ sizeof(struct lttng_channel
);
1182 * Ask the session daemon for all available events of a session channel.
1183 * Sets the contents of the events array.
1184 * Returns the number of lttng_event entries in events;
1185 * on error, returns a negative value.
1187 int lttng_list_events(struct lttng_handle
*handle
,
1188 const char *channel_name
, struct lttng_event
**events
)
1191 struct lttcomm_session_msg lsm
;
1193 /* Safety check. An handle and channel name are mandatory */
1194 if (handle
== NULL
|| channel_name
== NULL
) {
1198 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
1199 copy_string(lsm
.session
.name
, handle
->session_name
,
1200 sizeof(lsm
.session
.name
));
1201 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
1202 sizeof(lsm
.u
.list
.channel_name
));
1204 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1206 ret
= ask_sessiond(&lsm
, (void**) events
);
1211 return ret
/ sizeof(struct lttng_event
);
1215 * Sets the tracing_group variable with name.
1216 * This function allocates memory pointed to by tracing_group.
1217 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1219 int lttng_set_tracing_group(const char *name
)
1225 if (asprintf(&tracing_group
, "%s", name
) < 0) {
1233 * Returns size of returned session payload data or a negative error code.
1235 int lttng_calibrate(struct lttng_handle
*handle
,
1236 struct lttng_calibrate
*calibrate
)
1238 struct lttcomm_session_msg lsm
;
1240 /* Safety check. NULL pointer are forbidden */
1241 if (handle
== NULL
|| calibrate
== NULL
) {
1245 lsm
.cmd_type
= LTTNG_CALIBRATE
;
1246 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1248 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
1250 return ask_sessiond(&lsm
, NULL
);
1254 * Set default channel attributes.
1255 * If either or both of the arguments are null, attr content is zeroe'd.
1257 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
1258 struct lttng_channel_attr
*attr
)
1261 if (attr
== NULL
|| domain
== NULL
) {
1265 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
1267 switch (domain
->type
) {
1268 case LTTNG_DOMAIN_KERNEL
:
1269 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1270 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1271 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1273 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1274 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1275 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1277 case LTTNG_DOMAIN_UST
:
1279 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1280 case LTTNG_DOMAIN_UST_PID
:
1281 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1283 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1284 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1285 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1287 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
1288 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
1289 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
1292 /* Default behavior: leave set to 0. */
1298 * Check if session daemon is alive.
1300 * Return 1 if alive or 0 if not.
1301 * On error returns a negative value.
1303 int lttng_session_daemon_alive(void)
1307 ret
= set_session_daemon_path();
1313 if (strlen(sessiond_sock_path
) == 0) {
1314 /* No socket path set. Weird error */
1318 ret
= try_connect_sessiond(sessiond_sock_path
);
1329 * Set URL for a consumer for a session and domain.
1331 * Return 0 on success, else a negative value.
1333 int lttng_set_consumer_url(struct lttng_handle
*handle
,
1334 const char *control_url
, const char *data_url
)
1337 struct lttcomm_session_msg lsm
;
1338 struct lttng_uri
*uris
= NULL
;
1340 if (handle
== NULL
|| (control_url
== NULL
&& data_url
== NULL
)) {
1344 memset(&lsm
, 0, sizeof(lsm
));
1346 lsm
.cmd_type
= LTTNG_SET_CONSUMER_URI
;
1348 copy_string(lsm
.session
.name
, handle
->session_name
,
1349 sizeof(lsm
.session
.name
));
1350 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1352 size
= parse_str_urls_to_uri(control_url
, data_url
, &uris
);
1354 return LTTCOMM_INVALID
;
1357 lsm
.u
.uri
.size
= size
;
1359 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1364 * Enable consumer for a session and domain.
1366 * Return 0 on success, else a negative value.
1368 int lttng_enable_consumer(struct lttng_handle
*handle
)
1370 struct lttcomm_session_msg lsm
;
1372 if (handle
== NULL
) {
1376 lsm
.cmd_type
= LTTNG_ENABLE_CONSUMER
;
1378 copy_string(lsm
.session
.name
, handle
->session_name
,
1379 sizeof(lsm
.session
.name
));
1380 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1382 return ask_sessiond(&lsm
, NULL
);
1386 * Disable consumer for a session and domain.
1388 * Return 0 on success, else a negative value.
1390 int lttng_disable_consumer(struct lttng_handle
*handle
)
1392 struct lttcomm_session_msg lsm
;
1394 if (handle
== NULL
) {
1398 lsm
.cmd_type
= LTTNG_DISABLE_CONSUMER
;
1400 copy_string(lsm
.session
.name
, handle
->session_name
,
1401 sizeof(lsm
.session
.name
));
1402 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
1404 return ask_sessiond(&lsm
, NULL
);
1408 * Set health socket path by putting it in the global health_sock_path
1411 * Returns 0 on success or assert(0) on ENOMEM.
1413 static int set_health_socket_path(void)
1416 int in_tgroup
= 0; /* In tracing group */
1423 /* Are we in the tracing group ? */
1424 in_tgroup
= check_tracing_group(tracing_group
);
1427 if ((uid
== 0) || in_tgroup
) {
1428 copy_string(health_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
,
1429 sizeof(health_sock_path
));
1434 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1435 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1437 home
= getenv("HOME");
1439 /* Fallback in /tmp .. */
1443 ret
= snprintf(health_sock_path
, sizeof(health_sock_path
),
1444 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home
);
1445 if ((ret
< 0) || (ret
>= sizeof(health_sock_path
))) {
1446 /* ENOMEM at this point... just kill the control lib. */
1455 * Check session daemon health for a specific health component.
1457 * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate
1458 * that the control library was not able to connect to the session daemon
1461 * Any other positive value is an lttcomm error which can be translate with
1464 int lttng_health_check(enum lttng_health_component c
)
1467 struct lttcomm_health_msg msg
;
1468 struct lttcomm_health_data reply
;
1470 /* Connect to the sesssion daemon */
1471 sock
= lttcomm_connect_unix_sock(health_sock_path
);
1477 msg
.cmd
= LTTNG_HEALTH_CHECK
;
1480 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
1485 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
1490 ret
= reply
.ret_code
;
1500 * This is an extension of create session that is ONLY and SHOULD only be used
1501 * by the lttng command line program. It exists to avoid using URI parsing in
1504 * We need the date and time for the trace path subdirectory for the case where
1505 * the user does NOT define one using either -o or -U. Using the normal
1506 * lttng_create_session API call, we have no clue on the session daemon side if
1507 * the URL was generated automatically by the client or define by the user.
1509 * So this function "wrapper" is hidden from the public API, takes the datetime
1510 * string and appends it if necessary to the URI subdirectory before sending it
1511 * to the session daemon.
1513 * With this extra function, the lttng_create_session call behavior is not
1514 * changed and the timestamp is appended to the URI on the session daemon side
1517 int _lttng_create_session_ext(const char *name
, const char *url
,
1518 const char *datetime
)
1522 struct lttcomm_session_msg lsm
;
1523 struct lttng_uri
*uris
= NULL
;
1525 if (name
== NULL
|| datetime
== NULL
) {
1529 memset(&lsm
, 0, sizeof(lsm
));
1531 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
1532 if (!strncmp(name
, DEFAULT_SESSION_NAME
, strlen(DEFAULT_SESSION_NAME
))) {
1533 ret
= snprintf(lsm
.session
.name
, sizeof(lsm
.session
.name
), "%s-%s",
1536 PERROR("snprintf session name datetime");
1540 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
1543 /* There should never be a data URL */
1544 size
= parse_str_urls_to_uri(url
, NULL
, &uris
);
1546 return LTTCOMM_INVALID
;
1549 lsm
.u
.uri
.size
= size
;
1551 if (uris
[0].dtype
!= LTTNG_DST_PATH
&& strlen(uris
[0].subdir
) == 0) {
1552 ret
= snprintf(uris
[0].subdir
, sizeof(uris
[0].subdir
), "/%s-%s", name
,
1555 PERROR("snprintf uri subdir");
1560 return ask_sessiond_varlen(&lsm
, uris
, sizeof(struct lttng_uri
) * size
,
1567 static void __attribute__((constructor
)) init()
1569 /* Set default session group */
1570 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);
1571 /* Set socket for health check */
1572 (void) set_health_socket_path();