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