Health check: implement health check query in sessiond and consumerd
[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 *
d14d33bf
AM
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
82a3637f
DG
11 *
12 * This library is distributed in the hope that it will be useful,
fac6795d 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
d14d33bf
AM
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
20 */
21
22#define _GNU_SOURCE
44a5e5eb 23#include <assert.h>
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>
a4b92340 34#include <common/uri.h>
feb0f3e5 35#include <common/utils.h>
1e307fab 36#include <lttng/lttng.h>
0c89d795 37#include <lttng/health-internal.h>
fac6795d 38
d00c599e
DG
39#include "filter/filter-ast.h"
40#include "filter/filter-parser.h"
41#include "filter/filter-bytecode.h"
42#include "filter/memstream.h"
cac3069d 43#include "lttng-ctl-helper.h"
53a80697
MD
44
45#ifdef DEBUG
d00c599e 46static const int print_xml = 1;
53a80697
MD
47#define dbg_printf(fmt, args...) \
48 printf("[debug liblttng-ctl] " fmt, ## args)
49#else
d00c599e 50static const int print_xml = 0;
53a80697
MD
51#define dbg_printf(fmt, args...) \
52do { \
53 /* do nothing but check printf format */ \
54 if (0) \
55 printf("[debug liblttnctl] " fmt, ## args); \
56} while (0)
57#endif
58
59
fac6795d
DG
60/* Socket to session daemon for communication */
61static int sessiond_socket;
62static char sessiond_sock_path[PATH_MAX];
63
fac6795d
DG
64/* Variables */
65static char *tracing_group;
66static int connected;
67
97e19046
DG
68/* Global */
69
70/*
71 * Those two variables are used by error.h to silent or control the verbosity of
72 * error message. They are global to the library so application linking with it
73 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
74 */
75int lttng_opt_quiet;
76int lttng_opt_verbose;
77
99497cd0
MD
78/*
79 * Copy string from src to dst and enforce null terminated byte.
80 */
cac3069d
DG
81LTTNG_HIDDEN
82void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 83{
e7d6716d 84 if (src && dst) {
99497cd0
MD
85 strncpy(dst, src, len);
86 /* Enforce the NULL terminated byte */
87 dst[len - 1] = '\0';
cd80958d
DG
88 } else if (dst) {
89 dst[0] = '\0';
99497cd0
MD
90 }
91}
92
fac6795d 93/*
cd80958d 94 * Copy domain to lttcomm_session_msg domain.
fac6795d 95 *
cd80958d
DG
96 * If domain is unknown, default domain will be the kernel.
97 */
cac3069d
DG
98LTTNG_HIDDEN
99void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
100 struct lttng_domain *src)
cd80958d
DG
101{
102 if (src && dst) {
103 switch (src->type) {
00e2e675
DG
104 case LTTNG_DOMAIN_KERNEL:
105 case LTTNG_DOMAIN_UST:
00e2e675
DG
106 memcpy(dst, src, sizeof(struct lttng_domain));
107 break;
108 default:
109 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 110 break;
cd80958d
DG
111 }
112 }
113}
114
115/*
116 * Send lttcomm_session_msg to the session daemon.
fac6795d 117 *
1c8d13c8
TD
118 * On success, returns the number of bytes sent (>=0)
119 * On error, returns -1
fac6795d 120 */
cd80958d 121static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
122{
123 int ret;
124
125 if (!connected) {
2f70b271 126 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 127 goto end;
fac6795d
DG
128 }
129
a4b92340
DG
130 DBG("LSM cmd type : %d", lsm->cmd_type);
131
be040666 132 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 133 sizeof(struct lttcomm_session_msg));
2f70b271
DG
134 if (ret < 0) {
135 ret = -LTTNG_ERR_FATAL;
136 }
e065084a
DG
137
138end:
139 return ret;
140}
141
53a80697
MD
142/*
143 * Send var len data to the session daemon.
144 *
145 * On success, returns the number of bytes sent (>=0)
146 * On error, returns -1
147 */
148static int send_session_varlen(void *data, size_t len)
149{
150 int ret;
151
152 if (!connected) {
2f70b271 153 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
154 goto end;
155 }
a4b92340 156
53a80697
MD
157 if (!data || !len) {
158 ret = 0;
159 goto end;
160 }
161
162 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
163 if (ret < 0) {
164 ret = -LTTNG_ERR_FATAL;
165 }
53a80697
MD
166
167end:
168 return ret;
169}
170
e065084a 171/*
cd80958d 172 * Receive data from the sessiond socket.
e065084a 173 *
1c8d13c8
TD
174 * On success, returns the number of bytes received (>=0)
175 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 176 */
ca95a216 177static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
178{
179 int ret;
180
181 if (!connected) {
2f70b271 182 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 183 goto end;
fac6795d
DG
184 }
185
ca95a216 186 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
187 if (ret < 0) {
188 ret = -LTTNG_ERR_FATAL;
189 }
fac6795d 190
e065084a 191end:
fac6795d
DG
192 return ret;
193}
194
195/*
1c8d13c8 196 * Check if we are in the specified group.
65beb5ff 197 *
2269e89e 198 * If yes return 1, else return -1.
947308c4 199 */
6c71277b
MD
200LTTNG_HIDDEN
201int lttng_check_tracing_group(void)
947308c4
DG
202{
203 struct group *grp_tracing; /* no free(). See getgrnam(3) */
204 gid_t *grp_list;
205 int grp_list_size, grp_id, i;
206 int ret = -1;
6c71277b 207 const char *grp_name = tracing_group;
947308c4
DG
208
209 /* Get GID of group 'tracing' */
210 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
211 if (!grp_tracing) {
212 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
213 goto end;
214 }
215
216 /* Get number of supplementary group IDs */
217 grp_list_size = getgroups(0, NULL);
218 if (grp_list_size < 0) {
219 perror("getgroups");
220 goto end;
221 }
222
223 /* Alloc group list of the right size */
224 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 225 if (!grp_list) {
1c8d13c8 226 perror("malloc");
00795392
MD
227 goto end;
228 }
947308c4 229 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 230 if (grp_id < 0) {
947308c4
DG
231 perror("getgroups");
232 goto free_list;
233 }
234
235 for (i = 0; i < grp_list_size; i++) {
236 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 237 ret = 1;
947308c4
DG
238 break;
239 }
240 }
241
242free_list:
243 free(grp_list);
244
245end:
246 return ret;
247}
248
249/*
2269e89e
DG
250 * Try connect to session daemon with sock_path.
251 *
252 * Return 0 on success, else -1
253 */
254static int try_connect_sessiond(const char *sock_path)
255{
256 int ret;
257
258 /* If socket exist, we check if the daemon listens for connect. */
259 ret = access(sock_path, F_OK);
260 if (ret < 0) {
261 /* Not alive */
2f70b271 262 goto error;
2269e89e
DG
263 }
264
265 ret = lttcomm_connect_unix_sock(sock_path);
266 if (ret < 0) {
267 /* Not alive */
2f70b271 268 goto error;
2269e89e
DG
269 }
270
271 ret = lttcomm_close_unix_sock(ret);
272 if (ret < 0) {
273 perror("lttcomm_close_unix_sock");
274 }
275
276 return 0;
2f70b271
DG
277
278error:
279 return -1;
2269e89e
DG
280}
281
282/*
2f70b271
DG
283 * Set sessiond socket path by putting it in the global sessiond_sock_path
284 * variable.
285 *
286 * Returns 0 on success, negative value on failure (the sessiond socket path
287 * is somehow too long or ENOMEM).
947308c4
DG
288 */
289static int set_session_daemon_path(void)
290{
2269e89e
DG
291 int in_tgroup = 0; /* In tracing group */
292 uid_t uid;
293
294 uid = getuid();
947308c4 295
2269e89e
DG
296 if (uid != 0) {
297 /* Are we in the tracing group ? */
6c71277b 298 in_tgroup = lttng_check_tracing_group();
2269e89e
DG
299 }
300
08a9c49f 301 if ((uid == 0) || in_tgroup) {
cac3069d
DG
302 lttng_ctl_copy_string(sessiond_sock_path,
303 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 304 }
2269e89e 305
08a9c49f 306 if (uid != 0) {
c617c0c6
MD
307 int ret;
308
08a9c49f
TD
309 if (in_tgroup) {
310 /* Tracing group */
311 ret = try_connect_sessiond(sessiond_sock_path);
312 if (ret >= 0) {
313 goto end;
2269e89e 314 }
08a9c49f 315 /* Global session daemon not available... */
2269e89e 316 }
08a9c49f
TD
317 /* ...or not in tracing group (and not root), default */
318
319 /*
320 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
321 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
322 */
323 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 324 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 325 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 326 goto error;
947308c4 327 }
947308c4 328 }
08a9c49f 329end:
947308c4 330 return 0;
2f70b271
DG
331
332error:
333 return -1;
947308c4
DG
334}
335
65beb5ff
DG
336/*
337 * Connect to the LTTng session daemon.
338 *
339 * On success, return 0. On error, return -1.
340 */
341static int connect_sessiond(void)
342{
343 int ret;
344
da3c9ec1
DG
345 /* Don't try to connect if already connected. */
346 if (connected) {
347 return 0;
348 }
349
65beb5ff
DG
350 ret = set_session_daemon_path();
351 if (ret < 0) {
2f70b271 352 goto error;
65beb5ff
DG
353 }
354
355 /* Connect to the sesssion daemon */
356 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
357 if (ret < 0) {
2f70b271 358 goto error;
65beb5ff
DG
359 }
360
361 sessiond_socket = ret;
362 connected = 1;
363
364 return 0;
2f70b271
DG
365
366error:
367 return -1;
65beb5ff
DG
368}
369
370/*
1c8d13c8
TD
371 * Clean disconnect from the session daemon.
372 * On success, return 0. On error, return -1.
65beb5ff
DG
373 */
374static int disconnect_sessiond(void)
375{
376 int ret = 0;
377
378 if (connected) {
379 ret = lttcomm_close_unix_sock(sessiond_socket);
380 sessiond_socket = 0;
381 connected = 0;
382 }
383
384 return ret;
385}
386
35a6fdb7 387/*
cd80958d 388 * Ask the session daemon a specific command and put the data into buf.
53a80697 389 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 390 *
af87c45a 391 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 392 */
cac3069d
DG
393LTTNG_HIDDEN
394int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 395 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
396{
397 int ret;
398 size_t size;
399 void *data = NULL;
cd80958d 400 struct lttcomm_lttng_msg llm;
65beb5ff
DG
401
402 ret = connect_sessiond();
403 if (ret < 0) {
2f70b271 404 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
405 goto end;
406 }
407
65beb5ff 408 /* Send command to session daemon */
cd80958d 409 ret = send_session_msg(lsm);
65beb5ff 410 if (ret < 0) {
2f70b271 411 /* Ret value is a valid lttng error code. */
65beb5ff
DG
412 goto end;
413 }
53a80697
MD
414 /* Send var len data */
415 ret = send_session_varlen(vardata, varlen);
416 if (ret < 0) {
2f70b271 417 /* Ret value is a valid lttng error code. */
53a80697
MD
418 goto end;
419 }
65beb5ff
DG
420
421 /* Get header from data transmission */
422 ret = recv_data_sessiond(&llm, sizeof(llm));
423 if (ret < 0) {
2f70b271 424 /* Ret value is a valid lttng error code. */
65beb5ff
DG
425 goto end;
426 }
427
428 /* Check error code if OK */
f73fabfd 429 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
430 ret = -llm.ret_code;
431 goto end;
432 }
433
434 size = llm.data_size;
435 if (size == 0) {
874d3f84 436 /* If client free with size 0 */
a45d5536
DG
437 if (buf != NULL) {
438 *buf = NULL;
439 }
7d29a247 440 ret = 0;
65beb5ff
DG
441 goto end;
442 }
443
444 data = (void*) malloc(size);
445
446 /* Get payload data */
447 ret = recv_data_sessiond(data, size);
448 if (ret < 0) {
449 free(data);
450 goto end;
451 }
452
83009e5e
DG
453 /*
454 * Extra protection not to dereference a NULL pointer. If buf is NULL at
455 * this point, an error is returned and data is freed.
456 */
457 if (buf == NULL) {
2f70b271 458 ret = -LTTNG_ERR_INVALID;
83009e5e
DG
459 free(data);
460 goto end;
461 }
462
65beb5ff
DG
463 *buf = data;
464 ret = size;
465
466end:
467 disconnect_sessiond();
468 return ret;
469}
470
9f19cc17 471/*
cd80958d 472 * Create lttng handle and return pointer.
1c8d13c8 473 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 474 */
cd80958d
DG
475struct lttng_handle *lttng_create_handle(const char *session_name,
476 struct lttng_domain *domain)
9f19cc17 477{
2f70b271
DG
478 struct lttng_handle *handle = NULL;
479
480 if (domain == NULL) {
481 goto end;
482 }
cd80958d
DG
483
484 handle = malloc(sizeof(struct lttng_handle));
485 if (handle == NULL) {
2f70b271 486 PERROR("malloc handle");
cd80958d
DG
487 goto end;
488 }
489
490 /* Copy session name */
cac3069d 491 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
492 sizeof(handle->session_name));
493
494 /* Copy lttng domain */
cac3069d 495 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
cd80958d
DG
496
497end:
498 return handle;
499}
500
501/*
502 * Destroy handle by free(3) the pointer.
503 */
504void lttng_destroy_handle(struct lttng_handle *handle)
505{
0e428499 506 free(handle);
eb354453
DG
507}
508
d9800920
DG
509/*
510 * Register an outside consumer.
1c8d13c8 511 * Returns size of returned session payload data or a negative error code.
d9800920
DG
512 */
513int lttng_register_consumer(struct lttng_handle *handle,
514 const char *socket_path)
515{
516 struct lttcomm_session_msg lsm;
517
2f70b271
DG
518 if (handle == NULL || socket_path == NULL) {
519 return -LTTNG_ERR_INVALID;
520 }
521
d9800920 522 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 523 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 524 sizeof(lsm.session.name));
cac3069d 525 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 526
cac3069d 527 lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
d9800920 528
cac3069d 529 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
530}
531
1df4dedd 532/*
1c8d13c8
TD
533 * Start tracing for all traces of the session.
534 * Returns size of returned session payload data or a negative error code.
1df4dedd 535 */
6a4f824d 536int lttng_start_tracing(const char *session_name)
f3ed775e 537{
cd80958d
DG
538 struct lttcomm_session_msg lsm;
539
6a4f824d 540 if (session_name == NULL) {
2f70b271 541 return -LTTNG_ERR_INVALID;
cd80958d
DG
542 }
543
544 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 545
cac3069d
DG
546 lttng_ctl_copy_string(lsm.session.name, session_name,
547 sizeof(lsm.session.name));
cd80958d 548
cac3069d 549 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 550}
1df4dedd
DG
551
552/*
38ee087f 553 * Stop tracing for all traces of the session.
f3ed775e 554 */
38ee087f 555static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 556{
38ee087f 557 int ret, data_ret;
cd80958d
DG
558 struct lttcomm_session_msg lsm;
559
6a4f824d 560 if (session_name == NULL) {
2f70b271 561 return -LTTNG_ERR_INVALID;
6a4f824d
DG
562 }
563
cd80958d 564 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 565
cac3069d
DG
566 lttng_ctl_copy_string(lsm.session.name, session_name,
567 sizeof(lsm.session.name));
cd80958d 568
cac3069d 569 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
570 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
571 goto error;
572 }
573
574 if (!wait) {
575 goto end;
576 }
577
578 _MSG("Waiting for data availability");
b7a81c24 579 fflush(stdout);
38ee087f
DG
580
581 /* Check for data availability */
582 do {
6d805429 583 data_ret = lttng_data_pending(session_name);
38ee087f
DG
584 if (data_ret < 0) {
585 /* Return the data available call error. */
586 ret = data_ret;
587 goto error;
588 }
589
590 /*
591 * Data sleep time before retrying (in usec). Don't sleep if the call
592 * returned value indicates availability.
593 */
6d805429 594 if (data_ret) {
38ee087f
DG
595 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
596 _MSG(".");
b7a81c24 597 fflush(stdout);
38ee087f 598 }
6d805429 599 } while (data_ret != 0);
38ee087f
DG
600
601 MSG("");
602
603end:
604error:
605 return ret;
606}
607
608/*
609 * Stop tracing and wait for data availability.
610 */
611int lttng_stop_tracing(const char *session_name)
612{
613 return _lttng_stop_tracing(session_name, 1);
614}
615
616/*
617 * Stop tracing but _don't_ wait for data availability.
618 */
619int lttng_stop_tracing_no_wait(const char *session_name)
620{
621 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
622}
623
624/*
601d5acf
DG
625 * Add context to a channel.
626 *
627 * If the given channel is NULL, add the contexts to all channels.
628 * The event_name param is ignored.
af87c45a
DG
629 *
630 * Returns the size of the returned payload data or a negative error code.
1df4dedd 631 */
cd80958d 632int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
633 struct lttng_event_context *ctx, const char *event_name,
634 const char *channel_name)
d65106b1 635{
cd80958d
DG
636 struct lttcomm_session_msg lsm;
637
9d697d3d
DG
638 /* Safety check. Both are mandatory */
639 if (handle == NULL || ctx == NULL) {
2f70b271 640 return -LTTNG_ERR_INVALID;
cd80958d
DG
641 }
642
441c16a7
MD
643 memset(&lsm, 0, sizeof(lsm));
644
cd80958d
DG
645 lsm.cmd_type = LTTNG_ADD_CONTEXT;
646
85076754
MD
647 /* If no channel name, send empty string. */
648 if (channel_name == NULL) {
649 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
650 sizeof(lsm.u.context.channel_name));
651 } else {
652 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
653 sizeof(lsm.u.context.channel_name));
654 }
cd80958d 655
cac3069d 656 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 657
9d697d3d 658 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 659
cac3069d 660 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
661 sizeof(lsm.session.name));
662
cac3069d 663 return lttng_ctl_ask_sessiond(&lsm, NULL);
d65106b1
DG
664}
665
f3ed775e 666/*
1c8d13c8
TD
667 * Enable event(s) for a channel.
668 * If no event name is specified, all events are enabled.
669 * If no channel name is specified, the default 'channel0' is used.
670 * Returns size of returned session payload data or a negative error code.
f3ed775e 671 */
cd80958d 672int lttng_enable_event(struct lttng_handle *handle,
38057ed1 673 struct lttng_event *ev, const char *channel_name)
1df4dedd 674{
cd80958d
DG
675 struct lttcomm_session_msg lsm;
676
5117eeec 677 if (handle == NULL || ev == NULL) {
2f70b271 678 return -LTTNG_ERR_INVALID;
cd80958d 679 }
33a2b854 680
441c16a7
MD
681 memset(&lsm, 0, sizeof(lsm));
682
85076754 683 /* If no channel name, send empty string. */
94cf3c47 684 if (channel_name == NULL) {
85076754 685 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
cd80958d 686 sizeof(lsm.u.enable.channel_name));
33a2b854 687 } else {
cac3069d 688 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
cd80958d 689 sizeof(lsm.u.enable.channel_name));
eb354453
DG
690 }
691
cac3069d 692 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 693
8c9ae521 694 if (ev->name[0] != '\0') {
cd80958d 695 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 696 } else {
cd80958d 697 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 698 }
8c9ae521 699 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 700
cac3069d 701 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
702 sizeof(lsm.session.name));
703
cac3069d 704 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
705}
706
53a80697 707/*
025faf73 708 * Create or enable an event with a filter expression.
178191b3 709 *
53a80697
MD
710 * Return negative error value on error.
711 * Return size of returned session payload data if OK.
712 */
025faf73 713int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 714 struct lttng_event *event, const char *channel_name,
53a80697
MD
715 const char *filter_expression)
716{
717 struct lttcomm_session_msg lsm;
718 struct filter_parser_ctx *ctx;
719 FILE *fmem;
720 int ret = 0;
721
52df2401
MD
722 if (!filter_expression) {
723 /*
724 * Fall back to normal event enabling if no filter
725 * specified.
726 */
727 return lttng_enable_event(handle, event, channel_name);
728 }
729
730 /*
731 * Empty filter string will always be rejected by the parser
732 * anyway, so treat this corner-case early to eliminate
733 * lttng_fmemopen error for 0-byte allocation.
734 */
735 if (handle == NULL || filter_expression[0] == '\0') {
2f70b271 736 return -LTTNG_ERR_INVALID;
53a80697
MD
737 }
738
53a80697
MD
739 /*
740 * casting const to non-const, as the underlying function will
741 * use it in read-only mode.
742 */
743 fmem = lttng_fmemopen((void *) filter_expression,
744 strlen(filter_expression), "r");
745 if (!fmem) {
746 fprintf(stderr, "Error opening memory as stream\n");
ff7ef7b8 747 return -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
748 }
749 ctx = filter_parser_ctx_alloc(fmem);
750 if (!ctx) {
751 fprintf(stderr, "Error allocating parser\n");
ff7ef7b8 752 ret = -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
753 goto alloc_error;
754 }
755 ret = filter_parser_ctx_append_ast(ctx);
756 if (ret) {
757 fprintf(stderr, "Parse error\n");
ff7ef7b8 758 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
759 goto parse_error;
760 }
761 ret = filter_visitor_set_parent(ctx);
762 if (ret) {
763 fprintf(stderr, "Set parent error\n");
ff7ef7b8 764 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
765 goto parse_error;
766 }
767 if (print_xml) {
768 ret = filter_visitor_print_xml(ctx, stdout, 0);
769 if (ret) {
770 fflush(stdout);
771 fprintf(stderr, "XML print error\n");
ff7ef7b8 772 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
773 goto parse_error;
774 }
775 }
776
777 dbg_printf("Generating IR... ");
778 fflush(stdout);
779 ret = filter_visitor_ir_generate(ctx);
780 if (ret) {
781 fprintf(stderr, "Generate IR error\n");
ff7ef7b8 782 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
783 goto parse_error;
784 }
785 dbg_printf("done\n");
786
787 dbg_printf("Validating IR... ");
788 fflush(stdout);
789 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
790 if (ret) {
ff7ef7b8 791 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
792 goto parse_error;
793 }
794 dbg_printf("done\n");
795
796 dbg_printf("Generating bytecode... ");
797 fflush(stdout);
798 ret = filter_visitor_bytecode_generate(ctx);
799 if (ret) {
800 fprintf(stderr, "Generate bytecode error\n");
ff7ef7b8 801 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
802 goto parse_error;
803 }
804 dbg_printf("done\n");
805 dbg_printf("Size of bytecode generated: %u bytes.\n",
806 bytecode_get_len(&ctx->bytecode->b));
807
808 memset(&lsm, 0, sizeof(lsm));
809
025faf73 810 lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER;
53a80697 811
85076754
MD
812 /* If no channel name, send empty string. */
813 if (channel_name == NULL) {
814 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
815 sizeof(lsm.u.enable.channel_name));
816 } else {
817 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
818 sizeof(lsm.u.enable.channel_name));
819 }
820
53a80697 821 /* Copy event name */
178191b3
DG
822 if (event) {
823 memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event));
824 }
825
025faf73 826 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
53a80697
MD
827 + bytecode_get_len(&ctx->bytecode->b);
828
cac3069d 829 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
53a80697 830
cac3069d 831 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
53a80697
MD
832 sizeof(lsm.session.name));
833
cac3069d 834 ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
025faf73 835 lsm.u.enable.bytecode_len, NULL);
53a80697
MD
836
837 filter_bytecode_free(ctx);
838 filter_ir_free(ctx);
839 filter_parser_ctx_free(ctx);
840 if (fclose(fmem) != 0) {
841 perror("fclose");
842 }
843 return ret;
844
845parse_error:
846 filter_bytecode_free(ctx);
847 filter_ir_free(ctx);
848 filter_parser_ctx_free(ctx);
849alloc_error:
850 if (fclose(fmem) != 0) {
851 perror("fclose");
852 }
853 return ret;
854}
855
1df4dedd 856/*
1c8d13c8
TD
857 * Disable event(s) of a channel and domain.
858 * If no event name is specified, all events are disabled.
859 * If no channel name is specified, the default 'channel0' is used.
860 * Returns size of returned session payload data or a negative error code.
1df4dedd 861 */
cd80958d 862int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 863 const char *channel_name)
1df4dedd 864{
cd80958d 865 struct lttcomm_session_msg lsm;
1df4dedd 866
9d697d3d 867 if (handle == NULL) {
2f70b271 868 return -LTTNG_ERR_INVALID;
cd80958d
DG
869 }
870
441c16a7
MD
871 memset(&lsm, 0, sizeof(lsm));
872
85076754
MD
873 /* If no channel name, send empty string. */
874 if (channel_name == NULL) {
875 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 876 sizeof(lsm.u.disable.channel_name));
f3ed775e 877 } else {
85076754 878 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 879 sizeof(lsm.u.disable.channel_name));
eb354453
DG
880 }
881
cac3069d 882 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 883
f84efadf 884 if (name != NULL) {
cac3069d
DG
885 lttng_ctl_copy_string(lsm.u.disable.name, name,
886 sizeof(lsm.u.disable.name));
cd80958d 887 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 888 } else {
cd80958d 889 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
890 }
891
cac3069d 892 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
893 sizeof(lsm.session.name));
894
cac3069d 895 return lttng_ctl_ask_sessiond(&lsm, NULL);
1df4dedd
DG
896}
897
898/*
1c8d13c8
TD
899 * Enable channel per domain
900 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 901 */
cd80958d 902int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 903 struct lttng_channel *chan)
a5c5a2bd 904{
cd80958d
DG
905 struct lttcomm_session_msg lsm;
906
5117eeec
DG
907 /*
908 * NULL arguments are forbidden. No default values.
909 */
910 if (handle == NULL || chan == NULL) {
2f70b271 911 return -LTTNG_ERR_INVALID;
cd80958d
DG
912 }
913
441c16a7
MD
914 memset(&lsm, 0, sizeof(lsm));
915
5117eeec 916 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 917
cd80958d
DG
918 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
919
cac3069d 920 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 921
cac3069d 922 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
923 sizeof(lsm.session.name));
924
cac3069d 925 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 926}
1df4dedd 927
2ef84c95 928/*
1c8d13c8
TD
929 * All tracing will be stopped for registered events of the channel.
930 * Returns size of returned session payload data or a negative error code.
2ef84c95 931 */
cd80958d 932int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 933{
cd80958d
DG
934 struct lttcomm_session_msg lsm;
935
9d697d3d
DG
936 /* Safety check. Both are mandatory */
937 if (handle == NULL || name == NULL) {
2f70b271 938 return -LTTNG_ERR_INVALID;
cd80958d
DG
939 }
940
441c16a7
MD
941 memset(&lsm, 0, sizeof(lsm));
942
cd80958d 943 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 944
cac3069d 945 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
946 sizeof(lsm.u.disable.channel_name));
947
cac3069d 948 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 949
cac3069d 950 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
951 sizeof(lsm.session.name));
952
cac3069d 953 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
954}
955
fac6795d 956/*
1c8d13c8
TD
957 * Lists all available tracepoints of domain.
958 * Sets the contents of the events array.
959 * Returns the number of lttng_event entries in events;
960 * on error, returns a negative value.
fac6795d 961 */
cd80958d 962int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 963 struct lttng_event **events)
fac6795d 964{
052da939 965 int ret;
cd80958d
DG
966 struct lttcomm_session_msg lsm;
967
9d697d3d 968 if (handle == NULL) {
2f70b271 969 return -LTTNG_ERR_INVALID;
cd80958d 970 }
fac6795d 971
cd80958d 972 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 973 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 974
cac3069d 975 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
976 if (ret < 0) {
977 return ret;
eb354453 978 }
fac6795d 979
9f19cc17 980 return ret / sizeof(struct lttng_event);
fac6795d
DG
981}
982
f37d259d
MD
983/*
984 * Lists all available tracepoint fields of domain.
985 * Sets the contents of the event field array.
986 * Returns the number of lttng_event_field entries in events;
987 * on error, returns a negative value.
988 */
989int lttng_list_tracepoint_fields(struct lttng_handle *handle,
990 struct lttng_event_field **fields)
991{
992 int ret;
993 struct lttcomm_session_msg lsm;
994
995 if (handle == NULL) {
2f70b271 996 return -LTTNG_ERR_INVALID;
f37d259d
MD
997 }
998
999 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1000 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1001
cac3069d 1002 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1003 if (ret < 0) {
1004 return ret;
1005 }
1006
1007 return ret / sizeof(struct lttng_event_field);
1008}
1009
1657e9bb 1010/*
1c8d13c8
TD
1011 * Returns a human readable string describing
1012 * the error code (a negative value).
1657e9bb 1013 */
9a745bc7 1014const char *lttng_strerror(int code)
1657e9bb 1015{
f73fabfd 1016 return error_get_str(code);
1657e9bb
DG
1017}
1018
aaf97519 1019/*
a4b92340
DG
1020 * Create a brand new session using name and url for destination.
1021 *
f73fabfd 1022 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1023 */
a4b92340 1024int lttng_create_session(const char *name, const char *url)
00e2e675 1025{
3dd05a85 1026 int ret;
a4b92340 1027 ssize_t size;
00e2e675 1028 struct lttcomm_session_msg lsm;
a4b92340 1029 struct lttng_uri *uris = NULL;
00e2e675 1030
a4b92340 1031 if (name == NULL) {
2f70b271 1032 return -LTTNG_ERR_INVALID;
00e2e675
DG
1033 }
1034
a4b92340 1035 memset(&lsm, 0, sizeof(lsm));
00e2e675 1036
a4b92340 1037 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1038 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1039
1040 /* There should never be a data URL */
bc894455 1041 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1042 if (size < 0) {
2f70b271 1043 return -LTTNG_ERR_INVALID;
00e2e675
DG
1044 }
1045
a4b92340
DG
1046 lsm.u.uri.size = size;
1047
cac3069d
DG
1048 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1049 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1050
1051 free(uris);
1052 return ret;
00e2e675
DG
1053}
1054
8028d920 1055/*
8028d920 1056 * Destroy session using name.
1c8d13c8 1057 * Returns size of returned session payload data or a negative error code.
8028d920 1058 */
843f5df9 1059int lttng_destroy_session(const char *session_name)
8028d920 1060{
cd80958d
DG
1061 struct lttcomm_session_msg lsm;
1062
843f5df9 1063 if (session_name == NULL) {
2f70b271 1064 return -LTTNG_ERR_INVALID;
cd80958d
DG
1065 }
1066
1067 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1068
cac3069d
DG
1069 lttng_ctl_copy_string(lsm.session.name, session_name,
1070 sizeof(lsm.session.name));
cd80958d 1071
cac3069d 1072 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1073}
1074
57167058 1075/*
57167058 1076 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1077 * Sets the contents of the sessions array.
1078 * Returns the number of lttng_session entries in sessions;
1079 * on error, returns a negative value.
57167058 1080 */
ca95a216 1081int lttng_list_sessions(struct lttng_session **sessions)
57167058 1082{
ca95a216 1083 int ret;
cd80958d 1084 struct lttcomm_session_msg lsm;
57167058 1085
cd80958d 1086 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1087 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1088 if (ret < 0) {
ca95a216 1089 return ret;
57167058
DG
1090 }
1091
ca95a216 1092 return ret / sizeof(struct lttng_session);
57167058
DG
1093}
1094
9f19cc17 1095/*
1c8d13c8
TD
1096 * Ask the session daemon for all available domains of a session.
1097 * Sets the contents of the domains array.
1098 * Returns the number of lttng_domain entries in domains;
1099 * on error, returns a negative value.
9f19cc17 1100 */
330be774 1101int lttng_list_domains(const char *session_name,
cd80958d 1102 struct lttng_domain **domains)
9f19cc17
DG
1103{
1104 int ret;
cd80958d
DG
1105 struct lttcomm_session_msg lsm;
1106
330be774 1107 if (session_name == NULL) {
2f70b271 1108 return -LTTNG_ERR_INVALID;
cd80958d 1109 }
9f19cc17 1110
cd80958d
DG
1111 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1112
cac3069d
DG
1113 lttng_ctl_copy_string(lsm.session.name, session_name,
1114 sizeof(lsm.session.name));
cd80958d 1115
cac3069d 1116 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1117 if (ret < 0) {
1118 return ret;
1119 }
1120
1121 return ret / sizeof(struct lttng_domain);
1122}
1123
1124/*
1c8d13c8
TD
1125 * Ask the session daemon for all available channels of a session.
1126 * Sets the contents of the channels array.
1127 * Returns the number of lttng_channel entries in channels;
1128 * on error, returns a negative value.
9f19cc17 1129 */
cd80958d
DG
1130int lttng_list_channels(struct lttng_handle *handle,
1131 struct lttng_channel **channels)
9f19cc17
DG
1132{
1133 int ret;
cd80958d
DG
1134 struct lttcomm_session_msg lsm;
1135
9d697d3d 1136 if (handle == NULL) {
2f70b271 1137 return -LTTNG_ERR_INVALID;
cd80958d
DG
1138 }
1139
1140 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1141 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1142 sizeof(lsm.session.name));
9f19cc17 1143
cac3069d 1144 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1145
cac3069d 1146 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1147 if (ret < 0) {
1148 return ret;
1149 }
1150
1151 return ret / sizeof(struct lttng_channel);
1152}
1153
1154/*
1c8d13c8
TD
1155 * Ask the session daemon for all available events of a session channel.
1156 * Sets the contents of the events array.
1157 * Returns the number of lttng_event entries in events;
1158 * on error, returns a negative value.
9f19cc17 1159 */
cd80958d
DG
1160int lttng_list_events(struct lttng_handle *handle,
1161 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1162{
1163 int ret;
cd80958d 1164 struct lttcomm_session_msg lsm;
9f19cc17 1165
9d697d3d
DG
1166 /* Safety check. An handle and channel name are mandatory */
1167 if (handle == NULL || channel_name == NULL) {
2f70b271 1168 return -LTTNG_ERR_INVALID;
cd80958d
DG
1169 }
1170
1171 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1172 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1173 sizeof(lsm.session.name));
cac3069d 1174 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d
DG
1175 sizeof(lsm.u.list.channel_name));
1176
cac3069d 1177 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1178
cac3069d 1179 ret = lttng_ctl_ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1180 if (ret < 0) {
1181 return ret;
1182 }
1183
1184 return ret / sizeof(struct lttng_event);
1185}
1186
fac6795d 1187/*
1c8d13c8
TD
1188 * Sets the tracing_group variable with name.
1189 * This function allocates memory pointed to by tracing_group.
1190 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1191 */
1192int lttng_set_tracing_group(const char *name)
1193{
9d697d3d 1194 if (name == NULL) {
2f70b271 1195 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1196 }
1197
fac6795d 1198 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1199 return -LTTNG_ERR_FATAL;
fac6795d
DG
1200 }
1201
1202 return 0;
1203}
1204
d0254c7c 1205/*
af87c45a 1206 * Returns size of returned session payload data or a negative error code.
d0254c7c 1207 */
cd80958d 1208int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1209 struct lttng_calibrate *calibrate)
1210{
cd80958d 1211 struct lttcomm_session_msg lsm;
d0254c7c 1212
9d697d3d
DG
1213 /* Safety check. NULL pointer are forbidden */
1214 if (handle == NULL || calibrate == NULL) {
2f70b271 1215 return -LTTNG_ERR_INVALID;
cd80958d 1216 }
d0254c7c 1217
cd80958d 1218 lsm.cmd_type = LTTNG_CALIBRATE;
cac3069d 1219 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1220
cd80958d
DG
1221 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1222
cac3069d 1223 return lttng_ctl_ask_sessiond(&lsm, NULL);
d0254c7c
MD
1224}
1225
5edd7e09
DG
1226/*
1227 * Set default channel attributes.
441c16a7 1228 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1229 */
1230void lttng_channel_set_default_attr(struct lttng_domain *domain,
1231 struct lttng_channel_attr *attr)
1232{
1233 /* Safety check */
1234 if (attr == NULL || domain == NULL) {
1235 return;
1236 }
1237
eacaa7a6
DS
1238 memset(attr, 0, sizeof(struct lttng_channel_attr));
1239
0a9c6494
DG
1240 /* Same for all domains. */
1241 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1242 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
1243 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
1244
5edd7e09
DG
1245 switch (domain->type) {
1246 case LTTNG_DOMAIN_KERNEL:
d92ff3ef
DG
1247 attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
1248 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 1249 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
1250 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1251 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1252 break;
1253 case LTTNG_DOMAIN_UST:
0a9c6494
DG
1254 switch (domain->buf_type) {
1255 case LTTNG_BUFFER_PER_UID:
1256 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
1257 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
1258 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
1259 attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
1260 attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER;
1261 break;
1262 case LTTNG_BUFFER_PER_PID:
1263 default:
1264 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
1265 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
1266 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
1267 attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
1268 attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER;
1269 break;
1270 }
5edd7e09 1271 default:
441c16a7 1272 /* Default behavior: leave set to 0. */
5edd7e09
DG
1273 break;
1274 }
1275}
1276
fac6795d 1277/*
2269e89e 1278 * Check if session daemon is alive.
fac6795d 1279 *
2269e89e 1280 * Return 1 if alive or 0 if not.
1c8d13c8 1281 * On error returns a negative value.
fac6795d 1282 */
947308c4 1283int lttng_session_daemon_alive(void)
fac6795d
DG
1284{
1285 int ret;
1286
1287 ret = set_session_daemon_path();
1288 if (ret < 0) {
947308c4 1289 /* Error */
fac6795d
DG
1290 return ret;
1291 }
1292
9d035200 1293 if (*sessiond_sock_path == '\0') {
2f70b271
DG
1294 /*
1295 * No socket path set. Weird error which means the constructor was not
1296 * called.
1297 */
1298 assert(0);
fac6795d
DG
1299 }
1300
2269e89e 1301 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1302 if (ret < 0) {
1303 /* Not alive */
1304 return 0;
1305 }
7d8234d9 1306
947308c4
DG
1307 /* Is alive */
1308 return 1;
fac6795d
DG
1309}
1310
00e2e675 1311/*
a4b92340 1312 * Set URL for a consumer for a session and domain.
00e2e675
DG
1313 *
1314 * Return 0 on success, else a negative value.
1315 */
a4b92340
DG
1316int lttng_set_consumer_url(struct lttng_handle *handle,
1317 const char *control_url, const char *data_url)
00e2e675 1318{
3dd05a85 1319 int ret;
a4b92340 1320 ssize_t size;
00e2e675 1321 struct lttcomm_session_msg lsm;
a4b92340 1322 struct lttng_uri *uris = NULL;
00e2e675 1323
a4b92340 1324 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1325 return -LTTNG_ERR_INVALID;
00e2e675
DG
1326 }
1327
a4b92340
DG
1328 memset(&lsm, 0, sizeof(lsm));
1329
00e2e675
DG
1330 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1331
cac3069d 1332 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 1333 sizeof(lsm.session.name));
cac3069d 1334 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 1335
bc894455 1336 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 1337 if (size < 0) {
2f70b271 1338 return -LTTNG_ERR_INVALID;
a4b92340
DG
1339 }
1340
1341 lsm.u.uri.size = size;
00e2e675 1342
cac3069d
DG
1343 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1344 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1345
1346 free(uris);
1347 return ret;
00e2e675
DG
1348}
1349
1350/*
9c6bda17 1351 * [OBSOLETE]
00e2e675
DG
1352 */
1353int lttng_enable_consumer(struct lttng_handle *handle)
1354{
785d2d0d 1355 return -ENOSYS;
00e2e675
DG
1356}
1357
1358/*
9c6bda17 1359 * [OBSOLETE]
00e2e675
DG
1360 */
1361int lttng_disable_consumer(struct lttng_handle *handle)
1362{
785d2d0d 1363 return -ENOSYS;
00e2e675
DG
1364}
1365
07424f16
DG
1366/*
1367 * This is an extension of create session that is ONLY and SHOULD only be used
1368 * by the lttng command line program. It exists to avoid using URI parsing in
1369 * the lttng client.
1370 *
1371 * We need the date and time for the trace path subdirectory for the case where
1372 * the user does NOT define one using either -o or -U. Using the normal
1373 * lttng_create_session API call, we have no clue on the session daemon side if
1374 * the URL was generated automatically by the client or define by the user.
1375 *
1376 * So this function "wrapper" is hidden from the public API, takes the datetime
1377 * string and appends it if necessary to the URI subdirectory before sending it
1378 * to the session daemon.
1379 *
1380 * With this extra function, the lttng_create_session call behavior is not
1381 * changed and the timestamp is appended to the URI on the session daemon side
1382 * if necessary.
1383 */
1384int _lttng_create_session_ext(const char *name, const char *url,
1385 const char *datetime)
1386{
3dd05a85 1387 int ret;
07424f16
DG
1388 ssize_t size;
1389 struct lttcomm_session_msg lsm;
1390 struct lttng_uri *uris = NULL;
1391
2bba9e53 1392 if (name == NULL || datetime == NULL) {
2f70b271 1393 return -LTTNG_ERR_INVALID;
07424f16
DG
1394 }
1395
1396 memset(&lsm, 0, sizeof(lsm));
1397
1398 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1399 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16
DG
1400
1401 /* There should never be a data URL */
bc894455 1402 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 1403 if (size < 0) {
3dd05a85
DG
1404 ret = -LTTNG_ERR_INVALID;
1405 goto error;
07424f16
DG
1406 }
1407
1408 lsm.u.uri.size = size;
1409
4b35a6b3 1410 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
1411 /* Don't append datetime if the name was automatically created. */
1412 if (strncmp(name, DEFAULT_SESSION_NAME "-",
1413 strlen(DEFAULT_SESSION_NAME) + 1)) {
1414 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
1415 name, datetime);
1416 } else {
1417 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
1418 }
07424f16
DG
1419 if (ret < 0) {
1420 PERROR("snprintf uri subdir");
3dd05a85
DG
1421 ret = -LTTNG_ERR_FATAL;
1422 goto error;
07424f16
DG
1423 }
1424 }
1425
cac3069d
DG
1426 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1427 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1428
1429error:
1430 free(uris);
1431 return ret;
07424f16
DG
1432}
1433
806e2684
DG
1434/*
1435 * For a given session name, this call checks if the data is ready to be read
1436 * or is still being extracted by the consumer(s) hence not ready to be used by
1437 * any readers.
1438 */
6d805429 1439int lttng_data_pending(const char *session_name)
806e2684
DG
1440{
1441 int ret;
1442 struct lttcomm_session_msg lsm;
1443
1444 if (session_name == NULL) {
1445 return -LTTNG_ERR_INVALID;
1446 }
1447
6d805429 1448 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 1449
cac3069d
DG
1450 lttng_ctl_copy_string(lsm.session.name, session_name,
1451 sizeof(lsm.session.name));
806e2684 1452
cac3069d 1453 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
806e2684
DG
1454
1455 /*
cac3069d
DG
1456 * The lttng_ctl_ask_sessiond function negate the return code if it's not
1457 * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning
1458 * that the data is available. Yes it is hackish but for now this is the
1459 * only way.
806e2684
DG
1460 */
1461 if (ret == -1) {
1462 ret = 1;
1463 }
1464
1465 return ret;
1466}
1467
27babd3a
DG
1468/*
1469 * Create a session exclusively used for snapshot.
1470 *
1471 * Returns LTTNG_OK on success or a negative error code.
1472 */
1473int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1474{
1475 int ret;
1476 ssize_t size;
1477 struct lttcomm_session_msg lsm;
1478 struct lttng_uri *uris = NULL;
1479
1480 if (name == NULL) {
1481 return -LTTNG_ERR_INVALID;
1482 }
1483
1484 memset(&lsm, 0, sizeof(lsm));
1485
1486 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
1487 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1488
1489 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1490 if (size < 0) {
1491 return -LTTNG_ERR_INVALID;
1492 }
1493
1494 lsm.u.uri.size = size;
1495
1496 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1497 sizeof(struct lttng_uri) * size, NULL);
1498
1499 free(uris);
1500 return ret;
1501}
1502
ecc48a90
JD
1503/*
1504 * Create a session exclusively used for live.
1505 *
1506 * Returns LTTNG_OK on success or a negative error code.
1507 */
1508int lttng_create_session_live(const char *name, const char *url,
1509 unsigned int timer_interval)
1510{
1511 int ret;
1512 ssize_t size;
1513 struct lttcomm_session_msg lsm;
1514 struct lttng_uri *uris = NULL;
1515
1516 if (name == NULL) {
1517 return -LTTNG_ERR_INVALID;
1518 }
1519
1520 memset(&lsm, 0, sizeof(lsm));
1521
1522 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
1523 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1524
1525 size = uri_parse_str_urls(url, NULL, &uris);
1526 if (size < 0) {
1527 ret = -LTTNG_ERR_INVALID;
1528 goto end;
1529 }
1530
1531 /* file:// is not accepted for live session. */
1532 if (uris[0].dtype == LTTNG_DST_PATH) {
1533 ret = -LTTNG_ERR_INVALID;
1534 goto end;
1535 }
1536
1537 lsm.u.session_live.nb_uri = size;
1538 lsm.u.session_live.timer_interval = timer_interval;
1539
1540 ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris,
1541 sizeof(struct lttng_uri) * size, NULL);
1542
1543end:
1544 free(uris);
1545 return ret;
1546}
1547
fac6795d
DG
1548/*
1549 * lib constructor
1550 */
1551static void __attribute__((constructor)) init()
1552{
1553 /* Set default session group */
bbccc3d2 1554 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 1555}
49cca668
DG
1556
1557/*
1558 * lib destructor
1559 */
1560static void __attribute__((destructor)) lttng_ctl_exit()
1561{
1562 free(tracing_group);
1563}
This page took 0.122233 seconds and 4 git commands to generate.