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