Fix: destroy called twice on quit pipe
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
eb5c4f4e 2 * lttng-ctl.c
82a3637f
DG
3 *
4 * Linux Trace Toolkit Control Library
5 *
826d496d 6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
2001793c 7 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 8 *
d14d33bf
AM
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
82a3637f
DG
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 *
d14d33bf
AM
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
21 */
22
6c1c0768 23#define _LGPL_SOURCE
44a5e5eb 24#include <assert.h>
fac6795d 25#include <grp.h>
1e307fab 26#include <errno.h>
fac6795d
DG
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
990570ed 32#include <common/common.h>
15e37663 33#include <common/compat/string.h>
990570ed 34#include <common/defaults.h>
db758600 35#include <common/sessiond-comm/sessiond-comm.h>
a4b92340 36#include <common/uri.h>
feb0f3e5 37#include <common/utils.h>
de453daa 38#include <common/dynamic-buffer.h>
1e307fab 39#include <lttng/lttng.h>
0c89d795 40#include <lttng/health-internal.h>
a58c490f
JG
41#include <lttng/trigger/trigger-internal.h>
42#include <lttng/endpoint.h>
43#include <lttng/channel-internal.h>
de453daa 44#include <lttng/event-internal.h>
15e37663 45#include <lttng/userspace-probe-internal.h>
fac6795d 46
d00c599e
DG
47#include "filter/filter-ast.h"
48#include "filter/filter-parser.h"
49#include "filter/filter-bytecode.h"
50#include "filter/memstream.h"
cac3069d 51#include "lttng-ctl-helper.h"
53a80697
MD
52
53#ifdef DEBUG
d00c599e 54static const int print_xml = 1;
53a80697
MD
55#define dbg_printf(fmt, args...) \
56 printf("[debug liblttng-ctl] " fmt, ## args)
57#else
d00c599e 58static const int print_xml = 0;
53a80697
MD
59#define dbg_printf(fmt, args...) \
60do { \
61 /* do nothing but check printf format */ \
62 if (0) \
63 printf("[debug liblttnctl] " fmt, ## args); \
64} while (0)
65#endif
66
67
fac6795d
DG
68/* Socket to session daemon for communication */
69static int sessiond_socket;
70static char sessiond_sock_path[PATH_MAX];
71
fac6795d
DG
72/* Variables */
73static char *tracing_group;
74static int connected;
75
97e19046
DG
76/* Global */
77
78/*
79 * Those two variables are used by error.h to silent or control the verbosity of
80 * error message. They are global to the library so application linking with it
81 * are able to compile correctly and also control verbosity of the library.
97e19046
DG
82 */
83int lttng_opt_quiet;
84int lttng_opt_verbose;
c7e35b03 85int lttng_opt_mi;
97e19046 86
99497cd0
MD
87/*
88 * Copy string from src to dst and enforce null terminated byte.
89 */
cac3069d
DG
90LTTNG_HIDDEN
91void lttng_ctl_copy_string(char *dst, const char *src, size_t len)
99497cd0 92{
e7d6716d 93 if (src && dst) {
99497cd0
MD
94 strncpy(dst, src, len);
95 /* Enforce the NULL terminated byte */
96 dst[len - 1] = '\0';
cd80958d
DG
97 } else if (dst) {
98 dst[0] = '\0';
99497cd0
MD
99 }
100}
101
fac6795d 102/*
cd80958d 103 * Copy domain to lttcomm_session_msg domain.
fac6795d 104 *
cd80958d
DG
105 * If domain is unknown, default domain will be the kernel.
106 */
cac3069d
DG
107LTTNG_HIDDEN
108void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
109 struct lttng_domain *src)
cd80958d
DG
110{
111 if (src && dst) {
112 switch (src->type) {
00e2e675
DG
113 case LTTNG_DOMAIN_KERNEL:
114 case LTTNG_DOMAIN_UST:
b9dfb167 115 case LTTNG_DOMAIN_JUL:
5cdb6027 116 case LTTNG_DOMAIN_LOG4J:
0e115563 117 case LTTNG_DOMAIN_PYTHON:
00e2e675
DG
118 memcpy(dst, src, sizeof(struct lttng_domain));
119 break;
120 default:
121 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 122 break;
cd80958d
DG
123 }
124 }
125}
126
127/*
128 * Send lttcomm_session_msg to the session daemon.
fac6795d 129 *
1c8d13c8
TD
130 * On success, returns the number of bytes sent (>=0)
131 * On error, returns -1
fac6795d 132 */
cd80958d 133static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
134{
135 int ret;
136
137 if (!connected) {
2f70b271 138 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 139 goto end;
fac6795d
DG
140 }
141
a4b92340
DG
142 DBG("LSM cmd type : %d", lsm->cmd_type);
143
be040666 144 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 145 sizeof(struct lttcomm_session_msg));
2f70b271
DG
146 if (ret < 0) {
147 ret = -LTTNG_ERR_FATAL;
148 }
e065084a
DG
149
150end:
151 return ret;
152}
153
53a80697
MD
154/*
155 * Send var len data to the session daemon.
156 *
157 * On success, returns the number of bytes sent (>=0)
158 * On error, returns -1
159 */
c2d69327 160static int send_session_varlen(const void *data, size_t len)
53a80697
MD
161{
162 int ret;
163
164 if (!connected) {
2f70b271 165 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
166 goto end;
167 }
a4b92340 168
53a80697
MD
169 if (!data || !len) {
170 ret = 0;
171 goto end;
172 }
173
174 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
175 if (ret < 0) {
176 ret = -LTTNG_ERR_FATAL;
177 }
53a80697
MD
178
179end:
180 return ret;
181}
182
a04d53fc
FD
183/*
184 * Send file descriptors to the session daemon.
185 *
186 * On success, returns the number of bytes sent (>=0)
187 * On error, returns -1
188 */
189static int send_session_fds(const int *fds, size_t nb_fd)
190{
191 int ret;
192
193 if (!connected) {
194 ret = -LTTNG_ERR_NO_SESSIOND;
195 goto end;
196 }
197
198 if (!fds || !nb_fd) {
199 ret = 0;
200 goto end;
201 }
202
203 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
204 if (ret < 0) {
205 ret = -LTTNG_ERR_FATAL;
206 }
207
208end:
209 return ret;
210}
211
e065084a 212/*
cd80958d 213 * Receive data from the sessiond socket.
e065084a 214 *
1c8d13c8
TD
215 * On success, returns the number of bytes received (>=0)
216 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 217 */
ca95a216 218static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
219{
220 int ret;
221
222 if (!connected) {
2f70b271 223 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 224 goto end;
fac6795d
DG
225 }
226
ca95a216 227 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
228 if (ret < 0) {
229 ret = -LTTNG_ERR_FATAL;
230 }
fac6795d 231
e065084a 232end:
fac6795d
DG
233 return ret;
234}
235
236/*
9ae110e2 237 * Check if we are in the specified group.
65beb5ff 238 *
9ae110e2 239 * If yes return 1, else return -1.
947308c4 240 */
6c71277b
MD
241LTTNG_HIDDEN
242int lttng_check_tracing_group(void)
947308c4
DG
243{
244 struct group *grp_tracing; /* no free(). See getgrnam(3) */
245 gid_t *grp_list;
246 int grp_list_size, grp_id, i;
247 int ret = -1;
6c71277b 248 const char *grp_name = tracing_group;
947308c4
DG
249
250 /* Get GID of group 'tracing' */
251 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
252 if (!grp_tracing) {
253 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
254 goto end;
255 }
256
257 /* Get number of supplementary group IDs */
258 grp_list_size = getgroups(0, NULL);
259 if (grp_list_size < 0) {
6f04ed72 260 PERROR("getgroups");
947308c4
DG
261 goto end;
262 }
263
264 /* Alloc group list of the right size */
3f451dc0 265 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
00795392 266 if (!grp_list) {
6f04ed72 267 PERROR("malloc");
00795392
MD
268 goto end;
269 }
947308c4 270 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 271 if (grp_id < 0) {
6f04ed72 272 PERROR("getgroups");
947308c4
DG
273 goto free_list;
274 }
275
276 for (i = 0; i < grp_list_size; i++) {
277 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 278 ret = 1;
947308c4
DG
279 break;
280 }
281 }
282
283free_list:
284 free(grp_list);
285
286end:
287 return ret;
288}
289
290/*
2269e89e
DG
291 * Try connect to session daemon with sock_path.
292 *
293 * Return 0 on success, else -1
294 */
295static int try_connect_sessiond(const char *sock_path)
296{
297 int ret;
298
299 /* If socket exist, we check if the daemon listens for connect. */
300 ret = access(sock_path, F_OK);
301 if (ret < 0) {
302 /* Not alive */
2f70b271 303 goto error;
2269e89e
DG
304 }
305
306 ret = lttcomm_connect_unix_sock(sock_path);
307 if (ret < 0) {
9ae110e2 308 /* Not alive. */
2f70b271 309 goto error;
2269e89e
DG
310 }
311
312 ret = lttcomm_close_unix_sock(ret);
313 if (ret < 0) {
6f04ed72 314 PERROR("lttcomm_close_unix_sock");
2269e89e
DG
315 }
316
317 return 0;
2f70b271
DG
318
319error:
320 return -1;
2269e89e
DG
321}
322
323/*
2f70b271
DG
324 * Set sessiond socket path by putting it in the global sessiond_sock_path
325 * variable.
326 *
327 * Returns 0 on success, negative value on failure (the sessiond socket path
328 * is somehow too long or ENOMEM).
947308c4
DG
329 */
330static int set_session_daemon_path(void)
331{
9ae110e2 332 int in_tgroup = 0; /* In tracing group. */
2269e89e
DG
333 uid_t uid;
334
335 uid = getuid();
947308c4 336
2269e89e
DG
337 if (uid != 0) {
338 /* Are we in the tracing group ? */
6c71277b 339 in_tgroup = lttng_check_tracing_group();
2269e89e
DG
340 }
341
08a9c49f 342 if ((uid == 0) || in_tgroup) {
cac3069d
DG
343 lttng_ctl_copy_string(sessiond_sock_path,
344 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path));
08a9c49f 345 }
2269e89e 346
08a9c49f 347 if (uid != 0) {
c617c0c6
MD
348 int ret;
349
08a9c49f 350 if (in_tgroup) {
9ae110e2 351 /* Tracing group. */
08a9c49f
TD
352 ret = try_connect_sessiond(sessiond_sock_path);
353 if (ret >= 0) {
354 goto end;
2269e89e 355 }
08a9c49f 356 /* Global session daemon not available... */
2269e89e 357 }
08a9c49f
TD
358 /* ...or not in tracing group (and not root), default */
359
360 /*
9ae110e2
JG
361 * With GNU C < 2.1, snprintf returns -1 if the target buffer
362 * is too small;
363 * With GNU C >= 2.1, snprintf returns the required size
364 * (excluding closing null)
08a9c49f
TD
365 */
366 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
feb0f3e5 367 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
08a9c49f 368 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 369 goto error;
947308c4 370 }
947308c4 371 }
08a9c49f 372end:
947308c4 373 return 0;
2f70b271
DG
374
375error:
376 return -1;
947308c4
DG
377}
378
65beb5ff 379/*
9ae110e2 380 * Connect to the LTTng session daemon.
65beb5ff 381 *
9ae110e2 382 * On success, return 0. On error, return -1.
65beb5ff
DG
383 */
384static int connect_sessiond(void)
385{
386 int ret;
387
da3c9ec1
DG
388 /* Don't try to connect if already connected. */
389 if (connected) {
390 return 0;
391 }
392
65beb5ff
DG
393 ret = set_session_daemon_path();
394 if (ret < 0) {
2f70b271 395 goto error;
65beb5ff
DG
396 }
397
9ae110e2 398 /* Connect to the sesssion daemon. */
65beb5ff
DG
399 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
400 if (ret < 0) {
2f70b271 401 goto error;
65beb5ff
DG
402 }
403
404 sessiond_socket = ret;
405 connected = 1;
406
407 return 0;
2f70b271
DG
408
409error:
410 return -1;
65beb5ff
DG
411}
412
413/*
1c8d13c8 414 * Clean disconnect from the session daemon.
9ae110e2 415 *
1c8d13c8 416 * On success, return 0. On error, return -1.
65beb5ff
DG
417 */
418static int disconnect_sessiond(void)
419{
420 int ret = 0;
421
422 if (connected) {
423 ret = lttcomm_close_unix_sock(sessiond_socket);
424 sessiond_socket = 0;
425 connected = 0;
426 }
427
428 return ret;
429}
430
795a978d
PP
431static int recv_sessiond_optional_data(size_t len, void **user_buf,
432 size_t *user_len)
433{
434 int ret = 0;
435 void *buf = NULL;
436
437 if (len) {
438 if (!user_len) {
439 ret = -LTTNG_ERR_INVALID;
440 goto end;
441 }
442
443 buf = zmalloc(len);
444 if (!buf) {
445 ret = -ENOMEM;
446 goto end;
447 }
448
449 ret = recv_data_sessiond(buf, len);
450 if (ret < 0) {
451 goto end;
452 }
453
454 if (!user_buf) {
455 ret = -LTTNG_ERR_INVALID;
456 goto end;
457 }
458
459 /* Move ownership of command header buffer to user. */
460 *user_buf = buf;
461 buf = NULL;
462 *user_len = len;
463 } else {
464 /* No command header. */
465 if (user_len) {
466 *user_len = 0;
467 }
468
469 if (user_buf) {
470 *user_buf = NULL;
471 }
472 }
473
474end:
475 free(buf);
476 return ret;
477}
478
35a6fdb7 479/*
cd80958d 480 * Ask the session daemon a specific command and put the data into buf.
a04d53fc
FD
481 * Takes extra var. len. data and file descriptors as input to send to the
482 * session daemon.
65beb5ff 483 *
af87c45a 484 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 485 */
cac3069d 486LTTNG_HIDDEN
a04d53fc
FD
487int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
488 const int *fds, size_t nb_fd, const void *vardata,
489 size_t vardata_len, void **user_payload_buf,
490 void **user_cmd_header_buf, size_t *user_cmd_header_len)
65beb5ff
DG
491{
492 int ret;
795a978d 493 size_t payload_len;
cd80958d 494 struct lttcomm_lttng_msg llm;
65beb5ff
DG
495
496 ret = connect_sessiond();
497 if (ret < 0) {
2f70b271 498 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
499 goto end;
500 }
501
65beb5ff 502 /* Send command to session daemon */
cd80958d 503 ret = send_session_msg(lsm);
65beb5ff 504 if (ret < 0) {
2f70b271 505 /* Ret value is a valid lttng error code. */
65beb5ff
DG
506 goto end;
507 }
53a80697 508 /* Send var len data */
795a978d 509 ret = send_session_varlen(vardata, vardata_len);
53a80697 510 if (ret < 0) {
2f70b271 511 /* Ret value is a valid lttng error code. */
53a80697
MD
512 goto end;
513 }
65beb5ff 514
a04d53fc
FD
515 /* Send fds */
516 ret = send_session_fds(fds, nb_fd);
517 if (ret < 0) {
518 /* Ret value is a valid lttng error code. */
519 goto end;
520 }
521
65beb5ff
DG
522 /* Get header from data transmission */
523 ret = recv_data_sessiond(&llm, sizeof(llm));
524 if (ret < 0) {
2f70b271 525 /* Ret value is a valid lttng error code. */
65beb5ff
DG
526 goto end;
527 }
528
529 /* Check error code if OK */
f73fabfd 530 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
531 ret = -llm.ret_code;
532 goto end;
533 }
534
795a978d
PP
535 /* Get command header from data transmission */
536 ret = recv_sessiond_optional_data(llm.cmd_header_size,
537 user_cmd_header_buf, user_cmd_header_len);
65beb5ff 538 if (ret < 0) {
65beb5ff
DG
539 goto end;
540 }
541
795a978d
PP
542 /* Get payload from data transmission */
543 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
544 &payload_len);
545 if (ret < 0) {
83009e5e
DG
546 goto end;
547 }
548
795a978d 549 ret = llm.data_size;
65beb5ff
DG
550
551end:
552 disconnect_sessiond();
553 return ret;
554}
555
9f19cc17 556/*
cd80958d 557 * Create lttng handle and return pointer.
9ae110e2 558 *
1c8d13c8 559 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 560 */
cd80958d
DG
561struct lttng_handle *lttng_create_handle(const char *session_name,
562 struct lttng_domain *domain)
9f19cc17 563{
2f70b271
DG
564 struct lttng_handle *handle = NULL;
565
3f451dc0 566 handle = zmalloc(sizeof(struct lttng_handle));
cd80958d 567 if (handle == NULL) {
2f70b271 568 PERROR("malloc handle");
cd80958d
DG
569 goto end;
570 }
571
572 /* Copy session name */
cac3069d 573 lttng_ctl_copy_string(handle->session_name, session_name,
cd80958d
DG
574 sizeof(handle->session_name));
575
95681498
JG
576 /* Copy lttng domain or leave initialized to 0. */
577 if (domain) {
578 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
579 }
cd80958d
DG
580
581end:
582 return handle;
583}
584
585/*
586 * Destroy handle by free(3) the pointer.
587 */
588void lttng_destroy_handle(struct lttng_handle *handle)
589{
0e428499 590 free(handle);
eb354453
DG
591}
592
d9800920
DG
593/*
594 * Register an outside consumer.
9ae110e2 595 *
1c8d13c8 596 * Returns size of returned session payload data or a negative error code.
d9800920
DG
597 */
598int lttng_register_consumer(struct lttng_handle *handle,
599 const char *socket_path)
600{
601 struct lttcomm_session_msg lsm;
602
2f70b271
DG
603 if (handle == NULL || socket_path == NULL) {
604 return -LTTNG_ERR_INVALID;
605 }
606
53efb85a 607 memset(&lsm, 0, sizeof(lsm));
d9800920 608 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
cac3069d 609 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
d9800920 610 sizeof(lsm.session.name));
cac3069d 611 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
d9800920 612
9ae110e2
JG
613 lttng_ctl_copy_string(lsm.u.reg.path, socket_path,
614 sizeof(lsm.u.reg.path));
d9800920 615
cac3069d 616 return lttng_ctl_ask_sessiond(&lsm, NULL);
d9800920
DG
617}
618
1df4dedd 619/*
9ae110e2
JG
620 * Start tracing for all traces of the session.
621 *
622 * Returns size of returned session payload data or a negative error code.
1df4dedd 623 */
6a4f824d 624int lttng_start_tracing(const char *session_name)
f3ed775e 625{
cd80958d
DG
626 struct lttcomm_session_msg lsm;
627
6a4f824d 628 if (session_name == NULL) {
2f70b271 629 return -LTTNG_ERR_INVALID;
cd80958d
DG
630 }
631
53efb85a 632 memset(&lsm, 0, sizeof(lsm));
cd80958d 633 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d 634
cac3069d
DG
635 lttng_ctl_copy_string(lsm.session.name, session_name,
636 sizeof(lsm.session.name));
cd80958d 637
cac3069d 638 return lttng_ctl_ask_sessiond(&lsm, NULL);
f3ed775e 639}
1df4dedd
DG
640
641/*
38ee087f 642 * Stop tracing for all traces of the session.
f3ed775e 643 */
38ee087f 644static int _lttng_stop_tracing(const char *session_name, int wait)
f3ed775e 645{
38ee087f 646 int ret, data_ret;
cd80958d
DG
647 struct lttcomm_session_msg lsm;
648
6a4f824d 649 if (session_name == NULL) {
2f70b271 650 return -LTTNG_ERR_INVALID;
6a4f824d
DG
651 }
652
53efb85a 653 memset(&lsm, 0, sizeof(lsm));
cd80958d 654 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d 655
cac3069d
DG
656 lttng_ctl_copy_string(lsm.session.name, session_name,
657 sizeof(lsm.session.name));
cd80958d 658
cac3069d 659 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
38ee087f
DG
660 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
661 goto error;
662 }
663
664 if (!wait) {
665 goto end;
666 }
667
38ee087f
DG
668 /* Check for data availability */
669 do {
6d805429 670 data_ret = lttng_data_pending(session_name);
38ee087f
DG
671 if (data_ret < 0) {
672 /* Return the data available call error. */
673 ret = data_ret;
674 goto error;
675 }
676
677 /*
9ae110e2
JG
678 * Data sleep time before retrying (in usec). Don't sleep if the
679 * call returned value indicates availability.
38ee087f 680 */
6d805429 681 if (data_ret) {
38ee087f 682 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
38ee087f 683 }
6d805429 684 } while (data_ret != 0);
38ee087f 685
38ee087f
DG
686end:
687error:
688 return ret;
689}
690
691/*
692 * Stop tracing and wait for data availability.
693 */
694int lttng_stop_tracing(const char *session_name)
695{
696 return _lttng_stop_tracing(session_name, 1);
697}
698
699/*
700 * Stop tracing but _don't_ wait for data availability.
701 */
702int lttng_stop_tracing_no_wait(const char *session_name)
703{
704 return _lttng_stop_tracing(session_name, 0);
f3ed775e
DG
705}
706
707/*
601d5acf
DG
708 * Add context to a channel.
709 *
710 * If the given channel is NULL, add the contexts to all channels.
711 * The event_name param is ignored.
af87c45a
DG
712 *
713 * Returns the size of the returned payload data or a negative error code.
1df4dedd 714 */
cd80958d 715int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
716 struct lttng_event_context *ctx, const char *event_name,
717 const char *channel_name)
d65106b1 718{
2001793c
JG
719 int ret;
720 size_t len = 0;
721 char *buf = NULL;
cd80958d
DG
722 struct lttcomm_session_msg lsm;
723
9ae110e2 724 /* Safety check. Both are mandatory. */
9d697d3d 725 if (handle == NULL || ctx == NULL) {
2001793c
JG
726 ret = -LTTNG_ERR_INVALID;
727 goto end;
cd80958d
DG
728 }
729
441c16a7 730 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
731 lsm.cmd_type = LTTNG_ADD_CONTEXT;
732
85076754
MD
733 /* If no channel name, send empty string. */
734 if (channel_name == NULL) {
735 lttng_ctl_copy_string(lsm.u.context.channel_name, "",
736 sizeof(lsm.u.context.channel_name));
737 } else {
738 lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name,
739 sizeof(lsm.u.context.channel_name));
740 }
cd80958d 741
cac3069d 742 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cac3069d 743 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
744 sizeof(lsm.session.name));
745
2001793c
JG
746 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
747 size_t provider_len, ctx_len;
748 const char *provider_name = ctx->u.app_ctx.provider_name;
749 const char *ctx_name = ctx->u.app_ctx.ctx_name;
750
751 if (!provider_name || !ctx_name) {
752 ret = -LTTNG_ERR_INVALID;
753 goto end;
754 }
755
756 provider_len = strlen(provider_name);
757 if (provider_len == 0) {
758 ret = -LTTNG_ERR_INVALID;
759 goto end;
760 }
761 lsm.u.context.provider_name_len = provider_len;
762
763 ctx_len = strlen(ctx_name);
764 if (ctx_len == 0) {
765 ret = -LTTNG_ERR_INVALID;
766 goto end;
767 }
768 lsm.u.context.context_name_len = ctx_len;
769
770 len = provider_len + ctx_len;
771 buf = zmalloc(len);
772 if (!buf) {
773 ret = -LTTNG_ERR_NOMEM;
774 goto end;
775 }
776
777 memcpy(buf, provider_name, provider_len);
778 memcpy(buf + provider_len, ctx_name, ctx_len);
779 }
780 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
46f44e2a
JR
781
782 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
783 /*
784 * Don't leak application addresses to the sessiond.
785 * This is only necessary when ctx is for an app ctx otherwise
786 * the values inside the union (type & config) are overwritten.
787 */
788 lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
789 lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
790 }
2001793c 791
795a978d 792 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
2001793c
JG
793end:
794 free(buf);
795 return ret;
d65106b1
DG
796}
797
f3ed775e 798/*
9ae110e2
JG
799 * Enable event(s) for a channel.
800 *
801 * If no event name is specified, all events are enabled.
802 * If no channel name is specified, the default 'channel0' is used.
803 *
804 * Returns size of returned session payload data or a negative error code.
f3ed775e 805 */
cd80958d 806int lttng_enable_event(struct lttng_handle *handle,
38057ed1 807 struct lttng_event *ev, const char *channel_name)
1df4dedd 808{
f8a96544
JI
809 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
810 NULL, 0, NULL);
1df4dedd
DG
811}
812
53a80697 813/*
025faf73 814 * Create or enable an event with a filter expression.
178191b3 815 *
53a80697
MD
816 * Return negative error value on error.
817 * Return size of returned session payload data if OK.
818 */
025faf73 819int lttng_enable_event_with_filter(struct lttng_handle *handle,
178191b3 820 struct lttng_event *event, const char *channel_name,
53a80697
MD
821 const char *filter_expression)
822{
f8a96544
JI
823 return lttng_enable_event_with_exclusions(handle, event, channel_name,
824 filter_expression, 0, NULL);
53a80697
MD
825}
826
347c5ab5 827/*
0e115563 828 * Depending on the event, return a newly allocated agent filter expression or
347c5ab5
DG
829 * NULL if not applicable.
830 *
831 * An event with NO loglevel and the name is * will return NULL.
832 */
0e115563 833static char *set_agent_filter(const char *filter, struct lttng_event *ev)
347c5ab5
DG
834{
835 int err;
0e115563 836 char *agent_filter = NULL;
347c5ab5
DG
837
838 assert(ev);
839
840 /* Don't add filter for the '*' event. */
9f449915 841 if (strcmp(ev->name, "*") != 0) {
347c5ab5 842 if (filter) {
0e115563 843 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
347c5ab5
DG
844 ev->name);
845 } else {
0e115563 846 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
347c5ab5
DG
847 }
848 if (err < 0) {
849 PERROR("asprintf");
6a556f7b 850 goto error;
347c5ab5
DG
851 }
852 }
853
854 /* Add loglevel filtering if any for the JUL domain. */
855 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
856 char *op;
857
858 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
859 op = ">=";
860 } else {
861 op = "==";
862 }
863
0e115563 864 if (filter || agent_filter) {
6a556f7b
JG
865 char *new_filter;
866
fb0edb23 867 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
0e115563 868 agent_filter ? agent_filter : filter, op,
347c5ab5 869 ev->loglevel);
0e115563
DG
870 if (agent_filter) {
871 free(agent_filter);
6a556f7b 872 }
0e115563 873 agent_filter = new_filter;
347c5ab5 874 } else {
0e115563 875 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
347c5ab5
DG
876 ev->loglevel);
877 }
878 if (err < 0) {
879 PERROR("asprintf");
6a556f7b 880 goto error;
347c5ab5
DG
881 }
882 }
883
0e115563 884 return agent_filter;
6a556f7b 885error:
0e115563 886 free(agent_filter);
6a556f7b 887 return NULL;
347c5ab5
DG
888}
889
137b9942 890/*
ec166985 891 * Generate the filter bytecode from a given filter expression string. Put the
137b9942
DG
892 * newly allocated parser context in ctxp and populate the lsm object with the
893 * expression len.
894 *
895 * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
896 */
897static int generate_filter(char *filter_expression,
898 struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
899{
900 int ret;
901 struct filter_parser_ctx *ctx = NULL;
902 FILE *fmem = NULL;
903
904 assert(filter_expression);
905 assert(lsm);
906 assert(ctxp);
907
908 /*
909 * Casting const to non-const, as the underlying function will use it in
910 * read-only mode.
911 */
912 fmem = lttng_fmemopen((void *) filter_expression,
913 strlen(filter_expression), "r");
914 if (!fmem) {
915 fprintf(stderr, "Error opening memory as stream\n");
916 ret = -LTTNG_ERR_FILTER_NOMEM;
917 goto error;
918 }
919 ctx = filter_parser_ctx_alloc(fmem);
920 if (!ctx) {
921 fprintf(stderr, "Error allocating parser\n");
922 ret = -LTTNG_ERR_FILTER_NOMEM;
923 goto filter_alloc_error;
924 }
925 ret = filter_parser_ctx_append_ast(ctx);
926 if (ret) {
927 fprintf(stderr, "Parse error\n");
928 ret = -LTTNG_ERR_FILTER_INVAL;
929 goto parse_error;
930 }
137b9942
DG
931 if (print_xml) {
932 ret = filter_visitor_print_xml(ctx, stdout, 0);
933 if (ret) {
934 fflush(stdout);
935 fprintf(stderr, "XML print error\n");
936 ret = -LTTNG_ERR_FILTER_INVAL;
937 goto parse_error;
938 }
939 }
940
941 dbg_printf("Generating IR... ");
942 fflush(stdout);
943 ret = filter_visitor_ir_generate(ctx);
944 if (ret) {
945 fprintf(stderr, "Generate IR error\n");
946 ret = -LTTNG_ERR_FILTER_INVAL;
947 goto parse_error;
948 }
949 dbg_printf("done\n");
950
951 dbg_printf("Validating IR... ");
952 fflush(stdout);
953 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
954 if (ret) {
955 ret = -LTTNG_ERR_FILTER_INVAL;
956 goto parse_error;
957 }
9f449915
PP
958
959 /* Normalize globbing patterns in the expression. */
960 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
961 if (ret) {
962 ret = -LTTNG_ERR_FILTER_INVAL;
963 goto parse_error;
964 }
965
9ae110e2 966 /* Validate strings used as literals in the expression. */
dcd5daf2
JG
967 ret = filter_visitor_ir_validate_string(ctx);
968 if (ret) {
969 ret = -LTTNG_ERR_FILTER_INVAL;
970 goto parse_error;
971 }
9f449915
PP
972
973 /* Validate globbing patterns in the expression. */
974 ret = filter_visitor_ir_validate_globbing(ctx);
975 if (ret) {
976 ret = -LTTNG_ERR_FILTER_INVAL;
977 goto parse_error;
978 }
979
137b9942
DG
980 dbg_printf("done\n");
981
982 dbg_printf("Generating bytecode... ");
983 fflush(stdout);
984 ret = filter_visitor_bytecode_generate(ctx);
985 if (ret) {
986 fprintf(stderr, "Generate bytecode error\n");
987 ret = -LTTNG_ERR_FILTER_INVAL;
988 goto parse_error;
989 }
990 dbg_printf("done\n");
991 dbg_printf("Size of bytecode generated: %u bytes.\n",
992 bytecode_get_len(&ctx->bytecode->b));
993
994 lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
995 + bytecode_get_len(&ctx->bytecode->b);
996 lsm->u.enable.expression_len = strlen(filter_expression) + 1;
997
998 /* No need to keep the memory stream. */
999 if (fclose(fmem) != 0) {
6f04ed72 1000 PERROR("fclose");
137b9942
DG
1001 }
1002
1003 *ctxp = ctx;
1004 return 0;
1005
1006parse_error:
137b9942
DG
1007 filter_ir_free(ctx);
1008 filter_parser_ctx_free(ctx);
1009filter_alloc_error:
1010 if (fclose(fmem) != 0) {
6f04ed72 1011 PERROR("fclose");
137b9942
DG
1012 }
1013error:
1014 return ret;
1015}
1016
93deb080
JI
1017/*
1018 * Enable event(s) for a channel, possibly with exclusions and a filter.
1019 * If no event name is specified, all events are enabled.
1020 * If no channel name is specified, the default name is used.
1021 * If filter expression is not NULL, the filter is set for the event.
1022 * If exclusion count is not zero, the exclusions are set for the event.
1023 * Returns size of returned session payload data or a negative error code.
1024 */
1025int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1026 struct lttng_event *ev, const char *channel_name,
e9efbcd3 1027 const char *original_filter_expression,
93deb080
JI
1028 int exclusion_count, char **exclusion_list)
1029{
1030 struct lttcomm_session_msg lsm;
15e37663
JG
1031 struct lttng_dynamic_buffer send_buffer;
1032 int ret = 0, i, fd_to_send = -1;
1033 bool send_fd = false;
137b9942 1034 unsigned int free_filter_expression = 0;
93deb080 1035 struct filter_parser_ctx *ctx = NULL;
ec005ec6
FD
1036
1037 memset(&send_buffer, 0, sizeof(send_buffer));
1038
e9efbcd3
JG
1039 /*
1040 * Cast as non-const since we may replace the filter expression
1041 * by a dynamically allocated string. Otherwise, the original
1042 * string is not modified.
1043 */
1044 char *filter_expression = (char *) original_filter_expression;
93deb080
JI
1045
1046 if (handle == NULL || ev == NULL) {
137b9942
DG
1047 ret = -LTTNG_ERR_INVALID;
1048 goto error;
93deb080
JI
1049 }
1050
9ae110e2
JG
1051 /*
1052 * Empty filter string will always be rejected by the parser
93deb080
JI
1053 * anyway, so treat this corner-case early to eliminate
1054 * lttng_fmemopen error for 0-byte allocation.
1055 */
1056 if (filter_expression && filter_expression[0] == '\0') {
137b9942
DG
1057 ret = -LTTNG_ERR_INVALID;
1058 goto error;
93deb080
JI
1059 }
1060
1061 memset(&lsm, 0, sizeof(lsm));
1062
1063 /* If no channel name, send empty string. */
1064 if (channel_name == NULL) {
1065 lttng_ctl_copy_string(lsm.u.enable.channel_name, "",
1066 sizeof(lsm.u.enable.channel_name));
1067 } else {
1068 lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name,
1069 sizeof(lsm.u.enable.channel_name));
1070 }
1071
18a720cd
MD
1072 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1073 if (ev->name[0] == '\0') {
1074 /* Enable all events */
1075 lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name));
6565421f 1076 }
93deb080 1077
6565421f 1078 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
6e911cad 1079 /* FIXME: copying non-packed struct to packed struct. */
93deb080
JI
1080 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1081
1082 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1083 sizeof(lsm.session.name));
1084 lsm.u.enable.exclusion_count = exclusion_count;
1085 lsm.u.enable.bytecode_len = 0;
1086
93deb080
JI
1087 /*
1088 * We have either a filter or some exclusions, so we need to set up
9ae110e2 1089 * a variable-length memory block from where to send the data.
93deb080 1090 */
15e37663 1091 lttng_dynamic_buffer_init(&send_buffer);
93deb080 1092
9ae110e2 1093 /* Parse filter expression. */
5cdb6027 1094 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1095 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1096 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
5cdb6027 1097 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1098 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1099 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1100 char *agent_filter;
64226865 1101
347c5ab5 1102 /* Setup JUL filter if needed. */
0e115563
DG
1103 agent_filter = set_agent_filter(filter_expression, ev);
1104 if (!agent_filter) {
137b9942 1105 if (!filter_expression) {
9ae110e2
JG
1106 /*
1107 * No JUL and no filter, just skip
1108 * everything below.
1109 */
137b9942
DG
1110 goto ask_sessiond;
1111 }
64226865
DG
1112 } else {
1113 /*
9ae110e2
JG
1114 * With an agent filter, the original filter has
1115 * been added to it thus replace the filter
1116 * expression.
64226865 1117 */
0e115563 1118 filter_expression = agent_filter;
e9efbcd3 1119 free_filter_expression = 1;
9b21e6d5 1120 }
9b21e6d5 1121 }
93deb080 1122
137b9942 1123 ret = generate_filter(filter_expression, &lsm, &ctx);
93deb080 1124 if (ret) {
137b9942 1125 goto filter_error;
93deb080 1126 }
6b453b5e
JG
1127 }
1128
15e37663
JG
1129 ret = lttng_dynamic_buffer_set_capacity(&send_buffer,
1130 lsm.u.enable.bytecode_len
137b9942
DG
1131 + lsm.u.enable.expression_len
1132 + LTTNG_SYMBOL_NAME_LEN * exclusion_count);
15e37663 1133 if (ret) {
6b453b5e 1134 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
7ca1dc6f 1135 goto mem_error;
6b453b5e 1136 }
137b9942 1137
9ae110e2 1138 /* Put exclusion names first in the data. */
15e37663
JG
1139 for (i = 0; i < exclusion_count; i++) {
1140 size_t exclusion_len;
1141
1142 exclusion_len = lttng_strnlen(*(exclusion_list + i),
1143 LTTNG_SYMBOL_NAME_LEN);
1144 if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) {
1145 /* Exclusion is not NULL-terminated. */
1146 ret = -LTTNG_ERR_INVALID;
1147 goto mem_error;
1148 }
1149
1150 ret = lttng_dynamic_buffer_append(&send_buffer,
1151 *(exclusion_list + i),
1152 LTTNG_SYMBOL_NAME_LEN);
1153 if (ret) {
1154 goto mem_error;
1155 }
6b453b5e 1156 }
15e37663 1157
9ae110e2 1158 /* Add filter expression next. */
15e37663
JG
1159 if (filter_expression) {
1160 ret = lttng_dynamic_buffer_append(&send_buffer,
1161 filter_expression, lsm.u.enable.expression_len);
1162 if (ret) {
1163 goto mem_error;
1164 }
6b453b5e 1165 }
9ae110e2 1166 /* Add filter bytecode next. */
137b9942 1167 if (ctx && lsm.u.enable.bytecode_len != 0) {
15e37663
JG
1168 ret = lttng_dynamic_buffer_append(&send_buffer,
1169 &ctx->bytecode->b, lsm.u.enable.bytecode_len);
1170 if (ret) {
1171 goto mem_error;
1172 }
1173 }
1174 if (ev->extended.ptr) {
1175 struct lttng_event_extended *ev_ext =
1176 (struct lttng_event_extended *) ev->extended.ptr;
1177
1178 if (ev_ext->probe_location) {
1179 /*
1180 * lttng_userspace_probe_location_serialize returns the
1181 * number of bytes that was appended to the buffer.
1182 */
1183 ret = lttng_userspace_probe_location_serialize(
1184 ev_ext->probe_location, &send_buffer,
1185 &fd_to_send);
56f0bc67 1186 if (ret < 0) {
15e37663
JG
1187 goto mem_error;
1188 }
1189
1190 send_fd = fd_to_send >= 0;
1191 /*
1192 * Set the size of the userspace probe location element
1193 * of the buffer so that the receiving side knows where
1194 * to split it.
1195 */
1196 lsm.u.enable.userspace_probe_location_len = ret;
1197 }
93deb080
JI
1198 }
1199
15e37663
JG
1200 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1201 send_fd ? &fd_to_send : NULL,
1202 send_fd ? 1 : 0,
1203 send_buffer.size ? send_buffer.data : NULL,
1204 send_buffer.size, NULL, NULL, 0);
93deb080 1205
7ca1dc6f
DG
1206mem_error:
1207 if (filter_expression && ctx) {
93deb080
JI
1208 filter_bytecode_free(ctx);
1209 filter_ir_free(ctx);
1210 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1211 }
1212filter_error:
1213 if (free_filter_expression) {
1214 /*
9ae110e2
JG
1215 * The filter expression has been replaced and must be freed as
1216 * it is not the original filter expression received as a
1217 * parameter.
7ca1dc6f
DG
1218 */
1219 free(filter_expression);
93deb080 1220 }
137b9942
DG
1221error:
1222 /*
9ae110e2
JG
1223 * Return directly to the caller and don't ask the sessiond since
1224 * something went wrong in the parsing of data above.
137b9942 1225 */
15e37663 1226 lttng_dynamic_buffer_reset(&send_buffer);
93deb080 1227 return ret;
3e1c9ff7
DG
1228
1229ask_sessiond:
1230 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1231 return ret;
93deb080
JI
1232}
1233
6e911cad
MD
1234int lttng_disable_event_ext(struct lttng_handle *handle,
1235 struct lttng_event *ev, const char *channel_name,
1236 const char *original_filter_expression)
1df4dedd 1237{
cd80958d 1238 struct lttcomm_session_msg lsm;
6e911cad
MD
1239 char *varlen_data;
1240 int ret = 0;
1241 unsigned int free_filter_expression = 0;
1242 struct filter_parser_ctx *ctx = NULL;
1243 /*
1244 * Cast as non-const since we may replace the filter expression
1245 * by a dynamically allocated string. Otherwise, the original
1246 * string is not modified.
1247 */
1248 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1249
6e911cad
MD
1250 if (handle == NULL || ev == NULL) {
1251 ret = -LTTNG_ERR_INVALID;
1252 goto error;
1253 }
1254
9ae110e2
JG
1255 /*
1256 * Empty filter string will always be rejected by the parser
6e911cad
MD
1257 * anyway, so treat this corner-case early to eliminate
1258 * lttng_fmemopen error for 0-byte allocation.
1259 */
1260 if (filter_expression && filter_expression[0] == '\0') {
1261 ret = -LTTNG_ERR_INVALID;
1262 goto error;
cd80958d
DG
1263 }
1264
441c16a7
MD
1265 memset(&lsm, 0, sizeof(lsm));
1266
85076754
MD
1267 /* If no channel name, send empty string. */
1268 if (channel_name == NULL) {
1269 lttng_ctl_copy_string(lsm.u.disable.channel_name, "",
cd80958d 1270 sizeof(lsm.u.disable.channel_name));
f3ed775e 1271 } else {
85076754 1272 lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name,
cd80958d 1273 sizeof(lsm.u.disable.channel_name));
eb354453
DG
1274 }
1275
18a720cd 1276 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1277
6e911cad
MD
1278 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1279 /* FIXME: copying non-packed struct to packed struct. */
1280 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1281
cac3069d 1282 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1283 sizeof(lsm.session.name));
6e911cad 1284 lsm.u.disable.bytecode_len = 0;
cd80958d 1285
6e911cad
MD
1286 /*
1287 * For the JUL domain, a filter is enforced except for the
1288 * disable all event. This is done to avoid having the event in
1289 * all sessions thus filtering by logger name.
1290 */
1291 if (filter_expression == NULL &&
1292 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1293 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1294 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1295 goto ask_sessiond;
1296 }
1297
1298 /*
1299 * We have a filter, so we need to set up a variable-length
1300 * memory block from where to send the data.
1301 */
1302
1303 /* Parse filter expression */
1304 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1305 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1306 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1307 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1308 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1309 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1310 char *agent_filter;
6e911cad
MD
1311
1312 /* Setup JUL filter if needed. */
0e115563
DG
1313 agent_filter = set_agent_filter(filter_expression, ev);
1314 if (!agent_filter) {
6e911cad 1315 if (!filter_expression) {
9ae110e2
JG
1316 /*
1317 * No JUL and no filter, just skip
1318 * everything below.
1319 */
6e911cad
MD
1320 goto ask_sessiond;
1321 }
1322 } else {
1323 /*
9ae110e2
JG
1324 * With a JUL filter, the original filter has
1325 * been added to it thus replace the filter
1326 * expression.
6e911cad 1327 */
0e115563 1328 filter_expression = agent_filter;
6e911cad
MD
1329 free_filter_expression = 1;
1330 }
1331 }
1332
1333 ret = generate_filter(filter_expression, &lsm, &ctx);
1334 if (ret) {
1335 goto filter_error;
1336 }
1337 }
1338
1339 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1340 + lsm.u.disable.expression_len);
1341 if (!varlen_data) {
1342 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1343 goto mem_error;
1344 }
1345
9ae110e2 1346 /* Add filter expression. */
6e911cad
MD
1347 if (lsm.u.disable.expression_len != 0) {
1348 memcpy(varlen_data,
1349 filter_expression,
1350 lsm.u.disable.expression_len);
1351 }
9ae110e2 1352 /* Add filter bytecode next. */
6e911cad
MD
1353 if (ctx && lsm.u.disable.bytecode_len != 0) {
1354 memcpy(varlen_data
1355 + lsm.u.disable.expression_len,
1356 &ctx->bytecode->b,
1357 lsm.u.disable.bytecode_len);
1358 }
1359
795a978d 1360 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
6e911cad
MD
1361 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1362 free(varlen_data);
1363
1364mem_error:
1365 if (filter_expression && ctx) {
1366 filter_bytecode_free(ctx);
1367 filter_ir_free(ctx);
1368 filter_parser_ctx_free(ctx);
1369 }
1370filter_error:
1371 if (free_filter_expression) {
1372 /*
9ae110e2
JG
1373 * The filter expression has been replaced and must be freed as
1374 * it is not the original filter expression received as a
1375 * parameter.
6e911cad
MD
1376 */
1377 free(filter_expression);
1378 }
1379error:
1380 /*
9ae110e2
JG
1381 * Return directly to the caller and don't ask the sessiond since
1382 * something went wrong in the parsing of data above.
6e911cad
MD
1383 */
1384 return ret;
1385
1386ask_sessiond:
1387 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1388 return ret;
1389}
1390
1391/*
9ae110e2
JG
1392 * Disable event(s) of a channel and domain.
1393 * If no event name is specified, all events are disabled.
1394 * If no channel name is specified, the default 'channel0' is used.
1395 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1396 */
1397int lttng_disable_event(struct lttng_handle *handle, const char *name,
1398 const char *channel_name)
1399{
1400 struct lttng_event ev;
1401
1402 memset(&ev, 0, sizeof(ev));
9b7431cf 1403 ev.loglevel = -1;
6e911cad
MD
1404 ev.type = LTTNG_EVENT_ALL;
1405 lttng_ctl_copy_string(ev.name, name, sizeof(ev.name));
1406 return lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1df4dedd
DG
1407}
1408
cf0bcb51
JG
1409struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1410{
1411 struct lttng_channel *channel = NULL;
1412 struct lttng_channel_extended *extended = NULL;
1413
1414 if (!domain) {
1415 goto error;
1416 }
1417
1418 /* Validate domain. */
1419 switch (domain->type) {
1420 case LTTNG_DOMAIN_UST:
1421 switch (domain->buf_type) {
1422 case LTTNG_BUFFER_PER_UID:
1423 case LTTNG_BUFFER_PER_PID:
1424 break;
1425 default:
1426 goto error;
1427 }
1428 break;
1429 case LTTNG_DOMAIN_KERNEL:
1430 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1431 goto error;
1432 }
1433 break;
1434 default:
1435 goto error;
1436 }
1437
1438 channel = zmalloc(sizeof(*channel));
1439 if (!channel) {
1440 goto error;
1441 }
1442
1443 extended = zmalloc(sizeof(*extended));
1444 if (!extended) {
1445 goto error;
1446 }
1447
1448 channel->attr.extended.ptr = extended;
1449
1450 lttng_channel_set_default_attr(domain, &channel->attr);
1451 return channel;
1452error:
1453 free(channel);
1454 free(extended);
1455 return NULL;
1456}
1457
1458void lttng_channel_destroy(struct lttng_channel *channel)
1459{
1460 if (!channel) {
1461 return;
1462 }
1463
1464 if (channel->attr.extended.ptr) {
1465 free(channel->attr.extended.ptr);
1466 }
1467 free(channel);
1468}
1469
1df4dedd 1470/*
9ae110e2
JG
1471 * Enable channel per domain
1472 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1473 */
cd80958d 1474int lttng_enable_channel(struct lttng_handle *handle,
cf0bcb51 1475 struct lttng_channel *in_chan)
a5c5a2bd 1476{
cd80958d
DG
1477 struct lttcomm_session_msg lsm;
1478
9ae110e2 1479 /* NULL arguments are forbidden. No default values. */
cf0bcb51 1480 if (handle == NULL || in_chan == NULL) {
2f70b271 1481 return -LTTNG_ERR_INVALID;
cd80958d
DG
1482 }
1483
441c16a7 1484 memset(&lsm, 0, sizeof(lsm));
cf0bcb51
JG
1485 memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
1486 lsm.u.channel.chan.attr.extended.ptr = NULL;
441c16a7 1487
cf0bcb51
JG
1488 if (!in_chan->attr.extended.ptr) {
1489 struct lttng_channel *channel;
1490 struct lttng_channel_extended *extended;
7d29a247 1491
cf0bcb51
JG
1492 channel = lttng_channel_create(&handle->domain);
1493 if (!channel) {
1494 return -LTTNG_ERR_NOMEM;
1495 }
1496
1497 /*
1498 * Create a new channel in order to use default extended
1499 * attribute values.
1500 */
1501 extended = (struct lttng_channel_extended *)
1502 channel->attr.extended.ptr;
1503 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1504 lttng_channel_destroy(channel);
1505 } else {
1506 struct lttng_channel_extended *extended;
1507
1508 extended = (struct lttng_channel_extended *)
1509 in_chan->attr.extended.ptr;
1510 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1511 }
cd80958d 1512
cf0bcb51 1513 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
cac3069d 1514 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 1515
cac3069d 1516 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1517 sizeof(lsm.session.name));
1518
cac3069d 1519 return lttng_ctl_ask_sessiond(&lsm, NULL);
8c0faa1d 1520}
1df4dedd 1521
2ef84c95 1522/*
9ae110e2
JG
1523 * All tracing will be stopped for registered events of the channel.
1524 * Returns size of returned session payload data or a negative error code.
2ef84c95 1525 */
cd80958d 1526int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1527{
cd80958d
DG
1528 struct lttcomm_session_msg lsm;
1529
9ae110e2 1530 /* Safety check. Both are mandatory. */
9d697d3d 1531 if (handle == NULL || name == NULL) {
2f70b271 1532 return -LTTNG_ERR_INVALID;
cd80958d
DG
1533 }
1534
441c16a7
MD
1535 memset(&lsm, 0, sizeof(lsm));
1536
cd80958d 1537 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1538
cac3069d 1539 lttng_ctl_copy_string(lsm.u.disable.channel_name, name,
9d697d3d
DG
1540 sizeof(lsm.u.disable.channel_name));
1541
cac3069d 1542 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
cd80958d 1543
cac3069d 1544 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d
DG
1545 sizeof(lsm.session.name));
1546
cac3069d 1547 return lttng_ctl_ask_sessiond(&lsm, NULL);
ca95a216
DG
1548}
1549
ccf10263 1550/*
9ae110e2
JG
1551 * Add PID to session tracker.
1552 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1553 */
1554int lttng_track_pid(struct lttng_handle *handle, int pid)
1555{
1556 struct lttcomm_session_msg lsm;
1557
9ae110e2 1558 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1559 if (handle == NULL) {
1560 return -LTTNG_ERR_INVALID;
1561 }
1562
1563 memset(&lsm, 0, sizeof(lsm));
1564
1565 lsm.cmd_type = LTTNG_TRACK_PID;
1566 lsm.u.pid_tracker.pid = pid;
1567
1568 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1569
1570 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1571 sizeof(lsm.session.name));
1572
1573 return lttng_ctl_ask_sessiond(&lsm, NULL);
1574}
1575
1576/*
9ae110e2
JG
1577 * Remove PID from session tracker.
1578 * Return 0 on success else a negative LTTng error code.
ccf10263
MD
1579 */
1580int lttng_untrack_pid(struct lttng_handle *handle, int pid)
1581{
1582 struct lttcomm_session_msg lsm;
1583
9ae110e2 1584 /* NULL arguments are forbidden. No default values. */
ccf10263
MD
1585 if (handle == NULL) {
1586 return -LTTNG_ERR_INVALID;
1587 }
1588
1589 memset(&lsm, 0, sizeof(lsm));
1590
1591 lsm.cmd_type = LTTNG_UNTRACK_PID;
1592 lsm.u.pid_tracker.pid = pid;
1593
1594 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
1595
1596 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
1597 sizeof(lsm.session.name));
1598
1599 return lttng_ctl_ask_sessiond(&lsm, NULL);
1600}
1601
fac6795d 1602/*
9ae110e2
JG
1603 * Lists all available tracepoints of domain.
1604 * Sets the contents of the events array.
1605 * Returns the number of lttng_event entries in events;
1606 * on error, returns a negative value.
fac6795d 1607 */
cd80958d 1608int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1609 struct lttng_event **events)
fac6795d 1610{
052da939 1611 int ret;
cd80958d
DG
1612 struct lttcomm_session_msg lsm;
1613
9d697d3d 1614 if (handle == NULL) {
2f70b271 1615 return -LTTNG_ERR_INVALID;
cd80958d 1616 }
fac6795d 1617
53efb85a 1618 memset(&lsm, 0, sizeof(lsm));
cd80958d 1619 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
cac3069d 1620 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1621
cac3069d 1622 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1623 if (ret < 0) {
1624 return ret;
eb354453 1625 }
fac6795d 1626
9f19cc17 1627 return ret / sizeof(struct lttng_event);
fac6795d
DG
1628}
1629
f37d259d 1630/*
9ae110e2
JG
1631 * Lists all available tracepoint fields of domain.
1632 * Sets the contents of the event field array.
1633 * Returns the number of lttng_event_field entries in events;
1634 * on error, returns a negative value.
f37d259d
MD
1635 */
1636int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1637 struct lttng_event_field **fields)
1638{
1639 int ret;
1640 struct lttcomm_session_msg lsm;
1641
1642 if (handle == NULL) {
2f70b271 1643 return -LTTNG_ERR_INVALID;
f37d259d
MD
1644 }
1645
53efb85a 1646 memset(&lsm, 0, sizeof(lsm));
f37d259d 1647 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
cac3069d 1648 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
f37d259d 1649
cac3069d 1650 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1651 if (ret < 0) {
1652 return ret;
1653 }
1654
1655 return ret / sizeof(struct lttng_event_field);
1656}
1657
834978fd 1658/*
9ae110e2
JG
1659 * Lists all available kernel system calls. Allocates and sets the contents of
1660 * the events array.
834978fd 1661 *
9ae110e2
JG
1662 * Returns the number of lttng_event entries in events; on error, returns a
1663 * negative value.
834978fd
DG
1664 */
1665int lttng_list_syscalls(struct lttng_event **events)
1666{
1667 int ret;
1668 struct lttcomm_session_msg lsm;
1669
1670 if (!events) {
1671 return -LTTNG_ERR_INVALID;
1672 }
1673
1674 memset(&lsm, 0, sizeof(lsm));
1675 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1676 /* Force kernel domain for system calls. */
1677 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1678
1679 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1680 if (ret < 0) {
1681 return ret;
1682 }
1683
1684 return ret / sizeof(struct lttng_event);
1685}
1686
1657e9bb 1687/*
9ae110e2
JG
1688 * Returns a human readable string describing
1689 * the error code (a negative value).
1657e9bb 1690 */
9a745bc7 1691const char *lttng_strerror(int code)
1657e9bb 1692{
f73fabfd 1693 return error_get_str(code);
1657e9bb
DG
1694}
1695
aaf97519 1696/*
a4b92340
DG
1697 * Create a brand new session using name and url for destination.
1698 *
f73fabfd 1699 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1700 */
a4b92340 1701int lttng_create_session(const char *name, const char *url)
00e2e675 1702{
3dd05a85 1703 int ret;
a4b92340 1704 ssize_t size;
00e2e675 1705 struct lttcomm_session_msg lsm;
a4b92340 1706 struct lttng_uri *uris = NULL;
00e2e675 1707
a4b92340 1708 if (name == NULL) {
2f70b271 1709 return -LTTNG_ERR_INVALID;
00e2e675
DG
1710 }
1711
a4b92340 1712 memset(&lsm, 0, sizeof(lsm));
00e2e675 1713
a4b92340 1714 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 1715 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1716
1717 /* There should never be a data URL */
bc894455 1718 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1719 if (size < 0) {
2f70b271 1720 return -LTTNG_ERR_INVALID;
00e2e675
DG
1721 }
1722
a4b92340
DG
1723 lsm.u.uri.size = size;
1724
795a978d 1725 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 1726 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
1727
1728 free(uris);
1729 return ret;
00e2e675
DG
1730}
1731
8028d920 1732/*
9ae110e2
JG
1733 * Destroy session using name.
1734 * Returns size of returned session payload data or a negative error code.
8028d920 1735 */
8fd2c92c 1736static
e20ee7c2 1737int _lttng_destroy_session(const char *session_name)
8028d920 1738{
cd80958d
DG
1739 struct lttcomm_session_msg lsm;
1740
843f5df9 1741 if (session_name == NULL) {
2f70b271 1742 return -LTTNG_ERR_INVALID;
cd80958d
DG
1743 }
1744
53efb85a 1745 memset(&lsm, 0, sizeof(lsm));
cd80958d 1746 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9 1747
cac3069d
DG
1748 lttng_ctl_copy_string(lsm.session.name, session_name,
1749 sizeof(lsm.session.name));
cd80958d 1750
cac3069d 1751 return lttng_ctl_ask_sessiond(&lsm, NULL);
aaf97519
DG
1752}
1753
e20ee7c2
JD
1754/*
1755 * Stop the session and wait for the data before destroying it
1756 */
1757int lttng_destroy_session(const char *session_name)
1758{
1759 int ret;
1760
1761 /*
1762 * Stop the tracing and wait for the data.
1763 */
1764 ret = _lttng_stop_tracing(session_name, 1);
1765 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1766 goto end;
1767 }
1768
1769 ret = _lttng_destroy_session(session_name);
1770end:
1771 return ret;
1772}
1773
1774/*
1775 * Destroy the session without waiting for the data.
1776 */
1777int lttng_destroy_session_no_wait(const char *session_name)
1778{
1779 int ret;
1780
1781 /*
1782 * Stop the tracing without waiting for the data.
1783 * The session might already have been stopped, so just
1784 * skip this error.
1785 */
1786 ret = _lttng_stop_tracing(session_name, 0);
1787 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
1788 goto end;
1789 }
1790
1791 ret = _lttng_destroy_session(session_name);
1792end:
1793 return ret;
1794}
1795
57167058 1796/*
9ae110e2
JG
1797 * Ask the session daemon for all available sessions.
1798 * Sets the contents of the sessions array.
1799 * Returns the number of lttng_session entries in sessions;
1800 * on error, returns a negative value.
57167058 1801 */
ca95a216 1802int lttng_list_sessions(struct lttng_session **sessions)
57167058 1803{
ca95a216 1804 int ret;
cd80958d 1805 struct lttcomm_session_msg lsm;
57167058 1806
53efb85a 1807 memset(&lsm, 0, sizeof(lsm));
cd80958d 1808 lsm.cmd_type = LTTNG_LIST_SESSIONS;
cac3069d 1809 ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions);
57167058 1810 if (ret < 0) {
ca95a216 1811 return ret;
57167058
DG
1812 }
1813
ca95a216 1814 return ret / sizeof(struct lttng_session);
57167058
DG
1815}
1816
d7ba1388
MD
1817int lttng_set_session_shm_path(const char *session_name,
1818 const char *shm_path)
1819{
1820 struct lttcomm_session_msg lsm;
1821
1822 if (session_name == NULL) {
1823 return -LTTNG_ERR_INVALID;
1824 }
1825
1826 memset(&lsm, 0, sizeof(lsm));
1827 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
1828
1829 lttng_ctl_copy_string(lsm.session.name, session_name,
1830 sizeof(lsm.session.name));
1831 lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path,
1832 sizeof(lsm.u.set_shm_path.shm_path));
1833
1834 return lttng_ctl_ask_sessiond(&lsm, NULL);
1835}
1836
9f19cc17 1837/*
9ae110e2
JG
1838 * Ask the session daemon for all available domains of a session.
1839 * Sets the contents of the domains array.
1840 * Returns the number of lttng_domain entries in domains;
1841 * on error, returns a negative value.
9f19cc17 1842 */
330be774 1843int lttng_list_domains(const char *session_name,
cd80958d 1844 struct lttng_domain **domains)
9f19cc17
DG
1845{
1846 int ret;
cd80958d
DG
1847 struct lttcomm_session_msg lsm;
1848
330be774 1849 if (session_name == NULL) {
2f70b271 1850 return -LTTNG_ERR_INVALID;
cd80958d 1851 }
9f19cc17 1852
53efb85a 1853 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
1854 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1855
cac3069d
DG
1856 lttng_ctl_copy_string(lsm.session.name, session_name,
1857 sizeof(lsm.session.name));
cd80958d 1858
cac3069d 1859 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1860 if (ret < 0) {
1861 return ret;
1862 }
1863
1864 return ret / sizeof(struct lttng_domain);
1865}
1866
1867/*
9ae110e2
JG
1868 * Ask the session daemon for all available channels of a session.
1869 * Sets the contents of the channels array.
1870 * Returns the number of lttng_channel entries in channels;
1871 * on error, returns a negative value.
9f19cc17 1872 */
cd80958d
DG
1873int lttng_list_channels(struct lttng_handle *handle,
1874 struct lttng_channel **channels)
9f19cc17
DG
1875{
1876 int ret;
53e367f9
JG
1877 size_t channel_count, i;
1878 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51 1879 sizeof(struct lttng_channel_extended);
cd80958d 1880 struct lttcomm_session_msg lsm;
53e367f9 1881 void *extended_at;
cd80958d 1882
9d697d3d 1883 if (handle == NULL) {
53e367f9
JG
1884 ret = -LTTNG_ERR_INVALID;
1885 goto end;
cd80958d
DG
1886 }
1887
53efb85a 1888 memset(&lsm, 0, sizeof(lsm));
cd80958d 1889 lsm.cmd_type = LTTNG_LIST_CHANNELS;
cac3069d 1890 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1891 sizeof(lsm.session.name));
9f19cc17 1892
cac3069d 1893 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1894
cac3069d 1895 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17 1896 if (ret < 0) {
53e367f9
JG
1897 goto end;
1898 }
1899
1900 if (ret % channel_size) {
1901 ret = -LTTNG_ERR_UNK;
1902 free(*channels);
1903 *channels = NULL;
1904 goto end;
9f19cc17 1905 }
53e367f9 1906 channel_count = (size_t) ret / channel_size;
9f19cc17 1907
53e367f9
JG
1908 /* Set extended info pointers */
1909 extended_at = ((void *) *channels) +
1910 channel_count * sizeof(struct lttng_channel);
1911 for (i = 0; i < channel_count; i++) {
1912 struct lttng_channel *chan = &(*channels)[i];
1913
1914 chan->attr.extended.ptr = extended_at;
cf0bcb51 1915 extended_at += sizeof(struct lttng_channel_extended);
53e367f9
JG
1916 }
1917
1918 ret = (int) channel_count;
1919end:
1920 return ret;
9f19cc17
DG
1921}
1922
1923/*
9ae110e2
JG
1924 * Ask the session daemon for all available events of a session channel.
1925 * Sets the contents of the events array.
1926 * Returns the number of lttng_event entries in events;
1927 * on error, returns a negative value.
9f19cc17 1928 */
cd80958d
DG
1929int lttng_list_events(struct lttng_handle *handle,
1930 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1931{
1932 int ret;
cd80958d 1933 struct lttcomm_session_msg lsm;
b4e3ceb9
PP
1934 struct lttcomm_event_command_header *cmd_header = NULL;
1935 size_t cmd_header_len;
1936 uint32_t nb_events, i;
de453daa 1937 void *comm_ext_at;
de453daa 1938 char *reception_buffer = NULL;
56f0bc67 1939 struct lttng_dynamic_buffer listing;
de453daa 1940 size_t storage_req;
9f19cc17 1941
9d697d3d
DG
1942 /* Safety check. An handle and channel name are mandatory */
1943 if (handle == NULL || channel_name == NULL) {
2f70b271 1944 return -LTTNG_ERR_INVALID;
cd80958d
DG
1945 }
1946
53efb85a 1947 memset(&lsm, 0, sizeof(lsm));
cd80958d 1948 lsm.cmd_type = LTTNG_LIST_EVENTS;
cac3069d 1949 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
cd80958d 1950 sizeof(lsm.session.name));
cac3069d 1951 lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name,
cd80958d 1952 sizeof(lsm.u.list.channel_name));
cac3069d 1953 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1954
a04d53fc 1955 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
de453daa
JG
1956 (void **) &reception_buffer, (void **) &cmd_header,
1957 &cmd_header_len);
9f19cc17 1958 if (ret < 0) {
de453daa 1959 goto end;
9f19cc17
DG
1960 }
1961
d9f484bc
JG
1962 if (!cmd_header) {
1963 ret = -LTTNG_ERR_UNK;
1964 goto end;
1965 }
1966
b4e3ceb9
PP
1967 /* Set number of events and free command header */
1968 nb_events = cmd_header->nb_events;
1969 if (nb_events > INT_MAX) {
1970 ret = -EOVERFLOW;
de453daa 1971 goto end;
b4e3ceb9 1972 }
b4e3ceb9
PP
1973 free(cmd_header);
1974 cmd_header = NULL;
1975
de453daa
JG
1976 /*
1977 * The buffer that is returned must contain a "flat" version of
1978 * the events that are returned. In other words, all pointers
1979 * within an lttng_event must point to a location within the returned
56f0bc67
JG
1980 * buffer so that the user may free everything by simply calling free()
1981 * on the returned buffer. This is needed in order to maintain API
de453daa
JG
1982 * compatibility.
1983 *
56f0bc67
JG
1984 * A first pass is performed to compute the size of the buffer that
1985 * must be allocated. A second pass is then performed to setup
de453daa
JG
1986 * the returned events so that their members always point within the
1987 * buffer.
1988 *
1989 * The layout of the returned buffer is as follows:
1990 * - struct lttng_event[nb_events],
1991 * - nb_events times the following:
1992 * - struct lttng_event_extended,
1993 * - flattened version of userspace_probe_location
1994 * - filter_expression
1995 * - exclusions
1996 * - padding to align to 64-bits
1997 */
1998 comm_ext_at = reception_buffer +
1999 (nb_events * sizeof(struct lttng_event));
2000 storage_req = nb_events * sizeof(struct lttng_event);
b4e3ceb9
PP
2001
2002 for (i = 0; i < nb_events; i++) {
de453daa
JG
2003 struct lttcomm_event_extended_header *ext_comm =
2004 (struct lttcomm_event_extended_header *) comm_ext_at;
56f0bc67 2005 int probe_storage_req = 0;
b4e3ceb9 2006
de453daa
JG
2007 comm_ext_at += sizeof(*ext_comm);
2008 comm_ext_at += ext_comm->filter_len;
2009 comm_ext_at +=
2010 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2011
56f0bc67
JG
2012 if (ext_comm->userspace_probe_location_len) {
2013 struct lttng_userspace_probe_location *probe_location = NULL;
2014 struct lttng_buffer_view probe_location_view;
2015
2016 probe_location_view = lttng_buffer_view_init(
2017 comm_ext_at, 0,
2018 ext_comm->userspace_probe_location_len);
2019
2020 /*
2021 * Create a temporary userspace probe location to
2022 * determine the size needed by a "flattened" version
2023 * of that same probe location.
2024 */
2025 ret = lttng_userspace_probe_location_create_from_buffer(
2026 &probe_location_view, &probe_location);
2027 if (ret < 0) {
2028 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2029 goto end;
2030 }
2031
2032 ret = lttng_userspace_probe_location_flatten(
2033 probe_location, NULL);
2034 lttng_userspace_probe_location_destroy(probe_location);
2035 if (ret < 0) {
2036 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2037 goto end;
2038 }
2039
2040 probe_storage_req = ret;
2041 comm_ext_at += ext_comm->userspace_probe_location_len;
56f0bc67
JG
2042 }
2043
de453daa 2044 storage_req += sizeof(struct lttng_event_extended);
de453daa
JG
2045 storage_req += ext_comm->filter_len;
2046 storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
56f0bc67
JG
2047 /* Padding to ensure the flat probe is aligned. */
2048 storage_req = ALIGN_TO(storage_req, sizeof(uint64_t));
2049 storage_req += probe_storage_req;
b4e3ceb9
PP
2050 }
2051
56f0bc67
JG
2052 lttng_dynamic_buffer_init(&listing);
2053 /*
2054 * We must ensure that "listing" is never resized so as to preserve
2055 * the validity of the flattened objects.
2056 */
2057 ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req);
2058 if (ret) {
2059 ret = -LTTNG_ERR_NOMEM;
de453daa
JG
2060 goto end;
2061 }
56f0bc67
JG
2062
2063 ret = lttng_dynamic_buffer_append(&listing, reception_buffer,
2064 nb_events * sizeof(struct lttng_event));
2065 if (ret) {
2066 ret = -LTTNG_ERR_NOMEM;
2067 goto free_dynamic_buffer;
2068 }
de453daa
JG
2069
2070 comm_ext_at = reception_buffer +
56f0bc67 2071 (nb_events * sizeof(struct lttng_event));
de453daa
JG
2072 for (i = 0; i < nb_events; i++) {
2073 struct lttng_event *event = (struct lttng_event *)
56f0bc67 2074 (listing.data + (sizeof(struct lttng_event) * i));
de453daa
JG
2075 struct lttcomm_event_extended_header *ext_comm =
2076 (struct lttcomm_event_extended_header *) comm_ext_at;
2077 struct lttng_event_extended *event_extended =
56f0bc67
JG
2078 (struct lttng_event_extended *)
2079 (listing.data + listing.size);
de453daa 2080
56f0bc67
JG
2081 /* Insert struct lttng_event_extended. */
2082 ret = lttng_dynamic_buffer_set_size(&listing,
2083 listing.size + sizeof(*event_extended));
2084 if (ret) {
2085 ret = -LTTNG_ERR_NOMEM;
2086 goto free_dynamic_buffer;
2087 }
de453daa 2088 event->extended.ptr = event_extended;
56f0bc67 2089
de453daa
JG
2090 comm_ext_at += sizeof(*ext_comm);
2091
56f0bc67
JG
2092 /* Insert filter expression. */
2093 if (ext_comm->filter_len) {
2094 event_extended->filter_expression = listing.data +
2095 listing.size;
2096 ret = lttng_dynamic_buffer_append(&listing, comm_ext_at,
2097 ext_comm->filter_len);
2098 if (ret) {
2099 ret = -LTTNG_ERR_NOMEM;
2100 goto free_dynamic_buffer;
2101 }
2102 comm_ext_at += ext_comm->filter_len;
2103 }
2104
2105 /* Insert exclusions. */
2106 if (ext_comm->nb_exclusions) {
2107 event_extended->exclusions.count =
2108 ext_comm->nb_exclusions;
2109 event_extended->exclusions.strings =
2110 listing.data + listing.size;
2111
2112 ret = lttng_dynamic_buffer_append(&listing,
2113 comm_ext_at,
2114 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN);
2115 if (ret) {
2116 ret = -LTTNG_ERR_NOMEM;
5e4f4898 2117 goto free_dynamic_buffer;
56f0bc67
JG
2118 }
2119 comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2120 }
2121
2122 /* Insert padding to align to 64-bits. */
2123 ret = lttng_dynamic_buffer_set_size(&listing,
2124 ALIGN_TO(listing.size, sizeof(uint64_t)));
2125 if (ret) {
2126 ret = -LTTNG_ERR_NOMEM;
2127 goto free_dynamic_buffer;
2128 }
2129
2130 /* Insert flattened userspace probe location. */
2131 if (ext_comm->userspace_probe_location_len) {
2132 struct lttng_userspace_probe_location *probe_location = NULL;
2133 struct lttng_buffer_view probe_location_view;
de453daa 2134
56f0bc67
JG
2135 probe_location_view = lttng_buffer_view_init(
2136 comm_ext_at, 0,
2137 ext_comm->userspace_probe_location_len);
de453daa 2138
56f0bc67
JG
2139 ret = lttng_userspace_probe_location_create_from_buffer(
2140 &probe_location_view, &probe_location);
2141 if (ret < 0) {
2142 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2143 goto free_dynamic_buffer;
2144 }
2145
2146 event_extended->probe_location = (struct lttng_userspace_probe_location *)
2147 (listing.data + listing.size);
2148 ret = lttng_userspace_probe_location_flatten(
2149 probe_location, &listing);
2150 lttng_userspace_probe_location_destroy(probe_location);
2151 if (ret < 0) {
2152 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2153 goto free_dynamic_buffer;
2154 }
2155
2156 comm_ext_at += ext_comm->userspace_probe_location_len;
2157 }
de453daa
JG
2158 }
2159
56f0bc67
JG
2160 /* Don't reset listing buffer as we return its content. */
2161 *events = (struct lttng_event *) listing.data;
2162 lttng_dynamic_buffer_init(&listing);
de453daa 2163 ret = (int) nb_events;
56f0bc67
JG
2164free_dynamic_buffer:
2165 lttng_dynamic_buffer_reset(&listing);
de453daa 2166end:
b4e3ceb9 2167 free(cmd_header);
de453daa 2168 free(reception_buffer);
b4e3ceb9 2169 return ret;
9f19cc17
DG
2170}
2171
fac6795d 2172/*
1c8d13c8
TD
2173 * Sets the tracing_group variable with name.
2174 * This function allocates memory pointed to by tracing_group.
2175 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
2176 */
2177int lttng_set_tracing_group(const char *name)
2178{
9d697d3d 2179 if (name == NULL) {
2f70b271 2180 return -LTTNG_ERR_INVALID;
9d697d3d
DG
2181 }
2182
fac6795d 2183 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 2184 return -LTTNG_ERR_FATAL;
fac6795d
DG
2185 }
2186
2187 return 0;
2188}
2189
cd80958d 2190int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
2191 struct lttng_calibrate *calibrate)
2192{
b812e5ca
PP
2193 /*
2194 * This command was removed in LTTng 2.9.
2195 */
2196 return -LTTNG_ERR_UND;
d0254c7c
MD
2197}
2198
5edd7e09
DG
2199/*
2200 * Set default channel attributes.
441c16a7 2201 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
2202 */
2203void lttng_channel_set_default_attr(struct lttng_domain *domain,
2204 struct lttng_channel_attr *attr)
2205{
294851b0 2206 struct lttng_channel_extended *extended;
cf0bcb51 2207
5edd7e09
DG
2208 /* Safety check */
2209 if (attr == NULL || domain == NULL) {
2210 return;
2211 }
2212
294851b0 2213 extended = (struct lttng_channel_extended *) attr->extended.ptr;
eacaa7a6
DS
2214 memset(attr, 0, sizeof(struct lttng_channel_attr));
2215
0a9c6494
DG
2216 /* Same for all domains. */
2217 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2218 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2219 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2220
5edd7e09
DG
2221 switch (domain->type) {
2222 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
2223 attr->switch_timer_interval =
2224 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 2225 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 2226 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
2227 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2228 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
cf0bcb51
JG
2229 if (extended) {
2230 extended->monitor_timer_interval =
2231 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
491d1539
MD
2232 extended->blocking_timeout =
2233 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2234 }
5edd7e09
DG
2235 break;
2236 case LTTNG_DOMAIN_UST:
0a9c6494
DG
2237 switch (domain->buf_type) {
2238 case LTTNG_BUFFER_PER_UID:
2239 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2240 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2241 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
2242 attr->switch_timer_interval =
2243 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2244 attr->read_timer_interval =
2245 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2246 if (extended) {
2247 extended->monitor_timer_interval =
2248 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2249 extended->blocking_timeout =
2250 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2251 }
0a9c6494
DG
2252 break;
2253 case LTTNG_BUFFER_PER_PID:
2254 default:
2255 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2256 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2257 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
2258 attr->switch_timer_interval =
2259 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2260 attr->read_timer_interval =
2261 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2262 if (extended) {
2263 extended->monitor_timer_interval =
2264 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2265 extended->blocking_timeout =
2266 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2267 }
0a9c6494
DG
2268 break;
2269 }
5edd7e09 2270 default:
441c16a7 2271 /* Default behavior: leave set to 0. */
5edd7e09
DG
2272 break;
2273 }
cf0bcb51
JG
2274
2275 attr->extended.ptr = extended;
5edd7e09
DG
2276}
2277
5ba3702f
JG
2278int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2279 uint64_t *discarded_events)
2280{
2281 int ret = 0;
cf0bcb51 2282 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2283
2284 if (!channel || !discarded_events) {
2285 ret = -LTTNG_ERR_INVALID;
2286 goto end;
2287 }
2288
2289 chan_ext = channel->attr.extended.ptr;
2290 if (!chan_ext) {
2291 /*
2292 * This can happen since the lttng_channel structure is
2293 * used for other tasks where this pointer is never set.
2294 */
2295 *discarded_events = 0;
2296 goto end;
2297 }
2298
2299 *discarded_events = chan_ext->discarded_events;
2300end:
2301 return ret;
2302}
2303
2304int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2305 uint64_t *lost_packets)
2306{
2307 int ret = 0;
cf0bcb51 2308 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2309
2310 if (!channel || !lost_packets) {
2311 ret = -LTTNG_ERR_INVALID;
2312 goto end;
2313 }
2314
2315 chan_ext = channel->attr.extended.ptr;
2316 if (!chan_ext) {
2317 /*
2318 * This can happen since the lttng_channel structure is
2319 * used for other tasks where this pointer is never set.
2320 */
2321 *lost_packets = 0;
2322 goto end;
2323 }
2324
2325 *lost_packets = chan_ext->lost_packets;
2326end:
2327 return ret;
2328}
2329
cf0bcb51
JG
2330int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2331 uint64_t *monitor_timer_interval)
2332{
2333 int ret = 0;
2334
2335 if (!chan || !monitor_timer_interval) {
2336 ret = -LTTNG_ERR_INVALID;
2337 goto end;
2338 }
2339
2340 if (!chan->attr.extended.ptr) {
2341 ret = -LTTNG_ERR_INVALID;
2342 goto end;
2343 }
2344
2345 *monitor_timer_interval = ((struct lttng_channel_extended *)
2346 chan->attr.extended.ptr)->monitor_timer_interval;
2347end:
2348 return ret;
2349}
2350
2351int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2352 uint64_t monitor_timer_interval)
2353{
2354 int ret = 0;
2355
2356 if (!chan || !chan->attr.extended.ptr) {
2357 ret = -LTTNG_ERR_INVALID;
2358 goto end;
2359 }
2360
2361 ((struct lttng_channel_extended *)
2362 chan->attr.extended.ptr)->monitor_timer_interval =
2363 monitor_timer_interval;
2364end:
2365 return ret;
2366}
2367
491d1539
MD
2368int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2369 int64_t *blocking_timeout)
2370{
2371 int ret = 0;
2372
2373 if (!chan || !blocking_timeout) {
2374 ret = -LTTNG_ERR_INVALID;
2375 goto end;
2376 }
2377
2378 if (!chan->attr.extended.ptr) {
2379 ret = -LTTNG_ERR_INVALID;
2380 goto end;
2381 }
2382
2383 *blocking_timeout = ((struct lttng_channel_extended *)
2384 chan->attr.extended.ptr)->blocking_timeout;
2385end:
2386 return ret;
2387}
2388
2389int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2390 int64_t blocking_timeout)
2391{
2392 int ret = 0;
2393 int64_t msec_timeout;
2394
2395 if (!chan || !chan->attr.extended.ptr) {
2396 ret = -LTTNG_ERR_INVALID;
2397 goto end;
2398 }
2399
2400 if (blocking_timeout < 0 && blocking_timeout != -1) {
2401 ret = -LTTNG_ERR_INVALID;
2402 goto end;
2403 }
2404
2405 /*
2406 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2407 * us to accept a narrower range of values (msecs expressed as a signed
2408 * 32-bit integer).
2409 */
2410 msec_timeout = blocking_timeout / 1000;
2411 if (msec_timeout != (int32_t) msec_timeout) {
2412 ret = -LTTNG_ERR_INVALID;
2413 goto end;
2414 }
2415
2416 ((struct lttng_channel_extended *)
2417 chan->attr.extended.ptr)->blocking_timeout =
2418 blocking_timeout;
2419end:
2420 return ret;
2421}
2422
fac6795d 2423/*
2269e89e 2424 * Check if session daemon is alive.
fac6795d 2425 *
2269e89e 2426 * Return 1 if alive or 0 if not.
1c8d13c8 2427 * On error returns a negative value.
fac6795d 2428 */
947308c4 2429int lttng_session_daemon_alive(void)
fac6795d
DG
2430{
2431 int ret;
2432
2433 ret = set_session_daemon_path();
2434 if (ret < 0) {
9ae110e2 2435 /* Error. */
fac6795d
DG
2436 return ret;
2437 }
2438
9d035200 2439 if (*sessiond_sock_path == '\0') {
2f70b271 2440 /*
9ae110e2
JG
2441 * No socket path set. Weird error which means the constructor
2442 * was not called.
2f70b271
DG
2443 */
2444 assert(0);
fac6795d
DG
2445 }
2446
2269e89e 2447 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 2448 if (ret < 0) {
9ae110e2 2449 /* Not alive. */
7d8234d9
MD
2450 return 0;
2451 }
7d8234d9 2452
9ae110e2 2453 /* Is alive. */
947308c4 2454 return 1;
fac6795d
DG
2455}
2456
00e2e675 2457/*
a4b92340 2458 * Set URL for a consumer for a session and domain.
00e2e675
DG
2459 *
2460 * Return 0 on success, else a negative value.
2461 */
a4b92340
DG
2462int lttng_set_consumer_url(struct lttng_handle *handle,
2463 const char *control_url, const char *data_url)
00e2e675 2464{
3dd05a85 2465 int ret;
a4b92340 2466 ssize_t size;
00e2e675 2467 struct lttcomm_session_msg lsm;
a4b92340 2468 struct lttng_uri *uris = NULL;
00e2e675 2469
a4b92340 2470 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 2471 return -LTTNG_ERR_INVALID;
00e2e675
DG
2472 }
2473
a4b92340
DG
2474 memset(&lsm, 0, sizeof(lsm));
2475
00e2e675
DG
2476 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2477
cac3069d 2478 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
00e2e675 2479 sizeof(lsm.session.name));
cac3069d 2480 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
00e2e675 2481
bc894455 2482 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 2483 if (size < 0) {
2f70b271 2484 return -LTTNG_ERR_INVALID;
a4b92340
DG
2485 }
2486
2487 lsm.u.uri.size = size;
00e2e675 2488
795a978d 2489 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2490 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2491
2492 free(uris);
2493 return ret;
00e2e675
DG
2494}
2495
2496/*
9c6bda17 2497 * [OBSOLETE]
00e2e675
DG
2498 */
2499int lttng_enable_consumer(struct lttng_handle *handle)
2500{
785d2d0d 2501 return -ENOSYS;
00e2e675
DG
2502}
2503
2504/*
9c6bda17 2505 * [OBSOLETE]
00e2e675
DG
2506 */
2507int lttng_disable_consumer(struct lttng_handle *handle)
2508{
785d2d0d 2509 return -ENOSYS;
00e2e675
DG
2510}
2511
07424f16
DG
2512/*
2513 * This is an extension of create session that is ONLY and SHOULD only be used
2514 * by the lttng command line program. It exists to avoid using URI parsing in
2515 * the lttng client.
2516 *
2517 * We need the date and time for the trace path subdirectory for the case where
2518 * the user does NOT define one using either -o or -U. Using the normal
2519 * lttng_create_session API call, we have no clue on the session daemon side if
2520 * the URL was generated automatically by the client or define by the user.
2521 *
2522 * So this function "wrapper" is hidden from the public API, takes the datetime
2523 * string and appends it if necessary to the URI subdirectory before sending it
2524 * to the session daemon.
2525 *
2526 * With this extra function, the lttng_create_session call behavior is not
2527 * changed and the timestamp is appended to the URI on the session daemon side
2528 * if necessary.
2529 */
2530int _lttng_create_session_ext(const char *name, const char *url,
2531 const char *datetime)
2532{
3dd05a85 2533 int ret;
07424f16
DG
2534 ssize_t size;
2535 struct lttcomm_session_msg lsm;
2536 struct lttng_uri *uris = NULL;
2537
2bba9e53 2538 if (name == NULL || datetime == NULL) {
2f70b271 2539 return -LTTNG_ERR_INVALID;
07424f16
DG
2540 }
2541
2542 memset(&lsm, 0, sizeof(lsm));
2543
2544 lsm.cmd_type = LTTNG_CREATE_SESSION;
cac3069d 2545 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
07424f16 2546
9ae110e2 2547 /* There should never be a data URL. */
bc894455 2548 size = uri_parse_str_urls(url, NULL, &uris);
07424f16 2549 if (size < 0) {
3dd05a85
DG
2550 ret = -LTTNG_ERR_INVALID;
2551 goto error;
07424f16
DG
2552 }
2553
2554 lsm.u.uri.size = size;
2555
4b35a6b3 2556 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
da4aa2b8
DG
2557 /* Don't append datetime if the name was automatically created. */
2558 if (strncmp(name, DEFAULT_SESSION_NAME "-",
2559 strlen(DEFAULT_SESSION_NAME) + 1)) {
2560 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s",
2561 name, datetime);
2562 } else {
2563 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2564 }
07424f16
DG
2565 if (ret < 0) {
2566 PERROR("snprintf uri subdir");
3dd05a85
DG
2567 ret = -LTTNG_ERR_FATAL;
2568 goto error;
07424f16
DG
2569 }
2570 }
2571
795a978d 2572 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2573 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2574
2575error:
2576 free(uris);
2577 return ret;
07424f16
DG
2578}
2579
806e2684
DG
2580/*
2581 * For a given session name, this call checks if the data is ready to be read
2582 * or is still being extracted by the consumer(s) hence not ready to be used by
2583 * any readers.
2584 */
6d805429 2585int lttng_data_pending(const char *session_name)
806e2684
DG
2586{
2587 int ret;
2588 struct lttcomm_session_msg lsm;
f6151c55 2589 uint8_t *pending = NULL;
806e2684
DG
2590
2591 if (session_name == NULL) {
2592 return -LTTNG_ERR_INVALID;
2593 }
2594
53efb85a 2595 memset(&lsm, 0, sizeof(lsm));
6d805429 2596 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2597
cac3069d
DG
2598 lttng_ctl_copy_string(lsm.session.name, session_name,
2599 sizeof(lsm.session.name));
806e2684 2600
f6151c55
JG
2601 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2602 if (ret < 0) {
2603 goto end;
2604 } else if (ret != 1) {
2605 /* Unexpected payload size */
2606 ret = -LTTNG_ERR_INVALID;
2607 goto end;
806e2684
DG
2608 }
2609
f6151c55
JG
2610 ret = (int) *pending;
2611end:
2612 free(pending);
806e2684
DG
2613 return ret;
2614}
2615
27babd3a
DG
2616/*
2617 * Create a session exclusively used for snapshot.
2618 *
2619 * Returns LTTNG_OK on success or a negative error code.
2620 */
2621int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2622{
2623 int ret;
2624 ssize_t size;
2625 struct lttcomm_session_msg lsm;
2626 struct lttng_uri *uris = NULL;
2627
2628 if (name == NULL) {
2629 return -LTTNG_ERR_INVALID;
2630 }
2631
2632 memset(&lsm, 0, sizeof(lsm));
2633
2634 lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT;
2635 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2636
2637 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2638 if (size < 0) {
2639 return -LTTNG_ERR_INVALID;
2640 }
2641
2642 lsm.u.uri.size = size;
2643
10d65351
JD
2644 /*
2645 * If the user does not specify a custom subdir, use the session name.
2646 */
2647 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
2648 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name);
2649 if (ret < 0) {
2650 PERROR("snprintf uri subdir");
2651 ret = -LTTNG_ERR_FATAL;
2652 goto error;
2653 }
2654 }
2655
795a978d 2656 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
27babd3a
DG
2657 sizeof(struct lttng_uri) * size, NULL);
2658
10d65351 2659error:
27babd3a
DG
2660 free(uris);
2661 return ret;
2662}
2663
ecc48a90
JD
2664/*
2665 * Create a session exclusively used for live.
2666 *
2667 * Returns LTTNG_OK on success or a negative error code.
2668 */
2669int lttng_create_session_live(const char *name, const char *url,
2670 unsigned int timer_interval)
2671{
2672 int ret;
2673 ssize_t size;
2674 struct lttcomm_session_msg lsm;
2675 struct lttng_uri *uris = NULL;
2676
92805ee4 2677 if (name == NULL || timer_interval == 0) {
ecc48a90
JD
2678 return -LTTNG_ERR_INVALID;
2679 }
2680
2681 memset(&lsm, 0, sizeof(lsm));
2682
2683 lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE;
2684 lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name));
2685
1a241656
DG
2686 if (url) {
2687 size = uri_parse_str_urls(url, NULL, &uris);
2688 if (size <= 0) {
2689 ret = -LTTNG_ERR_INVALID;
2690 goto end;
2691 }
ecc48a90 2692
1a241656
DG
2693 /* file:// is not accepted for live session. */
2694 if (uris[0].dtype == LTTNG_DST_PATH) {
2695 ret = -LTTNG_ERR_INVALID;
2696 goto end;
2697 }
2698 } else {
2699 size = 0;
ecc48a90
JD
2700 }
2701
2702 lsm.u.session_live.nb_uri = size;
2703 lsm.u.session_live.timer_interval = timer_interval;
2704
795a978d 2705 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
ecc48a90
JD
2706 sizeof(struct lttng_uri) * size, NULL);
2707
2708end:
2709 free(uris);
2710 return ret;
2711}
2712
a5dfbb9d
MD
2713/*
2714 * List PIDs in the tracker.
2715 *
1b18ce93
JG
2716 * enabled is set to whether the PID tracker is enabled.
2717 * pids is set to an allocated array of PIDs currently tracked. On
2718 * success, pids must be freed by the caller.
2719 * nr_pids is set to the number of entries contained by the pids array.
a5dfbb9d
MD
2720 *
2721 * Returns 0 on success, else a negative LTTng error code.
2722 */
2723int lttng_list_tracker_pids(struct lttng_handle *handle,
2724 int *_enabled, int32_t **_pids, size_t *_nr_pids)
2725{
ebbf5ab7
JR
2726 int ret;
2727 int enabled = 1;
a5dfbb9d
MD
2728 struct lttcomm_session_msg lsm;
2729 size_t nr_pids;
ff75c11e 2730 int32_t *pids = NULL;
a5dfbb9d
MD
2731
2732 if (handle == NULL) {
2733 return -LTTNG_ERR_INVALID;
2734 }
2735
2736 memset(&lsm, 0, sizeof(lsm));
2737 lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS;
2738 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2739 sizeof(lsm.session.name));
2740 lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain);
2741
2742 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids);
2743 if (ret < 0) {
2744 return ret;
2745 }
2746 nr_pids = ret / sizeof(int32_t);
de8218d4
JG
2747 if (nr_pids > 0 && !pids) {
2748 return -LTTNG_ERR_UNK;
2749 }
a5dfbb9d
MD
2750 if (nr_pids == 1 && pids[0] == -1) {
2751 free(pids);
2752 pids = NULL;
2753 enabled = 0;
2754 nr_pids = 0;
2755 }
2756 *_enabled = enabled;
2757 *_pids = pids;
2758 *_nr_pids = nr_pids;
2759 return 0;
2760}
2761
93ec662e
JD
2762/*
2763 * Regenerate the metadata for a session.
2764 * Return 0 on success, a negative error code on error.
2765 */
eded6438 2766int lttng_regenerate_metadata(const char *session_name)
93ec662e
JD
2767{
2768 int ret;
2769 struct lttcomm_session_msg lsm;
2770
2771 if (!session_name) {
2772 ret = -LTTNG_ERR_INVALID;
2773 goto end;
2774 }
2775
2776 memset(&lsm, 0, sizeof(lsm));
eded6438 2777 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
93ec662e
JD
2778
2779 lttng_ctl_copy_string(lsm.session.name, session_name,
2780 sizeof(lsm.session.name));
2781
2782 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2783 if (ret < 0) {
2784 goto end;
2785 }
2786
2787 ret = 0;
2788end:
2789 return ret;
2790}
2791
eded6438
JD
2792/*
2793 * Deprecated, replaced by lttng_regenerate_metadata.
2794 */
2795int lttng_metadata_regenerate(const char *session_name)
2796{
2797 return lttng_regenerate_metadata(session_name);
2798}
2799
c2561365
JD
2800/*
2801 * Regenerate the statedump of a session.
2802 * Return 0 on success, a negative error code on error.
2803 */
2804int lttng_regenerate_statedump(const char *session_name)
2805{
2806 int ret;
2807 struct lttcomm_session_msg lsm;
2808
2809 if (!session_name) {
2810 ret = -LTTNG_ERR_INVALID;
2811 goto end;
2812 }
2813
2814 memset(&lsm, 0, sizeof(lsm));
2815 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2816
2817 lttng_ctl_copy_string(lsm.session.name, session_name,
2818 sizeof(lsm.session.name));
2819
2820 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2821 if (ret < 0) {
2822 goto end;
2823 }
2824
2825 ret = 0;
2826end:
2827 return ret;
2828}
2829
a58c490f
JG
2830int lttng_register_trigger(struct lttng_trigger *trigger)
2831{
2832 int ret;
2833 struct lttcomm_session_msg lsm;
3647288f 2834 struct lttng_dynamic_buffer buffer;
a58c490f 2835
3647288f 2836 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
2837 if (!trigger) {
2838 ret = -LTTNG_ERR_INVALID;
2839 goto end;
2840 }
2841
2842 if (!lttng_trigger_validate(trigger)) {
eac4828d 2843 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
2844 goto end;
2845 }
2846
3647288f
JG
2847 ret = lttng_trigger_serialize(trigger, &buffer);
2848 if (ret < 0) {
a58c490f
JG
2849 ret = -LTTNG_ERR_UNK;
2850 goto end;
2851 }
2852
a58c490f
JG
2853 memset(&lsm, 0, sizeof(lsm));
2854 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
3647288f
JG
2855 lsm.u.trigger.length = (uint32_t) buffer.size;
2856 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
2857 buffer.size, NULL);
a58c490f 2858end:
3647288f 2859 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
2860 return ret;
2861}
2862
2863int lttng_unregister_trigger(struct lttng_trigger *trigger)
2864{
2865 int ret;
2866 struct lttcomm_session_msg lsm;
3647288f 2867 struct lttng_dynamic_buffer buffer;
a58c490f 2868
3647288f 2869 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
2870 if (!trigger) {
2871 ret = -LTTNG_ERR_INVALID;
2872 goto end;
2873 }
2874
2875 if (!lttng_trigger_validate(trigger)) {
3647288f 2876 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
2877 goto end;
2878 }
2879
3647288f
JG
2880 ret = lttng_trigger_serialize(trigger, &buffer);
2881 if (ret < 0) {
a58c490f
JG
2882 ret = -LTTNG_ERR_UNK;
2883 goto end;
2884 }
2885
a58c490f
JG
2886 memset(&lsm, 0, sizeof(lsm));
2887 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3647288f
JG
2888 lsm.u.trigger.length = (uint32_t) buffer.size;
2889 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
2890 buffer.size, NULL);
a58c490f 2891end:
3647288f 2892 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
2893 return ret;
2894}
2895
fac6795d 2896/*
9ae110e2 2897 * lib constructor.
fac6795d 2898 */
b2b89e8a 2899static void __attribute__((constructor)) init(void)
fac6795d
DG
2900{
2901 /* Set default session group */
bbccc3d2 2902 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 2903}
49cca668
DG
2904
2905/*
9ae110e2 2906 * lib destructor.
49cca668 2907 */
b2b89e8a 2908static void __attribute__((destructor)) lttng_ctl_exit(void)
49cca668
DG
2909{
2910 free(tracing_group);
2911}
This page took 0.352005 seconds and 4 git commands to generate.