lttng-ctl: separate support of named/unnamed trigger registration
[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
08a9c49f 391 if ((uid == 0) || in_tgroup) {
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
1207 exclusion_len = lttng_strnlen(*(exclusion_list + i),
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
JG
1215 ret = lttng_dynamic_buffer_append(&payload.buffer,
1216 *(exclusion_list + i), LTTNG_SYMBOL_NAME_LEN);
15e37663
JG
1217 if (ret) {
1218 goto mem_error;
1219 }
6b453b5e 1220 }
15e37663 1221
9ae110e2 1222 /* Add filter expression next. */
15e37663 1223 if (filter_expression) {
e368fb43 1224 ret = lttng_dynamic_buffer_append(&payload.buffer,
15e37663
JG
1225 filter_expression, lsm.u.enable.expression_len);
1226 if (ret) {
1227 goto mem_error;
1228 }
6b453b5e 1229 }
9ae110e2 1230 /* Add filter bytecode next. */
137b9942 1231 if (ctx && lsm.u.enable.bytecode_len != 0) {
e368fb43 1232 ret = lttng_dynamic_buffer_append(&payload.buffer,
15e37663
JG
1233 &ctx->bytecode->b, lsm.u.enable.bytecode_len);
1234 if (ret) {
1235 goto mem_error;
1236 }
1237 }
1238 if (ev->extended.ptr) {
1239 struct lttng_event_extended *ev_ext =
1240 (struct lttng_event_extended *) ev->extended.ptr;
1241
1242 if (ev_ext->probe_location) {
1243 /*
1244 * lttng_userspace_probe_location_serialize returns the
1245 * number of bytes that was appended to the buffer.
1246 */
1247 ret = lttng_userspace_probe_location_serialize(
e368fb43 1248 ev_ext->probe_location, &payload);
56f0bc67 1249 if (ret < 0) {
15e37663
JG
1250 goto mem_error;
1251 }
1252
15e37663
JG
1253 /*
1254 * Set the size of the userspace probe location element
1255 * of the buffer so that the receiving side knows where
1256 * to split it.
1257 */
1258 lsm.u.enable.userspace_probe_location_len = ret;
1259 }
93deb080
JI
1260 }
1261
e368fb43
JG
1262 {
1263 struct lttng_payload_view view = lttng_payload_view_from_payload(
1264 &payload, 0, -1);
fe489250 1265 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
e368fb43
JG
1266 int fd_to_send;
1267
1268 if (fd_count < 0) {
1269 goto mem_error;
1270 }
1271
1272 assert(fd_count == 0 || fd_count == 1);
1273 if (fd_count == 1) {
e2fc8e9d 1274 struct fd_handle *h =
fe489250
JG
1275 lttng_payload_view_pop_fd_handle(&view);
1276
e2fc8e9d 1277 if (!h) {
e368fb43
JG
1278 goto mem_error;
1279 }
1280
e2fc8e9d
SM
1281 fd_to_send = fd_handle_get_fd(h);
1282 fd_handle_put(h);
e368fb43
JG
1283 }
1284
1285 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1286 fd_count ? &fd_to_send : NULL, fd_count,
1287 view.buffer.size ? view.buffer.data : NULL,
1288 view.buffer.size, NULL, NULL, 0);
1289 }
93deb080 1290
7ca1dc6f
DG
1291mem_error:
1292 if (filter_expression && ctx) {
93deb080
JI
1293 filter_bytecode_free(ctx);
1294 filter_ir_free(ctx);
1295 filter_parser_ctx_free(ctx);
7ca1dc6f
DG
1296 }
1297filter_error:
1298 if (free_filter_expression) {
1299 /*
9ae110e2
JG
1300 * The filter expression has been replaced and must be freed as
1301 * it is not the original filter expression received as a
1302 * parameter.
7ca1dc6f
DG
1303 */
1304 free(filter_expression);
93deb080 1305 }
137b9942
DG
1306error:
1307 /*
9ae110e2
JG
1308 * Return directly to the caller and don't ask the sessiond since
1309 * something went wrong in the parsing of data above.
137b9942 1310 */
e368fb43 1311 lttng_payload_reset(&payload);
93deb080 1312 return ret;
3e1c9ff7
DG
1313
1314ask_sessiond:
1315 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1316 return ret;
93deb080
JI
1317}
1318
6e911cad
MD
1319int lttng_disable_event_ext(struct lttng_handle *handle,
1320 struct lttng_event *ev, const char *channel_name,
1321 const char *original_filter_expression)
1df4dedd 1322{
cd80958d 1323 struct lttcomm_session_msg lsm;
6e911cad
MD
1324 char *varlen_data;
1325 int ret = 0;
1326 unsigned int free_filter_expression = 0;
1327 struct filter_parser_ctx *ctx = NULL;
1328 /*
1329 * Cast as non-const since we may replace the filter expression
1330 * by a dynamically allocated string. Otherwise, the original
1331 * string is not modified.
1332 */
1333 char *filter_expression = (char *) original_filter_expression;
1df4dedd 1334
6e911cad
MD
1335 if (handle == NULL || ev == NULL) {
1336 ret = -LTTNG_ERR_INVALID;
1337 goto error;
1338 }
1339
9ae110e2
JG
1340 /*
1341 * Empty filter string will always be rejected by the parser
6e911cad
MD
1342 * anyway, so treat this corner-case early to eliminate
1343 * lttng_fmemopen error for 0-byte allocation.
1344 */
1345 if (filter_expression && filter_expression[0] == '\0') {
1346 ret = -LTTNG_ERR_INVALID;
1347 goto error;
cd80958d
DG
1348 }
1349
441c16a7
MD
1350 memset(&lsm, 0, sizeof(lsm));
1351
85076754 1352 /* If no channel name, send empty string. */
e1b624d0
JG
1353 ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "",
1354 sizeof(lsm.u.disable.channel_name));
1355 if (ret) {
1356 ret = -LTTNG_ERR_INVALID;
1357 goto error;
eb354453
DG
1358 }
1359
18a720cd 1360 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f3ed775e 1361
60160d2a 1362 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
6e911cad
MD
1363 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1364
e1b624d0 1365 ret = lttng_strncpy(lsm.session.name, handle->session_name,
cd80958d 1366 sizeof(lsm.session.name));
e1b624d0
JG
1367 if (ret) {
1368 ret = -LTTNG_ERR_INVALID;
1369 goto error;
1370 }
1371
6e911cad 1372 lsm.u.disable.bytecode_len = 0;
cd80958d 1373
6e911cad
MD
1374 /*
1375 * For the JUL domain, a filter is enforced except for the
1376 * disable all event. This is done to avoid having the event in
1377 * all sessions thus filtering by logger name.
1378 */
1379 if (filter_expression == NULL &&
1380 (handle->domain.type != LTTNG_DOMAIN_JUL &&
0e115563
DG
1381 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1382 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
6e911cad
MD
1383 goto ask_sessiond;
1384 }
1385
1386 /*
1387 * We have a filter, so we need to set up a variable-length
1388 * memory block from where to send the data.
1389 */
1390
1391 /* Parse filter expression */
1392 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
0e115563
DG
1393 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1394 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
6e911cad 1395 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
0e115563
DG
1396 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1397 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1398 char *agent_filter;
6e911cad
MD
1399
1400 /* Setup JUL filter if needed. */
0e115563
DG
1401 agent_filter = set_agent_filter(filter_expression, ev);
1402 if (!agent_filter) {
6e911cad 1403 if (!filter_expression) {
9ae110e2
JG
1404 /*
1405 * No JUL and no filter, just skip
1406 * everything below.
1407 */
6e911cad
MD
1408 goto ask_sessiond;
1409 }
1410 } else {
1411 /*
9ae110e2
JG
1412 * With a JUL filter, the original filter has
1413 * been added to it thus replace the filter
1414 * expression.
6e911cad 1415 */
0e115563 1416 filter_expression = agent_filter;
6e911cad
MD
1417 free_filter_expression = 1;
1418 }
1419 }
1420
e4d2f27a 1421 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
6e911cad
MD
1422 if (ret) {
1423 goto filter_error;
1424 }
e4d2f27a
JR
1425
1426 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
1427 + bytecode_get_len(&ctx->bytecode->b);
1428 lsm.u.enable.expression_len = strlen(filter_expression) + 1;
6e911cad
MD
1429 }
1430
1431 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1432 + lsm.u.disable.expression_len);
1433 if (!varlen_data) {
1434 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1435 goto mem_error;
1436 }
1437
9ae110e2 1438 /* Add filter expression. */
6e911cad
MD
1439 if (lsm.u.disable.expression_len != 0) {
1440 memcpy(varlen_data,
1441 filter_expression,
1442 lsm.u.disable.expression_len);
1443 }
9ae110e2 1444 /* Add filter bytecode next. */
6e911cad
MD
1445 if (ctx && lsm.u.disable.bytecode_len != 0) {
1446 memcpy(varlen_data
1447 + lsm.u.disable.expression_len,
1448 &ctx->bytecode->b,
1449 lsm.u.disable.bytecode_len);
1450 }
1451
795a978d 1452 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
6e911cad
MD
1453 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1454 free(varlen_data);
1455
1456mem_error:
1457 if (filter_expression && ctx) {
1458 filter_bytecode_free(ctx);
1459 filter_ir_free(ctx);
1460 filter_parser_ctx_free(ctx);
1461 }
1462filter_error:
1463 if (free_filter_expression) {
1464 /*
9ae110e2
JG
1465 * The filter expression has been replaced and must be freed as
1466 * it is not the original filter expression received as a
1467 * parameter.
6e911cad
MD
1468 */
1469 free(filter_expression);
1470 }
1471error:
1472 /*
9ae110e2
JG
1473 * Return directly to the caller and don't ask the sessiond since
1474 * something went wrong in the parsing of data above.
6e911cad
MD
1475 */
1476 return ret;
1477
1478ask_sessiond:
1479 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1480 return ret;
1481}
1482
1483/*
9ae110e2
JG
1484 * Disable event(s) of a channel and domain.
1485 * If no event name is specified, all events are disabled.
1486 * If no channel name is specified, the default 'channel0' is used.
1487 * Returns size of returned session payload data or a negative error code.
6e911cad
MD
1488 */
1489int lttng_disable_event(struct lttng_handle *handle, const char *name,
1490 const char *channel_name)
1491{
e1b624d0 1492 int ret;
6e911cad
MD
1493 struct lttng_event ev;
1494
1495 memset(&ev, 0, sizeof(ev));
9b7431cf 1496 ev.loglevel = -1;
6e911cad 1497 ev.type = LTTNG_EVENT_ALL;
e1b624d0
JG
1498 ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name));
1499 if (ret) {
1500 ret = -LTTNG_ERR_INVALID;
1501 goto end;
1502 }
1503
1504 ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1505end:
1506 return ret;
1df4dedd
DG
1507}
1508
cf0bcb51
JG
1509struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1510{
1511 struct lttng_channel *channel = NULL;
1512 struct lttng_channel_extended *extended = NULL;
1513
1514 if (!domain) {
1515 goto error;
1516 }
1517
1518 /* Validate domain. */
1519 switch (domain->type) {
1520 case LTTNG_DOMAIN_UST:
1521 switch (domain->buf_type) {
1522 case LTTNG_BUFFER_PER_UID:
1523 case LTTNG_BUFFER_PER_PID:
1524 break;
1525 default:
1526 goto error;
1527 }
1528 break;
1529 case LTTNG_DOMAIN_KERNEL:
1530 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1531 goto error;
1532 }
1533 break;
1534 default:
1535 goto error;
1536 }
1537
1538 channel = zmalloc(sizeof(*channel));
1539 if (!channel) {
1540 goto error;
1541 }
1542
1543 extended = zmalloc(sizeof(*extended));
1544 if (!extended) {
1545 goto error;
1546 }
1547
1548 channel->attr.extended.ptr = extended;
1549
1550 lttng_channel_set_default_attr(domain, &channel->attr);
1551 return channel;
1552error:
1553 free(channel);
1554 free(extended);
1555 return NULL;
1556}
1557
1558void lttng_channel_destroy(struct lttng_channel *channel)
1559{
1560 if (!channel) {
1561 return;
1562 }
1563
1564 if (channel->attr.extended.ptr) {
1565 free(channel->attr.extended.ptr);
1566 }
1567 free(channel);
1568}
1569
1df4dedd 1570/*
9ae110e2
JG
1571 * Enable channel per domain
1572 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 1573 */
cd80958d 1574int lttng_enable_channel(struct lttng_handle *handle,
cf0bcb51 1575 struct lttng_channel *in_chan)
a5c5a2bd 1576{
e1b624d0 1577 int ret;
cd80958d 1578 struct lttcomm_session_msg lsm;
09b72f7a 1579 size_t total_buffer_size_needed_per_cpu = 0;
cd80958d 1580
9ae110e2 1581 /* NULL arguments are forbidden. No default values. */
cf0bcb51 1582 if (handle == NULL || in_chan == NULL) {
2f70b271 1583 return -LTTNG_ERR_INVALID;
cd80958d
DG
1584 }
1585
441c16a7 1586 memset(&lsm, 0, sizeof(lsm));
cf0bcb51
JG
1587 memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan));
1588 lsm.u.channel.chan.attr.extended.ptr = NULL;
441c16a7 1589
cf0bcb51
JG
1590 if (!in_chan->attr.extended.ptr) {
1591 struct lttng_channel *channel;
1592 struct lttng_channel_extended *extended;
7d29a247 1593
cf0bcb51
JG
1594 channel = lttng_channel_create(&handle->domain);
1595 if (!channel) {
1596 return -LTTNG_ERR_NOMEM;
1597 }
1598
1599 /*
1600 * Create a new channel in order to use default extended
1601 * attribute values.
1602 */
1603 extended = (struct lttng_channel_extended *)
1604 channel->attr.extended.ptr;
1605 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1606 lttng_channel_destroy(channel);
1607 } else {
1608 struct lttng_channel_extended *extended;
1609
1610 extended = (struct lttng_channel_extended *)
1611 in_chan->attr.extended.ptr;
1612 memcpy(&lsm.u.channel.extended, extended, sizeof(*extended));
1613 }
cd80958d 1614
09b72f7a
FD
1615 /*
1616 * Verify that the amount of memory required to create the requested
1617 * buffer is available on the system at the moment.
1618 */
1619 total_buffer_size_needed_per_cpu = lsm.u.channel.chan.attr.num_subbuf *
1620 lsm.u.channel.chan.attr.subbuf_size;
1621 if (!check_enough_available_memory(total_buffer_size_needed_per_cpu)) {
1622 return -LTTNG_ERR_NOMEM;
1623 }
1624
cf0bcb51 1625 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
60160d2a 1626 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
7d29a247 1627
e1b624d0
JG
1628 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1629 sizeof(lsm.session.name));
1630 if (ret) {
1631 ret = -LTTNG_ERR_INVALID;
1632 goto end;
1633 }
cd80958d 1634
e1b624d0
JG
1635 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1636end:
1637 return ret;
8c0faa1d 1638}
1df4dedd 1639
2ef84c95 1640/*
9ae110e2
JG
1641 * All tracing will be stopped for registered events of the channel.
1642 * Returns size of returned session payload data or a negative error code.
2ef84c95 1643 */
cd80958d 1644int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 1645{
e1b624d0 1646 int ret;
cd80958d
DG
1647 struct lttcomm_session_msg lsm;
1648
9ae110e2 1649 /* Safety check. Both are mandatory. */
9d697d3d 1650 if (handle == NULL || name == NULL) {
2f70b271 1651 return -LTTNG_ERR_INVALID;
cd80958d
DG
1652 }
1653
441c16a7
MD
1654 memset(&lsm, 0, sizeof(lsm));
1655
cd80958d 1656 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1657
e1b624d0 1658 ret = lttng_strncpy(lsm.u.disable.channel_name, name,
9d697d3d 1659 sizeof(lsm.u.disable.channel_name));
e1b624d0
JG
1660 if (ret) {
1661 ret = -LTTNG_ERR_INVALID;
1662 goto end;
1663 }
9d697d3d 1664
60160d2a 1665 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
cd80958d 1666
e1b624d0
JG
1667 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1668 sizeof(lsm.session.name));
1669 if (ret) {
1670 ret = -LTTNG_ERR_INVALID;
1671 goto end;
1672 }
cd80958d 1673
e1b624d0
JG
1674 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1675end:
1676 return ret;
ca95a216
DG
1677}
1678
fac6795d 1679/*
9ae110e2
JG
1680 * Lists all available tracepoints of domain.
1681 * Sets the contents of the events array.
1682 * Returns the number of lttng_event entries in events;
1683 * on error, returns a negative value.
fac6795d 1684 */
cd80958d 1685int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1686 struct lttng_event **events)
fac6795d 1687{
052da939 1688 int ret;
cd80958d
DG
1689 struct lttcomm_session_msg lsm;
1690
9d697d3d 1691 if (handle == NULL) {
2f70b271 1692 return -LTTNG_ERR_INVALID;
cd80958d 1693 }
fac6795d 1694
53efb85a 1695 memset(&lsm, 0, sizeof(lsm));
cd80958d 1696 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
60160d2a 1697 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2a71efd5 1698
cac3069d 1699 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
052da939
DG
1700 if (ret < 0) {
1701 return ret;
eb354453 1702 }
fac6795d 1703
9f19cc17 1704 return ret / sizeof(struct lttng_event);
fac6795d
DG
1705}
1706
f37d259d 1707/*
9ae110e2
JG
1708 * Lists all available tracepoint fields of domain.
1709 * Sets the contents of the event field array.
1710 * Returns the number of lttng_event_field entries in events;
1711 * on error, returns a negative value.
f37d259d
MD
1712 */
1713int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1714 struct lttng_event_field **fields)
1715{
1716 int ret;
1717 struct lttcomm_session_msg lsm;
1718
1719 if (handle == NULL) {
2f70b271 1720 return -LTTNG_ERR_INVALID;
f37d259d
MD
1721 }
1722
53efb85a 1723 memset(&lsm, 0, sizeof(lsm));
f37d259d 1724 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
60160d2a 1725 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
f37d259d 1726
cac3069d 1727 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
f37d259d
MD
1728 if (ret < 0) {
1729 return ret;
1730 }
1731
1732 return ret / sizeof(struct lttng_event_field);
1733}
1734
834978fd 1735/*
9ae110e2
JG
1736 * Lists all available kernel system calls. Allocates and sets the contents of
1737 * the events array.
834978fd 1738 *
9ae110e2
JG
1739 * Returns the number of lttng_event entries in events; on error, returns a
1740 * negative value.
834978fd
DG
1741 */
1742int lttng_list_syscalls(struct lttng_event **events)
1743{
1744 int ret;
1745 struct lttcomm_session_msg lsm;
1746
1747 if (!events) {
1748 return -LTTNG_ERR_INVALID;
1749 }
1750
1751 memset(&lsm, 0, sizeof(lsm));
1752 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1753 /* Force kernel domain for system calls. */
1754 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1755
1756 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1757 if (ret < 0) {
1758 return ret;
1759 }
1760
1761 return ret / sizeof(struct lttng_event);
1762}
1763
1657e9bb 1764/*
9ae110e2
JG
1765 * Returns a human readable string describing
1766 * the error code (a negative value).
1657e9bb 1767 */
9a745bc7 1768const char *lttng_strerror(int code)
1657e9bb 1769{
f73fabfd 1770 return error_get_str(code);
1657e9bb
DG
1771}
1772
b178f53e
JG
1773enum lttng_error_code lttng_create_session_ext(
1774 struct lttng_session_descriptor *session_descriptor)
1775{
1776 enum lttng_error_code ret_code;
1777 struct lttcomm_session_msg lsm = {
1778 .cmd_type = LTTNG_CREATE_SESSION_EXT,
1779 };
1780 void *reply = NULL;
1781 struct lttng_buffer_view reply_view;
1782 int reply_ret;
1783 bool sessiond_must_generate_ouput;
1784 struct lttng_dynamic_buffer payload;
1785 int ret;
1786 size_t descriptor_size;
1787 struct lttng_session_descriptor *descriptor_reply = NULL;
1788
1789 lttng_dynamic_buffer_init(&payload);
1790 if (!session_descriptor) {
1791 ret_code = LTTNG_ERR_INVALID;
1792 goto end;
1793 }
1794
1795 sessiond_must_generate_ouput =
1796 !lttng_session_descriptor_is_output_destination_initialized(
1797 session_descriptor);
1798 if (sessiond_must_generate_ouput) {
1799 const char *home_dir = utils_get_home_dir();
1800 size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0;
1801
1802 if (!home_dir || home_dir_len > LTTNG_PATH_MAX) {
1803 ret_code = LTTNG_ERR_FATAL;
1804 goto end;
1805 }
1806
1807 lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len;
1808 ret = lttng_dynamic_buffer_append(&payload, home_dir,
1809 home_dir_len);
1810 if (ret) {
1811 ret_code = LTTNG_ERR_NOMEM;
1812 goto end;
1813 }
1814 }
1815
1816 descriptor_size = payload.size;
1817 ret = lttng_session_descriptor_serialize(session_descriptor,
1818 &payload);
1819 if (ret) {
1820 ret_code = LTTNG_ERR_INVALID;
1821 goto end;
1822 }
1823 descriptor_size = payload.size - descriptor_size;
1824 lsm.u.create_session.session_descriptor_size = descriptor_size;
1825
1826 /* Command returns a session descriptor on success. */
1827 reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data,
1828 payload.size, &reply);
1829 if (reply_ret < 0) {
1830 ret_code = -reply_ret;
1831 goto end;
1832 } else if (reply_ret == 0) {
1833 /* Socket unexpectedly closed by the session daemon. */
1834 ret_code = LTTNG_ERR_FATAL;
1835 goto end;
1836 }
1837
1838 reply_view = lttng_buffer_view_init(reply, 0, reply_ret);
1839 ret = lttng_session_descriptor_create_from_buffer(&reply_view,
1840 &descriptor_reply);
1841 if (ret < 0) {
1842 ret_code = LTTNG_ERR_FATAL;
1843 goto end;
1844 }
1845 ret_code = LTTNG_OK;
1846 lttng_session_descriptor_assign(session_descriptor, descriptor_reply);
1847end:
1848 free(reply);
1849 lttng_dynamic_buffer_reset(&payload);
1850 lttng_session_descriptor_destroy(descriptor_reply);
1851 return ret_code;
1852}
1853
aaf97519 1854/*
b178f53e 1855 * Create a new session using name and url for destination.
a4b92340 1856 *
780d4bb8 1857 * Return 0 on success else a negative LTTng error code.
00e2e675 1858 */
a4b92340 1859int lttng_create_session(const char *name, const char *url)
00e2e675 1860{
3dd05a85 1861 int ret;
a4b92340 1862 ssize_t size;
a4b92340 1863 struct lttng_uri *uris = NULL;
b178f53e
JG
1864 struct lttng_session_descriptor *descriptor = NULL;
1865 enum lttng_error_code ret_code;
00e2e675 1866
b178f53e
JG
1867 if (!name) {
1868 ret = -LTTNG_ERR_INVALID;
1869 goto end;
00e2e675
DG
1870 }
1871
bc894455 1872 size = uri_parse_str_urls(url, NULL, &uris);
a4b92340 1873 if (size < 0) {
b178f53e
JG
1874 ret = -LTTNG_ERR_INVALID;
1875 goto end;
1876 }
1877 switch (size) {
1878 case 0:
1879 descriptor = lttng_session_descriptor_create(name);
1880 break;
1881 case 1:
1882 if (uris[0].dtype != LTTNG_DST_PATH) {
1883 ret = -LTTNG_ERR_INVALID;
1884 goto end;
1885 }
1886 descriptor = lttng_session_descriptor_local_create(name,
1887 uris[0].dst.path);
1888 break;
1889 case 2:
1890 descriptor = lttng_session_descriptor_network_create(name, url,
1891 NULL);
1892 break;
1893 default:
1894 ret = -LTTNG_ERR_INVALID;
1895 goto end;
1896 }
1897 if (!descriptor) {
1898 ret = -LTTNG_ERR_INVALID;
1899 goto end;
00e2e675 1900 }
b178f53e
JG
1901 ret_code = lttng_create_session_ext(descriptor);
1902 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
1903end:
1904 lttng_session_descriptor_destroy(descriptor);
1905 free(uris);
1906 return ret;
1907}
00e2e675 1908
b178f53e
JG
1909/*
1910 * Create a session exclusively used for snapshot.
1911 *
780d4bb8 1912 * Return 0 on success else a negative LTTng error code.
b178f53e
JG
1913 */
1914int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
1915{
1916 int ret;
1917 enum lttng_error_code ret_code;
1918 ssize_t size;
1919 struct lttng_uri *uris = NULL;
1920 struct lttng_session_descriptor *descriptor = NULL;
a4b92340 1921
b178f53e
JG
1922 if (!name) {
1923 ret = -LTTNG_ERR_INVALID;
1924 goto end;
1925 }
1926
1927 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
1928 if (size < 0) {
1929 ret = -LTTNG_ERR_INVALID;
1930 goto end;
1931 }
1932 /*
1933 * If the user does not specify a custom subdir, use the session name.
1934 */
1935 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH &&
1936 strlen(uris[0].subdir) == 0) {
1937 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s",
1938 name);
1939 if (ret < 0) {
1940 PERROR("Failed to set session name as network destination sub-directory");
1941 ret = -LTTNG_ERR_FATAL;
1942 goto end;
1943 } else if (ret >= sizeof(uris[0].subdir)) {
1944 /* Truncated output. */
1945 ret = -LTTNG_ERR_INVALID;
1946 goto end;
1947 }
1948 }
3dd05a85 1949
b178f53e
JG
1950 switch (size) {
1951 case 0:
1952 descriptor = lttng_session_descriptor_snapshot_create(name);
1953 break;
1954 case 1:
1955 if (uris[0].dtype != LTTNG_DST_PATH) {
1956 ret = -LTTNG_ERR_INVALID;
1957 goto end;
1958 }
1959 descriptor = lttng_session_descriptor_snapshot_local_create(
1960 name,
1961 uris[0].dst.path);
1962 break;
1963 case 2:
1964 descriptor = lttng_session_descriptor_snapshot_network_create(
1965 name,
1966 snapshot_url,
1967 NULL);
1968 break;
1969 default:
1970 ret = -LTTNG_ERR_INVALID;
1971 goto end;
1972 }
1973 if (!descriptor) {
1974 ret = -LTTNG_ERR_INVALID;
1975 goto end;
1976 }
1977 ret_code = lttng_create_session_ext(descriptor);
1978 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
1979end:
1980 lttng_session_descriptor_destroy(descriptor);
3dd05a85
DG
1981 free(uris);
1982 return ret;
00e2e675
DG
1983}
1984
b178f53e
JG
1985/*
1986 * Create a session exclusively used for live.
1987 *
780d4bb8 1988 * Return 0 on success else a negative LTTng error code.
b178f53e
JG
1989 */
1990int lttng_create_session_live(const char *name, const char *url,
1991 unsigned int timer_interval)
1992{
1993 int ret;
1994 enum lttng_error_code ret_code;
1995 struct lttng_session_descriptor *descriptor = NULL;
1996
1997 if (!name) {
1998 ret = -LTTNG_ERR_INVALID;
1999 goto end;
2000 }
2001
2002 if (url) {
2003 descriptor = lttng_session_descriptor_live_network_create(
2004 name, url, NULL, timer_interval);
2005 } else {
2006 descriptor = lttng_session_descriptor_live_create(
2007 name, timer_interval);
2008 }
2009 if (!descriptor) {
2010 ret = -LTTNG_ERR_INVALID;
2011 goto end;
2012 }
2013 ret_code = lttng_create_session_ext(descriptor);
2014 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2015end:
2016 lttng_session_descriptor_destroy(descriptor);
2017 return ret;
2018}
2019
e20ee7c2
JD
2020/*
2021 * Stop the session and wait for the data before destroying it
780d4bb8
MD
2022 *
2023 * Return 0 on success else a negative LTTng error code.
e20ee7c2
JD
2024 */
2025int lttng_destroy_session(const char *session_name)
2026{
2027 int ret;
3e3665b8
JG
2028 enum lttng_error_code ret_code;
2029 enum lttng_destruction_handle_status status;
2030 struct lttng_destruction_handle *handle = NULL;
e20ee7c2
JD
2031
2032 /*
3e3665b8
JG
2033 * Stop the tracing and wait for the data to be
2034 * consumed.
e20ee7c2
JD
2035 */
2036 ret = _lttng_stop_tracing(session_name, 1);
2037 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
2038 goto end;
2039 }
2040
3e3665b8
JG
2041 ret_code = lttng_destroy_session_ext(session_name, &handle);
2042 if (ret_code != LTTNG_OK) {
2043 ret = (int) -ret_code;
2044 goto end;
2045 }
2046 assert(handle);
2047
2048 /* Block until the completion of the destruction of the session. */
2049 status = lttng_destruction_handle_wait_for_completion(handle, -1);
2050 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) {
2051 ret = -LTTNG_ERR_UNK;
2052 goto end;
2053 }
2054
2055 status = lttng_destruction_handle_get_result(handle, &ret_code);
2056 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
2057 ret = -LTTNG_ERR_UNK;
2058 goto end;
2059 }
780d4bb8 2060 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
e20ee7c2 2061end:
3e3665b8 2062 lttng_destruction_handle_destroy(handle);
e20ee7c2
JD
2063 return ret;
2064}
2065
2066/*
2067 * Destroy the session without waiting for the data.
2068 */
2069int lttng_destroy_session_no_wait(const char *session_name)
2070{
3e3665b8 2071 enum lttng_error_code ret_code;
e20ee7c2 2072
3e3665b8 2073 ret_code = lttng_destroy_session_ext(session_name, NULL);
3204017e 2074 return ret_code == LTTNG_OK ? 0 : -ret_code;
e20ee7c2
JD
2075}
2076
57167058 2077/*
9ae110e2
JG
2078 * Ask the session daemon for all available sessions.
2079 * Sets the contents of the sessions array.
2080 * Returns the number of lttng_session entries in sessions;
2081 * on error, returns a negative value.
57167058 2082 */
b178f53e 2083int lttng_list_sessions(struct lttng_session **out_sessions)
57167058 2084{
ca95a216 2085 int ret;
cd80958d 2086 struct lttcomm_session_msg lsm;
b178f53e
JG
2087 const size_t session_size = sizeof(struct lttng_session) +
2088 sizeof(struct lttng_session_extended);
2089 size_t session_count, i;
2090 struct lttng_session_extended *sessions_extended_begin;
2091 struct lttng_session *sessions = NULL;
57167058 2092
53efb85a 2093 memset(&lsm, 0, sizeof(lsm));
cd80958d 2094 lsm.cmd_type = LTTNG_LIST_SESSIONS;
b178f53e
JG
2095 ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions);
2096 if (ret <= 0) {
b178f53e
JG
2097 goto end;
2098 }
2099 if (!sessions) {
2100 ret = -LTTNG_ERR_FATAL;
2101 goto end;
2102 }
2103
2104 if (ret % session_size) {
2105 ret = -LTTNG_ERR_UNK;
2106 free(sessions);
2107 *out_sessions = NULL;
2108 goto end;
57167058 2109 }
b178f53e
JG
2110 session_count = (size_t) ret / session_size;
2111 sessions_extended_begin = (struct lttng_session_extended *)
2112 (&sessions[session_count]);
57167058 2113
b178f53e
JG
2114 /* Set extended session info pointers. */
2115 for (i = 0; i < session_count; i++) {
2116 struct lttng_session *session = &sessions[i];
2117 struct lttng_session_extended *extended =
2118 &(sessions_extended_begin[i]);
2119
2120 session->extended.ptr = extended;
2121 }
2122
2123 ret = (int) session_count;
2124 *out_sessions = sessions;
2125end:
2126 return ret;
2127}
2128
2129enum lttng_error_code lttng_session_get_creation_time(
2130 const struct lttng_session *session, uint64_t *creation_time)
2131{
2132 enum lttng_error_code ret = LTTNG_OK;
2133 struct lttng_session_extended *extended;
2134
2135 if (!session || !creation_time || !session->extended.ptr) {
2136 ret = LTTNG_ERR_INVALID;
2137 goto end;
2138 }
2139
2140 extended = session->extended.ptr;
2141 if (!extended->creation_time.is_set) {
2142 /* Not created on the session daemon yet. */
2143 ret = LTTNG_ERR_SESSION_NOT_EXIST;
2144 goto end;
2145 }
2146 *creation_time = extended->creation_time.value;
2147end:
2148 return ret;
57167058
DG
2149}
2150
d7ba1388
MD
2151int lttng_set_session_shm_path(const char *session_name,
2152 const char *shm_path)
2153{
e1b624d0 2154 int ret;
d7ba1388
MD
2155 struct lttcomm_session_msg lsm;
2156
2157 if (session_name == NULL) {
2158 return -LTTNG_ERR_INVALID;
2159 }
2160
2161 memset(&lsm, 0, sizeof(lsm));
2162 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
2163
e1b624d0 2164 ret = lttng_strncpy(lsm.session.name, session_name,
d7ba1388 2165 sizeof(lsm.session.name));
e1b624d0
JG
2166 if (ret) {
2167 ret = -LTTNG_ERR_INVALID;
2168 goto end;
2169 }
2170
2171 ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "",
d7ba1388 2172 sizeof(lsm.u.set_shm_path.shm_path));
e1b624d0
JG
2173 if (ret) {
2174 ret = -LTTNG_ERR_INVALID;
2175 goto end;
2176 }
d7ba1388 2177
e1b624d0
JG
2178 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2179end:
2180 return ret;
d7ba1388
MD
2181}
2182
9f19cc17 2183/*
9ae110e2
JG
2184 * Ask the session daemon for all available domains of a session.
2185 * Sets the contents of the domains array.
2186 * Returns the number of lttng_domain entries in domains;
2187 * on error, returns a negative value.
9f19cc17 2188 */
330be774 2189int lttng_list_domains(const char *session_name,
cd80958d 2190 struct lttng_domain **domains)
9f19cc17
DG
2191{
2192 int ret;
cd80958d
DG
2193 struct lttcomm_session_msg lsm;
2194
330be774 2195 if (session_name == NULL) {
e1b624d0
JG
2196 ret = -LTTNG_ERR_INVALID;
2197 goto error;
cd80958d 2198 }
9f19cc17 2199
53efb85a 2200 memset(&lsm, 0, sizeof(lsm));
cd80958d
DG
2201 lsm.cmd_type = LTTNG_LIST_DOMAINS;
2202
e1b624d0 2203 ret = lttng_strncpy(lsm.session.name, session_name,
cac3069d 2204 sizeof(lsm.session.name));
e1b624d0
JG
2205 if (ret) {
2206 ret = -LTTNG_ERR_INVALID;
2207 goto error;
2208 }
cd80958d 2209
cac3069d 2210 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
9f19cc17 2211 if (ret < 0) {
e1b624d0 2212 goto error;
9f19cc17
DG
2213 }
2214
2215 return ret / sizeof(struct lttng_domain);
e1b624d0
JG
2216error:
2217 return ret;
9f19cc17
DG
2218}
2219
2220/*
9ae110e2
JG
2221 * Ask the session daemon for all available channels of a session.
2222 * Sets the contents of the channels array.
2223 * Returns the number of lttng_channel entries in channels;
2224 * on error, returns a negative value.
9f19cc17 2225 */
cd80958d
DG
2226int lttng_list_channels(struct lttng_handle *handle,
2227 struct lttng_channel **channels)
9f19cc17
DG
2228{
2229 int ret;
53e367f9
JG
2230 size_t channel_count, i;
2231 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51 2232 sizeof(struct lttng_channel_extended);
cd80958d 2233 struct lttcomm_session_msg lsm;
53e367f9 2234 void *extended_at;
cd80958d 2235
9d697d3d 2236 if (handle == NULL) {
53e367f9
JG
2237 ret = -LTTNG_ERR_INVALID;
2238 goto end;
cd80958d
DG
2239 }
2240
53efb85a 2241 memset(&lsm, 0, sizeof(lsm));
cd80958d 2242 lsm.cmd_type = LTTNG_LIST_CHANNELS;
e1b624d0 2243 ret = lttng_strncpy(lsm.session.name, handle->session_name,
cd80958d 2244 sizeof(lsm.session.name));
e1b624d0
JG
2245 if (ret) {
2246 ret = -LTTNG_ERR_INVALID;
2247 goto end;
2248 }
9f19cc17 2249
60160d2a 2250 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
9f19cc17 2251
cac3069d 2252 ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels);
9f19cc17 2253 if (ret < 0) {
53e367f9
JG
2254 goto end;
2255 }
2256
2257 if (ret % channel_size) {
2258 ret = -LTTNG_ERR_UNK;
2259 free(*channels);
2260 *channels = NULL;
2261 goto end;
9f19cc17 2262 }
53e367f9 2263 channel_count = (size_t) ret / channel_size;
9f19cc17 2264
53e367f9
JG
2265 /* Set extended info pointers */
2266 extended_at = ((void *) *channels) +
2267 channel_count * sizeof(struct lttng_channel);
2268 for (i = 0; i < channel_count; i++) {
2269 struct lttng_channel *chan = &(*channels)[i];
2270
2271 chan->attr.extended.ptr = extended_at;
cf0bcb51 2272 extended_at += sizeof(struct lttng_channel_extended);
53e367f9
JG
2273 }
2274
2275 ret = (int) channel_count;
2276end:
2277 return ret;
9f19cc17
DG
2278}
2279
2280/*
9ae110e2
JG
2281 * Ask the session daemon for all available events of a session channel.
2282 * Sets the contents of the events array.
2283 * Returns the number of lttng_event entries in events;
2284 * on error, returns a negative value.
9f19cc17 2285 */
cd80958d
DG
2286int lttng_list_events(struct lttng_handle *handle,
2287 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
2288{
2289 int ret;
e368fb43
JG
2290 struct lttcomm_session_msg lsm = {};
2291 const struct lttcomm_event_command_header *cmd_header = NULL;
b4e3ceb9 2292 uint32_t nb_events, i;
e368fb43 2293 const void *comm_ext_at;
56f0bc67 2294 struct lttng_dynamic_buffer listing;
de453daa 2295 size_t storage_req;
e368fb43
JG
2296 struct lttng_payload payload;
2297 struct lttng_payload payload_copy;
2298 struct lttng_payload_view lsm_view =
2299 lttng_payload_view_init_from_buffer(
2300 (const char *) &lsm, 0, sizeof(lsm));
2301 struct lttng_buffer_view cmd_header_view;
2302 struct lttng_buffer_view cmd_payload_view;
2303 struct lttng_buffer_view flat_events_view;
2304 struct lttng_buffer_view ext_view;
9f19cc17 2305
9d697d3d
DG
2306 /* Safety check. An handle and channel name are mandatory */
2307 if (handle == NULL || channel_name == NULL) {
e368fb43
JG
2308 ret = -LTTNG_ERR_INVALID;
2309 goto end;
cd80958d
DG
2310 }
2311
e368fb43
JG
2312 lttng_payload_init(&payload);
2313 lttng_payload_init(&payload_copy);
2314
cd80958d 2315 lsm.cmd_type = LTTNG_LIST_EVENTS;
e1b624d0 2316 ret = lttng_strncpy(lsm.session.name, handle->session_name,
cd80958d 2317 sizeof(lsm.session.name));
e1b624d0
JG
2318 if (ret) {
2319 ret = -LTTNG_ERR_INVALID;
2320 goto end;
2321 }
2322
2323 ret = lttng_strncpy(lsm.u.list.channel_name, channel_name,
cd80958d 2324 sizeof(lsm.u.list.channel_name));
e1b624d0
JG
2325 if (ret) {
2326 ret = -LTTNG_ERR_INVALID;
2327 goto end;
2328 }
2329
60160d2a 2330 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
9f19cc17 2331
e368fb43 2332 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &payload);
9f19cc17 2333 if (ret < 0) {
de453daa 2334 goto end;
9f19cc17
DG
2335 }
2336
e368fb43
JG
2337 /*
2338 * A copy of the payload is performed since it will be
2339 * consumed twice. Consuming the same payload twice is invalid since
2340 * it will cause any received file descriptor to become "shared"
2341 * between different instances of the resulting objects.
2342 */
2343 ret = lttng_payload_copy(&payload, &payload_copy);
2344 if (ret) {
2345 ret = -LTTNG_ERR_NOMEM;
2346 goto end;
2347 }
2348
2349 cmd_header_view = lttng_buffer_view_from_dynamic_buffer(
2350 &payload.buffer, 0, sizeof(*cmd_header));
3e6e0df2 2351 if (!lttng_buffer_view_is_valid(&cmd_header_view)) {
e368fb43 2352 ret = -LTTNG_ERR_INVALID_PROTOCOL;
d9f484bc
JG
2353 goto end;
2354 }
2355
e368fb43
JG
2356 cmd_header = (typeof(cmd_header)) cmd_header_view.data;
2357
b4e3ceb9
PP
2358 /* Set number of events and free command header */
2359 nb_events = cmd_header->nb_events;
2360 if (nb_events > INT_MAX) {
55c9e7ca 2361 ret = -LTTNG_ERR_OVERFLOW;
de453daa 2362 goto end;
b4e3ceb9 2363 }
e368fb43
JG
2364
2365 cmd_payload_view = lttng_buffer_view_from_dynamic_buffer(
2366 &payload.buffer, sizeof(*cmd_header), -1);
b4e3ceb9 2367
de453daa
JG
2368 /*
2369 * The buffer that is returned must contain a "flat" version of
2370 * the events that are returned. In other words, all pointers
2371 * within an lttng_event must point to a location within the returned
56f0bc67
JG
2372 * buffer so that the user may free everything by simply calling free()
2373 * on the returned buffer. This is needed in order to maintain API
de453daa
JG
2374 * compatibility.
2375 *
56f0bc67
JG
2376 * A first pass is performed to compute the size of the buffer that
2377 * must be allocated. A second pass is then performed to setup
de453daa
JG
2378 * the returned events so that their members always point within the
2379 * buffer.
2380 *
2381 * The layout of the returned buffer is as follows:
2382 * - struct lttng_event[nb_events],
2383 * - nb_events times the following:
2384 * - struct lttng_event_extended,
2385 * - flattened version of userspace_probe_location
2386 * - filter_expression
2387 * - exclusions
2388 * - padding to align to 64-bits
2389 */
e368fb43
JG
2390 ext_view = lttng_buffer_view_from_view(&cmd_payload_view,
2391 nb_events * sizeof(struct lttng_event), -1);
2392 comm_ext_at = ext_view.data;
de453daa 2393 storage_req = nb_events * sizeof(struct lttng_event);
e368fb43
JG
2394 {
2395 struct lttng_payload_view payload_view =
2396 lttng_payload_view_from_payload(&payload, 0, -1);
b4e3ceb9 2397
e368fb43
JG
2398 for (i = 0; i < nb_events; i++) {
2399 const struct lttcomm_event_extended_header *ext_comm =
2400 (struct lttcomm_event_extended_header *)
2401 comm_ext_at;
2402 int probe_storage_req = 0;
b4e3ceb9 2403
e368fb43
JG
2404 comm_ext_at += sizeof(*ext_comm);
2405 comm_ext_at += ext_comm->filter_len;
2406 comm_ext_at += ext_comm->nb_exclusions *
2407 LTTNG_SYMBOL_NAME_LEN;
2408
2409 if (ext_comm->userspace_probe_location_len) {
2410 struct lttng_userspace_probe_location
2411 *probe_location = NULL;
2412 struct lttng_payload_view probe_location_view = lttng_payload_view_from_view(
2413 &payload_view,
2414 (const char *) comm_ext_at -
2415 payload_view.buffer.data,
2416 ext_comm->userspace_probe_location_len);
56f0bc67 2417
3e6e0df2
JG
2418 if (!lttng_payload_view_is_valid(&probe_location_view)) {
2419 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2420 goto end;
2421 }
2422
e368fb43
JG
2423 /*
2424 * Create a temporary userspace probe location
2425 * to determine the size needed by a "flattened"
2426 * version of that same probe location.
2427 */
2428 ret = lttng_userspace_probe_location_create_from_payload(
2429 &probe_location_view,
2430 &probe_location);
2431 if (ret < 0) {
2432 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2433 goto end;
2434 }
56f0bc67 2435
e368fb43
JG
2436 ret = lttng_userspace_probe_location_flatten(
2437 probe_location, NULL);
2438 lttng_userspace_probe_location_destroy(
2439 probe_location);
2440 if (ret < 0) {
2441 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2442 goto end;
2443 }
56f0bc67 2444
e368fb43
JG
2445 probe_storage_req = ret;
2446 comm_ext_at += ext_comm->userspace_probe_location_len;
56f0bc67
JG
2447 }
2448
e368fb43
JG
2449 storage_req += sizeof(struct lttng_event_extended);
2450 storage_req += ext_comm->filter_len;
2451 storage_req += ext_comm->nb_exclusions *
2452 LTTNG_SYMBOL_NAME_LEN;
2453 /* Padding to ensure the flat probe is aligned. */
2454 storage_req = ALIGN_TO(storage_req, sizeof(uint64_t));
2455 storage_req += probe_storage_req;
56f0bc67 2456 }
b4e3ceb9
PP
2457 }
2458
56f0bc67
JG
2459 lttng_dynamic_buffer_init(&listing);
2460 /*
2461 * We must ensure that "listing" is never resized so as to preserve
2462 * the validity of the flattened objects.
2463 */
2464 ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req);
2465 if (ret) {
2466 ret = -LTTNG_ERR_NOMEM;
de453daa
JG
2467 goto end;
2468 }
56f0bc67 2469
e368fb43
JG
2470 cmd_payload_view = lttng_buffer_view_from_dynamic_buffer(
2471 &payload_copy.buffer, sizeof(*cmd_header), -1);
2472 flat_events_view = lttng_buffer_view_from_view(&cmd_payload_view, 0,
56f0bc67 2473 nb_events * sizeof(struct lttng_event));
e368fb43 2474 ret = lttng_dynamic_buffer_append_view(&listing, &flat_events_view);
56f0bc67
JG
2475 if (ret) {
2476 ret = -LTTNG_ERR_NOMEM;
2477 goto free_dynamic_buffer;
2478 }
de453daa 2479
e368fb43
JG
2480 ext_view = lttng_buffer_view_from_view(&cmd_payload_view,
2481 nb_events * sizeof(struct lttng_event), -1);
2482 comm_ext_at = ext_view.data;
2483
2484 {
2485 struct lttng_payload_view payload_copy_view =
2486 lttng_payload_view_from_payload(
2487 &payload_copy, 0, -1);
2488
2489 for (i = 0; i < nb_events; i++) {
2490 struct lttng_event *event = (typeof(event))(
2491 listing.data +
2492 (sizeof(struct lttng_event) * i));
2493 const struct lttcomm_event_extended_header *ext_comm =
2494 (typeof(ext_comm)) comm_ext_at;
2495 struct lttng_event_extended *event_extended =
2496 (typeof(event_extended))(listing.data +
2497 listing.size);
2498
2499 /* Insert struct lttng_event_extended. */
2500 ret = lttng_dynamic_buffer_set_size(&listing,
2501 listing.size + sizeof(*event_extended));
56f0bc67
JG
2502 if (ret) {
2503 ret = -LTTNG_ERR_NOMEM;
2504 goto free_dynamic_buffer;
2505 }
e368fb43
JG
2506 event->extended.ptr = event_extended;
2507
2508 comm_ext_at += sizeof(*ext_comm);
2509
2510 /* Insert filter expression. */
2511 if (ext_comm->filter_len) {
2512 event_extended->filter_expression =
2513 listing.data + listing.size;
2514 ret = lttng_dynamic_buffer_append(&listing,
2515 comm_ext_at,
2516 ext_comm->filter_len);
2517 if (ret) {
2518 ret = -LTTNG_ERR_NOMEM;
2519 goto free_dynamic_buffer;
2520 }
2521 comm_ext_at += ext_comm->filter_len;
2522 }
56f0bc67 2523
e368fb43
JG
2524 /* Insert exclusions. */
2525 if (ext_comm->nb_exclusions) {
2526 event_extended->exclusions.count =
2527 ext_comm->nb_exclusions;
2528 event_extended->exclusions.strings =
2529 listing.data + listing.size;
2530
2531 ret = lttng_dynamic_buffer_append(&listing,
2532 comm_ext_at,
2533 ext_comm->nb_exclusions *
2534 LTTNG_SYMBOL_NAME_LEN);
2535 if (ret) {
2536 ret = -LTTNG_ERR_NOMEM;
2537 goto free_dynamic_buffer;
2538 }
2539 comm_ext_at += ext_comm->nb_exclusions *
2540 LTTNG_SYMBOL_NAME_LEN;
2541 }
56f0bc67 2542
e368fb43
JG
2543 /* Insert padding to align to 64-bits. */
2544 ret = lttng_dynamic_buffer_set_size(&listing,
2545 ALIGN_TO(listing.size,
2546 sizeof(uint64_t)));
56f0bc67
JG
2547 if (ret) {
2548 ret = -LTTNG_ERR_NOMEM;
5e4f4898 2549 goto free_dynamic_buffer;
56f0bc67 2550 }
56f0bc67 2551
e368fb43
JG
2552 /* Insert flattened userspace probe location. */
2553 if (ext_comm->userspace_probe_location_len) {
2554 struct lttng_userspace_probe_location
2555 *probe_location = NULL;
2556 struct lttng_payload_view probe_location_view = lttng_payload_view_from_view(
2557 &payload_copy_view,
2558 (const char *) comm_ext_at -
2559 payload_copy_view.buffer.data,
2560 ext_comm->userspace_probe_location_len);
2561
3e6e0df2
JG
2562 if (!lttng_payload_view_is_valid(&probe_location_view)) {
2563 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2564 goto free_dynamic_buffer;
2565 }
2566
e368fb43
JG
2567 ret = lttng_userspace_probe_location_create_from_payload(
2568 &probe_location_view,
2569 &probe_location);
2570 if (ret < 0) {
2571 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2572 goto free_dynamic_buffer;
2573 }
de453daa 2574
e368fb43
JG
2575 event_extended->probe_location = (struct lttng_userspace_probe_location
2576 *) (listing.data +
2577 listing.size);
2578 ret = lttng_userspace_probe_location_flatten(
2579 probe_location, &listing);
2580 lttng_userspace_probe_location_destroy(
2581 probe_location);
2582 if (ret < 0) {
2583 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2584 goto free_dynamic_buffer;
2585 }
56f0bc67 2586
e368fb43 2587 comm_ext_at += ext_comm->userspace_probe_location_len;
56f0bc67 2588 }
56f0bc67 2589 }
de453daa
JG
2590 }
2591
56f0bc67
JG
2592 /* Don't reset listing buffer as we return its content. */
2593 *events = (struct lttng_event *) listing.data;
2594 lttng_dynamic_buffer_init(&listing);
de453daa 2595 ret = (int) nb_events;
56f0bc67
JG
2596free_dynamic_buffer:
2597 lttng_dynamic_buffer_reset(&listing);
de453daa 2598end:
e368fb43
JG
2599 lttng_payload_reset(&payload);
2600 lttng_payload_reset(&payload_copy);
b4e3ceb9 2601 return ret;
9f19cc17
DG
2602}
2603
fac6795d 2604/*
1c8d13c8
TD
2605 * Sets the tracing_group variable with name.
2606 * This function allocates memory pointed to by tracing_group.
2607 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
2608 */
2609int lttng_set_tracing_group(const char *name)
2610{
9d697d3d 2611 if (name == NULL) {
2f70b271 2612 return -LTTNG_ERR_INVALID;
9d697d3d
DG
2613 }
2614
fac6795d 2615 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 2616 return -LTTNG_ERR_FATAL;
fac6795d
DG
2617 }
2618
2619 return 0;
2620}
2621
cd80958d 2622int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
2623 struct lttng_calibrate *calibrate)
2624{
b812e5ca
PP
2625 /*
2626 * This command was removed in LTTng 2.9.
2627 */
2628 return -LTTNG_ERR_UND;
d0254c7c
MD
2629}
2630
5edd7e09
DG
2631/*
2632 * Set default channel attributes.
441c16a7 2633 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
2634 */
2635void lttng_channel_set_default_attr(struct lttng_domain *domain,
2636 struct lttng_channel_attr *attr)
2637{
294851b0 2638 struct lttng_channel_extended *extended;
cf0bcb51 2639
5edd7e09
DG
2640 /* Safety check */
2641 if (attr == NULL || domain == NULL) {
2642 return;
2643 }
2644
294851b0 2645 extended = (struct lttng_channel_extended *) attr->extended.ptr;
eacaa7a6
DS
2646 memset(attr, 0, sizeof(struct lttng_channel_attr));
2647
0a9c6494
DG
2648 /* Same for all domains. */
2649 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2650 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2651 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2652
5edd7e09
DG
2653 switch (domain->type) {
2654 case LTTNG_DOMAIN_KERNEL:
9ae110e2
JG
2655 attr->switch_timer_interval =
2656 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
d92ff3ef 2657 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
3e230f92 2658 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
5edd7e09
DG
2659 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2660 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
cf0bcb51
JG
2661 if (extended) {
2662 extended->monitor_timer_interval =
2663 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
491d1539
MD
2664 extended->blocking_timeout =
2665 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2666 }
5edd7e09
DG
2667 break;
2668 case LTTNG_DOMAIN_UST:
0a9c6494
DG
2669 switch (domain->buf_type) {
2670 case LTTNG_BUFFER_PER_UID:
2671 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2672 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2673 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
9ae110e2
JG
2674 attr->switch_timer_interval =
2675 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2676 attr->read_timer_interval =
2677 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2678 if (extended) {
2679 extended->monitor_timer_interval =
2680 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2681 extended->blocking_timeout =
2682 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2683 }
0a9c6494
DG
2684 break;
2685 case LTTNG_BUFFER_PER_PID:
2686 default:
2687 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2688 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2689 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
9ae110e2
JG
2690 attr->switch_timer_interval =
2691 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2692 attr->read_timer_interval =
2693 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
cf0bcb51
JG
2694 if (extended) {
2695 extended->monitor_timer_interval =
2696 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
491d1539
MD
2697 extended->blocking_timeout =
2698 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
cf0bcb51 2699 }
0a9c6494
DG
2700 break;
2701 }
5edd7e09 2702 default:
441c16a7 2703 /* Default behavior: leave set to 0. */
5edd7e09
DG
2704 break;
2705 }
cf0bcb51
JG
2706
2707 attr->extended.ptr = extended;
5edd7e09
DG
2708}
2709
5ba3702f
JG
2710int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2711 uint64_t *discarded_events)
2712{
2713 int ret = 0;
cf0bcb51 2714 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2715
2716 if (!channel || !discarded_events) {
2717 ret = -LTTNG_ERR_INVALID;
2718 goto end;
2719 }
2720
2721 chan_ext = channel->attr.extended.ptr;
2722 if (!chan_ext) {
2723 /*
2724 * This can happen since the lttng_channel structure is
2725 * used for other tasks where this pointer is never set.
2726 */
2727 *discarded_events = 0;
2728 goto end;
2729 }
2730
2731 *discarded_events = chan_ext->discarded_events;
2732end:
2733 return ret;
2734}
2735
2736int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2737 uint64_t *lost_packets)
2738{
2739 int ret = 0;
cf0bcb51 2740 struct lttng_channel_extended *chan_ext;
5ba3702f
JG
2741
2742 if (!channel || !lost_packets) {
2743 ret = -LTTNG_ERR_INVALID;
2744 goto end;
2745 }
2746
2747 chan_ext = channel->attr.extended.ptr;
2748 if (!chan_ext) {
2749 /*
2750 * This can happen since the lttng_channel structure is
2751 * used for other tasks where this pointer is never set.
2752 */
2753 *lost_packets = 0;
2754 goto end;
2755 }
2756
2757 *lost_packets = chan_ext->lost_packets;
2758end:
2759 return ret;
2760}
2761
cf0bcb51
JG
2762int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2763 uint64_t *monitor_timer_interval)
2764{
2765 int ret = 0;
2766
2767 if (!chan || !monitor_timer_interval) {
2768 ret = -LTTNG_ERR_INVALID;
2769 goto end;
2770 }
2771
2772 if (!chan->attr.extended.ptr) {
2773 ret = -LTTNG_ERR_INVALID;
2774 goto end;
2775 }
2776
2777 *monitor_timer_interval = ((struct lttng_channel_extended *)
2778 chan->attr.extended.ptr)->monitor_timer_interval;
2779end:
2780 return ret;
2781}
2782
2783int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2784 uint64_t monitor_timer_interval)
2785{
2786 int ret = 0;
2787
2788 if (!chan || !chan->attr.extended.ptr) {
2789 ret = -LTTNG_ERR_INVALID;
2790 goto end;
2791 }
2792
2793 ((struct lttng_channel_extended *)
2794 chan->attr.extended.ptr)->monitor_timer_interval =
2795 monitor_timer_interval;
2796end:
2797 return ret;
2798}
2799
491d1539
MD
2800int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2801 int64_t *blocking_timeout)
2802{
2803 int ret = 0;
2804
2805 if (!chan || !blocking_timeout) {
2806 ret = -LTTNG_ERR_INVALID;
2807 goto end;
2808 }
2809
2810 if (!chan->attr.extended.ptr) {
2811 ret = -LTTNG_ERR_INVALID;
2812 goto end;
2813 }
2814
2815 *blocking_timeout = ((struct lttng_channel_extended *)
2816 chan->attr.extended.ptr)->blocking_timeout;
2817end:
2818 return ret;
2819}
2820
2821int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2822 int64_t blocking_timeout)
2823{
2824 int ret = 0;
2825 int64_t msec_timeout;
2826
2827 if (!chan || !chan->attr.extended.ptr) {
2828 ret = -LTTNG_ERR_INVALID;
2829 goto end;
2830 }
2831
2832 if (blocking_timeout < 0 && blocking_timeout != -1) {
2833 ret = -LTTNG_ERR_INVALID;
2834 goto end;
2835 }
2836
2837 /*
2838 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2839 * us to accept a narrower range of values (msecs expressed as a signed
2840 * 32-bit integer).
2841 */
2842 msec_timeout = blocking_timeout / 1000;
2843 if (msec_timeout != (int32_t) msec_timeout) {
2844 ret = -LTTNG_ERR_INVALID;
2845 goto end;
2846 }
2847
2848 ((struct lttng_channel_extended *)
2849 chan->attr.extended.ptr)->blocking_timeout =
2850 blocking_timeout;
2851end:
2852 return ret;
2853}
2854
fac6795d 2855/*
2269e89e 2856 * Check if session daemon is alive.
fac6795d 2857 *
2269e89e 2858 * Return 1 if alive or 0 if not.
1c8d13c8 2859 * On error returns a negative value.
fac6795d 2860 */
947308c4 2861int lttng_session_daemon_alive(void)
fac6795d
DG
2862{
2863 int ret;
2864
2865 ret = set_session_daemon_path();
2866 if (ret < 0) {
9ae110e2 2867 /* Error. */
fac6795d
DG
2868 return ret;
2869 }
2870
9d035200 2871 if (*sessiond_sock_path == '\0') {
2f70b271 2872 /*
9ae110e2
JG
2873 * No socket path set. Weird error which means the constructor
2874 * was not called.
2f70b271
DG
2875 */
2876 assert(0);
fac6795d
DG
2877 }
2878
2269e89e 2879 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9 2880 if (ret < 0) {
9ae110e2 2881 /* Not alive. */
7d8234d9
MD
2882 return 0;
2883 }
7d8234d9 2884
9ae110e2 2885 /* Is alive. */
947308c4 2886 return 1;
fac6795d
DG
2887}
2888
00e2e675 2889/*
a4b92340 2890 * Set URL for a consumer for a session and domain.
00e2e675
DG
2891 *
2892 * Return 0 on success, else a negative value.
2893 */
a4b92340
DG
2894int lttng_set_consumer_url(struct lttng_handle *handle,
2895 const char *control_url, const char *data_url)
00e2e675 2896{
3dd05a85 2897 int ret;
a4b92340 2898 ssize_t size;
00e2e675 2899 struct lttcomm_session_msg lsm;
a4b92340 2900 struct lttng_uri *uris = NULL;
00e2e675 2901
a4b92340 2902 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
e1b624d0
JG
2903 ret = -LTTNG_ERR_INVALID;
2904 goto error;
00e2e675
DG
2905 }
2906
a4b92340
DG
2907 memset(&lsm, 0, sizeof(lsm));
2908
00e2e675
DG
2909 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2910
e1b624d0 2911 ret = lttng_strncpy(lsm.session.name, handle->session_name,
00e2e675 2912 sizeof(lsm.session.name));
e1b624d0
JG
2913 if (ret) {
2914 ret = -LTTNG_ERR_INVALID;
2915 goto error;
2916 }
2917
60160d2a 2918 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
00e2e675 2919
bc894455 2920 size = uri_parse_str_urls(control_url, data_url, &uris);
a4b92340 2921 if (size < 0) {
e1b624d0
JG
2922 ret = -LTTNG_ERR_INVALID;
2923 goto error;
a4b92340
DG
2924 }
2925
2926 lsm.u.uri.size = size;
00e2e675 2927
795a978d 2928 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
cac3069d 2929 sizeof(struct lttng_uri) * size, NULL);
3dd05a85
DG
2930
2931 free(uris);
e1b624d0 2932error:
3dd05a85 2933 return ret;
00e2e675
DG
2934}
2935
2936/*
9c6bda17 2937 * [OBSOLETE]
00e2e675 2938 */
2ec40111 2939int lttng_enable_consumer(struct lttng_handle *handle);
00e2e675
DG
2940int lttng_enable_consumer(struct lttng_handle *handle)
2941{
785d2d0d 2942 return -ENOSYS;
00e2e675
DG
2943}
2944
2945/*
9c6bda17 2946 * [OBSOLETE]
00e2e675 2947 */
2ec40111 2948int lttng_disable_consumer(struct lttng_handle *handle);
00e2e675
DG
2949int lttng_disable_consumer(struct lttng_handle *handle)
2950{
785d2d0d 2951 return -ENOSYS;
00e2e675
DG
2952}
2953
07424f16 2954/*
b178f53e 2955 * [OBSOLETE]
07424f16 2956 */
2ec40111
SM
2957int _lttng_create_session_ext(const char *name, const char *url,
2958 const char *datetime);
07424f16
DG
2959int _lttng_create_session_ext(const char *name, const char *url,
2960 const char *datetime)
2961{
b178f53e 2962 return -ENOSYS;
07424f16
DG
2963}
2964
806e2684
DG
2965/*
2966 * For a given session name, this call checks if the data is ready to be read
2967 * or is still being extracted by the consumer(s) hence not ready to be used by
2968 * any readers.
2969 */
6d805429 2970int lttng_data_pending(const char *session_name)
806e2684
DG
2971{
2972 int ret;
2973 struct lttcomm_session_msg lsm;
f6151c55 2974 uint8_t *pending = NULL;
806e2684
DG
2975
2976 if (session_name == NULL) {
2977 return -LTTNG_ERR_INVALID;
2978 }
2979
53efb85a 2980 memset(&lsm, 0, sizeof(lsm));
6d805429 2981 lsm.cmd_type = LTTNG_DATA_PENDING;
806e2684 2982
e1b624d0 2983 ret = lttng_strncpy(lsm.session.name, session_name,
cac3069d 2984 sizeof(lsm.session.name));
e1b624d0
JG
2985 if (ret) {
2986 ret = -LTTNG_ERR_INVALID;
2987 goto end;
2988 }
806e2684 2989
f6151c55
JG
2990 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2991 if (ret < 0) {
2992 goto end;
2993 } else if (ret != 1) {
2994 /* Unexpected payload size */
2995 ret = -LTTNG_ERR_INVALID;
2996 goto end;
e848d36d
JG
2997 } else if (!pending) {
2998 /* Internal error. */
2999 ret = -LTTNG_ERR_UNK;
3000 goto end;
806e2684
DG
3001 }
3002
f6151c55
JG
3003 ret = (int) *pending;
3004end:
3005 free(pending);
806e2684
DG
3006 return ret;
3007}
3008
93ec662e
JD
3009/*
3010 * Regenerate the metadata for a session.
3011 * Return 0 on success, a negative error code on error.
3012 */
eded6438 3013int lttng_regenerate_metadata(const char *session_name)
93ec662e
JD
3014{
3015 int ret;
3016 struct lttcomm_session_msg lsm;
3017
3018 if (!session_name) {
3019 ret = -LTTNG_ERR_INVALID;
3020 goto end;
3021 }
3022
3023 memset(&lsm, 0, sizeof(lsm));
eded6438 3024 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
93ec662e 3025
e1b624d0 3026 ret = lttng_strncpy(lsm.session.name, session_name,
93ec662e 3027 sizeof(lsm.session.name));
e1b624d0
JG
3028 if (ret) {
3029 ret = -LTTNG_ERR_INVALID;
3030 goto end;
3031 }
93ec662e
JD
3032
3033 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3034 if (ret < 0) {
3035 goto end;
3036 }
3037
3038 ret = 0;
3039end:
3040 return ret;
3041}
3042
eded6438
JD
3043/*
3044 * Deprecated, replaced by lttng_regenerate_metadata.
3045 */
3046int lttng_metadata_regenerate(const char *session_name)
3047{
3048 return lttng_regenerate_metadata(session_name);
3049}
3050
c2561365
JD
3051/*
3052 * Regenerate the statedump of a session.
3053 * Return 0 on success, a negative error code on error.
3054 */
3055int lttng_regenerate_statedump(const char *session_name)
3056{
3057 int ret;
3058 struct lttcomm_session_msg lsm;
3059
3060 if (!session_name) {
3061 ret = -LTTNG_ERR_INVALID;
3062 goto end;
3063 }
3064
3065 memset(&lsm, 0, sizeof(lsm));
3066 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
3067
e1b624d0 3068 ret = lttng_strncpy(lsm.session.name, session_name,
c2561365 3069 sizeof(lsm.session.name));
e1b624d0
JG
3070 if (ret) {
3071 ret = -LTTNG_ERR_INVALID;
3072 goto end;
3073 }
c2561365
JD
3074
3075 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3076 if (ret < 0) {
3077 goto end;
3078 }
3079
3080 ret = 0;
3081end:
3082 return ret;
3083}
3084
a5c2d2a7
JG
3085static
3086int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name,
3087 bool generate_name)
a58c490f
JG
3088{
3089 int ret;
99608320
JR
3090 struct lttcomm_session_msg lsm = {
3091 .cmd_type = LTTNG_REGISTER_TRIGGER,
a5c2d2a7 3092 .u.trigger.is_trigger_anonymous = !name && !generate_name,
99608320
JR
3093 };
3094 struct lttcomm_session_msg *message_lsm;
3095 struct lttng_payload message;
3096 struct lttng_payload reply;
242388e4 3097 struct lttng_trigger *reply_trigger = NULL;
9124c630 3098 enum lttng_domain_type domain_type;
64eafdf6
JR
3099 const struct lttng_credentials user_creds = {
3100 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3101 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3102 };
a5c2d2a7
JG
3103 const char *unused_trigger_name = NULL;
3104 enum lttng_trigger_status trigger_status;
99608320
JR
3105
3106 lttng_payload_init(&message);
3107 lttng_payload_init(&reply);
a58c490f
JG
3108
3109 if (!trigger) {
3110 ret = -LTTNG_ERR_INVALID;
3111 goto end;
3112 }
3113
a5c2d2a7
JG
3114 trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name);
3115 if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) {
3116 /* Re-using already registered trigger. */
3117 ret = -LTTNG_ERR_INVALID;
3118 goto end;
3119 }
3120
3121 if (name) {
3122 trigger_status = lttng_trigger_set_name(trigger, name);
3123 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3124 ret = -LTTNG_ERR_NOMEM;
3125 goto end;
3126 }
3127 }
3128
64eafdf6
JR
3129 if (!trigger->creds.uid.is_set) {
3130 /* Use the client's credentials as the trigger credentials. */
3131 lttng_trigger_set_credentials(trigger, &user_creds);
3132 } else {
3133 /*
3134 * Validate that either the current trigger credentials and the
3135 * client credentials are identical or that the current user is
3136 * root. The root user can register, unregister triggers for
3137 * himself and other users.
3138 *
3139 * This check is also present on the sessiond side, using the
3140 * credentials passed on the socket. These check are all
3141 * "safety" checks.
3142 */
3143 const struct lttng_credentials *trigger_creds =
3144 lttng_trigger_get_credentials(trigger);
3145
3146 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3147 if (lttng_credentials_get_uid(&user_creds) != 0) {
3148 ret = -LTTNG_ERR_EPERM;
a5c2d2a7 3149 goto end_unset_name;
64eafdf6
JR
3150 }
3151 }
3152 }
3153
a58c490f 3154 if (!lttng_trigger_validate(trigger)) {
eac4828d 3155 ret = -LTTNG_ERR_INVALID_TRIGGER;
a5c2d2a7 3156 goto end_unset_name;
a58c490f
JG
3157 }
3158
9124c630
JR
3159 domain_type = lttng_trigger_get_underlying_domain_type_restriction(
3160 trigger);
3161
3162 lsm.domain.type = domain_type;
3163
97285430
JG
3164 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3165 if (ret) {
3166 ret = -LTTNG_ERR_NOMEM;
a5c2d2a7 3167 goto end_unset_name;
97285430 3168 }
99608320 3169
99608320 3170 ret = lttng_trigger_serialize(trigger, &message);
3647288f 3171 if (ret < 0) {
a58c490f 3172 ret = -LTTNG_ERR_UNK;
a5c2d2a7 3173 goto end_unset_name;
a58c490f
JG
3174 }
3175
b22f4f54
JG
3176 /*
3177 * This is needed to populate the trigger object size for the command
3178 * header.
3179 */
3180 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3181
99608320
JR
3182 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3183
3184 {
3185 struct lttng_payload_view message_view =
3186 lttng_payload_view_from_payload(
3187 &message, 0, -1);
3188
3189 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3190 &message_view);
3191 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3192 if (ret < 0) {
a5c2d2a7 3193 goto end_unset_name;
99608320
JR
3194 }
3195 }
3196
242388e4
JR
3197 {
3198 struct lttng_payload_view reply_view =
3199 lttng_payload_view_from_payload(
3200 &reply, 0, reply.buffer.size);
3201
3202 ret = lttng_trigger_create_from_payload(
3203 &reply_view, &reply_trigger);
3204 if (ret < 0) {
a5c2d2a7
JG
3205 ret = -LTTNG_ERR_INVALID_PROTOCOL;
3206 goto end_unset_name;
242388e4
JR
3207 }
3208 }
3209
a5c2d2a7
JG
3210 if (name || generate_name) {
3211 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3212 if (ret < 0) {
3213 ret = -LTTNG_ERR_NOMEM;
3214 goto end;
3215 }
242388e4
JR
3216 }
3217
99608320 3218 ret = 0;
a5c2d2a7
JG
3219 goto end;
3220
3221end_unset_name:
3222 trigger_status = lttng_trigger_set_name(trigger, NULL);
3223 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3224 ret = -LTTNG_ERR_UNK;
3225 }
a58c490f 3226end:
99608320
JR
3227 lttng_payload_reset(&message);
3228 lttng_payload_reset(&reply);
242388e4 3229 lttng_trigger_destroy(reply_trigger);
a58c490f
JG
3230 return ret;
3231}
3232
a5c2d2a7
JG
3233int lttng_register_trigger(struct lttng_trigger *trigger)
3234{
3235 /* Register an anonymous trigger. */
3236 return _lttng_register_trigger(trigger, NULL, false);
3237}
3238
3239enum lttng_error_code lttng_register_trigger_with_name(
3240 struct lttng_trigger *trigger, const char *name)
3241{
3242 const int ret = _lttng_register_trigger(trigger, name, false);
3243
3244 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3245}
3246
3247enum lttng_error_code lttng_register_trigger_with_automatic_name(
3248 struct lttng_trigger *trigger)
3249{
3250 const int ret = _lttng_register_trigger(trigger, false, true);
3251
3252 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3253}
3254
b99a0cb3
JG
3255enum lttng_error_code lttng_error_query_execute(
3256 const struct lttng_error_query *query,
3257 const struct lttng_endpoint *endpoint,
3258 struct lttng_error_query_results **results)
3259{
3260 int ret;
3261 enum lttng_error_code ret_code;
3262 struct lttcomm_session_msg lsm = {
3263 .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
3264 };
3265 struct lttng_payload message;
3266 struct lttng_payload reply;
3267 struct lttcomm_session_msg *message_lsm;
3268
3269 lttng_payload_init(&message);
3270 lttng_payload_init(&reply);
3271
3272 if (!query || !results) {
3273 ret_code = LTTNG_ERR_INVALID;
3274 goto end;
3275 }
3276
3277 if (endpoint != lttng_session_daemon_command_endpoint) {
3278 ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
3279 goto end;
3280 }
3281
3282 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3283 if (ret) {
3284 ret_code = LTTNG_ERR_NOMEM;
3285 goto end;
3286 }
3287
3288 ret = lttng_error_query_serialize(query, &message);
3289 if (ret) {
3290 ret_code = LTTNG_ERR_UNK;
3291 goto end;
3292 }
3293
3294 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3295 message_lsm->u.error_query.length =
3296 (uint32_t) message.buffer.size - sizeof(lsm);
3297
3298 {
3299 struct lttng_payload_view message_view =
3300 lttng_payload_view_from_payload(
3301 &message, 0, -1);
3302
3303 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3304 &message_view);
3305 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3306 if (ret < 0) {
3307 ret_code = -ret;
3308 goto end;
3309 }
3310 }
3311
3312 {
3313 ssize_t reply_create_ret;
3314 struct lttng_payload_view reply_view =
3315 lttng_payload_view_from_payload(
3316 &reply, 0, reply.buffer.size);
3317
3318 reply_create_ret = lttng_error_query_results_create_from_payload(
3319 &reply_view, results);
3320 if (reply_create_ret < 0) {
3321 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3322 goto end;
3323 }
3324 }
3325
3326 ret_code = LTTNG_OK;
3327end:
3328 lttng_payload_reset(&message);
3329 lttng_payload_reset(&reply);
3330 return ret_code;
3331}
3332
b61776fb 3333int lttng_unregister_trigger(const struct lttng_trigger *trigger)
a58c490f
JG
3334{
3335 int ret;
3336 struct lttcomm_session_msg lsm;
99608320
JR
3337 struct lttcomm_session_msg *message_lsm;
3338 struct lttng_payload message;
3339 struct lttng_payload reply;
b61776fb 3340 struct lttng_trigger *copy = NULL;
64eafdf6
JR
3341 const struct lttng_credentials user_creds = {
3342 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3343 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3344 };
99608320
JR
3345
3346 lttng_payload_init(&message);
3347 lttng_payload_init(&reply);
a58c490f
JG
3348
3349 if (!trigger) {
3350 ret = -LTTNG_ERR_INVALID;
3351 goto end;
3352 }
3353
b61776fb
SM
3354 copy = lttng_trigger_copy(trigger);
3355 if (!copy) {
3356 ret = -LTTNG_ERR_UNK;
3357 goto end;
3358 }
3359
3360 if (!copy->creds.uid.is_set) {
3361 /* Use the client credentials as the trigger credentials */
3362 lttng_trigger_set_credentials(copy, &user_creds);
64eafdf6
JR
3363 } else {
3364 /*
3365 * Validate that either the current trigger credentials and the
3366 * client credentials are identical or that the current user is
3367 * root. The root user can register, unregister triggers for
3368 * himself and other users.
3369 *
3370 * This check is also present on the sessiond side, using the
3371 * credentials passed on the socket. These check are all
3372 * "safety" checks.
3373 */
3374 const struct lttng_credentials *trigger_creds =
b61776fb 3375 lttng_trigger_get_credentials(copy);
64eafdf6
JR
3376 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3377 if (lttng_credentials_get_uid(&user_creds) != 0) {
3378 ret = -LTTNG_ERR_EPERM;
3379 goto end;
3380 }
3381 }
3382 }
3383
b61776fb 3384 if (!lttng_trigger_validate(copy)) {
3647288f 3385 ret = -LTTNG_ERR_INVALID_TRIGGER;
a58c490f
JG
3386 goto end;
3387 }
3388
99608320
JR
3389 memset(&lsm, 0, sizeof(lsm));
3390 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3391
97285430
JG
3392 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3393 if (ret) {
3394 ret = -LTTNG_ERR_NOMEM;
3395 goto end;
3396 }
99608320 3397
b61776fb 3398 ret = lttng_trigger_serialize(copy, &message);
3647288f 3399 if (ret < 0) {
a58c490f
JG
3400 ret = -LTTNG_ERR_UNK;
3401 goto end;
3402 }
3403
b0a79813
FD
3404 /*
3405 * This is needed to populate the trigger object size for the command
3406 * header and number of fds sent.
3407 */
3408 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3409
99608320
JR
3410 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3411
3412 {
3413 struct lttng_payload_view message_view =
3414 lttng_payload_view_from_payload(
3415 &message, 0, -1);
3416
3417 /*
3418 * Update the message header with the number of fd that will be
3419 * sent.
3420 */
3421 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3422 &message_view);
3423
3424 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3425 if (ret < 0) {
3426 goto end;
3427 }
3428 }
3429
3430 ret = 0;
a58c490f 3431end:
b61776fb 3432 lttng_trigger_destroy(copy);
99608320
JR
3433 lttng_payload_reset(&message);
3434 lttng_payload_reset(&reply);
a58c490f
JG
3435 return ret;
3436}
3437
fbc9f37d
JR
3438/*
3439 * Ask the session daemon for all registered triggers for the current user.
3440 *
3441 * Allocates and return an lttng_triggers set.
3442 * On error, returns a suitable lttng_error_code.
3443 */
3444enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers)
3445{
3446 int ret;
3447 enum lttng_error_code ret_code = LTTNG_OK;
3448 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS };
3449 struct lttng_triggers *local_triggers = NULL;
3450 struct lttng_payload reply;
3451 struct lttng_payload_view lsm_view =
3452 lttng_payload_view_init_from_buffer(
3453 (const char *) &lsm, 0, sizeof(lsm));
3454
3455 lttng_payload_init(&reply);
3456
3457 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
3458 if (ret < 0) {
3459 ret_code = (enum lttng_error_code) -ret;
3460 goto end;
3461 }
3462
3463 {
3464 struct lttng_payload_view reply_view =
3465 lttng_payload_view_from_payload(
3466 &reply, 0, reply.buffer.size);
3467
3468 ret = lttng_triggers_create_from_payload(
3469 &reply_view, &local_triggers);
3470 if (ret < 0) {
3471 ret_code = LTTNG_ERR_FATAL;
3472 goto end;
3473 }
3474 }
3475
3476 *triggers = local_triggers;
3477 local_triggers = NULL;
3478end:
3479 lttng_payload_reset(&reply);
3480 lttng_triggers_destroy(local_triggers);
3481 return ret_code;
3482}
3483
fac6795d 3484/*
9ae110e2 3485 * lib constructor.
fac6795d 3486 */
b2b89e8a 3487static void __attribute__((constructor)) init(void)
fac6795d
DG
3488{
3489 /* Set default session group */
bbccc3d2 3490 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 3491}
49cca668
DG
3492
3493/*
9ae110e2 3494 * lib destructor.
49cca668 3495 */
b2b89e8a 3496static void __attribute__((destructor)) lttng_ctl_exit(void)
49cca668
DG
3497{
3498 free(tracing_group);
3499}
This page took 0.257541 seconds and 4 git commands to generate.