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/sessiond-comm/sessiond-comm.h>
32 #include <common/lttngerr.h>
33 #include <common/common.h>
34 #include <common/defaults.h>
35 #include <lttng/lttng.h>
37 /* Socket to session daemon for communication */
38 static int sessiond_socket
;
39 static char sessiond_sock_path
[PATH_MAX
];
42 static char *tracing_group
;
46 * Copy string from src to dst and enforce null terminated byte.
48 static void copy_string(char *dst
, const char *src
, size_t len
)
51 strncpy(dst
, src
, len
);
52 /* Enforce the NULL terminated byte */
60 * Copy domain to lttcomm_session_msg domain.
62 * If domain is unknown, default domain will be the kernel.
64 static void copy_lttng_domain(struct lttng_domain
*dst
, struct lttng_domain
*src
)
68 case LTTNG_DOMAIN_KERNEL
:
69 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
:
73 memcpy(dst
, src
, sizeof(struct lttng_domain
));
76 dst
->type
= LTTNG_DOMAIN_KERNEL
;
83 * Send lttcomm_session_msg to the session daemon.
85 * On success, return 0
86 * On error, return error code
88 static int send_session_msg(struct lttcomm_session_msg
*lsm
)
97 ret
= lttcomm_send_creds_unix_sock(sessiond_socket
, lsm
,
98 sizeof(struct lttcomm_session_msg
));
105 * Receive data from the sessiond socket.
107 * On success, return 0
108 * On error, return recv() error code
110 static int recv_data_sessiond(void *buf
, size_t len
)
119 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
126 * Check if the specified group name exist.
128 * If yes return 1, else return -1.
130 static int check_tracing_group(const char *grp_name
)
132 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
134 int grp_list_size
, grp_id
, i
;
137 /* Get GID of group 'tracing' */
138 grp_tracing
= getgrnam(grp_name
);
139 if (grp_tracing
== NULL
) {
140 /* NULL means not found also. getgrnam(3) */
147 /* Get number of supplementary group IDs */
148 grp_list_size
= getgroups(0, NULL
);
149 if (grp_list_size
< 0) {
154 /* Alloc group list of the right size */
155 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
160 grp_id
= getgroups(grp_list_size
, grp_list
);
166 for (i
= 0; i
< grp_list_size
; i
++) {
167 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
181 * Try connect to session daemon with sock_path.
183 * Return 0 on success, else -1
185 static int try_connect_sessiond(const char *sock_path
)
189 /* If socket exist, we check if the daemon listens for connect. */
190 ret
= access(sock_path
, F_OK
);
196 ret
= lttcomm_connect_unix_sock(sock_path
);
202 ret
= lttcomm_close_unix_sock(ret
);
204 perror("lttcomm_close_unix_sock");
211 * Set sessiond socket path by putting it in the global sessiond_sock_path
214 static int set_session_daemon_path(void)
217 int in_tgroup
= 0; /* In tracing group */
223 /* Are we in the tracing group ? */
224 in_tgroup
= check_tracing_group(tracing_group
);
229 copy_string(sessiond_sock_path
,
230 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
231 sizeof(sessiond_sock_path
));
232 } else if (in_tgroup
) {
234 copy_string(sessiond_sock_path
,
235 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
236 sizeof(sessiond_sock_path
));
238 ret
= try_connect_sessiond(sessiond_sock_path
);
240 /* Global session daemon not available */
241 if (snprintf(sessiond_sock_path
, sizeof(sessiond_sock_path
),
242 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
243 getenv("HOME")) < 0) {
248 /* Not in tracing group and not root, default */
249 if (snprintf(sessiond_sock_path
, PATH_MAX
,
250 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
251 getenv("HOME")) < 0) {
260 * Connect to the LTTng session daemon.
262 * On success, return 0. On error, return -1.
264 static int connect_sessiond(void)
268 ret
= set_session_daemon_path();
273 /* Connect to the sesssion daemon */
274 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
279 sessiond_socket
= ret
;
286 * Clean disconnect the session daemon.
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).
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.
376 struct lttng_handle
*lttng_create_handle(const char *session_name
,
377 struct lttng_domain
*domain
)
379 struct lttng_handle
*handle
;
381 handle
= malloc(sizeof(struct lttng_handle
));
382 if (handle
== NULL
) {
383 perror("malloc handle");
387 /* Copy session name */
388 copy_string(handle
->session_name
, session_name
,
389 sizeof(handle
->session_name
));
391 /* Copy lttng domain */
392 copy_lttng_domain(&handle
->domain
, domain
);
399 * Destroy handle by free(3) the pointer.
401 void lttng_destroy_handle(struct lttng_handle
*handle
)
409 * Register an outside consumer.
411 int lttng_register_consumer(struct lttng_handle
*handle
,
412 const char *socket_path
)
414 struct lttcomm_session_msg lsm
;
416 lsm
.cmd_type
= LTTNG_REGISTER_CONSUMER
;
417 copy_string(lsm
.session
.name
, handle
->session_name
,
418 sizeof(lsm
.session
.name
));
419 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
421 copy_string(lsm
.u
.reg
.path
, socket_path
, sizeof(lsm
.u
.reg
.path
));
423 return ask_sessiond(&lsm
, NULL
);
427 * Start tracing for all trace of the session.
429 int lttng_start_tracing(const char *session_name
)
431 struct lttcomm_session_msg lsm
;
433 if (session_name
== NULL
) {
437 lsm
.cmd_type
= LTTNG_START_TRACE
;
439 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
441 return ask_sessiond(&lsm
, NULL
);
445 * Stop tracing for all trace of the session.
447 int lttng_stop_tracing(const char *session_name
)
449 struct lttcomm_session_msg lsm
;
451 if (session_name
== NULL
) {
455 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
457 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
459 return ask_sessiond(&lsm
, NULL
);
463 * Add context to event or/and channel.
465 int lttng_add_context(struct lttng_handle
*handle
,
466 struct lttng_event_context
*ctx
, const char *event_name
,
467 const char *channel_name
)
469 struct lttcomm_session_msg lsm
;
471 /* Safety check. Both are mandatory */
472 if (handle
== NULL
|| ctx
== NULL
) {
476 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
478 /* Copy channel name */
479 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
480 sizeof(lsm
.u
.context
.channel_name
));
481 /* Copy event name */
482 copy_string(lsm
.u
.context
.event_name
, event_name
,
483 sizeof(lsm
.u
.context
.event_name
));
485 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
487 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
489 copy_string(lsm
.session
.name
, handle
->session_name
,
490 sizeof(lsm
.session
.name
));
492 return ask_sessiond(&lsm
, NULL
);
498 int lttng_enable_event(struct lttng_handle
*handle
,
499 struct lttng_event
*ev
, const char *channel_name
)
501 struct lttcomm_session_msg lsm
;
503 if (handle
== NULL
|| ev
== NULL
) {
507 /* If no channel name, we put the default name */
508 if (channel_name
== NULL
) {
509 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
510 sizeof(lsm
.u
.enable
.channel_name
));
512 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
513 sizeof(lsm
.u
.enable
.channel_name
));
516 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
518 if (ev
->name
[0] != '\0') {
519 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
521 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
523 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
525 copy_string(lsm
.session
.name
, handle
->session_name
,
526 sizeof(lsm
.session
.name
));
528 return ask_sessiond(&lsm
, NULL
);
532 * Disable event of a channel and domain.
534 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
535 const char *channel_name
)
537 struct lttcomm_session_msg lsm
;
539 if (handle
== NULL
) {
544 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
545 sizeof(lsm
.u
.disable
.channel_name
));
547 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
548 sizeof(lsm
.u
.disable
.channel_name
));
551 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
554 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
555 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
557 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
560 copy_string(lsm
.session
.name
, handle
->session_name
,
561 sizeof(lsm
.session
.name
));
563 return ask_sessiond(&lsm
, NULL
);
567 * Enable channel per domain
569 int lttng_enable_channel(struct lttng_handle
*handle
,
570 struct lttng_channel
*chan
)
572 struct lttcomm_session_msg lsm
;
575 * NULL arguments are forbidden. No default values.
577 if (handle
== NULL
|| chan
== NULL
) {
581 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
583 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
585 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
587 copy_string(lsm
.session
.name
, handle
->session_name
,
588 sizeof(lsm
.session
.name
));
590 return ask_sessiond(&lsm
, NULL
);
594 * All tracing will be stopped for registered events of the channel.
596 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
598 struct lttcomm_session_msg lsm
;
600 /* Safety check. Both are mandatory */
601 if (handle
== NULL
|| name
== NULL
) {
605 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
607 copy_string(lsm
.u
.disable
.channel_name
, name
,
608 sizeof(lsm
.u
.disable
.channel_name
));
610 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
612 copy_string(lsm
.session
.name
, handle
->session_name
,
613 sizeof(lsm
.session
.name
));
615 return ask_sessiond(&lsm
, NULL
);
619 * List all available tracepoints of domain.
621 * Return the size (bytes) of the list and set the events array.
622 * On error, return negative value.
624 int lttng_list_tracepoints(struct lttng_handle
*handle
,
625 struct lttng_event
**events
)
628 struct lttcomm_session_msg lsm
;
630 if (handle
== NULL
) {
634 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
635 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
637 ret
= ask_sessiond(&lsm
, (void **) events
);
642 return ret
/ sizeof(struct lttng_event
);
646 * Return a human readable string of code
648 const char *lttng_strerror(int code
)
650 if (code
> -LTTCOMM_OK
) {
651 return "Ended with errors";
654 return lttcomm_get_readable_code(code
);
658 * Create a brand new session using name.
660 int lttng_create_session(const char *name
, const char *path
)
662 struct lttcomm_session_msg lsm
;
664 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
665 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
666 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
668 return ask_sessiond(&lsm
, NULL
);
672 * Destroy session using name.
674 int lttng_destroy_session(const char *session_name
)
676 struct lttcomm_session_msg lsm
;
678 if (session_name
== NULL
) {
682 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
684 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
686 return ask_sessiond(&lsm
, NULL
);
690 * Ask the session daemon for all available sessions.
692 * Return number of session.
693 * On error, return negative value.
695 int lttng_list_sessions(struct lttng_session
**sessions
)
698 struct lttcomm_session_msg lsm
;
700 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
701 ret
= ask_sessiond(&lsm
, (void**) sessions
);
706 return ret
/ sizeof(struct lttng_session
);
710 * List domain of a session.
712 int lttng_list_domains(const char *session_name
,
713 struct lttng_domain
**domains
)
716 struct lttcomm_session_msg lsm
;
718 if (session_name
== NULL
) {
722 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
724 copy_string(lsm
.session
.name
, session_name
, sizeof(lsm
.session
.name
));
726 ret
= ask_sessiond(&lsm
, (void**) domains
);
731 return ret
/ sizeof(struct lttng_domain
);
735 * List channels of a session
737 int lttng_list_channels(struct lttng_handle
*handle
,
738 struct lttng_channel
**channels
)
741 struct lttcomm_session_msg lsm
;
743 if (handle
== NULL
) {
747 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
748 copy_string(lsm
.session
.name
, handle
->session_name
,
749 sizeof(lsm
.session
.name
));
751 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
753 ret
= ask_sessiond(&lsm
, (void**) channels
);
758 return ret
/ sizeof(struct lttng_channel
);
762 * List events of a session channel.
764 int lttng_list_events(struct lttng_handle
*handle
,
765 const char *channel_name
, struct lttng_event
**events
)
768 struct lttcomm_session_msg lsm
;
770 /* Safety check. An handle and channel name are mandatory */
771 if (handle
== NULL
|| channel_name
== NULL
) {
775 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
776 copy_string(lsm
.session
.name
, handle
->session_name
,
777 sizeof(lsm
.session
.name
));
778 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
779 sizeof(lsm
.u
.list
.channel_name
));
781 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
783 ret
= ask_sessiond(&lsm
, (void**) events
);
788 return ret
/ sizeof(struct lttng_event
);
792 * Set tracing group variable with name. This function allocate memory pointed
795 int lttng_set_tracing_group(const char *name
)
801 if (asprintf(&tracing_group
, "%s", name
) < 0) {
811 int lttng_calibrate(struct lttng_handle
*handle
,
812 struct lttng_calibrate
*calibrate
)
814 struct lttcomm_session_msg lsm
;
816 /* Safety check. NULL pointer are forbidden */
817 if (handle
== NULL
|| calibrate
== NULL
) {
821 lsm
.cmd_type
= LTTNG_CALIBRATE
;
822 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
824 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
826 return ask_sessiond(&lsm
, NULL
);
830 * Set default channel attributes.
832 void lttng_channel_set_default_attr(struct lttng_domain
*domain
,
833 struct lttng_channel_attr
*attr
)
836 if (attr
== NULL
|| domain
== NULL
) {
840 switch (domain
->type
) {
841 case LTTNG_DOMAIN_KERNEL
:
842 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
843 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
844 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
846 attr
->subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
847 attr
->num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
848 attr
->output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
850 case LTTNG_DOMAIN_UST
:
851 case LTTNG_DOMAIN_UST_EXEC_NAME
:
852 case LTTNG_DOMAIN_UST_PID
:
853 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
854 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
855 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
856 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
858 attr
->subbuf_size
= DEFAULT_UST_CHANNEL_SUBBUF_SIZE
;
859 attr
->num_subbuf
= DEFAULT_UST_CHANNEL_SUBBUF_NUM
;
860 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
863 /* Default behavior */
864 memset(attr
, 0, sizeof(struct lttng_channel_attr
));
870 * Check if session daemon is alive.
872 * Return 1 if alive or 0 if not.
875 int lttng_session_daemon_alive(void)
879 ret
= set_session_daemon_path();
885 if (strlen(sessiond_sock_path
) == 0) {
886 /* No socket path set. Weird error */
890 ret
= try_connect_sessiond(sessiond_sock_path
);
903 static void __attribute__((constructor
)) init()
905 /* Set default session group */
906 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);