Fix: liblttng-ctl comm: lttng_channel is not packed
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
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 <assert.h>
15 #include <grp.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <common/bytecode/bytecode.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.h>
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 int lttng_opt_quiet;
78 int lttng_opt_verbose;
79 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 LTTNG_HIDDEN
87 void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
88 struct lttng_domain *src)
89 {
90 if (src && dst) {
91 switch (src->type) {
92 case LTTNG_DOMAIN_KERNEL:
93 case LTTNG_DOMAIN_UST:
94 case LTTNG_DOMAIN_JUL:
95 case LTTNG_DOMAIN_LOG4J:
96 case LTTNG_DOMAIN_PYTHON:
97 memcpy(dst, src, sizeof(struct lttng_domain));
98 break;
99 default:
100 memset(dst, 0, sizeof(struct lttng_domain));
101 break;
102 }
103 }
104 }
105
106 /*
107 * Send lttcomm_session_msg to the session daemon.
108 *
109 * On success, returns the number of bytes sent (>=0)
110 * On error, returns -1
111 */
112 static int send_session_msg(struct lttcomm_session_msg *lsm)
113 {
114 int ret;
115
116 if (!connected) {
117 ret = -LTTNG_ERR_NO_SESSIOND;
118 goto end;
119 }
120
121 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm->cmd_type),
122 lsm->cmd_type);
123
124 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
125 sizeof(struct lttcomm_session_msg));
126 if (ret < 0) {
127 ret = -LTTNG_ERR_FATAL;
128 }
129
130 end:
131 return ret;
132 }
133
134 /*
135 * Send var len data to the session daemon.
136 *
137 * On success, returns the number of bytes sent (>=0)
138 * On error, returns -1
139 */
140 static int send_session_varlen(const void *data, size_t len)
141 {
142 int ret;
143
144 if (!connected) {
145 ret = -LTTNG_ERR_NO_SESSIOND;
146 goto end;
147 }
148
149 if (!data || !len) {
150 ret = 0;
151 goto end;
152 }
153
154 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
155 if (ret < 0) {
156 ret = -LTTNG_ERR_FATAL;
157 }
158
159 end:
160 return ret;
161 }
162
163 /*
164 * Send file descriptors to the session daemon.
165 *
166 * On success, returns the number of bytes sent (>=0)
167 * On error, returns -1
168 */
169 static int send_session_fds(const int *fds, size_t nb_fd)
170 {
171 int ret;
172
173 if (!connected) {
174 ret = -LTTNG_ERR_NO_SESSIOND;
175 goto end;
176 }
177
178 if (!fds || !nb_fd) {
179 ret = 0;
180 goto end;
181 }
182
183 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
184 if (ret < 0) {
185 ret = -LTTNG_ERR_FATAL;
186 }
187
188 end:
189 return ret;
190 }
191
192 /*
193 * Receive data from the sessiond socket.
194 *
195 * On success, returns the number of bytes received (>=0)
196 * On error, returns a negative lttng_error_code.
197 */
198 static int recv_data_sessiond(void *buf, size_t len)
199 {
200 int ret;
201
202 assert(len > 0);
203
204 if (!connected) {
205 ret = -LTTNG_ERR_NO_SESSIOND;
206 goto end;
207 }
208
209 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
210 if (ret < 0) {
211 ret = -LTTNG_ERR_FATAL;
212 } else if (ret == 0) {
213 ret = -LTTNG_ERR_NO_SESSIOND;
214 }
215
216 end:
217 return ret;
218 }
219
220 /*
221 * Receive a payload from the session daemon by appending to an existing
222 * payload.
223 * On success, returns the number of bytes received (>=0)
224 * On error, returns a negative lttng_error_code.
225 */
226 static int recv_payload_sessiond(struct lttng_payload *payload, size_t len)
227 {
228 int ret;
229 const size_t original_payload_size = payload->buffer.size;
230
231 ret = lttng_dynamic_buffer_set_size(
232 &payload->buffer, payload->buffer.size + len);
233 if (ret) {
234 ret = -LTTNG_ERR_NOMEM;
235 goto end;
236 }
237
238 ret = recv_data_sessiond(
239 payload->buffer.data + original_payload_size, len);
240 end:
241 return ret;
242 }
243
244 /*
245 * Check if we are in the specified group.
246 *
247 * If yes return 1, else return -1.
248 */
249 LTTNG_HIDDEN
250 int lttng_check_tracing_group(void)
251 {
252 gid_t *grp_list, tracing_gid;
253 int grp_list_size, grp_id, i;
254 int ret = -1;
255 const char *grp_name = tracing_group;
256
257 /* Get GID of group 'tracing' */
258 if (utils_get_group_id(grp_name, false, &tracing_gid)) {
259 /* If grp_tracing is NULL, the group does not exist. */
260 goto end;
261 }
262
263 /* Get number of supplementary group IDs */
264 grp_list_size = getgroups(0, NULL);
265 if (grp_list_size < 0) {
266 PERROR("getgroups");
267 goto end;
268 }
269
270 /* Alloc group list of the right size */
271 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
272 if (!grp_list) {
273 PERROR("malloc");
274 goto end;
275 }
276 grp_id = getgroups(grp_list_size, grp_list);
277 if (grp_id < 0) {
278 PERROR("getgroups");
279 goto free_list;
280 }
281
282 for (i = 0; i < grp_list_size; i++) {
283 if (grp_list[i] == tracing_gid) {
284 ret = 1;
285 break;
286 }
287 }
288
289 free_list:
290 free(grp_list);
291
292 end:
293 return ret;
294 }
295
296 static enum lttng_error_code check_enough_available_memory(
297 uint64_t num_bytes_requested_per_cpu)
298 {
299 int ret;
300 enum lttng_error_code ret_code;
301 long num_cpu;
302 uint64_t best_mem_info;
303 uint64_t num_bytes_requested_total;
304
305 /*
306 * Get the number of CPU currently online to compute the amount of
307 * memory needed to create a buffer for every CPU.
308 */
309 num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
310 if (num_cpu == -1) {
311 ret_code = LTTNG_ERR_FATAL;
312 goto end;
313 }
314
315 if (num_bytes_requested_per_cpu > UINT64_MAX / (uint64_t) num_cpu) {
316 /* Overflow */
317 ret_code = LTTNG_ERR_OVERFLOW;
318 goto end;
319 }
320
321 num_bytes_requested_total =
322 num_bytes_requested_per_cpu * (uint64_t) num_cpu;
323
324 /*
325 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
326 * reliable estimate we can get but it is only exposed by the kernel
327 * since 3.14. (See Linux kernel commit:
328 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
329 */
330 ret = utils_get_memory_available(&best_mem_info);
331 if (ret >= 0) {
332 goto success;
333 }
334
335 /*
336 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
337 * is a sanity check for obvious user error.
338 */
339 ret = utils_get_memory_total(&best_mem_info);
340 if (ret >= 0) {
341 goto success;
342 }
343
344 /* No valid source of information. */
345 ret_code = LTTNG_ERR_NOMEM;
346 goto end;
347
348 success:
349 if (best_mem_info >= num_bytes_requested_total) {
350 ret_code = LTTNG_OK;
351 } else {
352 ret_code = LTTNG_ERR_NOMEM;
353 }
354 end:
355 return ret_code;
356 }
357
358 /*
359 * Try connect to session daemon with sock_path.
360 *
361 * Return 0 on success, else -1
362 */
363 static int try_connect_sessiond(const char *sock_path)
364 {
365 int ret;
366
367 /* If socket exist, we check if the daemon listens for connect. */
368 ret = access(sock_path, F_OK);
369 if (ret < 0) {
370 /* Not alive */
371 goto error;
372 }
373
374 ret = lttcomm_connect_unix_sock(sock_path);
375 if (ret < 0) {
376 /* Not alive. */
377 goto error;
378 }
379
380 ret = lttcomm_close_unix_sock(ret);
381 if (ret < 0) {
382 PERROR("lttcomm_close_unix_sock");
383 }
384
385 return 0;
386
387 error:
388 return -1;
389 }
390
391 /*
392 * Set sessiond socket path by putting it in the global sessiond_sock_path
393 * variable.
394 *
395 * Returns 0 on success, negative value on failure (the sessiond socket path
396 * is somehow too long or ENOMEM).
397 */
398 static int set_session_daemon_path(void)
399 {
400 int in_tgroup = 0; /* In tracing group. */
401 uid_t uid;
402
403 uid = getuid();
404
405 if (uid != 0) {
406 /* Are we in the tracing group ? */
407 in_tgroup = lttng_check_tracing_group();
408 }
409
410 if ((uid == 0) || in_tgroup == 1) {
411 const int ret = lttng_strncpy(sessiond_sock_path,
412 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
413 sizeof(sessiond_sock_path));
414
415 if (ret) {
416 goto error;
417 }
418 }
419
420 if (uid != 0) {
421 int ret;
422
423 if (in_tgroup) {
424 /* Tracing group. */
425 ret = try_connect_sessiond(sessiond_sock_path);
426 if (ret >= 0) {
427 goto end;
428 }
429 /* Global session daemon not available... */
430 }
431 /* ...or not in tracing group (and not root), default */
432
433 /*
434 * With GNU C < 2.1, snprintf returns -1 if the target buffer
435 * is too small;
436 * With GNU C >= 2.1, snprintf returns the required size
437 * (excluding closing null)
438 */
439 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
440 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
441 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
442 goto error;
443 }
444 }
445 end:
446 return 0;
447
448 error:
449 return -1;
450 }
451
452 /*
453 * Connect to the LTTng session daemon.
454 *
455 * On success, return the socket's file descriptor. On error, return -1.
456 */
457 LTTNG_HIDDEN int connect_sessiond(void)
458 {
459 int ret;
460
461 ret = set_session_daemon_path();
462 if (ret < 0) {
463 goto error;
464 }
465
466 /* Connect to the sesssion daemon. */
467 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
468 if (ret < 0) {
469 goto error;
470 }
471
472 return ret;
473
474 error:
475 return -1;
476 }
477
478 static void reset_global_sessiond_connection_state(void)
479 {
480 sessiond_socket = -1;
481 connected = 0;
482 }
483
484 /*
485 * Clean disconnect from the session daemon.
486 *
487 * On success, return 0. On error, return -1.
488 */
489 static int disconnect_sessiond(void)
490 {
491 int ret = 0;
492
493 if (connected) {
494 ret = lttcomm_close_unix_sock(sessiond_socket);
495 reset_global_sessiond_connection_state();
496 }
497
498 return ret;
499 }
500
501 static int recv_sessiond_optional_data(size_t len, void **user_buf,
502 size_t *user_len)
503 {
504 int ret = 0;
505 void *buf = NULL;
506
507 if (len) {
508 if (!user_len) {
509 ret = -LTTNG_ERR_INVALID;
510 goto end;
511 }
512
513 buf = zmalloc(len);
514 if (!buf) {
515 ret = -ENOMEM;
516 goto end;
517 }
518
519 ret = recv_data_sessiond(buf, len);
520 if (ret < 0) {
521 goto end;
522 }
523
524 if (!user_buf) {
525 ret = -LTTNG_ERR_INVALID;
526 goto end;
527 }
528
529 /* Move ownership of command header buffer to user. */
530 *user_buf = buf;
531 buf = NULL;
532 *user_len = len;
533 } else {
534 /* No command header. */
535 if (user_len) {
536 *user_len = 0;
537 }
538
539 if (user_buf) {
540 *user_buf = NULL;
541 }
542 }
543
544 end:
545 free(buf);
546 return ret;
547 }
548
549 /*
550 * Ask the session daemon a specific command and put the data into buf.
551 * Takes extra var. len. data and file descriptors as input to send to the
552 * session daemon.
553 *
554 * Return size of data (only payload, not header) or a negative error code.
555 */
556 LTTNG_HIDDEN
557 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
558 const int *fds, size_t nb_fd, const void *vardata,
559 size_t vardata_len, void **user_payload_buf,
560 void **user_cmd_header_buf, size_t *user_cmd_header_len)
561 {
562 int ret;
563 size_t payload_len;
564 struct lttcomm_lttng_msg llm;
565
566 ret = connect_sessiond();
567 if (ret < 0) {
568 ret = -LTTNG_ERR_NO_SESSIOND;
569 goto end;
570 } else {
571 sessiond_socket = ret;
572 connected = 1;
573 }
574
575 ret = send_session_msg(lsm);
576 if (ret < 0) {
577 /* Ret value is a valid lttng error code. */
578 goto end;
579 }
580 /* Send var len data */
581 ret = send_session_varlen(vardata, vardata_len);
582 if (ret < 0) {
583 /* Ret value is a valid lttng error code. */
584 goto end;
585 }
586
587 /* Send fds */
588 ret = send_session_fds(fds, nb_fd);
589 if (ret < 0) {
590 /* Ret value is a valid lttng error code. */
591 goto end;
592 }
593
594 /* Get header from data transmission */
595 ret = recv_data_sessiond(&llm, sizeof(llm));
596 if (ret < 0) {
597 /* Ret value is a valid lttng error code. */
598 goto end;
599 }
600
601 /* Check error code if OK */
602 if (llm.ret_code != LTTNG_OK) {
603 ret = -llm.ret_code;
604 goto end;
605 }
606
607 /* Get command header from data transmission */
608 ret = recv_sessiond_optional_data(llm.cmd_header_size,
609 user_cmd_header_buf, user_cmd_header_len);
610 if (ret < 0) {
611 goto end;
612 }
613
614 /* Get payload from data transmission */
615 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
616 &payload_len);
617 if (ret < 0) {
618 goto end;
619 }
620
621 ret = llm.data_size;
622
623 end:
624 disconnect_sessiond();
625 return ret;
626 }
627
628 LTTNG_HIDDEN
629 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
630 struct lttng_payload *reply)
631 {
632 int ret;
633 struct lttcomm_lttng_msg llm;
634 const int fd_count = lttng_payload_view_get_fd_handle_count(message);
635
636 assert(reply->buffer.size == 0);
637 assert(lttng_dynamic_pointer_array_get_count(&reply->_fd_handles) == 0);
638
639 ret = connect_sessiond();
640 if (ret < 0) {
641 ret = -LTTNG_ERR_NO_SESSIOND;
642 goto end;
643 } else {
644 sessiond_socket = ret;
645 connected = 1;
646 }
647
648 /* Send command to session daemon */
649 ret = lttcomm_send_creds_unix_sock(sessiond_socket, message->buffer.data,
650 message->buffer.size);
651 if (ret < 0) {
652 ret = -LTTNG_ERR_FATAL;
653 goto end;
654 }
655
656 if (fd_count > 0) {
657 ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket,
658 message);
659 if (ret < 0) {
660 ret = -LTTNG_ERR_FATAL;
661 goto end;
662 }
663 }
664
665 /* Get header from data transmission */
666 ret = recv_payload_sessiond(reply, sizeof(llm));
667 if (ret < 0) {
668 /* Ret value is a valid lttng error code. */
669 goto end;
670 }
671
672 llm = *((typeof(llm) *) reply->buffer.data);
673
674 /* Check error code if OK */
675 if (llm.ret_code != LTTNG_OK) {
676 if (llm.ret_code < LTTNG_OK || llm.ret_code >= LTTNG_ERR_NR) {
677 /* Invalid error code received. */
678 ret = -LTTNG_ERR_UNK;
679 } else {
680 ret = -llm.ret_code;
681 }
682 goto end;
683 }
684
685 if (llm.cmd_header_size > 0) {
686 ret = recv_payload_sessiond(reply, llm.cmd_header_size);
687 if (ret < 0) {
688 goto end;
689 }
690 }
691
692 /* Get command header from data transmission */
693 if (llm.data_size > 0) {
694 ret = recv_payload_sessiond(reply, llm.data_size);
695 if (ret < 0) {
696 goto end;
697 }
698 }
699
700 if (llm.fd_count > 0) {
701 ret = lttcomm_recv_payload_fds_unix_sock(
702 sessiond_socket, llm.fd_count, reply);
703 if (ret < 0) {
704 goto end;
705 }
706 }
707
708 /* Don't return the llm header to the caller. */
709 memmove(reply->buffer.data, reply->buffer.data + sizeof(llm),
710 reply->buffer.size - sizeof(llm));
711 ret = lttng_dynamic_buffer_set_size(
712 &reply->buffer, reply->buffer.size - sizeof(llm));
713 if (ret) {
714 /* Can't happen as size is reduced. */
715 abort();
716 }
717
718 ret = reply->buffer.size;
719
720 end:
721 disconnect_sessiond();
722 return ret;
723 }
724
725 /*
726 * Create lttng handle and return pointer.
727 *
728 * The returned pointer will be NULL in case of malloc() error.
729 */
730 struct lttng_handle *lttng_create_handle(const char *session_name,
731 struct lttng_domain *domain)
732 {
733 int ret;
734 struct lttng_handle *handle = NULL;
735
736 handle = zmalloc(sizeof(struct lttng_handle));
737 if (handle == NULL) {
738 PERROR("malloc handle");
739 goto end;
740 }
741
742 /* Copy session name */
743 ret = lttng_strncpy(handle->session_name, session_name ? : "",
744 sizeof(handle->session_name));
745 if (ret) {
746 goto error;
747 }
748
749 /* Copy lttng domain or leave initialized to 0. */
750 if (domain) {
751 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
752 }
753
754 end:
755 return handle;
756 error:
757 free(handle);
758 return NULL;
759 }
760
761 /*
762 * Destroy handle by free(3) the pointer.
763 */
764 void lttng_destroy_handle(struct lttng_handle *handle)
765 {
766 free(handle);
767 }
768
769 /*
770 * Register an outside consumer.
771 *
772 * Returns size of returned session payload data or a negative error code.
773 */
774 int lttng_register_consumer(struct lttng_handle *handle,
775 const char *socket_path)
776 {
777 int ret;
778 struct lttcomm_session_msg lsm;
779
780 if (handle == NULL || socket_path == NULL) {
781 ret = -LTTNG_ERR_INVALID;
782 goto end;
783 }
784
785 memset(&lsm, 0, sizeof(lsm));
786 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
787 ret = lttng_strncpy(lsm.session.name, handle->session_name,
788 sizeof(lsm.session.name));
789 if (ret) {
790 ret = -LTTNG_ERR_INVALID;
791 goto end;
792 }
793
794 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
795
796 ret = lttng_strncpy(lsm.u.reg.path, socket_path,
797 sizeof(lsm.u.reg.path));
798 if (ret) {
799 ret = -LTTNG_ERR_INVALID;
800 goto end;
801 }
802
803 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
804 end:
805 return ret;
806 }
807
808 /*
809 * Start tracing for all traces of the session.
810 *
811 * Returns size of returned session payload data or a negative error code.
812 */
813 int lttng_start_tracing(const char *session_name)
814 {
815 int ret;
816 struct lttcomm_session_msg lsm;
817
818 if (session_name == NULL) {
819 ret = -LTTNG_ERR_INVALID;
820 goto end;
821 }
822
823 memset(&lsm, 0, sizeof(lsm));
824 lsm.cmd_type = LTTNG_START_TRACE;
825
826 ret = lttng_strncpy(lsm.session.name, session_name,
827 sizeof(lsm.session.name));
828 if (ret) {
829 ret = -LTTNG_ERR_INVALID;
830 goto end;
831 }
832
833 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
834 end:
835 return ret;
836 }
837
838 /*
839 * Stop tracing for all traces of the session.
840 */
841 static int _lttng_stop_tracing(const char *session_name, int wait)
842 {
843 int ret, data_ret;
844 struct lttcomm_session_msg lsm;
845
846 if (session_name == NULL) {
847 ret = -LTTNG_ERR_INVALID;
848 goto error;
849 }
850
851 memset(&lsm, 0, sizeof(lsm));
852 lsm.cmd_type = LTTNG_STOP_TRACE;
853
854 ret = lttng_strncpy(lsm.session.name, session_name,
855 sizeof(lsm.session.name));
856 if (ret) {
857 ret = -LTTNG_ERR_INVALID;
858 goto error;
859 }
860
861 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
862 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
863 goto error;
864 }
865
866 if (!wait) {
867 goto end;
868 }
869
870 /* Check for data availability */
871 do {
872 data_ret = lttng_data_pending(session_name);
873 if (data_ret < 0) {
874 /* Return the data available call error. */
875 ret = data_ret;
876 goto error;
877 }
878
879 /*
880 * Data sleep time before retrying (in usec). Don't sleep if the
881 * call returned value indicates availability.
882 */
883 if (data_ret) {
884 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
885 }
886 } while (data_ret != 0);
887
888 end:
889 error:
890 return ret;
891 }
892
893 /*
894 * Stop tracing and wait for data availability.
895 */
896 int lttng_stop_tracing(const char *session_name)
897 {
898 return _lttng_stop_tracing(session_name, 1);
899 }
900
901 /*
902 * Stop tracing but _don't_ wait for data availability.
903 */
904 int lttng_stop_tracing_no_wait(const char *session_name)
905 {
906 return _lttng_stop_tracing(session_name, 0);
907 }
908
909 /*
910 * Add context to a channel.
911 *
912 * If the given channel is NULL, add the contexts to all channels.
913 * The event_name param is ignored.
914 *
915 * Returns the size of the returned payload data or a negative error code.
916 */
917 int lttng_add_context(struct lttng_handle *handle,
918 struct lttng_event_context *ctx, const char *event_name,
919 const char *channel_name)
920 {
921 int ret;
922 size_t len = 0;
923 char *buf = NULL;
924 struct lttcomm_session_msg lsm;
925
926 /* Safety check. Both are mandatory. */
927 if (handle == NULL || ctx == NULL) {
928 ret = -LTTNG_ERR_INVALID;
929 goto end;
930 }
931
932 memset(&lsm, 0, sizeof(lsm));
933 lsm.cmd_type = LTTNG_ADD_CONTEXT;
934
935 /* If no channel name, send empty string. */
936 ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "",
937 sizeof(lsm.u.context.channel_name));
938 if (ret) {
939 ret = -LTTNG_ERR_INVALID;
940 goto end;
941 }
942
943 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
944 ret = lttng_strncpy(lsm.session.name, handle->session_name,
945 sizeof(lsm.session.name));
946 if (ret) {
947 ret = -LTTNG_ERR_INVALID;
948 goto end;
949 }
950
951 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
952 size_t provider_len, ctx_len;
953 const char *provider_name = ctx->u.app_ctx.provider_name;
954 const char *ctx_name = ctx->u.app_ctx.ctx_name;
955
956 if (!provider_name || !ctx_name) {
957 ret = -LTTNG_ERR_INVALID;
958 goto end;
959 }
960
961 provider_len = strlen(provider_name);
962 if (provider_len == 0) {
963 ret = -LTTNG_ERR_INVALID;
964 goto end;
965 }
966 lsm.u.context.provider_name_len = provider_len;
967
968 ctx_len = strlen(ctx_name);
969 if (ctx_len == 0) {
970 ret = -LTTNG_ERR_INVALID;
971 goto end;
972 }
973 lsm.u.context.context_name_len = ctx_len;
974
975 len = provider_len + ctx_len;
976 buf = zmalloc(len);
977 if (!buf) {
978 ret = -LTTNG_ERR_NOMEM;
979 goto end;
980 }
981
982 memcpy(buf, provider_name, provider_len);
983 memcpy(buf + provider_len, ctx_name, ctx_len);
984 }
985 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
986
987 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
988 /*
989 * Don't leak application addresses to the sessiond.
990 * This is only necessary when ctx is for an app ctx otherwise
991 * the values inside the union (type & config) are overwritten.
992 */
993 lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
994 lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
995 }
996
997 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
998 end:
999 free(buf);
1000 return ret;
1001 }
1002
1003 /*
1004 * Enable event(s) for a channel.
1005 *
1006 * If no event name is specified, all events are enabled.
1007 * If no channel name is specified, the default 'channel0' is used.
1008 *
1009 * Returns size of returned session payload data or a negative error code.
1010 */
1011 int lttng_enable_event(struct lttng_handle *handle,
1012 struct lttng_event *ev, const char *channel_name)
1013 {
1014 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
1015 NULL, 0, NULL);
1016 }
1017
1018 /*
1019 * Create or enable an event with a filter expression.
1020 *
1021 * Return negative error value on error.
1022 * Return size of returned session payload data if OK.
1023 */
1024 int lttng_enable_event_with_filter(struct lttng_handle *handle,
1025 struct lttng_event *event, const char *channel_name,
1026 const char *filter_expression)
1027 {
1028 return lttng_enable_event_with_exclusions(handle, event, channel_name,
1029 filter_expression, 0, NULL);
1030 }
1031
1032 /*
1033 * Depending on the event, return a newly allocated agent filter expression or
1034 * NULL if not applicable.
1035 *
1036 * An event with NO loglevel and the name is * will return NULL.
1037 */
1038 static char *set_agent_filter(const char *filter, struct lttng_event *ev)
1039 {
1040 int err;
1041 char *agent_filter = NULL;
1042
1043 assert(ev);
1044
1045 /* Don't add filter for the '*' event. */
1046 if (strcmp(ev->name, "*") != 0) {
1047 if (filter) {
1048 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
1049 ev->name);
1050 } else {
1051 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
1052 }
1053 if (err < 0) {
1054 PERROR("asprintf");
1055 goto error;
1056 }
1057 }
1058
1059 /* Add loglevel filtering if any for the JUL domain. */
1060 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
1061 const char *op;
1062
1063 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
1064 op = ">=";
1065 } else {
1066 op = "==";
1067 }
1068
1069 if (filter || agent_filter) {
1070 char *new_filter;
1071
1072 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
1073 agent_filter ? agent_filter : filter, op,
1074 ev->loglevel);
1075 if (agent_filter) {
1076 free(agent_filter);
1077 }
1078 agent_filter = new_filter;
1079 } else {
1080 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
1081 ev->loglevel);
1082 }
1083 if (err < 0) {
1084 PERROR("asprintf");
1085 goto error;
1086 }
1087 }
1088
1089 return agent_filter;
1090 error:
1091 free(agent_filter);
1092 return NULL;
1093 }
1094
1095 /*
1096 * Enable event(s) for a channel, possibly with exclusions and a filter.
1097 * If no event name is specified, all events are enabled.
1098 * If no channel name is specified, the default name is used.
1099 * If filter expression is not NULL, the filter is set for the event.
1100 * If exclusion count is not zero, the exclusions are set for the event.
1101 * Returns size of returned session payload data or a negative error code.
1102 */
1103 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1104 struct lttng_event *ev, const char *channel_name,
1105 const char *original_filter_expression,
1106 int exclusion_count, char **exclusion_list)
1107 {
1108 struct lttcomm_session_msg lsm;
1109 struct lttng_payload payload;
1110 int ret = 0, i;
1111 unsigned int free_filter_expression = 0;
1112 struct filter_parser_ctx *ctx = NULL;
1113
1114 /*
1115 * We have either a filter or some exclusions, so we need to set up
1116 * a variable-length payload from where to send the data.
1117 */
1118 lttng_payload_init(&payload);
1119
1120 /*
1121 * Cast as non-const since we may replace the filter expression
1122 * by a dynamically allocated string. Otherwise, the original
1123 * string is not modified.
1124 */
1125 char *filter_expression = (char *) original_filter_expression;
1126
1127 if (handle == NULL || ev == NULL) {
1128 ret = -LTTNG_ERR_INVALID;
1129 goto error;
1130 }
1131
1132 /*
1133 * Empty filter string will always be rejected by the parser
1134 * anyway, so treat this corner-case early to eliminate
1135 * lttng_fmemopen error for 0-byte allocation.
1136 */
1137 if (filter_expression && filter_expression[0] == '\0') {
1138 ret = -LTTNG_ERR_INVALID;
1139 goto error;
1140 }
1141
1142 memset(&lsm, 0, sizeof(lsm));
1143
1144 /* If no channel name, send empty string. */
1145 ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "",
1146 sizeof(lsm.u.enable.channel_name));
1147 if (ret) {
1148 ret = -LTTNG_ERR_INVALID;
1149 goto error;
1150 }
1151
1152 lsm.cmd_type = LTTNG_ENABLE_EVENT;
1153 if (ev->name[0] == '\0') {
1154 /* Enable all events. */
1155 ret = lttng_strncpy(ev->name, "*", sizeof(ev->name));
1156 assert(ret == 0);
1157 }
1158
1159 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1160 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
1161
1162 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1163 sizeof(lsm.session.name));
1164 if (ret) {
1165 ret = -LTTNG_ERR_INVALID;
1166 goto error;
1167 }
1168
1169 lsm.u.enable.exclusion_count = exclusion_count;
1170 lsm.u.enable.bytecode_len = 0;
1171
1172 /* Parse filter expression. */
1173 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1174 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1175 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1176 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1177 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1178 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1179 char *agent_filter;
1180
1181 /* Setup JUL filter if needed. */
1182 agent_filter = set_agent_filter(filter_expression, ev);
1183 if (!agent_filter) {
1184 if (!filter_expression) {
1185 /*
1186 * No JUL and no filter, just skip
1187 * everything below.
1188 */
1189 goto ask_sessiond;
1190 }
1191 } else {
1192 /*
1193 * With an agent filter, the original filter has
1194 * been added to it thus replace the filter
1195 * expression.
1196 */
1197 filter_expression = agent_filter;
1198 free_filter_expression = 1;
1199 }
1200 }
1201
1202 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1203 if (ret) {
1204 goto filter_error;
1205 }
1206
1207 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
1208 + bytecode_get_len(&ctx->bytecode->b);
1209 lsm.u.enable.expression_len = strlen(filter_expression) + 1;
1210 }
1211
1212 ret = lttng_dynamic_buffer_set_capacity(&payload.buffer,
1213 lsm.u.enable.bytecode_len +
1214 lsm.u.enable.expression_len +
1215 LTTNG_SYMBOL_NAME_LEN *
1216 exclusion_count);
1217 if (ret) {
1218 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1219 goto mem_error;
1220 }
1221
1222 /* Put exclusion names first in the data. */
1223 for (i = 0; i < exclusion_count; i++) {
1224 size_t exclusion_len;
1225
1226 exclusion_len = lttng_strnlen(exclusion_list[i],
1227 LTTNG_SYMBOL_NAME_LEN);
1228 if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) {
1229 /* Exclusion is not NULL-terminated. */
1230 ret = -LTTNG_ERR_INVALID;
1231 goto mem_error;
1232 }
1233
1234 ret = lttng_dynamic_buffer_append(&payload.buffer,
1235 exclusion_list[i], exclusion_len);
1236 if (ret) {
1237 goto mem_error;
1238 }
1239
1240 /*
1241 * Padding the rest of the entry with zeros. Every exclusion
1242 * entries take LTTNG_SYMBOL_NAME_LEN bytes in the buffer.
1243 */
1244 ret = lttng_dynamic_buffer_set_size(&payload.buffer,
1245 LTTNG_SYMBOL_NAME_LEN * (i + 1));
1246 if (ret) {
1247 goto mem_error;
1248 }
1249 }
1250
1251 /* Add filter expression next. */
1252 if (filter_expression) {
1253 ret = lttng_dynamic_buffer_append(&payload.buffer,
1254 filter_expression, lsm.u.enable.expression_len);
1255 if (ret) {
1256 goto mem_error;
1257 }
1258 }
1259 /* Add filter bytecode next. */
1260 if (ctx && lsm.u.enable.bytecode_len != 0) {
1261 ret = lttng_dynamic_buffer_append(&payload.buffer,
1262 &ctx->bytecode->b, lsm.u.enable.bytecode_len);
1263 if (ret) {
1264 goto mem_error;
1265 }
1266 }
1267 if (ev->extended.ptr) {
1268 struct lttng_event_extended *ev_ext =
1269 (struct lttng_event_extended *) ev->extended.ptr;
1270
1271 if (ev_ext->probe_location) {
1272 /*
1273 * lttng_userspace_probe_location_serialize returns the
1274 * number of bytes that was appended to the buffer.
1275 */
1276 ret = lttng_userspace_probe_location_serialize(
1277 ev_ext->probe_location, &payload);
1278 if (ret < 0) {
1279 goto mem_error;
1280 }
1281
1282 /*
1283 * Set the size of the userspace probe location element
1284 * of the buffer so that the receiving side knows where
1285 * to split it.
1286 */
1287 lsm.u.enable.userspace_probe_location_len = ret;
1288 }
1289 }
1290
1291 {
1292 struct lttng_payload_view view = lttng_payload_view_from_payload(
1293 &payload, 0, -1);
1294 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1295 int fd_to_send;
1296
1297 if (fd_count < 0) {
1298 goto mem_error;
1299 }
1300
1301 assert(fd_count == 0 || fd_count == 1);
1302 if (fd_count == 1) {
1303 struct fd_handle *h =
1304 lttng_payload_view_pop_fd_handle(&view);
1305
1306 if (!h) {
1307 goto mem_error;
1308 }
1309
1310 fd_to_send = fd_handle_get_fd(h);
1311 fd_handle_put(h);
1312 }
1313
1314 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1315 fd_count ? &fd_to_send : NULL, fd_count,
1316 view.buffer.size ? view.buffer.data : NULL,
1317 view.buffer.size, NULL, NULL, 0);
1318 }
1319
1320 mem_error:
1321 if (filter_expression && ctx) {
1322 filter_bytecode_free(ctx);
1323 filter_ir_free(ctx);
1324 filter_parser_ctx_free(ctx);
1325 }
1326 filter_error:
1327 if (free_filter_expression) {
1328 /*
1329 * The filter expression has been replaced and must be freed as
1330 * it is not the original filter expression received as a
1331 * parameter.
1332 */
1333 free(filter_expression);
1334 }
1335 error:
1336 /*
1337 * Return directly to the caller and don't ask the sessiond since
1338 * something went wrong in the parsing of data above.
1339 */
1340 lttng_payload_reset(&payload);
1341 return ret;
1342
1343 ask_sessiond:
1344 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1345 return ret;
1346 }
1347
1348 int lttng_disable_event_ext(struct lttng_handle *handle,
1349 struct lttng_event *ev, const char *channel_name,
1350 const char *original_filter_expression)
1351 {
1352 struct lttcomm_session_msg lsm;
1353 char *varlen_data;
1354 int ret = 0;
1355 unsigned int free_filter_expression = 0;
1356 struct filter_parser_ctx *ctx = NULL;
1357 /*
1358 * Cast as non-const since we may replace the filter expression
1359 * by a dynamically allocated string. Otherwise, the original
1360 * string is not modified.
1361 */
1362 char *filter_expression = (char *) original_filter_expression;
1363
1364 if (handle == NULL || ev == NULL) {
1365 ret = -LTTNG_ERR_INVALID;
1366 goto error;
1367 }
1368
1369 /*
1370 * Empty filter string will always be rejected by the parser
1371 * anyway, so treat this corner-case early to eliminate
1372 * lttng_fmemopen error for 0-byte allocation.
1373 */
1374 if (filter_expression && filter_expression[0] == '\0') {
1375 ret = -LTTNG_ERR_INVALID;
1376 goto error;
1377 }
1378
1379 memset(&lsm, 0, sizeof(lsm));
1380
1381 /* If no channel name, send empty string. */
1382 ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "",
1383 sizeof(lsm.u.disable.channel_name));
1384 if (ret) {
1385 ret = -LTTNG_ERR_INVALID;
1386 goto error;
1387 }
1388
1389 lsm.cmd_type = LTTNG_DISABLE_EVENT;
1390
1391 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1392 memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event));
1393
1394 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1395 sizeof(lsm.session.name));
1396 if (ret) {
1397 ret = -LTTNG_ERR_INVALID;
1398 goto error;
1399 }
1400
1401 lsm.u.disable.bytecode_len = 0;
1402
1403 /*
1404 * For the JUL domain, a filter is enforced except for the
1405 * disable all event. This is done to avoid having the event in
1406 * all sessions thus filtering by logger name.
1407 */
1408 if (filter_expression == NULL &&
1409 (handle->domain.type != LTTNG_DOMAIN_JUL &&
1410 handle->domain.type != LTTNG_DOMAIN_LOG4J &&
1411 handle->domain.type != LTTNG_DOMAIN_PYTHON)) {
1412 goto ask_sessiond;
1413 }
1414
1415 /*
1416 * We have a filter, so we need to set up a variable-length
1417 * memory block from where to send the data.
1418 */
1419
1420 /* Parse filter expression */
1421 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1422 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1423 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1424 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1425 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1426 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1427 char *agent_filter;
1428
1429 /* Setup JUL filter if needed. */
1430 agent_filter = set_agent_filter(filter_expression, ev);
1431 if (!agent_filter) {
1432 if (!filter_expression) {
1433 /*
1434 * No JUL and no filter, just skip
1435 * everything below.
1436 */
1437 goto ask_sessiond;
1438 }
1439 } else {
1440 /*
1441 * With a JUL filter, the original filter has
1442 * been added to it thus replace the filter
1443 * expression.
1444 */
1445 filter_expression = agent_filter;
1446 free_filter_expression = 1;
1447 }
1448 }
1449
1450 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1451 if (ret) {
1452 goto filter_error;
1453 }
1454
1455 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
1456 + bytecode_get_len(&ctx->bytecode->b);
1457 lsm.u.enable.expression_len = strlen(filter_expression) + 1;
1458 }
1459
1460 varlen_data = zmalloc(lsm.u.disable.bytecode_len
1461 + lsm.u.disable.expression_len);
1462 if (!varlen_data) {
1463 ret = -LTTNG_ERR_EXCLUSION_NOMEM;
1464 goto mem_error;
1465 }
1466
1467 /* Add filter expression. */
1468 if (lsm.u.disable.expression_len != 0) {
1469 memcpy(varlen_data,
1470 filter_expression,
1471 lsm.u.disable.expression_len);
1472 }
1473 /* Add filter bytecode next. */
1474 if (ctx && lsm.u.disable.bytecode_len != 0) {
1475 memcpy(varlen_data
1476 + lsm.u.disable.expression_len,
1477 &ctx->bytecode->b,
1478 lsm.u.disable.bytecode_len);
1479 }
1480
1481 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data,
1482 lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL);
1483 free(varlen_data);
1484
1485 mem_error:
1486 if (filter_expression && ctx) {
1487 filter_bytecode_free(ctx);
1488 filter_ir_free(ctx);
1489 filter_parser_ctx_free(ctx);
1490 }
1491 filter_error:
1492 if (free_filter_expression) {
1493 /*
1494 * The filter expression has been replaced and must be freed as
1495 * it is not the original filter expression received as a
1496 * parameter.
1497 */
1498 free(filter_expression);
1499 }
1500 error:
1501 /*
1502 * Return directly to the caller and don't ask the sessiond since
1503 * something went wrong in the parsing of data above.
1504 */
1505 return ret;
1506
1507 ask_sessiond:
1508 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1509 return ret;
1510 }
1511
1512 /*
1513 * Disable event(s) of a channel and domain.
1514 * If no event name is specified, all events are disabled.
1515 * If no channel name is specified, the default 'channel0' is used.
1516 * Returns size of returned session payload data or a negative error code.
1517 */
1518 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1519 const char *channel_name)
1520 {
1521 int ret;
1522 struct lttng_event ev;
1523
1524 memset(&ev, 0, sizeof(ev));
1525 ev.loglevel = -1;
1526 ev.type = LTTNG_EVENT_ALL;
1527 ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name));
1528 if (ret) {
1529 ret = -LTTNG_ERR_INVALID;
1530 goto end;
1531 }
1532
1533 ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1534 end:
1535 return ret;
1536 }
1537
1538 struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1539 {
1540 struct lttng_channel *channel = NULL;
1541
1542 if (!domain) {
1543 goto end;
1544 }
1545
1546 /* Validate domain. */
1547 switch (domain->type) {
1548 case LTTNG_DOMAIN_UST:
1549 switch (domain->buf_type) {
1550 case LTTNG_BUFFER_PER_UID:
1551 case LTTNG_BUFFER_PER_PID:
1552 break;
1553 default:
1554 goto end;
1555 }
1556 break;
1557 case LTTNG_DOMAIN_KERNEL:
1558 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1559 goto end;
1560 }
1561 break;
1562 default:
1563 goto end;
1564 }
1565
1566 channel = lttng_channel_create_internal();
1567 if (!channel) {
1568 goto end;
1569 }
1570
1571 lttng_channel_set_default_attr(domain, &channel->attr);
1572 end:
1573 return channel;
1574 }
1575
1576 void lttng_channel_destroy(struct lttng_channel *channel)
1577 {
1578 if (!channel) {
1579 return;
1580 }
1581
1582 if (channel->attr.extended.ptr) {
1583 free(channel->attr.extended.ptr);
1584 }
1585 free(channel);
1586 }
1587
1588 /*
1589 * Enable channel per domain
1590 * Returns size of returned session payload data or a negative error code.
1591 */
1592 int lttng_enable_channel(struct lttng_handle *handle,
1593 struct lttng_channel *in_chan)
1594 {
1595 enum lttng_error_code ret_code;
1596 int ret;
1597 struct lttng_dynamic_buffer buffer;
1598 struct lttcomm_session_msg lsm;
1599 uint64_t total_buffer_size_needed_per_cpu = 0;
1600 struct lttng_channel *channel = NULL;
1601
1602 lttng_dynamic_buffer_init(&buffer);
1603
1604 /* NULL arguments are forbidden. No default values. */
1605 if (handle == NULL || in_chan == NULL) {
1606 ret = -LTTNG_ERR_INVALID;
1607 goto end;
1608 }
1609
1610 /*
1611 * Verify that the amount of memory required to create the requested
1612 * buffer is available on the system at the moment.
1613 */
1614 if (in_chan->attr.num_subbuf >
1615 UINT64_MAX / in_chan->attr.subbuf_size) {
1616 /* Overflow */
1617 ret = -LTTNG_ERR_OVERFLOW;
1618 goto end;
1619 }
1620
1621 total_buffer_size_needed_per_cpu =
1622 in_chan->attr.num_subbuf * in_chan->attr.subbuf_size;
1623 ret_code = check_enough_available_memory(
1624 total_buffer_size_needed_per_cpu);
1625 if (ret_code != LTTNG_OK) {
1626 ret = -ret_code;
1627 goto end;
1628 }
1629
1630 /* Copy the channel for easier manipulation. */
1631 channel = lttng_channel_copy(in_chan);
1632 if (!channel) {
1633 ret = -LTTNG_ERR_NOMEM;
1634 goto end;
1635 }
1636
1637 /* Populate the channel extended attribute if necessary. */
1638 if (!channel->attr.extended.ptr) {
1639 struct lttng_channel_extended *extended =
1640 (struct lttng_channel_extended *) zmalloc(
1641 sizeof(*extended));
1642
1643 if (!extended) {
1644 ret = -LTTNG_ERR_NOMEM;
1645 goto end;
1646 }
1647 lttng_channel_set_default_extended_attr(
1648 &handle->domain, extended);
1649 channel->attr.extended.ptr = extended;
1650 }
1651
1652 /* Prepare the payload */
1653 memset(&lsm, 0, sizeof(lsm));
1654
1655 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1656 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1657
1658 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1659 sizeof(lsm.session.name));
1660 if (ret) {
1661 ret = -LTTNG_ERR_INVALID;
1662 goto end;
1663 }
1664
1665 ret = lttng_channel_serialize(channel, &buffer);
1666 if (ret) {
1667 ret = -LTTNG_ERR_FATAL;
1668 goto end;
1669 }
1670
1671 lsm.u.channel.length = buffer.size;
1672
1673 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1674 &lsm, buffer.data, buffer.size, NULL);
1675 end:
1676 lttng_channel_destroy(channel);
1677 lttng_dynamic_buffer_reset(&buffer);
1678 return ret;
1679 }
1680
1681 /*
1682 * All tracing will be stopped for registered events of the channel.
1683 * Returns size of returned session payload data or a negative error code.
1684 */
1685 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1686 {
1687 int ret;
1688 struct lttcomm_session_msg lsm;
1689
1690 /* Safety check. Both are mandatory. */
1691 if (handle == NULL || name == NULL) {
1692 return -LTTNG_ERR_INVALID;
1693 }
1694
1695 memset(&lsm, 0, sizeof(lsm));
1696
1697 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1698
1699 ret = lttng_strncpy(lsm.u.disable.channel_name, name,
1700 sizeof(lsm.u.disable.channel_name));
1701 if (ret) {
1702 ret = -LTTNG_ERR_INVALID;
1703 goto end;
1704 }
1705
1706 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1707
1708 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1709 sizeof(lsm.session.name));
1710 if (ret) {
1711 ret = -LTTNG_ERR_INVALID;
1712 goto end;
1713 }
1714
1715 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1716 end:
1717 return ret;
1718 }
1719
1720 /*
1721 * Lists all available tracepoints of domain.
1722 * Sets the contents of the events array.
1723 * Returns the number of lttng_event entries in events;
1724 * on error, returns a negative value.
1725 */
1726 int lttng_list_tracepoints(struct lttng_handle *handle,
1727 struct lttng_event **events)
1728 {
1729 int ret;
1730 struct lttcomm_session_msg lsm;
1731
1732 if (handle == NULL) {
1733 return -LTTNG_ERR_INVALID;
1734 }
1735
1736 memset(&lsm, 0, sizeof(lsm));
1737 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1738 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1739
1740 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1741 if (ret < 0) {
1742 return ret;
1743 }
1744
1745 return ret / sizeof(struct lttng_event);
1746 }
1747
1748 /*
1749 * Lists all available tracepoint fields of domain.
1750 * Sets the contents of the event field array.
1751 * Returns the number of lttng_event_field entries in events;
1752 * on error, returns a negative value.
1753 */
1754 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1755 struct lttng_event_field **fields)
1756 {
1757 int ret;
1758 struct lttcomm_session_msg lsm;
1759
1760 if (handle == NULL) {
1761 return -LTTNG_ERR_INVALID;
1762 }
1763
1764 memset(&lsm, 0, sizeof(lsm));
1765 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1766 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1767
1768 ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields);
1769 if (ret < 0) {
1770 return ret;
1771 }
1772
1773 return ret / sizeof(struct lttng_event_field);
1774 }
1775
1776 /*
1777 * Lists all available kernel system calls. Allocates and sets the contents of
1778 * the events array.
1779 *
1780 * Returns the number of lttng_event entries in events; on error, returns a
1781 * negative value.
1782 */
1783 int lttng_list_syscalls(struct lttng_event **events)
1784 {
1785 int ret;
1786 struct lttcomm_session_msg lsm;
1787
1788 if (!events) {
1789 return -LTTNG_ERR_INVALID;
1790 }
1791
1792 memset(&lsm, 0, sizeof(lsm));
1793 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1794 /* Force kernel domain for system calls. */
1795 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1796
1797 ret = lttng_ctl_ask_sessiond(&lsm, (void **) events);
1798 if (ret < 0) {
1799 return ret;
1800 }
1801
1802 return ret / sizeof(struct lttng_event);
1803 }
1804
1805 /*
1806 * Returns a human readable string describing
1807 * the error code (a negative value).
1808 */
1809 const char *lttng_strerror(int code)
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 = -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(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 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 = 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 void *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 = ALIGN_TO(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 ALIGN_TO(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 char *new_group;
2677 if (name == NULL) {
2678 return -LTTNG_ERR_INVALID;
2679 }
2680
2681 if (asprintf(&new_group, "%s", name) < 0) {
2682 return -LTTNG_ERR_FATAL;
2683 }
2684
2685 free(tracing_group);
2686 tracing_group = new_group;
2687 new_group = NULL;
2688
2689 return 0;
2690 }
2691
2692 int lttng_calibrate(struct lttng_handle *handle,
2693 struct lttng_calibrate *calibrate)
2694 {
2695 /*
2696 * This command was removed in LTTng 2.9.
2697 */
2698 return -LTTNG_ERR_UND;
2699 }
2700
2701 /*
2702 * Set default channel attributes.
2703 * If either or both of the arguments are null, attr content is zeroe'd.
2704 */
2705 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2706 struct lttng_channel_attr *attr)
2707 {
2708 struct lttng_channel_extended *extended;
2709
2710 /* Safety check */
2711 if (attr == NULL || domain == NULL) {
2712 return;
2713 }
2714
2715 /* Save the pointer for later use */
2716 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2717 memset(attr, 0, sizeof(struct lttng_channel_attr));
2718
2719 /* Same for all domains. */
2720 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2721 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2722 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2723
2724 switch (domain->type) {
2725 case LTTNG_DOMAIN_KERNEL:
2726 attr->switch_timer_interval =
2727 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2728 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2729 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2730 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2731 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2732 break;
2733 case LTTNG_DOMAIN_UST:
2734 switch (domain->buf_type) {
2735 case LTTNG_BUFFER_PER_UID:
2736 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2737 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2738 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2739 attr->switch_timer_interval =
2740 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2741 attr->read_timer_interval =
2742 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2743 break;
2744 case LTTNG_BUFFER_PER_PID:
2745 default:
2746 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2747 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2748 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2749 attr->switch_timer_interval =
2750 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2751 attr->read_timer_interval =
2752 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2753 break;
2754 }
2755 default:
2756 /* Default behavior: leave set to 0. */
2757 break;
2758 }
2759
2760 if (extended) {
2761 lttng_channel_set_default_extended_attr(domain, extended);
2762 }
2763
2764 /* Reassign the extended pointer. */
2765 attr->extended.ptr = extended;
2766 }
2767
2768 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2769 uint64_t *discarded_events)
2770 {
2771 int ret = 0;
2772 struct lttng_channel_extended *chan_ext;
2773
2774 if (!channel || !discarded_events) {
2775 ret = -LTTNG_ERR_INVALID;
2776 goto end;
2777 }
2778
2779 chan_ext = channel->attr.extended.ptr;
2780 if (!chan_ext) {
2781 /*
2782 * This can happen since the lttng_channel structure is
2783 * used for other tasks where this pointer is never set.
2784 */
2785 *discarded_events = 0;
2786 goto end;
2787 }
2788
2789 *discarded_events = chan_ext->discarded_events;
2790 end:
2791 return ret;
2792 }
2793
2794 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2795 uint64_t *lost_packets)
2796 {
2797 int ret = 0;
2798 struct lttng_channel_extended *chan_ext;
2799
2800 if (!channel || !lost_packets) {
2801 ret = -LTTNG_ERR_INVALID;
2802 goto end;
2803 }
2804
2805 chan_ext = channel->attr.extended.ptr;
2806 if (!chan_ext) {
2807 /*
2808 * This can happen since the lttng_channel structure is
2809 * used for other tasks where this pointer is never set.
2810 */
2811 *lost_packets = 0;
2812 goto end;
2813 }
2814
2815 *lost_packets = chan_ext->lost_packets;
2816 end:
2817 return ret;
2818 }
2819
2820 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2821 uint64_t *monitor_timer_interval)
2822 {
2823 int ret = 0;
2824
2825 if (!chan || !monitor_timer_interval) {
2826 ret = -LTTNG_ERR_INVALID;
2827 goto end;
2828 }
2829
2830 if (!chan->attr.extended.ptr) {
2831 ret = -LTTNG_ERR_INVALID;
2832 goto end;
2833 }
2834
2835 *monitor_timer_interval = ((struct lttng_channel_extended *)
2836 chan->attr.extended.ptr)->monitor_timer_interval;
2837 end:
2838 return ret;
2839 }
2840
2841 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2842 uint64_t monitor_timer_interval)
2843 {
2844 int ret = 0;
2845
2846 if (!chan || !chan->attr.extended.ptr) {
2847 ret = -LTTNG_ERR_INVALID;
2848 goto end;
2849 }
2850
2851 ((struct lttng_channel_extended *)
2852 chan->attr.extended.ptr)->monitor_timer_interval =
2853 monitor_timer_interval;
2854 end:
2855 return ret;
2856 }
2857
2858 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2859 int64_t *blocking_timeout)
2860 {
2861 int ret = 0;
2862
2863 if (!chan || !blocking_timeout) {
2864 ret = -LTTNG_ERR_INVALID;
2865 goto end;
2866 }
2867
2868 if (!chan->attr.extended.ptr) {
2869 ret = -LTTNG_ERR_INVALID;
2870 goto end;
2871 }
2872
2873 *blocking_timeout = ((struct lttng_channel_extended *)
2874 chan->attr.extended.ptr)->blocking_timeout;
2875 end:
2876 return ret;
2877 }
2878
2879 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2880 int64_t blocking_timeout)
2881 {
2882 int ret = 0;
2883 int64_t msec_timeout;
2884
2885 if (!chan || !chan->attr.extended.ptr) {
2886 ret = -LTTNG_ERR_INVALID;
2887 goto end;
2888 }
2889
2890 if (blocking_timeout < 0 && blocking_timeout != -1) {
2891 ret = -LTTNG_ERR_INVALID;
2892 goto end;
2893 }
2894
2895 /*
2896 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2897 * us to accept a narrower range of values (msecs expressed as a signed
2898 * 32-bit integer).
2899 */
2900 msec_timeout = blocking_timeout / 1000;
2901 if (msec_timeout != (int32_t) msec_timeout) {
2902 ret = -LTTNG_ERR_INVALID;
2903 goto end;
2904 }
2905
2906 ((struct lttng_channel_extended *)
2907 chan->attr.extended.ptr)->blocking_timeout =
2908 blocking_timeout;
2909 end:
2910 return ret;
2911 }
2912
2913 /*
2914 * Check if session daemon is alive.
2915 *
2916 * Return 1 if alive or 0 if not.
2917 * On error returns a negative value.
2918 */
2919 int lttng_session_daemon_alive(void)
2920 {
2921 int ret;
2922
2923 ret = set_session_daemon_path();
2924 if (ret < 0) {
2925 /* Error. */
2926 return ret;
2927 }
2928
2929 if (*sessiond_sock_path == '\0') {
2930 /*
2931 * No socket path set. Weird error which means the constructor
2932 * was not called.
2933 */
2934 assert(0);
2935 }
2936
2937 ret = try_connect_sessiond(sessiond_sock_path);
2938 if (ret < 0) {
2939 /* Not alive. */
2940 return 0;
2941 }
2942
2943 /* Is alive. */
2944 return 1;
2945 }
2946
2947 /*
2948 * Set URL for a consumer for a session and domain.
2949 *
2950 * Return 0 on success, else a negative value.
2951 */
2952 int lttng_set_consumer_url(struct lttng_handle *handle,
2953 const char *control_url, const char *data_url)
2954 {
2955 int ret;
2956 ssize_t size;
2957 struct lttcomm_session_msg lsm;
2958 struct lttng_uri *uris = NULL;
2959
2960 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2961 ret = -LTTNG_ERR_INVALID;
2962 goto error;
2963 }
2964
2965 memset(&lsm, 0, sizeof(lsm));
2966
2967 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2968
2969 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2970 sizeof(lsm.session.name));
2971 if (ret) {
2972 ret = -LTTNG_ERR_INVALID;
2973 goto error;
2974 }
2975
2976 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2977
2978 size = uri_parse_str_urls(control_url, data_url, &uris);
2979 if (size < 0) {
2980 ret = -LTTNG_ERR_INVALID;
2981 goto error;
2982 }
2983
2984 lsm.u.uri.size = size;
2985
2986 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2987 sizeof(struct lttng_uri) * size, NULL);
2988
2989 free(uris);
2990 error:
2991 return ret;
2992 }
2993
2994 /*
2995 * [OBSOLETE]
2996 */
2997 int lttng_enable_consumer(struct lttng_handle *handle);
2998 int lttng_enable_consumer(struct lttng_handle *handle)
2999 {
3000 return -ENOSYS;
3001 }
3002
3003 /*
3004 * [OBSOLETE]
3005 */
3006 int lttng_disable_consumer(struct lttng_handle *handle);
3007 int lttng_disable_consumer(struct lttng_handle *handle)
3008 {
3009 return -ENOSYS;
3010 }
3011
3012 /*
3013 * [OBSOLETE]
3014 */
3015 int _lttng_create_session_ext(const char *name, const char *url,
3016 const char *datetime);
3017 int _lttng_create_session_ext(const char *name, const char *url,
3018 const char *datetime)
3019 {
3020 return -ENOSYS;
3021 }
3022
3023 /*
3024 * For a given session name, this call checks if the data is ready to be read
3025 * or is still being extracted by the consumer(s) hence not ready to be used by
3026 * any readers.
3027 */
3028 int lttng_data_pending(const char *session_name)
3029 {
3030 int ret;
3031 struct lttcomm_session_msg lsm;
3032 uint8_t *pending = NULL;
3033
3034 if (session_name == NULL) {
3035 return -LTTNG_ERR_INVALID;
3036 }
3037
3038 memset(&lsm, 0, sizeof(lsm));
3039 lsm.cmd_type = LTTNG_DATA_PENDING;
3040
3041 ret = lttng_strncpy(lsm.session.name, session_name,
3042 sizeof(lsm.session.name));
3043 if (ret) {
3044 ret = -LTTNG_ERR_INVALID;
3045 goto end;
3046 }
3047
3048 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
3049 if (ret < 0) {
3050 goto end;
3051 } else if (ret != 1) {
3052 /* Unexpected payload size */
3053 ret = -LTTNG_ERR_INVALID;
3054 goto end;
3055 } else if (!pending) {
3056 /* Internal error. */
3057 ret = -LTTNG_ERR_UNK;
3058 goto end;
3059 }
3060
3061 ret = (int) *pending;
3062 end:
3063 free(pending);
3064 return ret;
3065 }
3066
3067 /*
3068 * Regenerate the metadata for a session.
3069 * Return 0 on success, a negative error code on error.
3070 */
3071 int lttng_regenerate_metadata(const char *session_name)
3072 {
3073 int ret;
3074 struct lttcomm_session_msg lsm;
3075
3076 if (!session_name) {
3077 ret = -LTTNG_ERR_INVALID;
3078 goto end;
3079 }
3080
3081 memset(&lsm, 0, sizeof(lsm));
3082 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
3083
3084 ret = lttng_strncpy(lsm.session.name, session_name,
3085 sizeof(lsm.session.name));
3086 if (ret) {
3087 ret = -LTTNG_ERR_INVALID;
3088 goto end;
3089 }
3090
3091 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3092 if (ret < 0) {
3093 goto end;
3094 }
3095
3096 ret = 0;
3097 end:
3098 return ret;
3099 }
3100
3101 /*
3102 * Deprecated, replaced by lttng_regenerate_metadata.
3103 */
3104 int lttng_metadata_regenerate(const char *session_name)
3105 {
3106 return lttng_regenerate_metadata(session_name);
3107 }
3108
3109 /*
3110 * Regenerate the statedump of a session.
3111 * Return 0 on success, a negative error code on error.
3112 */
3113 int lttng_regenerate_statedump(const char *session_name)
3114 {
3115 int ret;
3116 struct lttcomm_session_msg lsm;
3117
3118 if (!session_name) {
3119 ret = -LTTNG_ERR_INVALID;
3120 goto end;
3121 }
3122
3123 memset(&lsm, 0, sizeof(lsm));
3124 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
3125
3126 ret = lttng_strncpy(lsm.session.name, session_name,
3127 sizeof(lsm.session.name));
3128 if (ret) {
3129 ret = -LTTNG_ERR_INVALID;
3130 goto end;
3131 }
3132
3133 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3134 if (ret < 0) {
3135 goto end;
3136 }
3137
3138 ret = 0;
3139 end:
3140 return ret;
3141 }
3142
3143 static
3144 int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name,
3145 bool generate_name)
3146 {
3147 int ret;
3148 struct lttcomm_session_msg lsm = {
3149 .cmd_type = LTTNG_REGISTER_TRIGGER,
3150 .u.trigger.is_trigger_anonymous = !name && !generate_name,
3151 };
3152 struct lttcomm_session_msg *message_lsm;
3153 struct lttng_payload message;
3154 struct lttng_payload reply;
3155 struct lttng_trigger *reply_trigger = NULL;
3156 enum lttng_domain_type domain_type;
3157 const struct lttng_credentials user_creds = {
3158 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3159 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3160 };
3161 const char *unused_trigger_name = NULL;
3162 enum lttng_trigger_status trigger_status;
3163
3164 lttng_payload_init(&message);
3165 lttng_payload_init(&reply);
3166
3167 if (!trigger) {
3168 ret = -LTTNG_ERR_INVALID;
3169 goto end;
3170 }
3171
3172 trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name);
3173 if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) {
3174 /* Re-using already registered trigger. */
3175 ret = -LTTNG_ERR_INVALID;
3176 goto end;
3177 }
3178
3179 if (name) {
3180 trigger_status = lttng_trigger_set_name(trigger, name);
3181 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3182 ret = -LTTNG_ERR_NOMEM;
3183 goto end;
3184 }
3185 }
3186
3187 if (!trigger->creds.uid.is_set) {
3188 /* Use the client's credentials as the trigger credentials. */
3189 lttng_trigger_set_credentials(trigger, &user_creds);
3190 } else {
3191 /*
3192 * Validate that either the current trigger credentials and the
3193 * client credentials are identical or that the current user is
3194 * root. The root user can register, unregister triggers for
3195 * himself and other users.
3196 *
3197 * This check is also present on the sessiond side, using the
3198 * credentials passed on the socket. These check are all
3199 * "safety" checks.
3200 */
3201 const struct lttng_credentials *trigger_creds =
3202 lttng_trigger_get_credentials(trigger);
3203
3204 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3205 if (lttng_credentials_get_uid(&user_creds) != 0) {
3206 ret = -LTTNG_ERR_EPERM;
3207 goto end_unset_name;
3208 }
3209 }
3210 }
3211
3212 if (!lttng_trigger_validate(trigger)) {
3213 ret = -LTTNG_ERR_INVALID_TRIGGER;
3214 goto end_unset_name;
3215 }
3216
3217 domain_type = lttng_trigger_get_underlying_domain_type_restriction(
3218 trigger);
3219
3220 lsm.domain.type = domain_type;
3221
3222 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3223 if (ret) {
3224 ret = -LTTNG_ERR_NOMEM;
3225 goto end_unset_name;
3226 }
3227
3228 ret = lttng_trigger_serialize(trigger, &message);
3229 if (ret < 0) {
3230 ret = -LTTNG_ERR_UNK;
3231 goto end_unset_name;
3232 }
3233
3234 /*
3235 * This is needed to populate the trigger object size for the command
3236 * header.
3237 */
3238 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3239
3240 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3241
3242 {
3243 struct lttng_payload_view message_view =
3244 lttng_payload_view_from_payload(
3245 &message, 0, -1);
3246
3247 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3248 &message_view);
3249 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3250 if (ret < 0) {
3251 goto end_unset_name;
3252 }
3253 }
3254
3255 {
3256 struct lttng_payload_view reply_view =
3257 lttng_payload_view_from_payload(
3258 &reply, 0, reply.buffer.size);
3259
3260 ret = lttng_trigger_create_from_payload(
3261 &reply_view, &reply_trigger);
3262 if (ret < 0) {
3263 ret = -LTTNG_ERR_INVALID_PROTOCOL;
3264 goto end_unset_name;
3265 }
3266 }
3267
3268 if (name || generate_name) {
3269 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3270 if (ret < 0) {
3271 ret = -LTTNG_ERR_NOMEM;
3272 goto end;
3273 }
3274 }
3275
3276 ret = 0;
3277 goto end;
3278
3279 end_unset_name:
3280 trigger_status = lttng_trigger_set_name(trigger, NULL);
3281 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3282 ret = -LTTNG_ERR_UNK;
3283 }
3284 end:
3285 lttng_payload_reset(&message);
3286 lttng_payload_reset(&reply);
3287 lttng_trigger_destroy(reply_trigger);
3288 return ret;
3289 }
3290
3291 int lttng_register_trigger(struct lttng_trigger *trigger)
3292 {
3293 /* Register an anonymous trigger. */
3294 return _lttng_register_trigger(trigger, NULL, false);
3295 }
3296
3297 enum lttng_error_code lttng_register_trigger_with_name(
3298 struct lttng_trigger *trigger, const char *name)
3299 {
3300 const int ret = _lttng_register_trigger(trigger, name, false);
3301
3302 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3303 }
3304
3305 enum lttng_error_code lttng_register_trigger_with_automatic_name(
3306 struct lttng_trigger *trigger)
3307 {
3308 const int ret = _lttng_register_trigger(trigger, false, true);
3309
3310 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3311 }
3312
3313 enum lttng_error_code lttng_error_query_execute(
3314 const struct lttng_error_query *query,
3315 const struct lttng_endpoint *endpoint,
3316 struct lttng_error_query_results **results)
3317 {
3318 int ret;
3319 enum lttng_error_code ret_code;
3320 struct lttcomm_session_msg lsm = {
3321 .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
3322 };
3323 struct lttng_payload message;
3324 struct lttng_payload reply;
3325 struct lttcomm_session_msg *message_lsm;
3326
3327 lttng_payload_init(&message);
3328 lttng_payload_init(&reply);
3329
3330 if (!query || !results) {
3331 ret_code = LTTNG_ERR_INVALID;
3332 goto end;
3333 }
3334
3335 if (endpoint != lttng_session_daemon_command_endpoint) {
3336 ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
3337 goto end;
3338 }
3339
3340 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3341 if (ret) {
3342 ret_code = LTTNG_ERR_NOMEM;
3343 goto end;
3344 }
3345
3346 ret = lttng_error_query_serialize(query, &message);
3347 if (ret) {
3348 ret_code = LTTNG_ERR_UNK;
3349 goto end;
3350 }
3351
3352 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3353 message_lsm->u.error_query.length =
3354 (uint32_t) message.buffer.size - sizeof(lsm);
3355
3356 {
3357 struct lttng_payload_view message_view =
3358 lttng_payload_view_from_payload(
3359 &message, 0, -1);
3360
3361 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3362 &message_view);
3363 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3364 if (ret < 0) {
3365 ret_code = -ret;
3366 goto end;
3367 }
3368 }
3369
3370 {
3371 ssize_t reply_create_ret;
3372 struct lttng_payload_view reply_view =
3373 lttng_payload_view_from_payload(
3374 &reply, 0, reply.buffer.size);
3375
3376 reply_create_ret = lttng_error_query_results_create_from_payload(
3377 &reply_view, results);
3378 if (reply_create_ret < 0) {
3379 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3380 goto end;
3381 }
3382 }
3383
3384 ret_code = LTTNG_OK;
3385 end:
3386 lttng_payload_reset(&message);
3387 lttng_payload_reset(&reply);
3388 return ret_code;
3389 }
3390
3391 int lttng_unregister_trigger(const struct lttng_trigger *trigger)
3392 {
3393 int ret;
3394 struct lttcomm_session_msg lsm;
3395 struct lttcomm_session_msg *message_lsm;
3396 struct lttng_payload message;
3397 struct lttng_payload reply;
3398 struct lttng_trigger *copy = NULL;
3399 const struct lttng_credentials user_creds = {
3400 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3401 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3402 };
3403
3404 lttng_payload_init(&message);
3405 lttng_payload_init(&reply);
3406
3407 if (!trigger) {
3408 ret = -LTTNG_ERR_INVALID;
3409 goto end;
3410 }
3411
3412 copy = lttng_trigger_copy(trigger);
3413 if (!copy) {
3414 ret = -LTTNG_ERR_UNK;
3415 goto end;
3416 }
3417
3418 if (!copy->creds.uid.is_set) {
3419 /* Use the client credentials as the trigger credentials */
3420 lttng_trigger_set_credentials(copy, &user_creds);
3421 } else {
3422 /*
3423 * Validate that either the current trigger credentials and the
3424 * client credentials are identical or that the current user is
3425 * root. The root user can register, unregister triggers for
3426 * himself and other users.
3427 *
3428 * This check is also present on the sessiond side, using the
3429 * credentials passed on the socket. These check are all
3430 * "safety" checks.
3431 */
3432 const struct lttng_credentials *trigger_creds =
3433 lttng_trigger_get_credentials(copy);
3434 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3435 if (lttng_credentials_get_uid(&user_creds) != 0) {
3436 ret = -LTTNG_ERR_EPERM;
3437 goto end;
3438 }
3439 }
3440 }
3441
3442 if (!lttng_trigger_validate(copy)) {
3443 ret = -LTTNG_ERR_INVALID_TRIGGER;
3444 goto end;
3445 }
3446
3447 memset(&lsm, 0, sizeof(lsm));
3448 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3449
3450 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3451 if (ret) {
3452 ret = -LTTNG_ERR_NOMEM;
3453 goto end;
3454 }
3455
3456 ret = lttng_trigger_serialize(copy, &message);
3457 if (ret < 0) {
3458 ret = -LTTNG_ERR_UNK;
3459 goto end;
3460 }
3461
3462 /*
3463 * This is needed to populate the trigger object size for the command
3464 * header and number of fds sent.
3465 */
3466 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3467
3468 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3469
3470 {
3471 struct lttng_payload_view message_view =
3472 lttng_payload_view_from_payload(
3473 &message, 0, -1);
3474
3475 /*
3476 * Update the message header with the number of fd that will be
3477 * sent.
3478 */
3479 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3480 &message_view);
3481
3482 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3483 if (ret < 0) {
3484 goto end;
3485 }
3486 }
3487
3488 ret = 0;
3489 end:
3490 lttng_trigger_destroy(copy);
3491 lttng_payload_reset(&message);
3492 lttng_payload_reset(&reply);
3493 return ret;
3494 }
3495
3496 /*
3497 * Ask the session daemon for all registered triggers for the current user.
3498 *
3499 * Allocates and return an lttng_triggers set.
3500 * On error, returns a suitable lttng_error_code.
3501 */
3502 enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers)
3503 {
3504 int ret;
3505 enum lttng_error_code ret_code = LTTNG_OK;
3506 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS };
3507 struct lttng_triggers *local_triggers = NULL;
3508 struct lttng_payload reply;
3509 struct lttng_payload_view lsm_view =
3510 lttng_payload_view_init_from_buffer(
3511 (const char *) &lsm, 0, sizeof(lsm));
3512
3513 lttng_payload_init(&reply);
3514
3515 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
3516 if (ret < 0) {
3517 ret_code = (enum lttng_error_code) -ret;
3518 goto end;
3519 }
3520
3521 {
3522 struct lttng_payload_view reply_view =
3523 lttng_payload_view_from_payload(
3524 &reply, 0, reply.buffer.size);
3525
3526 ret = lttng_triggers_create_from_payload(
3527 &reply_view, &local_triggers);
3528 if (ret < 0) {
3529 ret_code = LTTNG_ERR_FATAL;
3530 goto end;
3531 }
3532 }
3533
3534 *triggers = local_triggers;
3535 local_triggers = NULL;
3536 end:
3537 lttng_payload_reset(&reply);
3538 lttng_triggers_destroy(local_triggers);
3539 return ret_code;
3540 }
3541
3542 /*
3543 * lib constructor.
3544 */
3545 static void __attribute__((constructor)) init(void)
3546 {
3547 /* Set default session group */
3548 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
3549 }
3550
3551 /*
3552 * lib destructor.
3553 */
3554 static void __attribute__((destructor)) lttng_ctl_exit(void)
3555 {
3556 free(tracing_group);
3557 }
This page took 0.103058 seconds and 4 git commands to generate.