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