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