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