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