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