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