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 <lttng/lttng.h>
33 #include <lttng-sessiond-comm.h>
35 #include "lttng-share.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_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 0, 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
));
156 grp_id
= getgroups(grp_list_size
, grp_list
);
162 for (i
= 0; i
< grp_list_size
; i
++) {
163 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
177 * Set sessiond socket path by putting it in the global sessiond_sock_path
180 static int set_session_daemon_path(void)
184 /* Are we in the tracing group ? */
185 ret
= check_tracing_group(tracing_group
);
186 if (ret
< 0 && getuid() != 0) {
187 if (snprintf(sessiond_sock_path
, PATH_MAX
,
188 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
189 getenv("HOME")) < 0) {
193 copy_string(sessiond_sock_path
,
194 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
202 * Connect to the LTTng session daemon.
204 * On success, return 0. On error, return -1.
206 static int connect_sessiond(void)
210 ret
= set_session_daemon_path();
215 /* Connect to the sesssion daemon */
216 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
221 sessiond_socket
= ret
;
228 * Clean disconnect the session daemon.
230 static int disconnect_sessiond(void)
235 ret
= lttcomm_close_unix_sock(sessiond_socket
);
244 * Ask the session daemon a specific command and put the data into buf.
246 * Return size of data (only payload, not header).
248 static int ask_sessiond(struct lttcomm_session_msg
*lsm
, void **buf
)
253 struct lttcomm_lttng_msg llm
;
255 ret
= connect_sessiond();
260 /* Send command to session daemon */
261 ret
= send_session_msg(lsm
);
266 /* Get header from data transmission */
267 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
272 /* Check error code if OK */
273 if (llm
.ret_code
!= LTTCOMM_OK
) {
278 size
= llm
.data_size
;
284 data
= (void*) malloc(size
);
286 /* Get payload data */
287 ret
= recv_data_sessiond(data
, size
);
297 disconnect_sessiond();
302 * Create lttng handle and return pointer.
304 struct lttng_handle
*lttng_create_handle(const char *session_name
,
305 struct lttng_domain
*domain
)
307 struct lttng_handle
*handle
;
309 handle
= malloc(sizeof(struct lttng_handle
));
310 if (handle
== NULL
) {
311 perror("malloc handle");
315 /* Copy session name */
316 copy_string(handle
->session_name
, session_name
,
317 sizeof(handle
->session_name
));
319 /* Copy lttng domain */
320 copy_lttng_domain(&handle
->domain
, domain
);
327 * Destroy handle by free(3) the pointer.
329 void lttng_destroy_handle(struct lttng_handle
*handle
)
337 * Start tracing for all trace of the session.
339 int lttng_start_tracing(struct lttng_handle
*handle
)
341 struct lttcomm_session_msg lsm
;
347 lsm
.cmd_type
= LTTNG_START_TRACE
;
348 copy_string(lsm
.session
.name
, handle
->session_name
,
349 sizeof(lsm
.session
.name
));
351 return ask_sessiond(&lsm
, NULL
);
355 * Stop tracing for all trace of the session.
357 int lttng_stop_tracing(struct lttng_handle
*handle
)
359 struct lttcomm_session_msg lsm
;
361 lsm
.cmd_type
= LTTNG_STOP_TRACE
;
362 copy_string(lsm
.session
.name
, handle
->session_name
,
363 sizeof(lsm
.session
.name
));
365 return ask_sessiond(&lsm
, NULL
);
369 * Add context to event or/and channel.
371 int lttng_add_context(struct lttng_handle
*handle
,
372 struct lttng_event_context
*ctx
, const char *event_name
,
373 const char *channel_name
)
375 struct lttcomm_session_msg lsm
;
381 lsm
.cmd_type
= LTTNG_ADD_CONTEXT
;
383 /* Copy channel name */
384 copy_string(lsm
.u
.context
.channel_name
, channel_name
,
385 sizeof(lsm
.u
.context
.channel_name
));
386 /* Copy event name */
387 copy_string(lsm
.u
.context
.event_name
, event_name
,
388 sizeof(lsm
.u
.context
.event_name
));
390 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
393 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
396 copy_string(lsm
.session
.name
, handle
->session_name
,
397 sizeof(lsm
.session
.name
));
399 return ask_sessiond(&lsm
, NULL
);
405 int lttng_enable_event(struct lttng_handle
*handle
,
406 struct lttng_event
*ev
, const char *channel_name
)
408 struct lttcomm_session_msg lsm
;
414 if (channel_name
== NULL
) {
415 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
,
416 sizeof(lsm
.u
.enable
.channel_name
));
418 copy_string(lsm
.u
.enable
.channel_name
, channel_name
,
419 sizeof(lsm
.u
.enable
.channel_name
));
422 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
425 lsm
.cmd_type
= LTTNG_ENABLE_EVENT
;
426 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(lsm
.u
.enable
.event
));
428 lsm
.cmd_type
= LTTNG_ENABLE_ALL_EVENT
;
431 copy_string(lsm
.session
.name
, handle
->session_name
,
432 sizeof(lsm
.session
.name
));
434 return ask_sessiond(&lsm
, NULL
);
438 * Disable event of a channel and domain.
440 int lttng_disable_event(struct lttng_handle
*handle
, const char *name
,
441 const char *channel_name
)
443 struct lttcomm_session_msg lsm
;
450 copy_string(lsm
.u
.disable
.channel_name
, channel_name
,
451 sizeof(lsm
.u
.disable
.channel_name
));
453 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
,
454 sizeof(lsm
.u
.disable
.channel_name
));
457 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
460 copy_string(lsm
.u
.disable
.name
, name
, sizeof(lsm
.u
.disable
.name
));
461 lsm
.cmd_type
= LTTNG_DISABLE_EVENT
;
463 lsm
.cmd_type
= LTTNG_DISABLE_ALL_EVENT
;
466 copy_string(lsm
.session
.name
, handle
->session_name
,
467 sizeof(lsm
.session
.name
));
469 return ask_sessiond(&lsm
, NULL
);
473 * Enable channel per domain
475 int lttng_enable_channel(struct lttng_handle
*handle
,
476 struct lttng_channel
*chan
)
478 struct lttcomm_session_msg lsm
;
485 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(lsm
.u
.channel
.chan
));
488 lsm
.cmd_type
= LTTNG_ENABLE_CHANNEL
;
490 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
492 copy_string(lsm
.session
.name
, handle
->session_name
,
493 sizeof(lsm
.session
.name
));
495 return ask_sessiond(&lsm
, NULL
);
499 * All tracing will be stopped for registered events of the channel.
501 int lttng_disable_channel(struct lttng_handle
*handle
, const char *name
)
503 struct lttcomm_session_msg lsm
;
510 copy_string(lsm
.u
.disable
.channel_name
, name
,
511 sizeof(lsm
.u
.disable
.channel_name
));
514 lsm
.cmd_type
= LTTNG_DISABLE_CHANNEL
;
516 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
518 copy_string(lsm
.session
.name
, handle
->session_name
,
519 sizeof(lsm
.session
.name
));
521 return ask_sessiond(&lsm
, NULL
);
525 * List all available tracepoints of domain.
527 * Return the size (bytes) of the list and set the events array.
528 * On error, return negative value.
530 int lttng_list_tracepoints(struct lttng_handle
*handle
,
531 struct lttng_event
**events
)
534 struct lttcomm_session_msg lsm
;
540 lsm
.cmd_type
= LTTNG_LIST_TRACEPOINTS
;
541 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
543 ret
= ask_sessiond(&lsm
, (void **) events
);
548 return ret
/ sizeof(struct lttng_event
);
552 * Return a human readable string of code
554 const char *lttng_get_readable_code(int code
)
556 if (code
> -LTTCOMM_OK
) {
557 return "Ended with errors";
560 return lttcomm_get_readable_code(code
);
564 * Create a brand new session using name.
566 int lttng_create_session(const char *name
, const char *path
)
568 struct lttcomm_session_msg lsm
;
570 lsm
.cmd_type
= LTTNG_CREATE_SESSION
;
571 copy_string(lsm
.session
.name
, name
, sizeof(lsm
.session
.name
));
572 copy_string(lsm
.session
.path
, path
, sizeof(lsm
.session
.path
));
574 return ask_sessiond(&lsm
, NULL
);
578 * Destroy session using name.
580 int lttng_destroy_session(struct lttng_handle
*handle
)
582 struct lttcomm_session_msg lsm
;
588 lsm
.cmd_type
= LTTNG_DESTROY_SESSION
;
589 copy_string(lsm
.session
.name
, handle
->session_name
,
590 sizeof(lsm
.session
.name
));
592 return ask_sessiond(&lsm
, NULL
);
596 * Ask the session daemon for all available sessions.
598 * Return number of session.
599 * On error, return negative value.
601 int lttng_list_sessions(struct lttng_session
**sessions
)
604 struct lttcomm_session_msg lsm
;
606 lsm
.cmd_type
= LTTNG_LIST_SESSIONS
;
607 ret
= ask_sessiond(&lsm
, (void**) sessions
);
612 return ret
/ sizeof(struct lttng_session
);
616 * List domain of a session.
618 int lttng_list_domains(struct lttng_handle
*handle
,
619 struct lttng_domain
**domains
)
622 struct lttcomm_session_msg lsm
;
628 lsm
.cmd_type
= LTTNG_LIST_DOMAINS
;
630 copy_string(lsm
.session
.name
, handle
->session_name
,
631 sizeof(lsm
.session
.name
));
633 ret
= ask_sessiond(&lsm
, (void**) domains
);
638 return ret
/ sizeof(struct lttng_domain
);
642 * List channels of a session
644 int lttng_list_channels(struct lttng_handle
*handle
,
645 struct lttng_channel
**channels
)
648 struct lttcomm_session_msg lsm
;
654 lsm
.cmd_type
= LTTNG_LIST_CHANNELS
;
655 copy_string(lsm
.session
.name
, handle
->session_name
,
656 sizeof(lsm
.session
.name
));
658 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
660 ret
= ask_sessiond(&lsm
, (void**) channels
);
665 return ret
/ sizeof(struct lttng_channel
);
669 * List events of a session channel.
671 int lttng_list_events(struct lttng_handle
*handle
,
672 const char *channel_name
, struct lttng_event
**events
)
675 struct lttcomm_session_msg lsm
;
681 lsm
.cmd_type
= LTTNG_LIST_EVENTS
;
682 copy_string(lsm
.session
.name
, handle
->session_name
,
683 sizeof(lsm
.session
.name
));
684 copy_string(lsm
.u
.list
.channel_name
, channel_name
,
685 sizeof(lsm
.u
.list
.channel_name
));
687 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
689 ret
= ask_sessiond(&lsm
, (void**) events
);
694 return ret
/ sizeof(struct lttng_event
);
698 * lttng_set_tracing_group
700 * Set tracing group variable with name. This function
701 * allocate memory pointed by tracing_group.
703 int lttng_set_tracing_group(const char *name
)
705 if (asprintf(&tracing_group
, "%s", name
) < 0) {
715 int lttng_calibrate(struct lttng_handle
*handle
,
716 struct lttng_calibrate
*calibrate
)
718 struct lttcomm_session_msg lsm
;
724 lsm
.cmd_type
= LTTNG_CALIBRATE
;
725 copy_lttng_domain(&lsm
.domain
, &handle
->domain
);
727 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(lsm
.u
.calibrate
));
729 return ask_sessiond(&lsm
, NULL
);
733 * lttng_check_session_daemon
737 * Error, return negative value
739 int lttng_session_daemon_alive(void)
743 ret
= set_session_daemon_path();
749 /* If socket exist, we check if the daemon listens to connect. */
750 ret
= access(sessiond_sock_path
, F_OK
);
756 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
761 ret
= lttcomm_close_unix_sock(ret
);
763 perror("lttcomm_close_unix_sock");
772 static void __attribute__((constructor
)) init()
774 /* Set default session group */
775 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);