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