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