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 dst
->type
= LTTNG_DOMAIN_KERNEL
;
84 * Send lttcomm_session_msg to the session daemon.
86 * On success, returns the number of bytes sent (>=0)
87 * On error, returns -1
89 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
98 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
99 sizeof(struct lttcomm_session_msg
));
106 * Receive data from the sessiond socket.
108 * On success, returns the number of bytes received (>=0)
109 * On error, returns -1 (recvmsg() error) or -ENOTCONN
111 static int recv_data_sessiond(void *buf
, size_t len
)
120 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
127 * Check if we are in the specified group.
129 * If yes return 1, else return -1.
131 static int check_tracing_group(const char *grp_name
)
133 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
135 int grp_list_size
, grp_id
, i
;
138 /* Get GID of group 'tracing' */
139 grp_tracing
= getgrnam(grp_name
);
140 if (grp_tracing
== NULL
) {
141 /* NULL means not found also. getgrnam(3) */
148 /* Get number of supplementary group IDs */
149 grp_list_size
= getgroups(0, NULL
);
150 if (grp_list_size
< 0) {
155 /* Alloc group list of the right size */
156 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
161 grp_id
= getgroups(grp_list_size
, grp_list
);
167 for (i
= 0; i
< grp_list_size
; i
++) {
168 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
182 * Try connect to session daemon with sock_path.
184 * Return 0 on success, else -1
186 static int try_connect_sessiond(const char *sock_path
)
190 /* If socket exist, we check if the daemon listens for connect. */
191 ret
= access(sock_path
, F_OK
);
197 ret
= lttcomm_connect_unix_sock(sock_path
);
203 ret
= lttcomm_close_unix_sock(ret
);
205 perror("lttcomm_close_unix_sock");
212 * Set sessiond socket path by putting it in the global
213 * sessiond_sock_path variable.
214 * Returns 0 on success,
215 * -ENOMEM on failure (the sessiond socket path is somehow too long)
217 static int set_session_daemon_path(void)
220 int in_tgroup
= 0; /* In tracing group */
226 /* Are we in the tracing group ? */
227 in_tgroup
= check_tracing_group(tracing_group
);
232 copy_string(sessiond_sock_path
,
233 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
234 sizeof(sessiond_sock_path
));
235 } else if (in_tgroup
) {
237 copy_string(sessiond_sock_path
,
238 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
239 sizeof(sessiond_sock_path
));
241 ret
= try_connect_sessiond(sessiond_sock_path
);
243 /* Global session daemon not available */
244 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
245 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
246 getenv("HOME")) < 0) {
251 /* Not in tracing group and not root, default */
252 if (snprintf(sessiond_sock_path
, PATH_MAX
,
253 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
254 getenv("HOME")) < 0) {
263 * Connect to the LTTng session daemon.
265 * On success, return 0. On error, return -1.
267 static int connect_sessiond(void)
271 ret
= set_session_daemon_path();
273 return -1; /* set_session_daemon_path() returns -ENOMEM */
276 /* Connect to the sesssion daemon */
277 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
282 sessiond_socket
= ret
;
289 * Clean disconnect from the session daemon.
290 * On success, return 0. On error, return -1.
292 static int disconnect_sessiond(void)
297 ret
= lttcomm_close_unix_sock(sessiond_socket
);
306 * Ask the session daemon a specific command and put the data into buf.
308 * Return size of data (only payload, not header) or a negative error code.
310 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
315 struct lttcomm_lttng_msg llm
;
317 ret
= connect_sessiond();
322 /* Send command to session daemon */
323 ret
= send_session_msg(lsm
);
328 /* Get header from data transmission */
329 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
334 /* Check error code if OK */
335 if (llm
.ret_code
!= LTTCOMM_OK
) {
340 size
= llm
.data_size
;
342 /* If client free with size 0 */
350 data
= (void*) malloc(size
);
352 /* Get payload data */
353 ret
= recv_data_sessiond(data
, size
);
360 * Extra protection not to dereference a NULL pointer. If buf is NULL at
361 * this point, an error is returned and data is freed.
373 disconnect_sessiond();
378 * Create lttng handle and return pointer.
379 * The returned pointer will be NULL in case of malloc() error.
381 struct lttng_handle
*lttng_create_handle(const char *session_name
,
382 struct lttng_domain
*domain
)
384 struct lttng_handle
*handle
;
386 handle
= malloc(sizeof(struct lttng_handle
));
387 if (handle
== NULL
) {
388 perror("malloc handle");
392 /* Copy session name */
393 copy_string(handle
->session_name
, session_name
,
394 sizeof(handle
->session_name
));
396 /* Copy lttng domain */
397 copy_lttng_domain(&handle
->domain
, domain
);
404 * Destroy handle by free(3) the pointer.
406 void lttng_destroy_handle(struct lttng_handle
*handle
)
414 * Register an outside consumer.
415 * Returns size of returned session payload data or a negative error code.
417 int lttng_register_consumer(struct lttng_handle
*handle
,
418 const char *socket_path
)
420 struct lttcomm_session_msg lsm
;
422 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
423 copy_string(lsm
.session
.name
, handle
->session_name
,
424 sizeof(lsm
.session
.name
));
425 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
427 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
429 return ask_sessiond(&lsm
, NULL
);
433 * Start tracing for all traces of the session.
434 * Returns size of returned session payload data or a negative error code.
436 int lttng_start_tracing(const char *session_name
)
438 struct lttcomm_session_msg lsm
;
440 if (session_name
== NULL
) {
444 lsm
.cmd_type
= LTTNG_START_TRACE
;
446 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
448 return ask_sessiond(&lsm
, NULL
);
452 * Stop tracing for all traces of the session.
453 * Returns size of returned session payload data or a negative error code.
455 int lttng_stop_tracing(const char *session_name
)
457 struct lttcomm_session_msg lsm
;
459 if (session_name
== NULL
) {
463 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
465 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
467 return ask_sessiond(&lsm
, NULL
);
471 * Add context to event or/and channel.
473 * Returns the size of the returned payload data or a negative error code.
475 int lttng_add_context(struct lttng_handle
*handle
,
476 struct lttng_event_context
*ctx
, const char *event_name
,
477 const char *channel_name
)
479 struct lttcomm_session_msg lsm
;
481 /* Safety check. Both are mandatory */
482 if (handle
== NULL
|| ctx
== NULL
) {
486 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
488 /* Copy channel name */
489 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
490 sizeof(lsm
.u
.context
.channel_name
));
491 /* Copy event name */
492 copy_string(lsm
.u
.context
.event_name
, event_name
,
493 sizeof(lsm
.u
.context
.event_name
));
495 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
497 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
499 copy_string(lsm
.session
.name
, handle
->session_name
,
500 sizeof(lsm
.session
.name
));
502 return ask_sessiond(&lsm
, NULL
);
506 * Enable event(s) for a channel.
507 * If no event name is specified, all events are enabled.
508 * If no channel name is specified, the default 'channel0' is used.
509 * Returns size of returned session payload data or a negative error code.
511 int lttng_enable_event(struct lttng_handle
*handle
,
512 struct lttng_event
*ev
, const char *channel_name
)
514 struct lttcomm_session_msg lsm
;
516 if (handle
== NULL
|| ev
== NULL
) {
520 /* If no channel name, we put the default name */
521 if (channel_name
== NULL
) {
522 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
523 sizeof(lsm
.u
.enable
.channel_name
));
525 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
526 sizeof(lsm
.u
.enable
.channel_name
));
529 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
531 if (ev
->name
[0] != '\0') {
532 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
534 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
536 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
538 copy_string(lsm
.session
.name
, handle
->session_name
,
539 sizeof(lsm
.session
.name
));
541 return ask_sessiond(&lsm
, NULL
);
545 * Disable event(s) of a channel and domain.
546 * If no event name is specified, all events are disabled.
547 * If no channel name is specified, the default 'channel0' is used.
548 * Returns size of returned session payload data or a negative error code.
550 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
551 const char *channel_name
)
553 struct lttcomm_session_msg lsm
;
555 if (handle
== NULL
) {
560 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
561 sizeof(lsm
.u
.disable
.channel_name
));
563 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
564 sizeof(lsm
.u
.disable
.channel_name
));
567 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
570 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
571 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
573 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
576 copy_string(lsm
.session
.name
, handle
->session_name
,
577 sizeof(lsm
.session
.name
));
579 return ask_sessiond(&lsm
, NULL
);
583 * Enable channel per domain
584 * Returns size of returned session payload data or a negative error code.
586 int lttng_enable_channel(struct lttng_handle
*handle
,
587 struct lttng_channel
*chan
)
589 struct lttcomm_session_msg lsm
;
592 * NULL arguments are forbidden. No default values.
594 if (handle
== NULL
|| chan
== NULL
) {
598 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
600 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
602 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
604 copy_string(lsm
.session
.name
, handle
->session_name
,
605 sizeof(lsm
.session
.name
));
607 return ask_sessiond(&lsm
, NULL
);
611 * All tracing will be stopped for registered events of the channel.
612 * Returns size of returned session payload data or a negative error code.
614 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
616 struct lttcomm_session_msg lsm
;
618 /* Safety check. Both are mandatory */
619 if (handle
== NULL
|| name
== NULL
) {
623 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
625 copy_string(lsm
.u
.disable
.channel_name
, name
,
626 sizeof(lsm
.u
.disable
.channel_name
));
628 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
630 copy_string(lsm
.session
.name
, handle
->session_name
,
631 sizeof(lsm
.session
.name
));
633 return ask_sessiond(&lsm
, NULL
);
637 * Lists all available tracepoints of domain.
638 * Sets the contents of the events array.
639 * Returns the number of lttng_event entries in events;
640 * on error, returns a negative value.
642 int lttng_list_tracepoints(struct lttng_handle
*handle
,
643 struct lttng_event
**events
)
646 struct lttcomm_session_msg lsm
;
648 if (handle
== NULL
) {
652 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
653 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
655 ret
= ask_sessiond(&lsm
, (void **) events
);
660 return ret
/ sizeof(struct lttng_event
);
664 * Returns a human readable string describing
665 * the error code (a negative value).
667 const char *lttng_strerror(int code
)
669 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
670 if (code
> -LTTCOMM_OK
) {
671 return "Ended with errors";
674 return lttcomm_get_readable_code(code
);
678 * Create a brand new session using name and path.
679 * Returns size of returned session payload data or a negative error code.
681 int lttng_create_session(const char *name
, const char *path
)
683 struct lttcomm_session_msg lsm
;
685 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
686 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
687 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
689 return ask_sessiond(&lsm
, NULL
);
693 * Destroy session using name.
694 * Returns size of returned session payload data or a negative error code.
696 int lttng_destroy_session(const char *session_name
)
698 struct lttcomm_session_msg lsm
;
700 if (session_name
== NULL
) {
704 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
706 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
708 return ask_sessiond(&lsm
, NULL
);
712 * Ask the session daemon for all available sessions.
713 * Sets the contents of the sessions array.
714 * Returns the number of lttng_session entries in sessions;
715 * on error, returns a negative value.
717 int lttng_list_sessions(struct lttng_session
**sessions
)
720 struct lttcomm_session_msg lsm
;
722 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
723 ret
= ask_sessiond(&lsm
, (void**) sessions
);
728 return ret
/ sizeof(struct lttng_session
);
732 * Ask the session daemon for all available domains of a session.
733 * Sets the contents of the domains array.
734 * Returns the number of lttng_domain entries in domains;
735 * on error, returns a negative value.
737 int lttng_list_domains(const char *session_name
,
738 struct lttng_domain
**domains
)
741 struct lttcomm_session_msg lsm
;
743 if (session_name
== NULL
) {
747 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
749 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
751 ret
= ask_sessiond(&lsm
, (void**) domains
);
756 return ret
/ sizeof(struct lttng_domain
);
760 * Ask the session daemon for all available channels of a session.
761 * Sets the contents of the channels array.
762 * Returns the number of lttng_channel entries in channels;
763 * on error, returns a negative value.
765 int lttng_list_channels(struct lttng_handle
*handle
,
766 struct lttng_channel
**channels
)
769 struct lttcomm_session_msg lsm
;
771 if (handle
== NULL
) {
775 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
776 copy_string(lsm
.session
.name
, handle
->session_name
,
777 sizeof(lsm
.session
.name
));
779 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
781 ret
= ask_sessiond(&lsm
, (void**) channels
);
786 return ret
/ sizeof(struct lttng_channel
);
790 * Ask the session daemon for all available events of a session channel.
791 * Sets the contents of the events array.
792 * Returns the number of lttng_event entries in events;
793 * on error, returns a negative value.
795 int lttng_list_events(struct lttng_handle
*handle
,
796 const char *channel_name
, struct lttng_event
**events
)
799 struct lttcomm_session_msg lsm
;
801 /* Safety check. An handle and channel name are mandatory */
802 if (handle
== NULL
|| channel_name
== NULL
) {
806 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
807 copy_string(lsm
.session
.name
, handle
->session_name
,
808 sizeof(lsm
.session
.name
));
809 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
810 sizeof(lsm
.u
.list
.channel_name
));
812 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
814 ret
= ask_sessiond(&lsm
, (void**) events
);
819 return ret
/ sizeof(struct lttng_event
);
823 * Sets the tracing_group variable with name.
824 * This function allocates memory pointed to by tracing_group.
825 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
827 int lttng_set_tracing_group(const char *name
)
833 if (asprintf(&tracing_group
, "%s", name
) < 0) {
841 * Returns size of returned session payload data or a negative error code.
843 int lttng_calibrate(struct lttng_handle
*handle
,
844 struct lttng_calibrate
*calibrate
)
846 struct lttcomm_session_msg lsm
;
848 /* Safety check. NULL pointer are forbidden */
849 if (handle
== NULL
|| calibrate
== NULL
) {
853 lsm
.cmd_type
= LTTNG_CALIBRATE
;
854 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
856 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
858 return ask_sessiond(&lsm
, NULL
);
862 * Set default channel attributes.
863 * If either or both of the arguments are null, nothing happens.
865 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
866 struct lttng_channel_attr
*attr
)
869 if (attr
== NULL
|| domain
== NULL
) {
873 switch (domain
->type
) {
874 case LTTNG_DOMAIN_KERNEL
:
875 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
876 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
877 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
879 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
880 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
881 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
883 case LTTNG_DOMAIN_UST
:
885 case LTTNG_DOMAIN_UST_EXEC_NAME
:
886 case LTTNG_DOMAIN_UST_PID
:
887 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
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_UST_CHANNEL_SUBBUF_SIZE
;
894 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
895 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
898 /* Default behavior */
899 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
905 * Check if session daemon is alive.
907 * Return 1 if alive or 0 if not.
908 * On error returns a negative value.
910 int lttng_session_daemon_alive(void)
914 ret
= set_session_daemon_path();
920 if (strlen(sessiond_sock_path
) == 0) {
921 /* No socket path set. Weird error */
925 ret
= try_connect_sessiond(sessiond_sock_path
);
938 static void __attribute__((constructor
)) init()
940 /* Set default session group */
941 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);