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