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