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
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, 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 <lttng/lttng.h>
36 /* Socket to session daemon for communication */
37 static int sessiond_socket
;
38 static char sessiond_sock_path
[PATH_MAX
];
41 static char *tracing_group
;
45 * Copy string from src to dst and enforce null terminated byte.
47 static void copy_string(char *dst
, const char *src
, size_t len
)
50 strncpy(dst
, src
, len
);
51 /* Enforce the NULL terminated byte */
59 * Copy domain to lttcomm_session_msg domain.
61 * If domain is unknown, default domain will be the kernel.
63 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
67 case LTTNG_DOMAIN_KERNEL
:
68 case LTTNG_DOMAIN_UST
:
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
74 memcpy(dst
, src
, sizeof(struct lttng_domain
));
77 memset(dst
, 0, sizeof(struct lttng_domain
));
78 dst
->type
= LTTNG_DOMAIN_KERNEL
;
85 * Send lttcomm_session_msg to the session daemon.
87 * On success, returns the number of bytes sent (>=0)
88 * On error, returns -1
90 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
99 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
100 sizeof(struct lttcomm_session_msg
));
107 * Receive data from the sessiond socket.
109 * On success, returns the number of bytes received (>=0)
110 * On error, returns -1 (recvmsg() error) or -ENOTCONN
112 static int recv_data_sessiond(void *buf
, size_t len
)
121 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
128 * Check if we are in the specified group.
130 * If yes return 1, else return -1.
132 static int check_tracing_group(const char *grp_name
)
134 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
136 int grp_list_size
, grp_id
, i
;
139 /* Get GID of group 'tracing' */
140 grp_tracing
= getgrnam(grp_name
);
141 if (grp_tracing
== NULL
) {
142 /* NULL means not found also. getgrnam(3) */
149 /* Get number of supplementary group IDs */
150 grp_list_size
= getgroups(0, NULL
);
151 if (grp_list_size
< 0) {
156 /* Alloc group list of the right size */
157 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
162 grp_id
= getgroups(grp_list_size
, grp_list
);
168 for (i
= 0; i
< grp_list_size
; i
++) {
169 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
183 * Try connect to session daemon with sock_path.
185 * Return 0 on success, else -1
187 static int try_connect_sessiond(const char *sock_path
)
191 /* If socket exist, we check if the daemon listens for connect. */
192 ret
= access(sock_path
, F_OK
);
198 ret
= lttcomm_connect_unix_sock(sock_path
);
204 ret
= lttcomm_close_unix_sock(ret
);
206 perror("lttcomm_close_unix_sock");
213 * Set sessiond socket path by putting it in the global
214 * sessiond_sock_path variable.
215 * Returns 0 on success,
216 * -ENOMEM on failure (the sessiond socket path is somehow too long)
218 static int set_session_daemon_path(void)
221 int in_tgroup
= 0; /* In tracing group */
227 /* Are we in the tracing group ? */
228 in_tgroup
= check_tracing_group(tracing_group
);
231 if ((uid
== 0) || in_tgroup
) {
232 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
233 sizeof(sessiond_sock_path
));
239 ret
= try_connect_sessiond(sessiond_sock_path
);
243 /* Global session daemon not available... */
245 /* ...or not in tracing group (and not root), default */
248 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
249 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
251 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
252 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
253 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
262 * Connect to the LTTng session daemon.
264 * On success, return 0. On error, return -1.
266 static int connect_sessiond(void)
270 ret
= set_session_daemon_path();
272 return -1; /* set_session_daemon_path() returns -ENOMEM */
275 /* Connect to the sesssion daemon */
276 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
281 sessiond_socket
= ret
;
288 * Clean disconnect from the session daemon.
289 * On success, return 0. On error, return -1.
291 static int disconnect_sessiond(void)
296 ret
= lttcomm_close_unix_sock(sessiond_socket
);
305 * Ask the session daemon a specific command and put the data into buf.
307 * Return size of data (only payload, not header) or a negative error code.
309 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
314 struct lttcomm_lttng_msg llm
;
316 ret
= connect_sessiond();
321 /* Send command to session daemon */
322 ret
= send_session_msg(lsm
);
327 /* Get header from data transmission */
328 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
333 /* Check error code if OK */
334 if (llm
.ret_code
!= LTTCOMM_OK
) {
339 size
= llm
.data_size
;
341 /* If client free with size 0 */
349 data
= (void*) malloc(size
);
351 /* Get payload data */
352 ret
= recv_data_sessiond(data
, size
);
359 * Extra protection not to dereference a NULL pointer. If buf is NULL at
360 * this point, an error is returned and data is freed.
372 disconnect_sessiond();
377 * Create lttng handle and return pointer.
378 * The returned pointer will be NULL in case of malloc() error.
380 struct lttng_handle
*lttng_create_handle(const char *session_name
,
381 struct lttng_domain
*domain
)
383 struct lttng_handle
*handle
;
385 handle
= malloc(sizeof(struct lttng_handle
));
386 if (handle
== NULL
) {
387 perror("malloc handle");
391 /* Copy session name */
392 copy_string(handle
->session_name
, session_name
,
393 sizeof(handle
->session_name
));
395 /* Copy lttng domain */
396 copy_lttng_domain(&handle
->domain
, domain
);
403 * Destroy handle by free(3) the pointer.
405 void lttng_destroy_handle(struct lttng_handle
*handle
)
413 * Register an outside consumer.
414 * Returns size of returned session payload data or a negative error code.
416 int lttng_register_consumer(struct lttng_handle
*handle
,
417 const char *socket_path
)
419 struct lttcomm_session_msg lsm
;
421 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
422 copy_string(lsm
.session
.name
, handle
->session_name
,
423 sizeof(lsm
.session
.name
));
424 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
426 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
428 return ask_sessiond(&lsm
, NULL
);
432 * Start tracing for all traces of the session.
433 * Returns size of returned session payload data or a negative error code.
435 int lttng_start_tracing(const char *session_name
)
437 struct lttcomm_session_msg lsm
;
439 if (session_name
== NULL
) {
443 lsm
.cmd_type
= LTTNG_START_TRACE
;
445 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
447 return ask_sessiond(&lsm
, NULL
);
451 * Stop tracing for all traces of the session.
452 * Returns size of returned session payload data or a negative error code.
454 int lttng_stop_tracing(const char *session_name
)
456 struct lttcomm_session_msg lsm
;
458 if (session_name
== NULL
) {
462 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
464 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
466 return ask_sessiond(&lsm
, NULL
);
470 * Add context to event and/or channel.
471 * If event_name is NULL, the context is applied to all events of the channel.
472 * If channel_name is NULL, a lookup of the event's channel is done.
473 * If both are NULL, the context is applied to all events of all channels.
475 * Returns the size of the returned payload data or a negative error code.
477 int lttng_add_context(struct lttng_handle
*handle
,
478 struct lttng_event_context
*ctx
, const char *event_name
,
479 const char *channel_name
)
481 struct lttcomm_session_msg lsm
;
483 /* Safety check. Both are mandatory */
484 if (handle
== NULL
|| ctx
== NULL
) {
488 memset(&lsm
, 0, sizeof(lsm
));
490 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
492 /* Copy channel name */
493 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
494 sizeof(lsm
.u
.context
.channel_name
));
495 /* Copy event name */
496 copy_string(lsm
.u
.context
.event_name
, event_name
,
497 sizeof(lsm
.u
.context
.event_name
));
499 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
501 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
503 copy_string(lsm
.session
.name
, handle
->session_name
,
504 sizeof(lsm
.session
.name
));
506 return ask_sessiond(&lsm
, NULL
);
510 * Enable event(s) for a channel.
511 * If no event name is specified, all events are enabled.
512 * If no channel name is specified, the default 'channel0' is used.
513 * Returns size of returned session payload data or a negative error code.
515 int lttng_enable_event(struct lttng_handle
*handle
,
516 struct lttng_event
*ev
, const char *channel_name
)
518 struct lttcomm_session_msg lsm
;
520 if (handle
== NULL
|| ev
== NULL
) {
524 memset(&lsm
, 0, sizeof(lsm
));
526 /* If no channel name, we put the default name */
527 if (channel_name
== NULL
) {
528 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
529 sizeof(lsm
.u
.enable
.channel_name
));
531 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
532 sizeof(lsm
.u
.enable
.channel_name
));
535 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
537 if (ev
->name
[0] != '\0') {
538 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
540 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
542 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
544 copy_string(lsm
.session
.name
, handle
->session_name
,
545 sizeof(lsm
.session
.name
));
547 return ask_sessiond(&lsm
, NULL
);
551 * Disable event(s) of a channel and domain.
552 * If no event name is specified, all events are disabled.
553 * If no channel name is specified, the default 'channel0' is used.
554 * Returns size of returned session payload data or a negative error code.
556 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
557 const char *channel_name
)
559 struct lttcomm_session_msg lsm
;
561 if (handle
== NULL
) {
565 memset(&lsm
, 0, sizeof(lsm
));
568 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
569 sizeof(lsm
.u
.disable
.channel_name
));
571 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
572 sizeof(lsm
.u
.disable
.channel_name
));
575 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
578 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
579 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
581 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
584 copy_string(lsm
.session
.name
, handle
->session_name
,
585 sizeof(lsm
.session
.name
));
587 return ask_sessiond(&lsm
, NULL
);
591 * Enable channel per domain
592 * Returns size of returned session payload data or a negative error code.
594 int lttng_enable_channel(struct lttng_handle
*handle
,
595 struct lttng_channel
*chan
)
597 struct lttcomm_session_msg lsm
;
600 * NULL arguments are forbidden. No default values.
602 if (handle
== NULL
|| chan
== NULL
) {
606 memset(&lsm
, 0, sizeof(lsm
));
608 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
610 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
612 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
614 copy_string(lsm
.session
.name
, handle
->session_name
,
615 sizeof(lsm
.session
.name
));
617 return ask_sessiond(&lsm
, NULL
);
621 * All tracing will be stopped for registered events of the channel.
622 * Returns size of returned session payload data or a negative error code.
624 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
626 struct lttcomm_session_msg lsm
;
628 /* Safety check. Both are mandatory */
629 if (handle
== NULL
|| name
== NULL
) {
633 memset(&lsm
, 0, sizeof(lsm
));
635 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
637 copy_string(lsm
.u
.disable
.channel_name
, name
,
638 sizeof(lsm
.u
.disable
.channel_name
));
640 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
642 copy_string(lsm
.session
.name
, handle
->session_name
,
643 sizeof(lsm
.session
.name
));
645 return ask_sessiond(&lsm
, NULL
);
649 * Lists all available tracepoints of domain.
650 * Sets the contents of the events array.
651 * Returns the number of lttng_event entries in events;
652 * on error, returns a negative value.
654 int lttng_list_tracepoints(struct lttng_handle
*handle
,
655 struct lttng_event
**events
)
658 struct lttcomm_session_msg lsm
;
660 if (handle
== NULL
) {
664 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
665 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
667 ret
= ask_sessiond(&lsm
, (void **) events
);
672 return ret
/ sizeof(struct lttng_event
);
676 * Returns a human readable string describing
677 * the error code (a negative value).
679 const char *lttng_strerror(int code
)
681 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
682 if (code
> -LTTCOMM_OK
) {
683 return "Ended with errors";
686 return lttcomm_get_readable_code(code
);
690 * Create a brand new session using name and path.
691 * Returns size of returned session payload data or a negative error code.
693 int lttng_create_session(const char *name
, const char *path
)
695 struct lttcomm_session_msg lsm
;
697 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
698 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
699 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
701 return ask_sessiond(&lsm
, NULL
);
705 * Destroy session using name.
706 * Returns size of returned session payload data or a negative error code.
708 int lttng_destroy_session(const char *session_name
)
710 struct lttcomm_session_msg lsm
;
712 if (session_name
== NULL
) {
716 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
718 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
720 return ask_sessiond(&lsm
, NULL
);
724 * Ask the session daemon for all available sessions.
725 * Sets the contents of the sessions array.
726 * Returns the number of lttng_session entries in sessions;
727 * on error, returns a negative value.
729 int lttng_list_sessions(struct lttng_session
**sessions
)
732 struct lttcomm_session_msg lsm
;
734 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
735 ret
= ask_sessiond(&lsm
, (void**) sessions
);
740 return ret
/ sizeof(struct lttng_session
);
744 * Ask the session daemon for all available domains of a session.
745 * Sets the contents of the domains array.
746 * Returns the number of lttng_domain entries in domains;
747 * on error, returns a negative value.
749 int lttng_list_domains(const char *session_name
,
750 struct lttng_domain
**domains
)
753 struct lttcomm_session_msg lsm
;
755 if (session_name
== NULL
) {
759 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
761 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
763 ret
= ask_sessiond(&lsm
, (void**) domains
);
768 return ret
/ sizeof(struct lttng_domain
);
772 * Ask the session daemon for all available channels of a session.
773 * Sets the contents of the channels array.
774 * Returns the number of lttng_channel entries in channels;
775 * on error, returns a negative value.
777 int lttng_list_channels(struct lttng_handle
*handle
,
778 struct lttng_channel
**channels
)
781 struct lttcomm_session_msg lsm
;
783 if (handle
== NULL
) {
787 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
788 copy_string(lsm
.session
.name
, handle
->session_name
,
789 sizeof(lsm
.session
.name
));
791 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
793 ret
= ask_sessiond(&lsm
, (void**) channels
);
798 return ret
/ sizeof(struct lttng_channel
);
802 * Ask the session daemon for all available events of a session channel.
803 * Sets the contents of the events array.
804 * Returns the number of lttng_event entries in events;
805 * on error, returns a negative value.
807 int lttng_list_events(struct lttng_handle
*handle
,
808 const char *channel_name
, struct lttng_event
**events
)
811 struct lttcomm_session_msg lsm
;
813 /* Safety check. An handle and channel name are mandatory */
814 if (handle
== NULL
|| channel_name
== NULL
) {
818 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
819 copy_string(lsm
.session
.name
, handle
->session_name
,
820 sizeof(lsm
.session
.name
));
821 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
822 sizeof(lsm
.u
.list
.channel_name
));
824 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
826 ret
= ask_sessiond(&lsm
, (void**) events
);
831 return ret
/ sizeof(struct lttng_event
);
835 * Sets the tracing_group variable with name.
836 * This function allocates memory pointed to by tracing_group.
837 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
839 int lttng_set_tracing_group(const char *name
)
845 if (asprintf(&tracing_group
, "%s", name
) < 0) {
853 * Returns size of returned session payload data or a negative error code.
855 int lttng_calibrate(struct lttng_handle
*handle
,
856 struct lttng_calibrate
*calibrate
)
858 struct lttcomm_session_msg lsm
;
860 /* Safety check. NULL pointer are forbidden */
861 if (handle
== NULL
|| calibrate
== NULL
) {
865 lsm
.cmd_type
= LTTNG_CALIBRATE
;
866 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
868 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
870 return ask_sessiond(&lsm
, NULL
);
874 * Set default channel attributes.
875 * If either or both of the arguments are null, attr content is zeroe'd.
877 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
878 struct lttng_channel_attr
*attr
)
880 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
883 if (attr
== NULL
|| domain
== NULL
) {
887 switch (domain
->type
) {
888 case LTTNG_DOMAIN_KERNEL
:
889 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
890 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
891 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
893 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
894 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
895 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
897 case LTTNG_DOMAIN_UST
:
899 case LTTNG_DOMAIN_UST_EXEC_NAME
:
900 case LTTNG_DOMAIN_UST_PID
:
901 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
903 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
904 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
905 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
907 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
908 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
909 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
912 /* Default behavior: leave set to 0. */
918 * Check if session daemon is alive.
920 * Return 1 if alive or 0 if not.
921 * On error returns a negative value.
923 int lttng_session_daemon_alive(void)
927 ret
= set_session_daemon_path();
933 if (strlen(sessiond_sock_path
) == 0) {
934 /* No socket path set. Weird error */
938 ret
= try_connect_sessiond(sessiond_sock_path
);
951 static void __attribute__((constructor
)) init()
953 /* Set default session group */
954 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);