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