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