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