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
);
142 /* If grp_tracing is NULL, the group does not exist. */
146 /* Get number of supplementary group IDs */
147 grp_list_size
= getgroups(0, NULL
);
148 if (grp_list_size
< 0) {
153 /* Alloc group list of the right size */
154 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
159 grp_id
= getgroups(grp_list_size
, grp_list
);
165 for (i
= 0; i
< grp_list_size
; i
++) {
166 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
180 * Try connect to session daemon with sock_path.
182 * Return 0 on success, else -1
184 static int try_connect_sessiond(const char *sock_path
)
188 /* If socket exist, we check if the daemon listens for connect. */
189 ret
= access(sock_path
, F_OK
);
195 ret
= lttcomm_connect_unix_sock(sock_path
);
201 ret
= lttcomm_close_unix_sock(ret
);
203 perror("lttcomm_close_unix_sock");
210 * Set sessiond socket path by putting it in the global
211 * sessiond_sock_path variable.
212 * Returns 0 on success,
213 * -ENOMEM on failure (the sessiond socket path is somehow too long)
215 static int set_session_daemon_path(void)
218 int in_tgroup
= 0; /* In tracing group */
224 /* Are we in the tracing group ? */
225 in_tgroup
= check_tracing_group(tracing_group
);
228 if ((uid
== 0) || in_tgroup
) {
229 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
230 sizeof(sessiond_sock_path
));
236 ret
= try_connect_sessiond(sessiond_sock_path
);
240 /* Global session daemon not available... */
242 /* ...or not in tracing group (and not root), default */
245 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
246 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
248 ret
= snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
249 DEFAULT_HOME_CLIENT_UNIX_SOCK
, getenv("HOME"));
250 if ((ret
< 0) || (ret
>= sizeof(sessiond_sock_path
))) {
259 * Connect to the LTTng session daemon.
261 * On success, return 0. On error, return -1.
263 static int connect_sessiond(void)
267 ret
= set_session_daemon_path();
269 return -1; /* set_session_daemon_path() returns -ENOMEM */
272 /* Connect to the sesssion daemon */
273 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
278 sessiond_socket
= ret
;
285 * Clean disconnect from the session daemon.
286 * On success, return 0. On error, return -1.
288 static int disconnect_sessiond(void)
293 ret
= lttcomm_close_unix_sock(sessiond_socket
);
302 * Ask the session daemon a specific command and put the data into buf.
304 * Return size of data (only payload, not header) or a negative error code.
306 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
311 struct lttcomm_lttng_msg llm
;
313 ret
= connect_sessiond();
318 /* Send command to session daemon */
319 ret
= send_session_msg(lsm
);
324 /* Get header from data transmission */
325 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
330 /* Check error code if OK */
331 if (llm
.ret_code
!= LTTCOMM_OK
) {
336 size
= llm
.data_size
;
338 /* If client free with size 0 */
346 data
= (void*) malloc(size
);
348 /* Get payload data */
349 ret
= recv_data_sessiond(data
, size
);
356 * Extra protection not to dereference a NULL pointer. If buf is NULL at
357 * this point, an error is returned and data is freed.
369 disconnect_sessiond();
374 * Create lttng handle and return pointer.
375 * The returned pointer will be NULL in case of malloc() error.
377 struct lttng_handle
*lttng_create_handle(const char *session_name
,
378 struct lttng_domain
*domain
)
380 struct lttng_handle
*handle
;
382 handle
= malloc(sizeof(struct lttng_handle
));
383 if (handle
== NULL
) {
384 perror("malloc handle");
388 /* Copy session name */
389 copy_string(handle
->session_name
, session_name
,
390 sizeof(handle
->session_name
));
392 /* Copy lttng domain */
393 copy_lttng_domain(&handle
->domain
, domain
);
400 * Destroy handle by free(3) the pointer.
402 void lttng_destroy_handle(struct lttng_handle
*handle
)
410 * Register an outside consumer.
411 * Returns size of returned session payload data or a negative error code.
413 int lttng_register_consumer(struct lttng_handle
*handle
,
414 const char *socket_path
)
416 struct lttcomm_session_msg lsm
;
418 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
419 copy_string(lsm
.session
.name
, handle
->session_name
,
420 sizeof(lsm
.session
.name
));
421 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
423 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
425 return ask_sessiond(&lsm
, NULL
);
429 * Start tracing for all traces of the session.
430 * Returns size of returned session payload data or a negative error code.
432 int lttng_start_tracing(const char *session_name
)
434 struct lttcomm_session_msg lsm
;
436 if (session_name
== NULL
) {
440 lsm
.cmd_type
= LTTNG_START_TRACE
;
442 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
444 return ask_sessiond(&lsm
, NULL
);
448 * Stop tracing for all traces of the session.
449 * Returns size of returned session payload data or a negative error code.
451 int lttng_stop_tracing(const char *session_name
)
453 struct lttcomm_session_msg lsm
;
455 if (session_name
== NULL
) {
459 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
461 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
463 return ask_sessiond(&lsm
, NULL
);
467 * Add context to event and/or channel.
468 * If event_name is NULL, the context is applied to all events of the channel.
469 * If channel_name is NULL, a lookup of the event's channel is done.
470 * If both are NULL, the context is applied to all events of all channels.
472 * Returns the size of the returned payload data or a negative error code.
474 int lttng_add_context(struct lttng_handle
*handle
,
475 struct lttng_event_context
*ctx
, const char *event_name
,
476 const char *channel_name
)
478 struct lttcomm_session_msg lsm
;
480 /* Safety check. Both are mandatory */
481 if (handle
== NULL
|| ctx
== NULL
) {
485 memset(&lsm
, 0, sizeof(lsm
));
487 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
489 /* Copy channel name */
490 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
491 sizeof(lsm
.u
.context
.channel_name
));
492 /* Copy event name */
493 copy_string(lsm
.u
.context
.event_name
, event_name
,
494 sizeof(lsm
.u
.context
.event_name
));
496 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
498 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
500 copy_string(lsm
.session
.name
, handle
->session_name
,
501 sizeof(lsm
.session
.name
));
503 return ask_sessiond(&lsm
, NULL
);
507 * Enable event(s) for a channel.
508 * If no event name is specified, all events are enabled.
509 * If no channel name is specified, the default 'channel0' is used.
510 * Returns size of returned session payload data or a negative error code.
512 int lttng_enable_event(struct lttng_handle
*handle
,
513 struct lttng_event
*ev
, const char *channel_name
)
515 struct lttcomm_session_msg lsm
;
517 if (handle
== NULL
|| ev
== NULL
) {
521 memset(&lsm
, 0, sizeof(lsm
));
523 /* If no channel name, we put the default name */
524 if (channel_name
== NULL
) {
525 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
526 sizeof(lsm
.u
.enable
.channel_name
));
528 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
529 sizeof(lsm
.u
.enable
.channel_name
));
532 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
534 if (ev
->name
[0] != '\0') {
535 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
537 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
539 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
541 copy_string(lsm
.session
.name
, handle
->session_name
,
542 sizeof(lsm
.session
.name
));
544 return ask_sessiond(&lsm
, NULL
);
548 * Disable event(s) of a channel and domain.
549 * If no event name is specified, all events are disabled.
550 * If no channel name is specified, the default 'channel0' is used.
551 * Returns size of returned session payload data or a negative error code.
553 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
554 const char *channel_name
)
556 struct lttcomm_session_msg lsm
;
558 if (handle
== NULL
) {
562 memset(&lsm
, 0, sizeof(lsm
));
565 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
566 sizeof(lsm
.u
.disable
.channel_name
));
568 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
569 sizeof(lsm
.u
.disable
.channel_name
));
572 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
575 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
576 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
578 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
581 copy_string(lsm
.session
.name
, handle
->session_name
,
582 sizeof(lsm
.session
.name
));
584 return ask_sessiond(&lsm
, NULL
);
588 * Enable channel per domain
589 * Returns size of returned session payload data or a negative error code.
591 int lttng_enable_channel(struct lttng_handle
*handle
,
592 struct lttng_channel
*chan
)
594 struct lttcomm_session_msg lsm
;
597 * NULL arguments are forbidden. No default values.
599 if (handle
== NULL
|| chan
== NULL
) {
603 memset(&lsm
, 0, sizeof(lsm
));
605 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
607 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
609 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
611 copy_string(lsm
.session
.name
, handle
->session_name
,
612 sizeof(lsm
.session
.name
));
614 return ask_sessiond(&lsm
, NULL
);
618 * All tracing will be stopped for registered events of the channel.
619 * Returns size of returned session payload data or a negative error code.
621 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
623 struct lttcomm_session_msg lsm
;
625 /* Safety check. Both are mandatory */
626 if (handle
== NULL
|| name
== NULL
) {
630 memset(&lsm
, 0, sizeof(lsm
));
632 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
634 copy_string(lsm
.u
.disable
.channel_name
, name
,
635 sizeof(lsm
.u
.disable
.channel_name
));
637 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
639 copy_string(lsm
.session
.name
, handle
->session_name
,
640 sizeof(lsm
.session
.name
));
642 return ask_sessiond(&lsm
, NULL
);
646 * Lists all available tracepoints of domain.
647 * Sets the contents of the events array.
648 * Returns the number of lttng_event entries in events;
649 * on error, returns a negative value.
651 int lttng_list_tracepoints(struct lttng_handle
*handle
,
652 struct lttng_event
**events
)
655 struct lttcomm_session_msg lsm
;
657 if (handle
== NULL
) {
661 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
662 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
664 ret
= ask_sessiond(&lsm
, (void **) events
);
669 return ret
/ sizeof(struct lttng_event
);
673 * Returns a human readable string describing
674 * the error code (a negative value).
676 const char *lttng_strerror(int code
)
678 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
679 if (code
> -LTTCOMM_OK
) {
680 return "Ended with errors";
683 return lttcomm_get_readable_code(code
);
687 * Create a brand new session using name and path.
688 * Returns size of returned session payload data or a negative error code.
690 int lttng_create_session(const char *name
, const char *path
)
692 struct lttcomm_session_msg lsm
;
694 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
695 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
696 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
698 return ask_sessiond(&lsm
, NULL
);
702 * Destroy session using name.
703 * Returns size of returned session payload data or a negative error code.
705 int lttng_destroy_session(const char *session_name
)
707 struct lttcomm_session_msg lsm
;
709 if (session_name
== NULL
) {
713 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
715 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
717 return ask_sessiond(&lsm
, NULL
);
721 * Ask the session daemon for all available sessions.
722 * Sets the contents of the sessions array.
723 * Returns the number of lttng_session entries in sessions;
724 * on error, returns a negative value.
726 int lttng_list_sessions(struct lttng_session
**sessions
)
729 struct lttcomm_session_msg lsm
;
731 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
732 ret
= ask_sessiond(&lsm
, (void**) sessions
);
737 return ret
/ sizeof(struct lttng_session
);
741 * Ask the session daemon for all available domains of a session.
742 * Sets the contents of the domains array.
743 * Returns the number of lttng_domain entries in domains;
744 * on error, returns a negative value.
746 int lttng_list_domains(const char *session_name
,
747 struct lttng_domain
**domains
)
750 struct lttcomm_session_msg lsm
;
752 if (session_name
== NULL
) {
756 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
758 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
760 ret
= ask_sessiond(&lsm
, (void**) domains
);
765 return ret
/ sizeof(struct lttng_domain
);
769 * Ask the session daemon for all available channels of a session.
770 * Sets the contents of the channels array.
771 * Returns the number of lttng_channel entries in channels;
772 * on error, returns a negative value.
774 int lttng_list_channels(struct lttng_handle
*handle
,
775 struct lttng_channel
**channels
)
778 struct lttcomm_session_msg lsm
;
780 if (handle
== NULL
) {
784 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
785 copy_string(lsm
.session
.name
, handle
->session_name
,
786 sizeof(lsm
.session
.name
));
788 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
790 ret
= ask_sessiond(&lsm
, (void**) channels
);
795 return ret
/ sizeof(struct lttng_channel
);
799 * Ask the session daemon for all available events of a session channel.
800 * Sets the contents of the events array.
801 * Returns the number of lttng_event entries in events;
802 * on error, returns a negative value.
804 int lttng_list_events(struct lttng_handle
*handle
,
805 const char *channel_name
, struct lttng_event
**events
)
808 struct lttcomm_session_msg lsm
;
810 /* Safety check. An handle and channel name are mandatory */
811 if (handle
== NULL
|| channel_name
== NULL
) {
815 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
816 copy_string(lsm
.session
.name
, handle
->session_name
,
817 sizeof(lsm
.session
.name
));
818 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
819 sizeof(lsm
.u
.list
.channel_name
));
821 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
823 ret
= ask_sessiond(&lsm
, (void**) events
);
828 return ret
/ sizeof(struct lttng_event
);
832 * Sets the tracing_group variable with name.
833 * This function allocates memory pointed to by tracing_group.
834 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
836 int lttng_set_tracing_group(const char *name
)
842 if (asprintf(&tracing_group
, "%s", name
) < 0) {
850 * Returns size of returned session payload data or a negative error code.
852 int lttng_calibrate(struct lttng_handle
*handle
,
853 struct lttng_calibrate
*calibrate
)
855 struct lttcomm_session_msg lsm
;
857 /* Safety check. NULL pointer are forbidden */
858 if (handle
== NULL
|| calibrate
== NULL
) {
862 lsm
.cmd_type
= LTTNG_CALIBRATE
;
863 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
865 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
867 return ask_sessiond(&lsm
, NULL
);
871 * Set default channel attributes.
872 * If either or both of the arguments are null, attr content is zeroe'd.
874 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
875 struct lttng_channel_attr
*attr
)
877 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
880 if (attr
== NULL
|| domain
== NULL
) {
884 switch (domain
->type
) {
885 case LTTNG_DOMAIN_KERNEL
:
886 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
887 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
888 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
890 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
891 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
892 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
894 case LTTNG_DOMAIN_UST
:
896 case LTTNG_DOMAIN_UST_EXEC_NAME
:
897 case LTTNG_DOMAIN_UST_PID
:
898 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
900 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
901 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
902 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
904 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
905 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
906 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
909 /* Default behavior: leave set to 0. */
915 * Check if session daemon is alive.
917 * Return 1 if alive or 0 if not.
918 * On error returns a negative value.
920 int lttng_session_daemon_alive(void)
924 ret
= set_session_daemon_path();
930 if (strlen(sessiond_sock_path
) == 0) {
931 /* No socket path set. Weird error */
935 ret
= try_connect_sessiond(sessiond_sock_path
);
948 static void __attribute__((constructor
)) init()
950 /* Set default session group */
951 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);