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