2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <lttng/lttng.h>
29 #include "liblttsessiondcomm.h"
32 /* Socket to session daemon for communication */
33 static int sessiond_socket
;
34 static char sessiond_sock_path
[PATH_MAX
];
36 /* Communication structure to ltt-sessiond */
37 static struct lttcomm_session_msg lsm
;
38 static struct lttcomm_lttng_msg llm
;
41 static int check_tracing_group(const char *grp_name
);
42 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
);
43 static int recv_data_sessiond(void *buf
, size_t len
);
44 static int send_data_sessiond(void);
45 static int set_session_daemon_path(void);
48 static char *tracing_group
;
54 * Send lttcomm_session_msg to the session daemon.
56 * On success, return 0
57 * On error, return error code
59 static int send_data_sessiond(void)
68 ret
= lttcomm_send_unix_sock(sessiond_socket
, &lsm
, sizeof(lsm
));
77 * Receive data from the sessiond socket.
79 * On success, return 0
80 * On error, return recv() error code
82 static int recv_data_sessiond(void *buf
, size_t len
)
91 ret
= lttcomm_recv_unix_sock(sessiond_socket
, buf
, len
);
103 * Ask the session daemon a specific command
104 * and put the data into buf.
106 * Return size of data (only payload, not header).
108 static int ask_sessiond(enum lttcomm_sessiond_command lct
, void **buf
)
114 ret
= lttng_connect_sessiond();
121 /* Send command to session daemon */
122 ret
= send_data_sessiond();
127 /* Get header from data transmission */
128 ret
= recv_data_sessiond(&llm
, sizeof(llm
));
133 /* Check error code if OK */
134 if (llm
.ret_code
!= LTTCOMM_OK
) {
139 size
= llm
.trace_name_offset
+ llm
.data_size
;
144 data
= (void*) malloc(size
);
146 /* Get payload data */
147 ret
= recv_data_sessiond(data
, size
);
156 lttng_disconnect_sessiond();
161 * BEGIN KERNEL CONTROL
165 * lttng_kernel_enable_event
167 * Enable an event in the kernel tracer.
169 int lttng_kernel_enable_event(char *event_name
)
171 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
172 return ask_sessiond(KERNEL_ENABLE_EVENT
, NULL
);
176 * lttng_kernel_disable_event
178 * Disable an event in the kernel tracer.
180 int lttng_kernel_disable_event(char *event_name
)
182 strncpy(lsm
.u
.event
.event_name
, event_name
, NAME_MAX
);
183 return ask_sessiond(KERNEL_DISABLE_EVENT
, NULL
);
187 * lttng_kernel_create_session
189 * Create a session in the kernel tracer.
191 int lttng_kernel_create_session(void)
193 return ask_sessiond(KERNEL_CREATE_SESSION
, NULL
);
197 * lttng_kernel_create_channel
199 * Create a channel in the kernel tracer.
201 int lttng_kernel_create_channel(int overwrite
,
202 u64 subbuf_size
, u64 num_subbuf
,
203 unsigned int switch_timer_interval
,
204 unsigned int read_timer_interval
)
206 /* Write setting to the session message */
207 lsm
.u
.create_channel
.overwrite
= overwrite
;
208 lsm
.u
.create_channel
.subbuf_size
= subbuf_size
;
209 lsm
.u
.create_channel
.num_subbuf
= num_subbuf
;
210 lsm
.u
.create_channel
.switch_timer_interval
= switch_timer_interval
;
211 lsm
.u
.create_channel
.read_timer_interval
= read_timer_interval
;
213 return ask_sessiond(KERNEL_CREATE_CHANNEL
, NULL
);
217 * lttng_kernel_start_tracing
219 * Start kernel tracing.
221 int lttng_kernel_start_tracing(void)
223 return ask_sessiond(KERNEL_START_TRACE
, NULL
);
227 * lttng_kernel_stop_tracing
229 * Stop kernel tracing.
231 int lttng_kernel_stop_tracing(void)
233 return ask_sessiond(KERNEL_STOP_TRACE
, NULL
);
241 * lttng_get_readable_code
243 * Return a human readable string of code
245 const char *lttng_get_readable_code(int code
)
247 if (code
> -LTTCOMM_OK
) {
248 return "Ended with errors";
251 return lttcomm_get_readable_code(code
);
255 * lttng_ust_start_trace
257 * Request a trace start for pid.
259 int lttng_ust_start_trace(pid_t pid
)
264 ret
= ask_sessiond(UST_START_TRACE
, NULL
);
270 * lttng_ust_stop_trace
272 * Request a trace stop for pid.
274 int lttng_ust_stop_trace(pid_t pid
)
279 ret
= ask_sessiond(UST_STOP_TRACE
, NULL
);
285 * lttng_ust_create_trace
287 * Request a trace creation for pid.
289 int lttng_ust_create_trace(pid_t pid
)
294 ret
= ask_sessiond(UST_CREATE_TRACE
, NULL
);
300 * lttng_ust_list_apps
302 * Ask the session daemon for all UST traceable
305 * Return the number of pids.
306 * On error, return negative value.
308 int lttng_ust_list_apps(pid_t
**pids
)
312 ret
= ask_sessiond(UST_LIST_APPS
, (void**) pids
);
317 return ret
/ sizeof(pid_t
);
323 * Ask the session daemon for all traces (kernel and ust)
324 * for the session identified by uuid.
326 * Return the number of traces.
328 int lttng_list_traces(uuid_t
*uuid
, struct lttng_trace
**traces
)
332 uuid_copy(lsm
.session_uuid
, *uuid
);
334 ret
= ask_sessiond(LTTNG_LIST_TRACES
, (void **) traces
);
339 return ret
/ sizeof(struct lttng_trace
);
343 * lttng_create_session
345 * Create a brand new session using name.
347 int lttng_create_session(char *name
)
351 strncpy(lsm
.session_name
, name
, sizeof(lsm
.session_name
));
352 lsm
.session_name
[sizeof(lsm
.session_name
) - 1] = '\0';
354 ret
= ask_sessiond(LTTNG_CREATE_SESSION
, NULL
);
364 * lttng_destroy_session
366 * Destroy session using name.
368 int lttng_destroy_session(uuid_t
*uuid
)
372 uuid_copy(lsm
.session_uuid
, *uuid
);
374 ret
= ask_sessiond(LTTNG_DESTROY_SESSION
, NULL
);
384 * lttng_list_sessions
386 * Ask the session daemon for all available sessions.
388 * Return number of session.
389 * On error, return negative value.
391 int lttng_list_sessions(struct lttng_session
**sessions
)
395 ret
= ask_sessiond(LTTNG_LIST_SESSIONS
, (void**) sessions
);
400 return ret
/ sizeof(struct lttng_session
);
404 * lttng_connect_sessiond
406 * Connect to the LTTng session daemon.
407 * On success, return 0
408 * On error, return a negative value
410 int lttng_connect_sessiond(void)
414 ret
= set_session_daemon_path();
419 /* Connect to the sesssion daemon */
420 ret
= lttcomm_connect_unix_sock(sessiond_sock_path
);
425 sessiond_socket
= ret
;
432 * lttng_disconnect_sessiond
434 * Clean disconnect the session daemon.
436 int lttng_disconnect_sessiond(void)
441 ret
= lttcomm_close_unix_sock(sessiond_socket
);
450 * lttng_set_current_session_uuid
452 * Set the session uuid for current lsm.
454 void lttng_set_current_session_uuid(uuid_t
*uuid
)
456 uuid_copy(lsm
.session_uuid
, *uuid
);
460 * lttng_set_tracing_group
462 * Set tracing group variable with name. This function
463 * allocate memory pointed by tracing_group.
465 int lttng_set_tracing_group(const char *name
)
467 if (asprintf(&tracing_group
, "%s", name
) < 0) {
475 * lttng_check_session_daemon
477 * Return 0 if a sesssion daemon is available
480 int lttng_check_session_daemon(void)
484 ret
= set_session_daemon_path();
489 /* If socket exist, we consider the daemon started */
490 ret
= access(sessiond_sock_path
, F_OK
);
499 * set_session_daemon_path
501 * Set sessiond socket path by putting it in
502 * the global sessiond_sock_path variable.
504 static int set_session_daemon_path(void)
508 /* Are we in the tracing group ? */
509 ret
= check_tracing_group(tracing_group
);
510 if (ret
< 0 && getuid() != 0) {
511 if (sprintf(sessiond_sock_path
, DEFAULT_HOME_CLIENT_UNIX_SOCK
,
512 getenv("HOME")) < 0) {
516 strncpy(sessiond_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
,
517 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
524 * check_tracing_group
526 * Check if the specified group name exist.
529 static int check_tracing_group(const char *grp_name
)
531 struct group
*grp_tracing
; /* no free(). See getgrnam(3) */
533 int grp_list_size
, grp_id
, i
;
536 /* Get GID of group 'tracing' */
537 grp_tracing
= getgrnam(grp_name
);
538 if (grp_tracing
== NULL
) {
539 /* NULL means not found also. getgrnam(3) */
546 /* Get number of supplementary group IDs */
547 grp_list_size
= getgroups(0, NULL
);
548 if (grp_list_size
< 0) {
553 /* Alloc group list of the right size */
554 grp_list
= malloc(grp_list_size
* sizeof(gid_t
));
555 grp_id
= getgroups(grp_list_size
, grp_list
);
561 for (i
= 0; i
< grp_list_size
; i
++) {
562 if (grp_list
[i
] == grp_tracing
->gr_gid
) {
578 static void __attribute__((constructor
)) init()
580 /* Set default session group */
581 lttng_set_tracing_group(DEFAULT_TRACING_GROUP
);