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