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