Fix: lttng-ctl: lttng_list_sessions: initialize out_sessions to NULL when returning 0
[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;
21c5a233
MD
2077 /*
2078 * Initialize out_sessions to NULL so it is initialized when
2079 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2080 * be subsequently freed.
2081 */
2082 *out_sessions = NULL;
b178f53e
JG
2083 ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions);
2084 if (ret <= 0) {
b178f53e
JG
2085 goto end;
2086 }
2087 if (!sessions) {
2088 ret = -LTTNG_ERR_FATAL;
2089 goto end;
2090 }
2091
2092 if (ret % session_size) {
2093 ret = -LTTNG_ERR_UNK;
2094 free(sessions);
b178f53e 2095 goto end;
57167058 2096 }
b178f53e
JG
2097 session_count = (size_t) ret / session_size;
2098 sessions_extended_begin = (struct lttng_session_extended *)
2099 (&sessions[session_count]);
57167058 2100
b178f53e
JG
2101 /* Set extended session info pointers. */
2102 for (i = 0; i < session_count; i++) {
2103 struct lttng_session *session = &sessions[i];
2104 struct lttng_session_extended *extended =
2105 &(sessions_extended_begin[i]);
2106
2107 session->extended.ptr = extended;
2108 }
2109
2110 ret = (int) session_count;
2111 *out_sessions = sessions;
2112end:
2113 return ret;
2114}
2115
2116enum lttng_error_code lttng_session_get_creation_time(
2117 const struct lttng_session *session, uint64_t *creation_time)
2118{
2119 enum lttng_error_code ret = LTTNG_OK;
2120 struct lttng_session_extended *extended;
2121
2122 if (!session || !creation_time || !session->extended.ptr) {
2123 ret = LTTNG_ERR_INVALID;
2124 goto end;
2125 }
2126
2127 extended = session->extended.ptr;
2128 if (!extended->creation_time.is_set) {
2129 /* Not created on the session daemon yet. */
2130 ret = LTTNG_ERR_SESSION_NOT_EXIST;
2131 goto end;
2132 }
2133 *creation_time = extended->creation_time.value;
2134end:
2135 return ret;
57167058
DG
2136}
2137
d7ba1388
MD
2138int lttng_set_session_shm_path(const char *session_name,
2139 const char *shm_path)
2140{
1da0efb1 2141 int ret;
d7ba1388
MD
2142 struct lttcomm_session_msg lsm;
2143
2144 if (session_name == NULL) {
2145 return -LTTNG_ERR_INVALID;
2146 }
2147
2148 memset(&lsm, 0, sizeof(lsm));
2149 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
2150
1da0efb1 2151 ret = lttng_strncpy(lsm.session.name, session_name,
d7ba1388 2152 sizeof(lsm.session.name));
1da0efb1
JG
2153 if (ret) {
2154 ret = -LTTNG_ERR_INVALID;
2155 goto end;
2156 }
2157
2158 ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "",
d7ba1388 2159 sizeof(lsm.u.set_shm_path.shm_path));
1da0efb1
JG
2160 if (ret) {
2161 ret = -LTTNG_ERR_INVALID;
2162 goto end;
2163 }
d7ba1388 2164
1da0efb1
JG
2165 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2166end:
2167 return ret;
d7ba1388
MD
2168}
2169
9f19cc17 2170/*
9ae110e2
JG
2171 * Ask the session daemon for all available domains of a session.
2172 * Sets the contents of the domains array.
2173 * Returns the number of lttng_domain entries in domains;
2174 * on error, returns a negative value.
9f19cc17 2175 */
330be774 2176int lttng_list_domains(const char *session_name,
cd80958d 2177 struct lttng_domain **domains)
9f19cc17
DG
2178{
2179 int ret;
cd80958d
DG
2180 struct lttcomm_session_msg lsm;
2181
330be774 2182 if (session_name == NULL) {
1da0efb1
JG
2183 ret = -LTTNG_ERR_INVALID;
2184 goto error;
cd80958d 2185 }
9f19cc17 2186
53efb85a 2187 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
2188 lsm.cmd_type = LTTNG_LIST_DOMAINS;
2189
1da0efb1 2190 ret = lttng_strncpy(lsm.session.name, session_name,
cac3069d 2191 sizeof(lsm.session.name));
1da0efb1
JG
2192 if (ret) {
2193 ret = -LTTNG_ERR_INVALID;
2194 goto error;
2195 }
cd80958d 2196
cac3069d 2197 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17 2198 if (ret < 0) {
1da0efb1 2199 goto error;
9f19cc17
DG
2200 }
2201
2202 return ret / sizeof(struct lttng_domain);
1da0efb1
JG
2203error:
2204 return ret;
9f19cc17
DG
2205}
2206
2207/*
9ae110e2
JG
2208 * Ask the session daemon for all available channels of a session.
2209 * Sets the contents of the channels array.
2210 * Returns the number of lttng_channel entries in channels;
2211 * on error, returns a negative value.
9f19cc17 2212 */
cd80958d
DG
2213int lttng_list_channels(struct lttng_handle *handle,
2214 struct lttng_channel **channels)
9f19cc17
DG
2215{
2216 int ret;
53e367f9
JG
2217 size_t channel_count, i;
2218 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51 2219 sizeof(struct lttng_channel_extended);
cd80958d 2220 struct lttcomm_session_msg lsm;
53e367f9 2221 void *extended_at;
cd80958d 2222
9d697d3d 2223 if (handle == NULL) {
53e367f9
JG
2224 ret = -LTTNG_ERR_INVALID;
2225 goto end;
cd80958d
DG
2226 }
2227
53efb85a 2228 memset(&lsm, 0, sizeof(lsm));
cd80958d 2229 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1da0efb1 2230 ret = lttng_strncpy(lsm.session.name, handle->session_name,
cd80958d 2231 sizeof(lsm.session.name));
1da0efb1
JG
2232 if (ret) {
2233 ret = -LTTNG_ERR_INVALID;
2234 goto end;
2235 }
9f19cc17 2236
60160d2a 2237 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
9f19cc17 2238
cac3069d 2239 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17 2240 if (ret < 0) {
53e367f9
JG
2241 goto end;
2242 }
2243
2244 if (ret % channel_size) {
2245 ret = -LTTNG_ERR_UNK;
2246 free(*channels);
2247 *channels = NULL;
2248 goto end;
9f19cc17 2249 }
53e367f9 2250 channel_count = (size_t) ret / channel_size;
9f19cc17 2251
53e367f9
JG
2252 /* Set extended info pointers */
2253 extended_at = ((void *) *channels) +
2254 channel_count * sizeof(struct lttng_channel);
2255 for (i = 0; i < channel_count; i++) {
2256 struct lttng_channel *chan = &(*channels)[i];
2257
2258 chan->attr.extended.ptr = extended_at;
cf0bcb51 2259 extended_at += sizeof(struct lttng_channel_extended);
53e367f9
JG
2260 }
2261
2262 ret = (int) channel_count;
2263end:
2264 return ret;
9f19cc17
DG
2265}
2266
2267/*
9ae110e2
JG
2268 * Ask the session daemon for all available events of a session channel.
2269 * Sets the contents of the events array.
2270 * Returns the number of lttng_event entries in events;
2271 * on error, returns a negative value.
9f19cc17 2272 */
cd80958d
DG
2273int lttng_list_events(struct lttng_handle *handle,
2274 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
2275{
2276 int ret;
cd80958d 2277 struct lttcomm_session_msg lsm;
b4e3ceb9
PP
2278 struct lttcomm_event_command_header *cmd_header = NULL;
2279 size_t cmd_header_len;
2280 uint32_t nb_events, i;
de453daa 2281 void *comm_ext_at;
de453daa 2282 char *reception_buffer = NULL;
56f0bc67 2283 struct lttng_dynamic_buffer listing;
de453daa 2284 size_t storage_req;
9f19cc17 2285
9d697d3d
DG
2286 /* Safety check. An handle and channel name are mandatory */
2287 if (handle == NULL || channel_name == NULL) {
2f70b271 2288 return -LTTNG_ERR_INVALID;
cd80958d
DG
2289 }
2290
53efb85a 2291 memset(&lsm, 0, sizeof(lsm));
cd80958d 2292 lsm.cmd_type = LTTNG_LIST_EVENTS;
1da0efb1 2293 ret = lttng_strncpy(lsm.session.name, handle->session_name,
cd80958d 2294 sizeof(lsm.session.name));
1da0efb1
JG
2295 if (ret) {
2296 ret = -LTTNG_ERR_INVALID;
2297 goto end;
2298 }
2299
2300 ret = lttng_strncpy(lsm.u.list.channel_name, channel_name,
cd80958d 2301 sizeof(lsm.u.list.channel_name));
1da0efb1
JG
2302 if (ret) {
2303 ret = -LTTNG_ERR_INVALID;
2304 goto end;
2305 }
2306
60160d2a 2307 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
9f19cc17 2308
a04d53fc 2309 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
de453daa
JG
2310 (void **) &reception_buffer, (void **) &cmd_header,
2311 &cmd_header_len);
9f19cc17 2312 if (ret < 0) {
de453daa 2313 goto end;
9f19cc17
DG
2314 }
2315
d9f484bc
JG
2316 if (!cmd_header) {
2317 ret = -LTTNG_ERR_UNK;
2318 goto end;
2319 }
2320
b4e3ceb9
PP
2321 /* Set number of events and free command header */
2322 nb_events = cmd_header->nb_events;
2323 if (nb_events > INT_MAX) {
55c9e7ca 2324 ret = -LTTNG_ERR_OVERFLOW;
de453daa 2325 goto end;
b4e3ceb9 2326 }
b4e3ceb9
PP
2327 free(cmd_header);
2328 cmd_header = NULL;
2329
de453daa
JG
2330 /*
2331 * The buffer that is returned must contain a "flat" version of
2332 * the events that are returned. In other words, all pointers
2333 * within an lttng_event must point to a location within the returned
56f0bc67
JG
2334 * buffer so that the user may free everything by simply calling free()
2335 * on the returned buffer. This is needed in order to maintain API
de453daa
JG
2336 * compatibility.
2337 *
56f0bc67
JG
2338 * A first pass is performed to compute the size of the buffer that
2339 * must be allocated. A second pass is then performed to setup
de453daa
JG
2340 * the returned events so that their members always point within the
2341 * buffer.
2342 *
2343 * The layout of the returned buffer is as follows:
2344 * - struct lttng_event[nb_events],
2345 * - nb_events times the following:
2346 * - struct lttng_event_extended,
2347 * - flattened version of userspace_probe_location
2348 * - filter_expression
2349 * - exclusions
2350 * - padding to align to 64-bits
2351 */
2352 comm_ext_at = reception_buffer +
2353 (nb_events * sizeof(struct lttng_event));
2354 storage_req = nb_events * sizeof(struct lttng_event);
b4e3ceb9
PP
2355
2356 for (i = 0; i < nb_events; i++) {
de453daa
JG
2357 struct lttcomm_event_extended_header *ext_comm =
2358 (struct lttcomm_event_extended_header *) comm_ext_at;
56f0bc67 2359 int probe_storage_req = 0;
b4e3ceb9 2360
de453daa
JG
2361 comm_ext_at += sizeof(*ext_comm);
2362 comm_ext_at += ext_comm->filter_len;
2363 comm_ext_at +=
2364 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2365
56f0bc67
JG
2366 if (ext_comm->userspace_probe_location_len) {
2367 struct lttng_userspace_probe_location *probe_location = NULL;
2368 struct lttng_buffer_view probe_location_view;
2369
2370 probe_location_view = lttng_buffer_view_init(
2371 comm_ext_at, 0,
2372 ext_comm->userspace_probe_location_len);
2373
2374 /*
2375 * Create a temporary userspace probe location to
2376 * determine the size needed by a "flattened" version
2377 * of that same probe location.
2378 */
2379 ret = lttng_userspace_probe_location_create_from_buffer(
2380 &probe_location_view, &probe_location);
2381 if (ret < 0) {
2382 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2383 goto end;
2384 }
2385
2386 ret = lttng_userspace_probe_location_flatten(
2387 probe_location, NULL);
2388 lttng_userspace_probe_location_destroy(probe_location);
2389 if (ret < 0) {
2390 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2391 goto end;
2392 }
2393
2394 probe_storage_req = ret;
2395 comm_ext_at += ext_comm->userspace_probe_location_len;
56f0bc67
JG
2396 }
2397
de453daa 2398 storage_req += sizeof(struct lttng_event_extended);
de453daa
JG
2399 storage_req += ext_comm->filter_len;
2400 storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
56f0bc67
JG
2401 /* Padding to ensure the flat probe is aligned. */
2402 storage_req = ALIGN_TO(storage_req, sizeof(uint64_t));
2403 storage_req += probe_storage_req;
b4e3ceb9
PP
2404 }
2405
56f0bc67
JG
2406 lttng_dynamic_buffer_init(&listing);
2407 /*
2408 * We must ensure that "listing" is never resized so as to preserve
2409 * the validity of the flattened objects.
2410 */
2411 ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req);
2412 if (ret) {
2413 ret = -LTTNG_ERR_NOMEM;
de453daa
JG
2414 goto end;
2415 }
56f0bc67
JG
2416
2417 ret = lttng_dynamic_buffer_append(&listing, reception_buffer,
2418 nb_events * sizeof(struct lttng_event));
2419 if (ret) {
2420 ret = -LTTNG_ERR_NOMEM;
2421 goto free_dynamic_buffer;
2422 }
de453daa
JG
2423
2424 comm_ext_at = reception_buffer +
56f0bc67 2425 (nb_events * sizeof(struct lttng_event));
de453daa
JG
2426 for (i = 0; i < nb_events; i++) {
2427 struct lttng_event *event = (struct lttng_event *)
56f0bc67 2428 (listing.data + (sizeof(struct lttng_event) * i));
de453daa
JG
2429 struct lttcomm_event_extended_header *ext_comm =
2430 (struct lttcomm_event_extended_header *) comm_ext_at;
2431 struct lttng_event_extended *event_extended =
56f0bc67
JG
2432 (struct lttng_event_extended *)
2433 (listing.data + listing.size);
de453daa 2434
56f0bc67
JG
2435 /* Insert struct lttng_event_extended. */
2436 ret = lttng_dynamic_buffer_set_size(&listing,
2437 listing.size + sizeof(*event_extended));
2438 if (ret) {
2439 ret = -LTTNG_ERR_NOMEM;
2440 goto free_dynamic_buffer;
2441 }
de453daa 2442 event->extended.ptr = event_extended;
56f0bc67 2443
de453daa
JG
2444 comm_ext_at += sizeof(*ext_comm);
2445
56f0bc67
JG
2446 /* Insert filter expression. */
2447 if (ext_comm->filter_len) {
2448 event_extended->filter_expression = listing.data +
2449 listing.size;
2450 ret = lttng_dynamic_buffer_append(&listing, comm_ext_at,
2451 ext_comm->filter_len);
2452 if (ret) {
2453 ret = -LTTNG_ERR_NOMEM;
2454 goto free_dynamic_buffer;
2455 }
2456 comm_ext_at += ext_comm->filter_len;
2457 }
2458
2459 /* Insert exclusions. */
2460 if (ext_comm->nb_exclusions) {
2461 event_extended->exclusions.count =
2462 ext_comm->nb_exclusions;
2463 event_extended->exclusions.strings =
2464 listing.data + listing.size;
2465
2466 ret = lttng_dynamic_buffer_append(&listing,
2467 comm_ext_at,
2468 ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN);
2469 if (ret) {
2470 ret = -LTTNG_ERR_NOMEM;
5e4f4898 2471 goto free_dynamic_buffer;
56f0bc67
JG
2472 }
2473 comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
2474 }
2475
2476 /* Insert padding to align to 64-bits. */
2477 ret = lttng_dynamic_buffer_set_size(&listing,
2478 ALIGN_TO(listing.size, sizeof(uint64_t)));
2479 if (ret) {
2480 ret = -LTTNG_ERR_NOMEM;
2481 goto free_dynamic_buffer;
2482 }
2483
2484 /* Insert flattened userspace probe location. */
2485 if (ext_comm->userspace_probe_location_len) {
2486 struct lttng_userspace_probe_location *probe_location = NULL;
2487 struct lttng_buffer_view probe_location_view;
de453daa 2488
56f0bc67
JG
2489 probe_location_view = lttng_buffer_view_init(
2490 comm_ext_at, 0,
2491 ext_comm->userspace_probe_location_len);
de453daa 2492
56f0bc67
JG
2493 ret = lttng_userspace_probe_location_create_from_buffer(
2494 &probe_location_view, &probe_location);
2495 if (ret < 0) {
2496 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2497 goto free_dynamic_buffer;
2498 }
2499
2500 event_extended->probe_location = (struct lttng_userspace_probe_location *)
2501 (listing.data + listing.size);
2502 ret = lttng_userspace_probe_location_flatten(
2503 probe_location, &listing);
2504 lttng_userspace_probe_location_destroy(probe_location);
2505 if (ret < 0) {
2506 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2507 goto free_dynamic_buffer;
2508 }
2509
2510 comm_ext_at += ext_comm->userspace_probe_location_len;
2511 }
de453daa
JG
2512 }
2513
56f0bc67
JG
2514 /* Don't reset listing buffer as we return its content. */
2515 *events = (struct lttng_event *) listing.data;
2516 lttng_dynamic_buffer_init(&listing);
de453daa 2517 ret = (int) nb_events;
56f0bc67
JG
2518free_dynamic_buffer:
2519 lttng_dynamic_buffer_reset(&listing);
de453daa 2520end:
b4e3ceb9 2521 free(cmd_header);
de453daa 2522 free(reception_buffer);
b4e3ceb9 2523 return ret;
9f19cc17
DG
2524}
2525
fac6795d 2526/*
1c8d13c8
TD
2527 * Sets the tracing_group variable with name.
2528 * This function allocates memory pointed to by tracing_group.
2529 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
2530 */
2531int lttng_set_tracing_group(const char *name)
2532{
fef43092 2533 char *new_group;
9d697d3d 2534 if (name == NULL) {
2f70b271 2535 return -LTTNG_ERR_INVALID;
9d697d3d
DG
2536 }
2537
fef43092 2538 if (asprintf(&new_group, "%s", name) < 0) {
2f70b271 2539 return -LTTNG_ERR_FATAL;
fac6795d
DG
2540 }
2541
fef43092
JR
2542 free(tracing_group);
2543 tracing_group = new_group;
2544 new_group = NULL;
2545
fac6795d
DG
2546 return 0;
2547}
2548
cd80958d 2549int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
2550 struct lttng_calibrate *calibrate)
2551{
b812e5ca
PP
2552 /*
2553 * This command was removed in LTTng 2.9.
2554 */
2555 return -LTTNG_ERR_UND;
d0254c7c
MD
2556}
2557
5edd7e09
DG
2558/*
2559 * Set default channel attributes.
441c16a7 2560 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
2561 */
2562void lttng_channel_set_default_attr(struct lttng_domain *domain,
2563 struct lttng_channel_attr *attr)
2564{
294851b0 2565 struct lttng_channel_extended *extended;
cf0bcb51 2566
5edd7e09
DG
2567 /* Safety check */
2568 if (attr == NULL || domain == NULL) {
2569 return;
2570 }
2571
294851b0 2572 extended = (struct lttng_channel_extended *) attr->extended.ptr;
eacaa7a6
DS
2573 memset(attr, 0, sizeof(struct lttng_channel_attr));
2574
0a9c6494
DG
2575 /* Same for all domains. */
2576 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2577 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2578 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2579
5edd7e09
DG
2580 switch (domain->type) {
2581 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
2582 attr->switch_timer_interval =
2583 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 2584 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 2585 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
2586 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2587 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
cf0bcb51
JG
2588 if (extended) {
2589 extended->monitor_timer_interval =
2590 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
491d1539
MD
2591 extended->blocking_timeout =
2592 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2593 }
5edd7e09
DG
2594 break;
2595 case LTTNG_DOMAIN_UST:
0a9c6494
DG
2596 switch (domain->buf_type) {
2597 case LTTNG_BUFFER_PER_UID:
2598 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2599 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2600 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
2601 attr->switch_timer_interval =
2602 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2603 attr->read_timer_interval =
2604 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2605 if (extended) {
2606 extended->monitor_timer_interval =
2607 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2608 extended->blocking_timeout =
2609 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2610 }
0a9c6494
DG
2611 break;
2612 case LTTNG_BUFFER_PER_PID:
2613 default:
2614 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2615 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2616 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
2617 attr->switch_timer_interval =
2618 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2619 attr->read_timer_interval =
2620 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2621 if (extended) {
2622 extended->monitor_timer_interval =
2623 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2624 extended->blocking_timeout =
2625 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2626 }
0a9c6494
DG
2627 break;
2628 }
5edd7e09 2629 default:
441c16a7 2630 /* Default behavior: leave set to 0. */
5edd7e09
DG
2631 break;
2632 }
cf0bcb51
JG
2633
2634 attr->extended.ptr = extended;
5edd7e09
DG
2635}
2636
5ba3702f
JG
2637int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2638 uint64_t *discarded_events)
2639{
2640 int ret = 0;
cf0bcb51 2641 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2642
2643 if (!channel || !discarded_events) {
2644 ret = -LTTNG_ERR_INVALID;
2645 goto end;
2646 }
2647
2648 chan_ext = channel->attr.extended.ptr;
2649 if (!chan_ext) {
2650 /*
2651 * This can happen since the lttng_channel structure is
2652 * used for other tasks where this pointer is never set.
2653 */
2654 *discarded_events = 0;
2655 goto end;
2656 }
2657
2658 *discarded_events = chan_ext->discarded_events;
2659end:
2660 return ret;
2661}
2662
2663int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2664 uint64_t *lost_packets)
2665{
2666 int ret = 0;
cf0bcb51 2667 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2668
2669 if (!channel || !lost_packets) {
2670 ret = -LTTNG_ERR_INVALID;
2671 goto end;
2672 }
2673
2674 chan_ext = channel->attr.extended.ptr;
2675 if (!chan_ext) {
2676 /*
2677 * This can happen since the lttng_channel structure is
2678 * used for other tasks where this pointer is never set.
2679 */
2680 *lost_packets = 0;
2681 goto end;
2682 }
2683
2684 *lost_packets = chan_ext->lost_packets;
2685end:
2686 return ret;
2687}
2688
cf0bcb51
JG
2689int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2690 uint64_t *monitor_timer_interval)
2691{
2692 int ret = 0;
2693
2694 if (!chan || !monitor_timer_interval) {
2695 ret = -LTTNG_ERR_INVALID;
2696 goto end;
2697 }
2698
2699 if (!chan->attr.extended.ptr) {
2700 ret = -LTTNG_ERR_INVALID;
2701 goto end;
2702 }
2703
2704 *monitor_timer_interval = ((struct lttng_channel_extended *)
2705 chan->attr.extended.ptr)->monitor_timer_interval;
2706end:
2707 return ret;
2708}
2709
2710int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2711 uint64_t monitor_timer_interval)
2712{
2713 int ret = 0;
2714
2715 if (!chan || !chan->attr.extended.ptr) {
2716 ret = -LTTNG_ERR_INVALID;
2717 goto end;
2718 }
2719
2720 ((struct lttng_channel_extended *)
2721 chan->attr.extended.ptr)->monitor_timer_interval =
2722 monitor_timer_interval;
2723end:
2724 return ret;
2725}
2726
491d1539
MD
2727int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2728 int64_t *blocking_timeout)
2729{
2730 int ret = 0;
2731
2732 if (!chan || !blocking_timeout) {
2733 ret = -LTTNG_ERR_INVALID;
2734 goto end;
2735 }
2736
2737 if (!chan->attr.extended.ptr) {
2738 ret = -LTTNG_ERR_INVALID;
2739 goto end;
2740 }
2741
2742 *blocking_timeout = ((struct lttng_channel_extended *)
2743 chan->attr.extended.ptr)->blocking_timeout;
2744end:
2745 return ret;
2746}
2747
2748int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2749 int64_t blocking_timeout)
2750{
2751 int ret = 0;
2752 int64_t msec_timeout;
2753
2754 if (!chan || !chan->attr.extended.ptr) {
2755 ret = -LTTNG_ERR_INVALID;
2756 goto end;
2757 }
2758
2759 if (blocking_timeout < 0 && blocking_timeout != -1) {
2760 ret = -LTTNG_ERR_INVALID;
2761 goto end;
2762 }
2763
2764 /*
2765 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2766 * us to accept a narrower range of values (msecs expressed as a signed
2767 * 32-bit integer).
2768 */
2769 msec_timeout = blocking_timeout / 1000;
2770 if (msec_timeout != (int32_t) msec_timeout) {
2771 ret = -LTTNG_ERR_INVALID;
2772 goto end;
2773 }
2774
2775 ((struct lttng_channel_extended *)
2776 chan->attr.extended.ptr)->blocking_timeout =
2777 blocking_timeout;
2778end:
2779 return ret;
2780}
2781
fac6795d 2782/*
2269e89e 2783 * Check if session daemon is alive.
fac6795d 2784 *
2269e89e 2785 * Return 1 if alive or 0 if not.
1c8d13c8 2786 * On error returns a negative value.
fac6795d 2787 */
947308c4 2788int lttng_session_daemon_alive(void)
fac6795d
DG
2789{
2790 int ret;
2791
2792 ret = set_session_daemon_path();
2793 if (ret < 0) {
9ae110e2 2794 /* Error. */
fac6795d
DG
2795 return ret;
2796 }
2797
9d035200 2798 if (*sessiond_sock_path == '\0') {
2f70b271 2799 /*
9ae110e2
JG
2800 * No socket path set. Weird error which means the constructor
2801 * was not called.
2f70b271
DG
2802 */
2803 assert(0);
fac6795d
DG
2804 }
2805
2269e89e 2806 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 2807 if (ret < 0) {
9ae110e2 2808 /* Not alive. */
7d8234d9
MD
2809 return 0;
2810 }
7d8234d9 2811
9ae110e2 2812 /* Is alive. */
947308c4 2813 return 1;
fac6795d
DG
2814}
2815
00e2e675 2816/*
a4b92340 2817 * Set URL for a consumer for a session and domain.
00e2e675
DG
2818 *
2819 * Return 0 on success, else a negative value.
2820 */
a4b92340
DG
2821int lttng_set_consumer_url(struct lttng_handle *handle,
2822 const char *control_url, const char *data_url)
00e2e675 2823{
3dd05a85 2824 int ret;
a4b92340 2825 ssize_t size;
00e2e675 2826 struct lttcomm_session_msg lsm;
a4b92340 2827 struct lttng_uri *uris = NULL;
00e2e675 2828
a4b92340 2829 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
1da0efb1
JG
2830 ret = -LTTNG_ERR_INVALID;
2831 goto error;
00e2e675
DG
2832 }
2833
a4b92340
DG
2834 memset(&lsm, 0, sizeof(lsm));
2835
00e2e675
DG
2836 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2837
1da0efb1 2838 ret = lttng_strncpy(lsm.session.name, handle->session_name,
00e2e675 2839 sizeof(lsm.session.name));
1da0efb1
JG
2840 if (ret) {
2841 ret = -LTTNG_ERR_INVALID;
2842 goto error;
2843 }
2844
60160d2a 2845 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
00e2e675 2846
bc894455 2847 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 2848 if (size < 0) {
1da0efb1
JG
2849 ret = -LTTNG_ERR_INVALID;
2850 goto error;
a4b92340
DG
2851 }
2852
2853 lsm.u.uri.size = size;
00e2e675 2854
795a978d 2855 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2856 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2857
2858 free(uris);
1da0efb1 2859error:
3dd05a85 2860 return ret;
00e2e675
DG
2861}
2862
2863/*
9c6bda17 2864 * [OBSOLETE]
00e2e675 2865 */
2ec40111 2866int lttng_enable_consumer(struct lttng_handle *handle);
00e2e675
DG
2867int lttng_enable_consumer(struct lttng_handle *handle)
2868{
785d2d0d 2869 return -ENOSYS;
00e2e675
DG
2870}
2871
2872/*
9c6bda17 2873 * [OBSOLETE]
00e2e675 2874 */
2ec40111 2875int lttng_disable_consumer(struct lttng_handle *handle);
00e2e675
DG
2876int lttng_disable_consumer(struct lttng_handle *handle)
2877{
785d2d0d 2878 return -ENOSYS;
00e2e675
DG
2879}
2880
07424f16 2881/*
b178f53e 2882 * [OBSOLETE]
07424f16 2883 */
2ec40111
SM
2884int _lttng_create_session_ext(const char *name, const char *url,
2885 const char *datetime);
07424f16
DG
2886int _lttng_create_session_ext(const char *name, const char *url,
2887 const char *datetime)
2888{
b178f53e 2889 return -ENOSYS;
07424f16
DG
2890}
2891
806e2684
DG
2892/*
2893 * For a given session name, this call checks if the data is ready to be read
2894 * or is still being extracted by the consumer(s) hence not ready to be used by
2895 * any readers.
2896 */
6d805429 2897int lttng_data_pending(const char *session_name)
806e2684
DG
2898{
2899 int ret;
2900 struct lttcomm_session_msg lsm;
f6151c55 2901 uint8_t *pending = NULL;
806e2684
DG
2902
2903 if (session_name == NULL) {
2904 return -LTTNG_ERR_INVALID;
2905 }
2906
53efb85a 2907 memset(&lsm, 0, sizeof(lsm));
6d805429 2908 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2909
1da0efb1 2910 ret = lttng_strncpy(lsm.session.name, session_name,
cac3069d 2911 sizeof(lsm.session.name));
1da0efb1
JG
2912 if (ret) {
2913 ret = -LTTNG_ERR_INVALID;
2914 goto end;
2915 }
806e2684 2916
f6151c55
JG
2917 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2918 if (ret < 0) {
2919 goto end;
2920 } else if (ret != 1) {
2921 /* Unexpected payload size */
2922 ret = -LTTNG_ERR_INVALID;
2923 goto end;
e848d36d
JG
2924 } else if (!pending) {
2925 /* Internal error. */
2926 ret = -LTTNG_ERR_UNK;
2927 goto end;
806e2684
DG
2928 }
2929
f6151c55
JG
2930 ret = (int) *pending;
2931end:
2932 free(pending);
806e2684
DG
2933 return ret;
2934}
2935
93ec662e
JD
2936/*
2937 * Regenerate the metadata for a session.
2938 * Return 0 on success, a negative error code on error.
2939 */
eded6438 2940int lttng_regenerate_metadata(const char *session_name)
93ec662e
JD
2941{
2942 int ret;
2943 struct lttcomm_session_msg lsm;
2944
2945 if (!session_name) {
2946 ret = -LTTNG_ERR_INVALID;
2947 goto end;
2948 }
2949
2950 memset(&lsm, 0, sizeof(lsm));
eded6438 2951 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
93ec662e 2952
1da0efb1 2953 ret = lttng_strncpy(lsm.session.name, session_name,
93ec662e 2954 sizeof(lsm.session.name));
1da0efb1
JG
2955 if (ret) {
2956 ret = -LTTNG_ERR_INVALID;
2957 goto end;
2958 }
93ec662e
JD
2959
2960 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2961 if (ret < 0) {
2962 goto end;
2963 }
2964
2965 ret = 0;
2966end:
2967 return ret;
2968}
2969
eded6438
JD
2970/*
2971 * Deprecated, replaced by lttng_regenerate_metadata.
2972 */
2973int lttng_metadata_regenerate(const char *session_name)
2974{
2975 return lttng_regenerate_metadata(session_name);
2976}
2977
c2561365
JD
2978/*
2979 * Regenerate the statedump of a session.
2980 * Return 0 on success, a negative error code on error.
2981 */
2982int lttng_regenerate_statedump(const char *session_name)
2983{
2984 int ret;
2985 struct lttcomm_session_msg lsm;
2986
2987 if (!session_name) {
2988 ret = -LTTNG_ERR_INVALID;
2989 goto end;
2990 }
2991
2992 memset(&lsm, 0, sizeof(lsm));
2993 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2994
1da0efb1 2995 ret = lttng_strncpy(lsm.session.name, session_name,
c2561365 2996 sizeof(lsm.session.name));
1da0efb1
JG
2997 if (ret) {
2998 ret = -LTTNG_ERR_INVALID;
2999 goto end;
3000 }
c2561365
JD
3001
3002 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3003 if (ret < 0) {
3004 goto end;
3005 }
3006
3007 ret = 0;
3008end:
3009 return ret;
3010}
3011
a58c490f
JG
3012int lttng_register_trigger(struct lttng_trigger *trigger)
3013{
3014 int ret;
3015 struct lttcomm_session_msg lsm;
3647288f 3016 struct lttng_dynamic_buffer buffer;
a58c490f 3017
3647288f 3018 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
3019 if (!trigger) {
3020 ret = -LTTNG_ERR_INVALID;
3021 goto end;
3022 }
3023
3024 if (!lttng_trigger_validate(trigger)) {
eac4828d 3025 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
3026 goto end;
3027 }
3028
3647288f
JG
3029 ret = lttng_trigger_serialize(trigger, &buffer);
3030 if (ret < 0) {
a58c490f
JG
3031 ret = -LTTNG_ERR_UNK;
3032 goto end;
3033 }
3034
a58c490f
JG
3035 memset(&lsm, 0, sizeof(lsm));
3036 lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
3647288f
JG
3037 lsm.u.trigger.length = (uint32_t) buffer.size;
3038 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
3039 buffer.size, NULL);
a58c490f 3040end:
3647288f 3041 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
3042 return ret;
3043}
3044
3045int lttng_unregister_trigger(struct lttng_trigger *trigger)
3046{
3047 int ret;
3048 struct lttcomm_session_msg lsm;
3647288f 3049 struct lttng_dynamic_buffer buffer;
a58c490f 3050
3647288f 3051 lttng_dynamic_buffer_init(&buffer);
a58c490f
JG
3052 if (!trigger) {
3053 ret = -LTTNG_ERR_INVALID;
3054 goto end;
3055 }
3056
3057 if (!lttng_trigger_validate(trigger)) {
3647288f 3058 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
3059 goto end;
3060 }
3061
3647288f
JG
3062 ret = lttng_trigger_serialize(trigger, &buffer);
3063 if (ret < 0) {
a58c490f
JG
3064 ret = -LTTNG_ERR_UNK;
3065 goto end;
3066 }
3067
a58c490f
JG
3068 memset(&lsm, 0, sizeof(lsm));
3069 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3647288f
JG
3070 lsm.u.trigger.length = (uint32_t) buffer.size;
3071 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
3072 buffer.size, NULL);
a58c490f 3073end:
3647288f 3074 lttng_dynamic_buffer_reset(&buffer);
a58c490f
JG
3075 return ret;
3076}
3077
fac6795d 3078/*
9ae110e2 3079 * lib constructor.
fac6795d 3080 */
b2b89e8a 3081static void __attribute__((constructor)) init(void)
fac6795d
DG
3082{
3083 /* Set default session group */
bbccc3d2 3084 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 3085}
49cca668
DG
3086
3087/*
9ae110e2 3088 * lib destructor.
49cca668 3089 */
b2b89e8a 3090static void __attribute__((destructor)) lttng_ctl_exit(void)
49cca668
DG
3091{
3092 free(tracing_group);
3093}
This page took 0.286854 seconds and 5 git commands to generate.