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