Initialize all stack variables to zero, fix uninitialized loglevel variables
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
82a3637f
DG
2 * liblttngctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
826d496d 6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d 7 *
82a3637f
DG
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.
12 *
13 * This library is distributed in the hope that it will be useful,
fac6795d 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
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
fac6795d
DG
21 */
22
23#define _GNU_SOURCE
fac6795d 24#include <grp.h>
1e307fab 25#include <errno.h>
fac6795d
DG
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
990570ed
DG
31#include <common/common.h>
32#include <common/defaults.h>
db758600 33#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 34#include <lttng/lttng.h>
fac6795d
DG
35
36/* Socket to session daemon for communication */
37static int sessiond_socket;
38static char sessiond_sock_path[PATH_MAX];
39
fac6795d
DG
40/* Variables */
41static char *tracing_group;
42static int connected;
43
99497cd0
MD
44/*
45 * Copy string from src to dst and enforce null terminated byte.
46 */
47static void copy_string(char *dst, const char *src, size_t len)
48{
e7d6716d 49 if (src && dst) {
99497cd0
MD
50 strncpy(dst, src, len);
51 /* Enforce the NULL terminated byte */
52 dst[len - 1] = '\0';
cd80958d
DG
53 } else if (dst) {
54 dst[0] = '\0';
99497cd0
MD
55 }
56}
57
fac6795d 58/*
cd80958d 59 * Copy domain to lttcomm_session_msg domain.
fac6795d 60 *
cd80958d
DG
61 * If domain is unknown, default domain will be the kernel.
62 */
63static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
64{
65 if (src && dst) {
66 switch (src->type) {
67 case LTTNG_DOMAIN_KERNEL:
68 case LTTNG_DOMAIN_UST:
d78d6610 69 /*
cd80958d
DG
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 73 */
cd80958d
DG
74 memcpy(dst, src, sizeof(struct lttng_domain));
75 break;
76 default:
441c16a7 77 memset(dst, 0, sizeof(struct lttng_domain));
cd80958d
DG
78 dst->type = LTTNG_DOMAIN_KERNEL;
79 break;
80 }
81 }
82}
83
84/*
85 * Send lttcomm_session_msg to the session daemon.
fac6795d 86 *
1c8d13c8
TD
87 * On success, returns the number of bytes sent (>=0)
88 * On error, returns -1
fac6795d 89 */
cd80958d 90static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
91{
92 int ret;
93
94 if (!connected) {
e065084a
DG
95 ret = -ENOTCONN;
96 goto end;
fac6795d
DG
97 }
98
be040666 99 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 100 sizeof(struct lttcomm_session_msg));
e065084a
DG
101
102end:
103 return ret;
104}
105
106/*
cd80958d 107 * Receive data from the sessiond socket.
e065084a 108 *
1c8d13c8
TD
109 * On success, returns the number of bytes received (>=0)
110 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 111 */
ca95a216 112static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
113{
114 int ret;
115
116 if (!connected) {
117 ret = -ENOTCONN;
118 goto end;
fac6795d
DG
119 }
120
ca95a216 121 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 122
e065084a 123end:
fac6795d
DG
124 return ret;
125}
126
127/*
1c8d13c8 128 * Check if we are in the specified group.
65beb5ff 129 *
2269e89e 130 * If yes return 1, else return -1.
947308c4
DG
131 */
132static int check_tracing_group(const char *grp_name)
133{
134 struct group *grp_tracing; /* no free(). See getgrnam(3) */
135 gid_t *grp_list;
136 int grp_list_size, grp_id, i;
137 int ret = -1;
138
139 /* Get GID of group 'tracing' */
140 grp_tracing = getgrnam(grp_name);
141 if (grp_tracing == NULL) {
142 /* NULL means not found also. getgrnam(3) */
143 if (errno != 0) {
144 perror("getgrnam");
145 }
146 goto end;
147 }
148
149 /* Get number of supplementary group IDs */
150 grp_list_size = getgroups(0, NULL);
151 if (grp_list_size < 0) {
152 perror("getgroups");
153 goto end;
154 }
155
156 /* Alloc group list of the right size */
157 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 158 if (!grp_list) {
1c8d13c8 159 perror("malloc");
00795392
MD
160 goto end;
161 }
947308c4 162 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 163 if (grp_id < 0) {
947308c4
DG
164 perror("getgroups");
165 goto free_list;
166 }
167
168 for (i = 0; i < grp_list_size; i++) {
169 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 170 ret = 1;
947308c4
DG
171 break;
172 }
173 }
174
175free_list:
176 free(grp_list);
177
178end:
179 return ret;
180}
181
182/*
2269e89e
DG
183 * Try connect to session daemon with sock_path.
184 *
185 * Return 0 on success, else -1
186 */
187static int try_connect_sessiond(const char *sock_path)
188{
189 int ret;
190
191 /* If socket exist, we check if the daemon listens for connect. */
192 ret = access(sock_path, F_OK);
193 if (ret < 0) {
194 /* Not alive */
195 return -1;
196 }
197
198 ret = lttcomm_connect_unix_sock(sock_path);
199 if (ret < 0) {
200 /* Not alive */
201 return -1;
202 }
203
204 ret = lttcomm_close_unix_sock(ret);
205 if (ret < 0) {
206 perror("lttcomm_close_unix_sock");
207 }
208
209 return 0;
210}
211
212/*
1c8d13c8
TD
213 * Set sessiond socket path by putting it in the global
214 * sessiond_sock_path variable.
215 * Returns 0 on success,
216 * -ENOMEM on failure (the sessiond socket path is somehow too long)
947308c4
DG
217 */
218static int set_session_daemon_path(void)
219{
220 int ret;
2269e89e
DG
221 int in_tgroup = 0; /* In tracing group */
222 uid_t uid;
223
224 uid = getuid();
947308c4 225
2269e89e
DG
226 if (uid != 0) {
227 /* Are we in the tracing group ? */
228 in_tgroup = check_tracing_group(tracing_group);
229 }
230
08a9c49f
TD
231 if ((uid == 0) || in_tgroup) {
232 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 233 sizeof(sessiond_sock_path));
08a9c49f 234 }
2269e89e 235
08a9c49f
TD
236 if (uid != 0) {
237 if (in_tgroup) {
238 /* Tracing group */
239 ret = try_connect_sessiond(sessiond_sock_path);
240 if (ret >= 0) {
241 goto end;
2269e89e 242 }
08a9c49f 243 /* Global session daemon not available... */
2269e89e 244 }
08a9c49f
TD
245 /* ...or not in tracing group (and not root), default */
246
247 /*
248 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
249 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
250 */
251 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
252 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
253 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
947308c4
DG
254 return -ENOMEM;
255 }
947308c4 256 }
08a9c49f 257end:
947308c4
DG
258 return 0;
259}
260
65beb5ff
DG
261/*
262 * Connect to the LTTng session daemon.
263 *
264 * On success, return 0. On error, return -1.
265 */
266static int connect_sessiond(void)
267{
268 int ret;
269
270 ret = set_session_daemon_path();
271 if (ret < 0) {
1c8d13c8 272 return -1; /* set_session_daemon_path() returns -ENOMEM */
65beb5ff
DG
273 }
274
275 /* Connect to the sesssion daemon */
276 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
277 if (ret < 0) {
278 return ret;
279 }
280
281 sessiond_socket = ret;
282 connected = 1;
283
284 return 0;
285}
286
287/*
1c8d13c8
TD
288 * Clean disconnect from the session daemon.
289 * On success, return 0. On error, return -1.
65beb5ff
DG
290 */
291static int disconnect_sessiond(void)
292{
293 int ret = 0;
294
295 if (connected) {
296 ret = lttcomm_close_unix_sock(sessiond_socket);
297 sessiond_socket = 0;
298 connected = 0;
299 }
300
301 return ret;
302}
303
35a6fdb7 304/*
cd80958d 305 * Ask the session daemon a specific command and put the data into buf.
65beb5ff 306 *
af87c45a 307 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 308 */
cd80958d 309static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
65beb5ff
DG
310{
311 int ret;
312 size_t size;
313 void *data = NULL;
cd80958d 314 struct lttcomm_lttng_msg llm;
65beb5ff
DG
315
316 ret = connect_sessiond();
317 if (ret < 0) {
318 goto end;
319 }
320
65beb5ff 321 /* Send command to session daemon */
cd80958d 322 ret = send_session_msg(lsm);
65beb5ff
DG
323 if (ret < 0) {
324 goto end;
325 }
326
327 /* Get header from data transmission */
328 ret = recv_data_sessiond(&llm, sizeof(llm));
329 if (ret < 0) {
330 goto end;
331 }
332
333 /* Check error code if OK */
334 if (llm.ret_code != LTTCOMM_OK) {
335 ret = -llm.ret_code;
336 goto end;
337 }
338
339 size = llm.data_size;
340 if (size == 0) {
874d3f84 341 /* If client free with size 0 */
a45d5536
DG
342 if (buf != NULL) {
343 *buf = NULL;
344 }
7d29a247 345 ret = 0;
65beb5ff
DG
346 goto end;
347 }
348
349 data = (void*) malloc(size);
350
351 /* Get payload data */
352 ret = recv_data_sessiond(data, size);
353 if (ret < 0) {
354 free(data);
355 goto end;
356 }
357
83009e5e
DG
358 /*
359 * Extra protection not to dereference a NULL pointer. If buf is NULL at
360 * this point, an error is returned and data is freed.
361 */
362 if (buf == NULL) {
363 ret = -1;
364 free(data);
365 goto end;
366 }
367
65beb5ff
DG
368 *buf = data;
369 ret = size;
370
371end:
372 disconnect_sessiond();
373 return ret;
374}
375
9f19cc17 376/*
cd80958d 377 * Create lttng handle and return pointer.
1c8d13c8 378 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 379 */
cd80958d
DG
380struct lttng_handle *lttng_create_handle(const char *session_name,
381 struct lttng_domain *domain)
9f19cc17 382{
cd80958d
DG
383 struct lttng_handle *handle;
384
385 handle = malloc(sizeof(struct lttng_handle));
386 if (handle == NULL) {
387 perror("malloc handle");
388 goto end;
389 }
390
391 /* Copy session name */
392 copy_string(handle->session_name, session_name,
393 sizeof(handle->session_name));
394
395 /* Copy lttng domain */
396 copy_lttng_domain(&handle->domain, domain);
397
398end:
399 return handle;
400}
401
402/*
403 * Destroy handle by free(3) the pointer.
404 */
405void lttng_destroy_handle(struct lttng_handle *handle)
406{
407 if (handle) {
408 free(handle);
eb354453
DG
409 }
410}
411
d9800920
DG
412/*
413 * Register an outside consumer.
1c8d13c8 414 * Returns size of returned session payload data or a negative error code.
d9800920
DG
415 */
416int lttng_register_consumer(struct lttng_handle *handle,
417 const char *socket_path)
418{
419 struct lttcomm_session_msg lsm;
420
421 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
422 copy_string(lsm.session.name, handle->session_name,
423 sizeof(lsm.session.name));
424 copy_lttng_domain(&lsm.domain, &handle->domain);
425
426 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
427
428 return ask_sessiond(&lsm, NULL);
429}
430
1df4dedd 431/*
1c8d13c8
TD
432 * Start tracing for all traces of the session.
433 * Returns size of returned session payload data or a negative error code.
1df4dedd 434 */
6a4f824d 435int lttng_start_tracing(const char *session_name)
f3ed775e 436{
cd80958d
DG
437 struct lttcomm_session_msg lsm;
438
6a4f824d 439 if (session_name == NULL) {
cd80958d
DG
440 return -1;
441 }
442
443 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
444
445 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
446
447 return ask_sessiond(&lsm, NULL);
f3ed775e 448}
1df4dedd
DG
449
450/*
1c8d13c8
TD
451 * Stop tracing for all traces of the session.
452 * Returns size of returned session payload data or a negative error code.
f3ed775e 453 */
6a4f824d 454int lttng_stop_tracing(const char *session_name)
f3ed775e 455{
cd80958d
DG
456 struct lttcomm_session_msg lsm;
457
6a4f824d
DG
458 if (session_name == NULL) {
459 return -1;
460 }
461
cd80958d 462 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
463
464 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
465
466 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
467}
468
469/*
1e46a50f
TD
470 * Add context to event and/or channel.
471 * If event_name is NULL, the context is applied to all events of the channel.
472 * If channel_name is NULL, a lookup of the event's channel is done.
473 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
474 *
475 * Returns the size of the returned payload data or a negative error code.
1df4dedd 476 */
cd80958d 477int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
478 struct lttng_event_context *ctx, const char *event_name,
479 const char *channel_name)
d65106b1 480{
cd80958d
DG
481 struct lttcomm_session_msg lsm;
482
9d697d3d
DG
483 /* Safety check. Both are mandatory */
484 if (handle == NULL || ctx == NULL) {
cd80958d
DG
485 return -1;
486 }
487
441c16a7
MD
488 memset(&lsm, 0, sizeof(lsm));
489
cd80958d
DG
490 lsm.cmd_type = LTTNG_ADD_CONTEXT;
491
492 /* Copy channel name */
493 copy_string(lsm.u.context.channel_name, channel_name,
494 sizeof(lsm.u.context.channel_name));
495 /* Copy event name */
496 copy_string(lsm.u.context.event_name, event_name,
497 sizeof(lsm.u.context.event_name));
498
499 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 500
9d697d3d 501 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 502
cd80958d
DG
503 copy_string(lsm.session.name, handle->session_name,
504 sizeof(lsm.session.name));
505
506 return ask_sessiond(&lsm, NULL);
d65106b1
DG
507}
508
f3ed775e 509/*
1c8d13c8
TD
510 * Enable event(s) for a channel.
511 * If no event name is specified, all events are enabled.
512 * If no channel name is specified, the default 'channel0' is used.
513 * Returns size of returned session payload data or a negative error code.
f3ed775e 514 */
cd80958d 515int lttng_enable_event(struct lttng_handle *handle,
38057ed1 516 struct lttng_event *ev, const char *channel_name)
1df4dedd 517{
cd80958d
DG
518 struct lttcomm_session_msg lsm;
519
5117eeec 520 if (handle == NULL || ev == NULL) {
cd80958d
DG
521 return -1;
522 }
33a2b854 523
441c16a7
MD
524 memset(&lsm, 0, sizeof(lsm));
525
5117eeec 526 /* If no channel name, we put the default name */
94cf3c47 527 if (channel_name == NULL) {
cd80958d
DG
528 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
529 sizeof(lsm.u.enable.channel_name));
33a2b854 530 } else {
cd80958d
DG
531 copy_string(lsm.u.enable.channel_name, channel_name,
532 sizeof(lsm.u.enable.channel_name));
eb354453
DG
533 }
534
cd80958d 535 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 536
8c9ae521 537 if (ev->name[0] != '\0') {
cd80958d 538 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 539 } else {
cd80958d 540 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 541 }
8c9ae521 542 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 543
cd80958d
DG
544 copy_string(lsm.session.name, handle->session_name,
545 sizeof(lsm.session.name));
546
547 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
548}
549
550/*
1c8d13c8
TD
551 * Disable event(s) of a channel and domain.
552 * If no event name is specified, all events are disabled.
553 * If no channel name is specified, the default 'channel0' is used.
554 * Returns size of returned session payload data or a negative error code.
1df4dedd 555 */
cd80958d 556int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 557 const char *channel_name)
1df4dedd 558{
cd80958d 559 struct lttcomm_session_msg lsm;
1df4dedd 560
9d697d3d 561 if (handle == NULL) {
cd80958d
DG
562 return -1;
563 }
564
441c16a7
MD
565 memset(&lsm, 0, sizeof(lsm));
566
cd80958d
DG
567 if (channel_name) {
568 copy_string(lsm.u.disable.channel_name, channel_name,
569 sizeof(lsm.u.disable.channel_name));
f3ed775e 570 } else {
cd80958d
DG
571 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
572 sizeof(lsm.u.disable.channel_name));
eb354453
DG
573 }
574
cd80958d 575 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 576
f84efadf 577 if (name != NULL) {
cd80958d
DG
578 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
579 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 580 } else {
cd80958d 581 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
582 }
583
cd80958d
DG
584 copy_string(lsm.session.name, handle->session_name,
585 sizeof(lsm.session.name));
586
587 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
588}
589
590/*
1c8d13c8
TD
591 * Enable channel per domain
592 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 593 */
cd80958d 594int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 595 struct lttng_channel *chan)
a5c5a2bd 596{
cd80958d
DG
597 struct lttcomm_session_msg lsm;
598
5117eeec
DG
599 /*
600 * NULL arguments are forbidden. No default values.
601 */
602 if (handle == NULL || chan == NULL) {
cd80958d
DG
603 return -1;
604 }
605
441c16a7
MD
606 memset(&lsm, 0, sizeof(lsm));
607
5117eeec 608 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 609
cd80958d
DG
610 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
611
612 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 613
cd80958d
DG
614 copy_string(lsm.session.name, handle->session_name,
615 sizeof(lsm.session.name));
616
617 return ask_sessiond(&lsm, NULL);
8c0faa1d 618}
1df4dedd 619
2ef84c95 620/*
1c8d13c8
TD
621 * All tracing will be stopped for registered events of the channel.
622 * Returns size of returned session payload data or a negative error code.
2ef84c95 623 */
cd80958d 624int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 625{
cd80958d
DG
626 struct lttcomm_session_msg lsm;
627
9d697d3d
DG
628 /* Safety check. Both are mandatory */
629 if (handle == NULL || name == NULL) {
cd80958d
DG
630 return -1;
631 }
632
441c16a7
MD
633 memset(&lsm, 0, sizeof(lsm));
634
cd80958d 635 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 636
9d697d3d
DG
637 copy_string(lsm.u.disable.channel_name, name,
638 sizeof(lsm.u.disable.channel_name));
639
cd80958d
DG
640 copy_lttng_domain(&lsm.domain, &handle->domain);
641
642 copy_string(lsm.session.name, handle->session_name,
643 sizeof(lsm.session.name));
644
645 return ask_sessiond(&lsm, NULL);
ca95a216
DG
646}
647
fac6795d 648/*
1c8d13c8
TD
649 * Lists all available tracepoints of domain.
650 * Sets the contents of the events array.
651 * Returns the number of lttng_event entries in events;
652 * on error, returns a negative value.
fac6795d 653 */
cd80958d 654int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 655 struct lttng_event **events)
fac6795d 656{
052da939 657 int ret;
cd80958d
DG
658 struct lttcomm_session_msg lsm;
659
9d697d3d 660 if (handle == NULL) {
cd80958d
DG
661 return -1;
662 }
fac6795d 663
cd80958d
DG
664 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
665 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 666
cd80958d 667 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
668 if (ret < 0) {
669 return ret;
eb354453 670 }
fac6795d 671
9f19cc17 672 return ret / sizeof(struct lttng_event);
fac6795d
DG
673}
674
1657e9bb 675/*
1c8d13c8
TD
676 * Returns a human readable string describing
677 * the error code (a negative value).
1657e9bb 678 */
9a745bc7 679const char *lttng_strerror(int code)
1657e9bb 680{
1c8d13c8 681 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
7d29a247
DG
682 if (code > -LTTCOMM_OK) {
683 return "Ended with errors";
1657e9bb
DG
684 }
685
7d29a247 686 return lttcomm_get_readable_code(code);
1657e9bb
DG
687}
688
aaf97519 689/*
1c8d13c8
TD
690 * Create a brand new session using name and path.
691 * Returns size of returned session payload data or a negative error code.
aaf97519 692 */
38057ed1 693int lttng_create_session(const char *name, const char *path)
aaf97519 694{
cd80958d
DG
695 struct lttcomm_session_msg lsm;
696
697 lsm.cmd_type = LTTNG_CREATE_SESSION;
698 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
699 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
700
701 return ask_sessiond(&lsm, NULL);
8028d920
DG
702}
703
704/*
8028d920 705 * Destroy session using name.
1c8d13c8 706 * Returns size of returned session payload data or a negative error code.
8028d920 707 */
843f5df9 708int lttng_destroy_session(const char *session_name)
8028d920 709{
cd80958d
DG
710 struct lttcomm_session_msg lsm;
711
843f5df9 712 if (session_name == NULL) {
cd80958d
DG
713 return -1;
714 }
715
716 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
717
718 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
719
720 return ask_sessiond(&lsm, NULL);
aaf97519
DG
721}
722
57167058 723/*
57167058 724 * Ask the session daemon for all available sessions.
1c8d13c8
TD
725 * Sets the contents of the sessions array.
726 * Returns the number of lttng_session entries in sessions;
727 * on error, returns a negative value.
57167058 728 */
ca95a216 729int lttng_list_sessions(struct lttng_session **sessions)
57167058 730{
ca95a216 731 int ret;
cd80958d 732 struct lttcomm_session_msg lsm;
57167058 733
cd80958d
DG
734 lsm.cmd_type = LTTNG_LIST_SESSIONS;
735 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 736 if (ret < 0) {
ca95a216 737 return ret;
57167058
DG
738 }
739
ca95a216 740 return ret / sizeof(struct lttng_session);
57167058
DG
741}
742
9f19cc17 743/*
1c8d13c8
TD
744 * Ask the session daemon for all available domains of a session.
745 * Sets the contents of the domains array.
746 * Returns the number of lttng_domain entries in domains;
747 * on error, returns a negative value.
9f19cc17 748 */
330be774 749int lttng_list_domains(const char *session_name,
cd80958d 750 struct lttng_domain **domains)
9f19cc17
DG
751{
752 int ret;
cd80958d
DG
753 struct lttcomm_session_msg lsm;
754
330be774 755 if (session_name == NULL) {
cd80958d
DG
756 return -1;
757 }
9f19cc17 758
cd80958d
DG
759 lsm.cmd_type = LTTNG_LIST_DOMAINS;
760
330be774 761 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
762
763 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
764 if (ret < 0) {
765 return ret;
766 }
767
768 return ret / sizeof(struct lttng_domain);
769}
770
771/*
1c8d13c8
TD
772 * Ask the session daemon for all available channels of a session.
773 * Sets the contents of the channels array.
774 * Returns the number of lttng_channel entries in channels;
775 * on error, returns a negative value.
9f19cc17 776 */
cd80958d
DG
777int lttng_list_channels(struct lttng_handle *handle,
778 struct lttng_channel **channels)
9f19cc17
DG
779{
780 int ret;
cd80958d
DG
781 struct lttcomm_session_msg lsm;
782
9d697d3d 783 if (handle == NULL) {
cd80958d
DG
784 return -1;
785 }
786
787 lsm.cmd_type = LTTNG_LIST_CHANNELS;
788 copy_string(lsm.session.name, handle->session_name,
789 sizeof(lsm.session.name));
9f19cc17 790
cd80958d 791 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 792
cd80958d 793 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
794 if (ret < 0) {
795 return ret;
796 }
797
798 return ret / sizeof(struct lttng_channel);
799}
800
801/*
1c8d13c8
TD
802 * Ask the session daemon for all available events of a session channel.
803 * Sets the contents of the events array.
804 * Returns the number of lttng_event entries in events;
805 * on error, returns a negative value.
9f19cc17 806 */
cd80958d
DG
807int lttng_list_events(struct lttng_handle *handle,
808 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
809{
810 int ret;
cd80958d 811 struct lttcomm_session_msg lsm;
9f19cc17 812
9d697d3d
DG
813 /* Safety check. An handle and channel name are mandatory */
814 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
815 return -1;
816 }
817
818 lsm.cmd_type = LTTNG_LIST_EVENTS;
819 copy_string(lsm.session.name, handle->session_name,
820 sizeof(lsm.session.name));
821 copy_string(lsm.u.list.channel_name, channel_name,
822 sizeof(lsm.u.list.channel_name));
823
824 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 825
cd80958d 826 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
827 if (ret < 0) {
828 return ret;
829 }
830
831 return ret / sizeof(struct lttng_event);
832}
833
fac6795d 834/*
1c8d13c8
TD
835 * Sets the tracing_group variable with name.
836 * This function allocates memory pointed to by tracing_group.
837 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
838 */
839int lttng_set_tracing_group(const char *name)
840{
9d697d3d
DG
841 if (name == NULL) {
842 return -1;
843 }
844
fac6795d
DG
845 if (asprintf(&tracing_group, "%s", name) < 0) {
846 return -ENOMEM;
847 }
848
849 return 0;
850}
851
d0254c7c 852/*
af87c45a 853 * Returns size of returned session payload data or a negative error code.
d0254c7c 854 */
cd80958d 855int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
856 struct lttng_calibrate *calibrate)
857{
cd80958d 858 struct lttcomm_session_msg lsm;
d0254c7c 859
9d697d3d
DG
860 /* Safety check. NULL pointer are forbidden */
861 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
862 return -1;
863 }
d0254c7c 864
cd80958d
DG
865 lsm.cmd_type = LTTNG_CALIBRATE;
866 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 867
cd80958d
DG
868 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
869
870 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
871}
872
5edd7e09
DG
873/*
874 * Set default channel attributes.
441c16a7 875 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
876 */
877void lttng_channel_set_default_attr(struct lttng_domain *domain,
878 struct lttng_channel_attr *attr)
879{
441c16a7
MD
880 memset(attr, 0, sizeof(struct lttng_channel_attr));
881
5edd7e09
DG
882 /* Safety check */
883 if (attr == NULL || domain == NULL) {
884 return;
885 }
886
887 switch (domain->type) {
888 case LTTNG_DOMAIN_KERNEL:
889 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
890 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
891 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
892
893 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
894 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
895 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
896 break;
897 case LTTNG_DOMAIN_UST:
d78d6610 898#if 0
5edd7e09
DG
899 case LTTNG_DOMAIN_UST_EXEC_NAME:
900 case LTTNG_DOMAIN_UST_PID:
901 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 902#endif
5edd7e09
DG
903 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
904 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
905 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
906
907 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
908 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
909 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
910 break;
911 default:
441c16a7 912 /* Default behavior: leave set to 0. */
5edd7e09
DG
913 break;
914 }
915}
916
fac6795d 917/*
2269e89e 918 * Check if session daemon is alive.
fac6795d 919 *
2269e89e 920 * Return 1 if alive or 0 if not.
1c8d13c8 921 * On error returns a negative value.
fac6795d 922 */
947308c4 923int lttng_session_daemon_alive(void)
fac6795d
DG
924{
925 int ret;
926
927 ret = set_session_daemon_path();
928 if (ret < 0) {
947308c4 929 /* Error */
fac6795d
DG
930 return ret;
931 }
932
2269e89e
DG
933 if (strlen(sessiond_sock_path) == 0) {
934 /* No socket path set. Weird error */
935 return -1;
fac6795d
DG
936 }
937
2269e89e 938 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
939 if (ret < 0) {
940 /* Not alive */
941 return 0;
942 }
7d8234d9 943
947308c4
DG
944 /* Is alive */
945 return 1;
fac6795d
DG
946}
947
948/*
949 * lib constructor
950 */
951static void __attribute__((constructor)) init()
952{
953 /* Set default session group */
bbccc3d2 954 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 955}
This page took 0.077275 seconds and 4 git commands to generate.