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/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
];
41 /* Communication structure to ltt-sessiond */
42 static struct lttcomm_session_msg lsm
;
43 static struct lttcomm_lttng_msg llm
;
46 static char *tracing_group
;
50 * Copy string from src to dst and enforce null terminated byte.
52 static void copy_string(char *dst
, const char *src
, size_t len
)
55 strncpy(dst
, src
, len
);
56 /* Enforce the NULL terminated byte */
64 * Send lttcomm_session_msg to the session daemon.
66 * On success, return 0
67 * On error, return error code
69 static int send_data_sessiond(void)
78 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
87 * Receive data from the sessiond socket.
89 * On success, return 0
90 * On error, return recv() error code
92 static int recv_data_sessiond(void *buf
, size_t len
)
101 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
108 * Check if the specified group name exist.
110 * If yes return 0, else return -1.
112 static int check_tracing_group(const char *grp_name
)
114 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
116 int grp_list_size
, grp_id
, i
;
119 /* Get GID of group 'tracing' */
120 grp_tracing
= getgrnam(grp_name
);
121 if (grp_tracing
== NULL
) {
122 /* NULL means not found also. getgrnam(3) */
129 /* Get number of supplementary group IDs */
130 grp_list_size
= getgroups(0, NULL
);
131 if (grp_list_size
< 0) {
136 /* Alloc group list of the right size */
137 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
138 grp_id
= getgroups(grp_list_size
, grp_list
);
144 for (i
= 0; i
< grp_list_size
; i
++) {
145 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
159 * Set sessiond socket path by putting it in the global sessiond_sock_path
162 static int set_session_daemon_path(void)
166 /* Are we in the tracing group ? */
167 ret
= check_tracing_group(tracing_group
);
168 if (ret
< 0 && getuid() != 0) {
169 if (snprintf(sessiond_sock_path
, PATH_MAX
,
170 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
171 getenv("HOME")) < 0) {
175 copy_string(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
183 * Connect to the LTTng session daemon.
185 * On success, return 0. On error, return -1.
187 static int connect_sessiond(void)
191 ret
= set_session_daemon_path();
196 /* Connect to the sesssion daemon */
197 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
202 sessiond_socket
= ret
;
209 * Clean disconnect the session daemon.
211 static int disconnect_sessiond(void)
216 ret
= lttcomm_close_unix_sock(sessiond_socket
);
225 * Reset the session message structure.
227 static void reset_session_msg(void)
229 memset(&lsm
, 0, sizeof(struct lttcomm_session_msg
));
235 * Ask the session daemon a specific command and put the data into buf.
237 * Return size of data (only payload, not header).
239 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
245 ret
= connect_sessiond();
252 /* Send command to session daemon */
253 ret
= send_data_sessiond();
258 /* Get header from data transmission */
259 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
264 /* Check error code if OK */
265 if (llm
.ret_code
!= LTTCOMM_OK
) {
270 size
= llm
.data_size
;
276 data
= (void*) malloc(size
);
278 /* Get payload data */
279 ret
= recv_data_sessiond(data
, size
);
289 disconnect_sessiond();
295 * Copy domain to lttcomm_session_msg domain. If unknown domain, default domain
296 * will be the kernel.
298 static void copy_lttng_domain(struct lttng_domain
*dom
)
302 case LTTNG_DOMAIN_KERNEL
:
303 case LTTNG_DOMAIN_UST
:
304 case LTTNG_DOMAIN_UST_EXEC_NAME
:
305 case LTTNG_DOMAIN_UST_PID
:
306 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
307 memcpy(&lsm
.domain
, dom
, sizeof(struct lttng_domain
));
310 lsm
.domain
.type
= LTTNG_DOMAIN_KERNEL
;
317 * Start tracing for all trace of the session.
319 int lttng_start_tracing(const char *session_name
)
321 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
322 return ask_sessiond(LTTNG_START_TRACE
, NULL
);
326 * Stop tracing for all trace of the session.
328 int lttng_stop_tracing(const char *session_name
)
330 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
331 return ask_sessiond(LTTNG_STOP_TRACE
, NULL
);
337 int lttng_add_context(struct lttng_domain
*domain
,
338 struct lttng_event_context
*ctx
, const char *event_name
,
339 const char *channel_name
)
341 copy_string(lsm
.u
.context
.channel_name
, channel_name
, NAME_MAX
);
342 copy_string(lsm
.u
.context
.event_name
, event_name
, NAME_MAX
);
343 copy_lttng_domain(domain
);
346 memcpy(&lsm
.u
.context
.ctx
, ctx
, sizeof(struct lttng_event_context
));
349 return ask_sessiond(LTTNG_ADD_CONTEXT
, NULL
);
355 int lttng_enable_event(struct lttng_domain
*domain
,
356 struct lttng_event
*ev
, const char *channel_name
)
360 if (channel_name
== NULL
) {
361 copy_string(lsm
.u
.enable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
363 copy_string(lsm
.u
.enable
.channel_name
, channel_name
, NAME_MAX
);
366 copy_lttng_domain(domain
);
369 ret
= ask_sessiond(LTTNG_ENABLE_ALL_EVENT
, NULL
);
371 memcpy(&lsm
.u
.enable
.event
, ev
, sizeof(struct lttng_event
));
372 ret
= ask_sessiond(LTTNG_ENABLE_EVENT
, NULL
);
379 * Disable event of a channel and domain.
381 int lttng_disable_event(struct lttng_domain
*domain
, const char *name
,
382 const char *channel_name
)
386 if (channel_name
== NULL
) {
387 copy_string(lsm
.u
.disable
.channel_name
, DEFAULT_CHANNEL_NAME
, NAME_MAX
);
389 copy_string(lsm
.u
.disable
.channel_name
, channel_name
, NAME_MAX
);
392 copy_lttng_domain(domain
);
395 ret
= ask_sessiond(LTTNG_DISABLE_ALL_EVENT
, NULL
);
397 copy_string(lsm
.u
.disable
.name
, name
, NAME_MAX
);
398 ret
= ask_sessiond(LTTNG_DISABLE_EVENT
, NULL
);
405 * Enable channel per domain
407 int lttng_enable_channel(struct lttng_domain
*domain
,
408 struct lttng_channel
*chan
)
411 memcpy(&lsm
.u
.channel
.chan
, chan
, sizeof(struct lttng_channel
));
414 copy_lttng_domain(domain
);
416 return ask_sessiond(LTTNG_ENABLE_CHANNEL
, NULL
);
420 * All tracing will be stopped for registered events of the channel.
422 int lttng_disable_channel(struct lttng_domain
*domain
, const char *name
)
424 copy_string(lsm
.u
.disable
.channel_name
, name
, NAME_MAX
);
425 copy_lttng_domain(domain
);
427 return ask_sessiond(LTTNG_DISABLE_CHANNEL
, NULL
);
431 * List all available tracepoints of domain.
433 * Return the size (bytes) of the list and set the events array.
434 * On error, return negative value.
436 int lttng_list_tracepoints(struct lttng_domain
*domain
,
437 struct lttng_event
**events
)
441 copy_lttng_domain(domain
);
443 ret
= ask_sessiond(LTTNG_LIST_TRACEPOINTS
, (void **) events
);
448 return ret
/ sizeof(struct lttng_event
);
452 * Return a human readable string of code
454 const char *lttng_get_readable_code(int code
)
456 if (code
> -LTTCOMM_OK
) {
457 return "Ended with errors";
460 return lttcomm_get_readable_code(code
);
464 * Create a brand new session using name.
466 int lttng_create_session(const char *name
, const char *path
)
468 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
469 copy_string(lsm
.session
.path
, path
, PATH_MAX
);
470 return ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
474 * Destroy session using name.
476 int lttng_destroy_session(const char *name
)
478 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
479 return ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
483 * Ask the session daemon for all available sessions.
485 * Return number of session.
486 * On error, return negative value.
488 int lttng_list_sessions(struct lttng_session
**sessions
)
492 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
497 return ret
/ sizeof(struct lttng_session
);
501 * List domain of a session.
503 int lttng_list_domains(const char *session_name
, struct lttng_domain
**domains
)
507 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
508 ret
= ask_sessiond(LTTNG_LIST_DOMAINS
, (void**) domains
);
513 return ret
/ sizeof(struct lttng_domain
);
517 * List channels of a session
519 int lttng_list_channels(struct lttng_domain
*domain
,
520 const char *session_name
, struct lttng_channel
**channels
)
524 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
525 copy_lttng_domain(domain
);
527 ret
= ask_sessiond(LTTNG_LIST_CHANNELS
, (void**) channels
);
532 return ret
/ sizeof(struct lttng_channel
);
536 * List events of a session channel.
538 int lttng_list_events(struct lttng_domain
*domain
,
539 const char *session_name
, const char *channel_name
,
540 struct lttng_event
**events
)
544 copy_string(lsm
.session
.name
, session_name
, NAME_MAX
);
545 copy_string(lsm
.u
.list
.channel_name
, channel_name
, NAME_MAX
);
546 copy_lttng_domain(domain
);
548 ret
= ask_sessiond(LTTNG_LIST_EVENTS
, (void**) events
);
553 return ret
/ sizeof(struct lttng_event
);
557 * Set session name for the current lsm.
559 void lttng_set_session_name(const char *name
)
561 copy_string(lsm
.session
.name
, name
, NAME_MAX
);
565 * lttng_set_tracing_group
567 * Set tracing group variable with name. This function
568 * allocate memory pointed by tracing_group.
570 int lttng_set_tracing_group(const char *name
)
572 if (asprintf(&tracing_group
, "%s", name
) < 0) {
582 int lttng_calibrate(struct lttng_domain
*domain
,
583 struct lttng_calibrate
*calibrate
)
587 copy_lttng_domain(domain
);
589 memcpy(&lsm
.u
.calibrate
, calibrate
, sizeof(struct lttng_calibrate
));
590 ret
= ask_sessiond(LTTNG_CALIBRATE
, NULL
);
596 * lttng_check_session_daemon
600 * Error, return negative value
602 int lttng_session_daemon_alive(void)
606 ret
= set_session_daemon_path();
612 /* If socket exist, we check if the daemon listens to connect. */
613 ret
= access(sessiond_sock_path
, F_OK
);
619 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
624 ret
= lttcomm_close_unix_sock(ret
);
626 perror("lttcomm_close_unix_sock");
635 static void __attribute__((constructor
)) init()
637 /* Set default session group */
638 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP
);