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