Implement filter expression to bytecode compiler in liblttng-ctl
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
82a3637f
DG
2 * liblttngctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
826d496d 6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d 7 *
d14d33bf
AM
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.
82a3637f
DG
11 *
12 * This library is distributed in the hope that it will be useful,
fac6795d 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
d14d33bf
AM
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
fac6795d
DG
20 */
21
22#define _GNU_SOURCE
fac6795d 23#include <grp.h>
1e307fab 24#include <errno.h>
fac6795d
DG
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
990570ed
DG
30#include <common/common.h>
31#include <common/defaults.h>
db758600 32#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 33#include <lttng/lttng.h>
fac6795d
DG
34
35/* Socket to session daemon for communication */
36static int sessiond_socket;
37static char sessiond_sock_path[PATH_MAX];
38
fac6795d
DG
39/* Variables */
40static char *tracing_group;
41static int connected;
42
97e19046
DG
43/* Global */
44
45/*
46 * Those two variables are used by error.h to silent or control the verbosity of
47 * error message. They are global to the library so application linking with it
48 * are able to compile correctly and also control verbosity of the library.
49 *
50 * Note that it is *not* possible to silent ERR() and PERROR() macros.
51 */
52int lttng_opt_quiet;
53int lttng_opt_verbose;
54
99497cd0
MD
55/*
56 * Copy string from src to dst and enforce null terminated byte.
57 */
58static void copy_string(char *dst, const char *src, size_t len)
59{
e7d6716d 60 if (src && dst) {
99497cd0
MD
61 strncpy(dst, src, len);
62 /* Enforce the NULL terminated byte */
63 dst[len - 1] = '\0';
cd80958d
DG
64 } else if (dst) {
65 dst[0] = '\0';
99497cd0
MD
66 }
67}
68
fac6795d 69/*
cd80958d 70 * Copy domain to lttcomm_session_msg domain.
fac6795d 71 *
cd80958d
DG
72 * If domain is unknown, default domain will be the kernel.
73 */
74static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
75{
76 if (src && dst) {
77 switch (src->type) {
00e2e675
DG
78 case LTTNG_DOMAIN_KERNEL:
79 case LTTNG_DOMAIN_UST:
80 /*
81 case LTTNG_DOMAIN_UST_EXEC_NAME:
82 case LTTNG_DOMAIN_UST_PID:
83 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
84 */
85 memcpy(dst, src, sizeof(struct lttng_domain));
86 break;
87 default:
88 memset(dst, 0, sizeof(struct lttng_domain));
89 dst->type = LTTNG_DOMAIN_KERNEL;
90 break;
cd80958d
DG
91 }
92 }
93}
94
95/*
96 * Send lttcomm_session_msg to the session daemon.
fac6795d 97 *
1c8d13c8
TD
98 * On success, returns the number of bytes sent (>=0)
99 * On error, returns -1
fac6795d 100 */
cd80958d 101static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
102{
103 int ret;
104
105 if (!connected) {
e065084a
DG
106 ret = -ENOTCONN;
107 goto end;
fac6795d
DG
108 }
109
be040666 110 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 111 sizeof(struct lttcomm_session_msg));
e065084a
DG
112
113end:
114 return ret;
115}
116
117/*
cd80958d 118 * Receive data from the sessiond socket.
e065084a 119 *
1c8d13c8
TD
120 * On success, returns the number of bytes received (>=0)
121 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 122 */
ca95a216 123static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
124{
125 int ret;
126
127 if (!connected) {
128 ret = -ENOTCONN;
129 goto end;
fac6795d
DG
130 }
131
ca95a216 132 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 133
e065084a 134end:
fac6795d
DG
135 return ret;
136}
137
138/*
1c8d13c8 139 * Check if we are in the specified group.
65beb5ff 140 *
2269e89e 141 * If yes return 1, else return -1.
947308c4
DG
142 */
143static int check_tracing_group(const char *grp_name)
144{
145 struct group *grp_tracing; /* no free(). See getgrnam(3) */
146 gid_t *grp_list;
147 int grp_list_size, grp_id, i;
148 int ret = -1;
149
150 /* Get GID of group 'tracing' */
151 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
152 if (!grp_tracing) {
153 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
154 goto end;
155 }
156
157 /* Get number of supplementary group IDs */
158 grp_list_size = getgroups(0, NULL);
159 if (grp_list_size < 0) {
160 perror("getgroups");
161 goto end;
162 }
163
164 /* Alloc group list of the right size */
165 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 166 if (!grp_list) {
1c8d13c8 167 perror("malloc");
00795392
MD
168 goto end;
169 }
947308c4 170 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 171 if (grp_id < 0) {
947308c4
DG
172 perror("getgroups");
173 goto free_list;
174 }
175
176 for (i = 0; i < grp_list_size; i++) {
177 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 178 ret = 1;
947308c4
DG
179 break;
180 }
181 }
182
183free_list:
184 free(grp_list);
185
186end:
187 return ret;
188}
189
190/*
2269e89e
DG
191 * Try connect to session daemon with sock_path.
192 *
193 * Return 0 on success, else -1
194 */
195static int try_connect_sessiond(const char *sock_path)
196{
197 int ret;
198
199 /* If socket exist, we check if the daemon listens for connect. */
200 ret = access(sock_path, F_OK);
201 if (ret < 0) {
202 /* Not alive */
203 return -1;
204 }
205
206 ret = lttcomm_connect_unix_sock(sock_path);
207 if (ret < 0) {
208 /* Not alive */
209 return -1;
210 }
211
212 ret = lttcomm_close_unix_sock(ret);
213 if (ret < 0) {
214 perror("lttcomm_close_unix_sock");
215 }
216
217 return 0;
218}
219
220/*
1c8d13c8
TD
221 * Set sessiond socket path by putting it in the global
222 * sessiond_sock_path variable.
223 * Returns 0 on success,
224 * -ENOMEM on failure (the sessiond socket path is somehow too long)
947308c4
DG
225 */
226static int set_session_daemon_path(void)
227{
228 int ret;
2269e89e
DG
229 int in_tgroup = 0; /* In tracing group */
230 uid_t uid;
231
232 uid = getuid();
947308c4 233
2269e89e
DG
234 if (uid != 0) {
235 /* Are we in the tracing group ? */
236 in_tgroup = check_tracing_group(tracing_group);
237 }
238
08a9c49f
TD
239 if ((uid == 0) || in_tgroup) {
240 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 241 sizeof(sessiond_sock_path));
08a9c49f 242 }
2269e89e 243
08a9c49f
TD
244 if (uid != 0) {
245 if (in_tgroup) {
246 /* Tracing group */
247 ret = try_connect_sessiond(sessiond_sock_path);
248 if (ret >= 0) {
249 goto end;
2269e89e 250 }
08a9c49f 251 /* Global session daemon not available... */
2269e89e 252 }
08a9c49f
TD
253 /* ...or not in tracing group (and not root), default */
254
255 /*
256 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
257 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
258 */
259 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
260 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
261 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
947308c4
DG
262 return -ENOMEM;
263 }
947308c4 264 }
08a9c49f 265end:
947308c4
DG
266 return 0;
267}
268
65beb5ff
DG
269/*
270 * Connect to the LTTng session daemon.
271 *
272 * On success, return 0. On error, return -1.
273 */
274static int connect_sessiond(void)
275{
276 int ret;
277
278 ret = set_session_daemon_path();
279 if (ret < 0) {
1c8d13c8 280 return -1; /* set_session_daemon_path() returns -ENOMEM */
65beb5ff
DG
281 }
282
283 /* Connect to the sesssion daemon */
284 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
285 if (ret < 0) {
286 return ret;
287 }
288
289 sessiond_socket = ret;
290 connected = 1;
291
292 return 0;
293}
294
295/*
1c8d13c8
TD
296 * Clean disconnect from the session daemon.
297 * On success, return 0. On error, return -1.
65beb5ff
DG
298 */
299static int disconnect_sessiond(void)
300{
301 int ret = 0;
302
303 if (connected) {
304 ret = lttcomm_close_unix_sock(sessiond_socket);
305 sessiond_socket = 0;
306 connected = 0;
307 }
308
309 return ret;
310}
311
35a6fdb7 312/*
cd80958d 313 * Ask the session daemon a specific command and put the data into buf.
65beb5ff 314 *
af87c45a 315 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 316 */
cd80958d 317static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
65beb5ff
DG
318{
319 int ret;
320 size_t size;
321 void *data = NULL;
cd80958d 322 struct lttcomm_lttng_msg llm;
65beb5ff
DG
323
324 ret = connect_sessiond();
325 if (ret < 0) {
326 goto end;
327 }
328
65beb5ff 329 /* Send command to session daemon */
cd80958d 330 ret = send_session_msg(lsm);
65beb5ff
DG
331 if (ret < 0) {
332 goto end;
333 }
334
335 /* Get header from data transmission */
336 ret = recv_data_sessiond(&llm, sizeof(llm));
337 if (ret < 0) {
338 goto end;
339 }
340
341 /* Check error code if OK */
342 if (llm.ret_code != LTTCOMM_OK) {
343 ret = -llm.ret_code;
344 goto end;
345 }
346
347 size = llm.data_size;
348 if (size == 0) {
874d3f84 349 /* If client free with size 0 */
a45d5536
DG
350 if (buf != NULL) {
351 *buf = NULL;
352 }
7d29a247 353 ret = 0;
65beb5ff
DG
354 goto end;
355 }
356
357 data = (void*) malloc(size);
358
359 /* Get payload data */
360 ret = recv_data_sessiond(data, size);
361 if (ret < 0) {
362 free(data);
363 goto end;
364 }
365
83009e5e
DG
366 /*
367 * Extra protection not to dereference a NULL pointer. If buf is NULL at
368 * this point, an error is returned and data is freed.
369 */
370 if (buf == NULL) {
371 ret = -1;
372 free(data);
373 goto end;
374 }
375
65beb5ff
DG
376 *buf = data;
377 ret = size;
378
379end:
380 disconnect_sessiond();
381 return ret;
382}
383
9f19cc17 384/*
cd80958d 385 * Create lttng handle and return pointer.
1c8d13c8 386 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 387 */
cd80958d
DG
388struct lttng_handle *lttng_create_handle(const char *session_name,
389 struct lttng_domain *domain)
9f19cc17 390{
cd80958d
DG
391 struct lttng_handle *handle;
392
393 handle = malloc(sizeof(struct lttng_handle));
394 if (handle == NULL) {
395 perror("malloc handle");
396 goto end;
397 }
398
399 /* Copy session name */
400 copy_string(handle->session_name, session_name,
401 sizeof(handle->session_name));
402
403 /* Copy lttng domain */
404 copy_lttng_domain(&handle->domain, domain);
405
406end:
407 return handle;
408}
409
410/*
411 * Destroy handle by free(3) the pointer.
412 */
413void lttng_destroy_handle(struct lttng_handle *handle)
414{
415 if (handle) {
416 free(handle);
eb354453
DG
417 }
418}
419
d9800920
DG
420/*
421 * Register an outside consumer.
1c8d13c8 422 * Returns size of returned session payload data or a negative error code.
d9800920
DG
423 */
424int lttng_register_consumer(struct lttng_handle *handle,
425 const char *socket_path)
426{
427 struct lttcomm_session_msg lsm;
428
429 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
430 copy_string(lsm.session.name, handle->session_name,
431 sizeof(lsm.session.name));
432 copy_lttng_domain(&lsm.domain, &handle->domain);
433
434 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
435
436 return ask_sessiond(&lsm, NULL);
437}
438
1df4dedd 439/*
1c8d13c8
TD
440 * Start tracing for all traces of the session.
441 * Returns size of returned session payload data or a negative error code.
1df4dedd 442 */
6a4f824d 443int lttng_start_tracing(const char *session_name)
f3ed775e 444{
cd80958d
DG
445 struct lttcomm_session_msg lsm;
446
6a4f824d 447 if (session_name == NULL) {
cd80958d
DG
448 return -1;
449 }
450
451 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
452
453 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
454
455 return ask_sessiond(&lsm, NULL);
f3ed775e 456}
1df4dedd
DG
457
458/*
1c8d13c8
TD
459 * Stop tracing for all traces of the session.
460 * Returns size of returned session payload data or a negative error code.
f3ed775e 461 */
6a4f824d 462int lttng_stop_tracing(const char *session_name)
f3ed775e 463{
cd80958d
DG
464 struct lttcomm_session_msg lsm;
465
6a4f824d
DG
466 if (session_name == NULL) {
467 return -1;
468 }
469
cd80958d 470 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
471
472 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
473
474 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
475}
476
477/*
1e46a50f
TD
478 * Add context to event and/or channel.
479 * If event_name is NULL, the context is applied to all events of the channel.
480 * If channel_name is NULL, a lookup of the event's channel is done.
481 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
482 *
483 * Returns the size of the returned payload data or a negative error code.
1df4dedd 484 */
cd80958d 485int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
486 struct lttng_event_context *ctx, const char *event_name,
487 const char *channel_name)
d65106b1 488{
cd80958d
DG
489 struct lttcomm_session_msg lsm;
490
9d697d3d
DG
491 /* Safety check. Both are mandatory */
492 if (handle == NULL || ctx == NULL) {
cd80958d
DG
493 return -1;
494 }
495
441c16a7
MD
496 memset(&lsm, 0, sizeof(lsm));
497
cd80958d
DG
498 lsm.cmd_type = LTTNG_ADD_CONTEXT;
499
500 /* Copy channel name */
501 copy_string(lsm.u.context.channel_name, channel_name,
502 sizeof(lsm.u.context.channel_name));
503 /* Copy event name */
504 copy_string(lsm.u.context.event_name, event_name,
505 sizeof(lsm.u.context.event_name));
506
507 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 508
9d697d3d 509 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 510
cd80958d
DG
511 copy_string(lsm.session.name, handle->session_name,
512 sizeof(lsm.session.name));
513
514 return ask_sessiond(&lsm, NULL);
d65106b1
DG
515}
516
f3ed775e 517/*
1c8d13c8
TD
518 * Enable event(s) for a channel.
519 * If no event name is specified, all events are enabled.
520 * If no channel name is specified, the default 'channel0' is used.
521 * Returns size of returned session payload data or a negative error code.
f3ed775e 522 */
cd80958d 523int lttng_enable_event(struct lttng_handle *handle,
38057ed1 524 struct lttng_event *ev, const char *channel_name)
1df4dedd 525{
cd80958d
DG
526 struct lttcomm_session_msg lsm;
527
5117eeec 528 if (handle == NULL || ev == NULL) {
cd80958d
DG
529 return -1;
530 }
33a2b854 531
441c16a7
MD
532 memset(&lsm, 0, sizeof(lsm));
533
5117eeec 534 /* If no channel name, we put the default name */
94cf3c47 535 if (channel_name == NULL) {
cd80958d
DG
536 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
537 sizeof(lsm.u.enable.channel_name));
33a2b854 538 } else {
cd80958d
DG
539 copy_string(lsm.u.enable.channel_name, channel_name,
540 sizeof(lsm.u.enable.channel_name));
eb354453
DG
541 }
542
cd80958d 543 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 544
8c9ae521 545 if (ev->name[0] != '\0') {
cd80958d 546 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 547 } else {
cd80958d 548 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 549 }
8c9ae521 550 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 551
cd80958d
DG
552 copy_string(lsm.session.name, handle->session_name,
553 sizeof(lsm.session.name));
554
555 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
556}
557
558/*
1c8d13c8
TD
559 * Disable event(s) of a channel and domain.
560 * If no event name is specified, all events are disabled.
561 * If no channel name is specified, the default 'channel0' is used.
562 * Returns size of returned session payload data or a negative error code.
1df4dedd 563 */
cd80958d 564int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 565 const char *channel_name)
1df4dedd 566{
cd80958d 567 struct lttcomm_session_msg lsm;
1df4dedd 568
9d697d3d 569 if (handle == NULL) {
cd80958d
DG
570 return -1;
571 }
572
441c16a7
MD
573 memset(&lsm, 0, sizeof(lsm));
574
cd80958d
DG
575 if (channel_name) {
576 copy_string(lsm.u.disable.channel_name, channel_name,
577 sizeof(lsm.u.disable.channel_name));
f3ed775e 578 } else {
cd80958d
DG
579 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
580 sizeof(lsm.u.disable.channel_name));
eb354453
DG
581 }
582
cd80958d 583 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 584
f84efadf 585 if (name != NULL) {
cd80958d
DG
586 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
587 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 588 } else {
cd80958d 589 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
590 }
591
cd80958d
DG
592 copy_string(lsm.session.name, handle->session_name,
593 sizeof(lsm.session.name));
594
595 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
596}
597
598/*
1c8d13c8
TD
599 * Enable channel per domain
600 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 601 */
cd80958d 602int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 603 struct lttng_channel *chan)
a5c5a2bd 604{
cd80958d
DG
605 struct lttcomm_session_msg lsm;
606
5117eeec
DG
607 /*
608 * NULL arguments are forbidden. No default values.
609 */
610 if (handle == NULL || chan == NULL) {
cd80958d
DG
611 return -1;
612 }
613
441c16a7
MD
614 memset(&lsm, 0, sizeof(lsm));
615
5117eeec 616 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 617
cd80958d
DG
618 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
619
620 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 621
cd80958d
DG
622 copy_string(lsm.session.name, handle->session_name,
623 sizeof(lsm.session.name));
624
625 return ask_sessiond(&lsm, NULL);
8c0faa1d 626}
1df4dedd 627
2ef84c95 628/*
1c8d13c8
TD
629 * All tracing will be stopped for registered events of the channel.
630 * Returns size of returned session payload data or a negative error code.
2ef84c95 631 */
cd80958d 632int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 633{
cd80958d
DG
634 struct lttcomm_session_msg lsm;
635
9d697d3d
DG
636 /* Safety check. Both are mandatory */
637 if (handle == NULL || name == NULL) {
cd80958d
DG
638 return -1;
639 }
640
441c16a7
MD
641 memset(&lsm, 0, sizeof(lsm));
642
cd80958d 643 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 644
9d697d3d
DG
645 copy_string(lsm.u.disable.channel_name, name,
646 sizeof(lsm.u.disable.channel_name));
647
cd80958d
DG
648 copy_lttng_domain(&lsm.domain, &handle->domain);
649
650 copy_string(lsm.session.name, handle->session_name,
651 sizeof(lsm.session.name));
652
653 return ask_sessiond(&lsm, NULL);
ca95a216
DG
654}
655
fac6795d 656/*
1c8d13c8
TD
657 * Lists all available tracepoints of domain.
658 * Sets the contents of the events array.
659 * Returns the number of lttng_event entries in events;
660 * on error, returns a negative value.
fac6795d 661 */
cd80958d 662int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 663 struct lttng_event **events)
fac6795d 664{
052da939 665 int ret;
cd80958d
DG
666 struct lttcomm_session_msg lsm;
667
9d697d3d 668 if (handle == NULL) {
cd80958d
DG
669 return -1;
670 }
fac6795d 671
cd80958d
DG
672 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
673 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 674
cd80958d 675 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
676 if (ret < 0) {
677 return ret;
eb354453 678 }
fac6795d 679
9f19cc17 680 return ret / sizeof(struct lttng_event);
fac6795d
DG
681}
682
f37d259d
MD
683/*
684 * Lists all available tracepoint fields of domain.
685 * Sets the contents of the event field array.
686 * Returns the number of lttng_event_field entries in events;
687 * on error, returns a negative value.
688 */
689int lttng_list_tracepoint_fields(struct lttng_handle *handle,
690 struct lttng_event_field **fields)
691{
692 int ret;
693 struct lttcomm_session_msg lsm;
694
695 if (handle == NULL) {
696 return -1;
697 }
698
699 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
700 copy_lttng_domain(&lsm.domain, &handle->domain);
701
702 ret = ask_sessiond(&lsm, (void **) fields);
703 if (ret < 0) {
704 return ret;
705 }
706
707 return ret / sizeof(struct lttng_event_field);
708}
709
1657e9bb 710/*
1c8d13c8
TD
711 * Returns a human readable string describing
712 * the error code (a negative value).
1657e9bb 713 */
9a745bc7 714const char *lttng_strerror(int code)
1657e9bb 715{
1c8d13c8 716 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
7d29a247
DG
717 if (code > -LTTCOMM_OK) {
718 return "Ended with errors";
1657e9bb
DG
719 }
720
7d29a247 721 return lttcomm_get_readable_code(code);
1657e9bb
DG
722}
723
aaf97519 724/*
1c8d13c8
TD
725 * Create a brand new session using name and path.
726 * Returns size of returned session payload data or a negative error code.
aaf97519 727 */
38057ed1 728int lttng_create_session(const char *name, const char *path)
aaf97519 729{
cd80958d
DG
730 struct lttcomm_session_msg lsm;
731
732 lsm.cmd_type = LTTNG_CREATE_SESSION;
733 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
734 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
735
736 return ask_sessiond(&lsm, NULL);
8028d920
DG
737}
738
00e2e675
DG
739/*
740 * Create a new tracing session using a name, URIs and a consumer enable flag.
741 */
742int lttng_create_session_uri(const char *name, struct lttng_uri *ctrl_uri,
743 struct lttng_uri *data_uri, unsigned int enable_consumer)
744{
745 struct lttcomm_session_msg lsm;
746
747 /* Name and ctrl_uri are mandatory */
748 if (name == NULL || ctrl_uri == NULL) {
749 return -1;
750 }
751
752 lsm.cmd_type = LTTNG_CREATE_SESSION_URI;
753
754 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
755 /* Anything bigger than zero, the consumer(s) will be enabled */
756 lsm.u.create_uri.enable_consumer = enable_consumer;
757 memcpy(&lsm.u.create_uri.ctrl_uri, ctrl_uri,
758 sizeof(lsm.u.create_uri.ctrl_uri));
759 if (data_uri) {
760 /*
761 * The only possible scenario where data_uri is NULL is for a local
762 * consumer where the output is at a specified path name on the
763 * filesystem.
764 */
765 memcpy(&lsm.u.create_uri.data_uri, data_uri,
766 sizeof(lsm.u.create_uri.data_uri));
767 }
768
769 return ask_sessiond(&lsm, NULL);
770}
771
8028d920 772/*
8028d920 773 * Destroy session using name.
1c8d13c8 774 * Returns size of returned session payload data or a negative error code.
8028d920 775 */
843f5df9 776int lttng_destroy_session(const char *session_name)
8028d920 777{
cd80958d
DG
778 struct lttcomm_session_msg lsm;
779
843f5df9 780 if (session_name == NULL) {
cd80958d
DG
781 return -1;
782 }
783
784 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
785
786 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
787
788 return ask_sessiond(&lsm, NULL);
aaf97519
DG
789}
790
57167058 791/*
57167058 792 * Ask the session daemon for all available sessions.
1c8d13c8
TD
793 * Sets the contents of the sessions array.
794 * Returns the number of lttng_session entries in sessions;
795 * on error, returns a negative value.
57167058 796 */
ca95a216 797int lttng_list_sessions(struct lttng_session **sessions)
57167058 798{
ca95a216 799 int ret;
cd80958d 800 struct lttcomm_session_msg lsm;
57167058 801
cd80958d
DG
802 lsm.cmd_type = LTTNG_LIST_SESSIONS;
803 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 804 if (ret < 0) {
ca95a216 805 return ret;
57167058
DG
806 }
807
ca95a216 808 return ret / sizeof(struct lttng_session);
57167058
DG
809}
810
9f19cc17 811/*
1c8d13c8
TD
812 * Ask the session daemon for all available domains of a session.
813 * Sets the contents of the domains array.
814 * Returns the number of lttng_domain entries in domains;
815 * on error, returns a negative value.
9f19cc17 816 */
330be774 817int lttng_list_domains(const char *session_name,
cd80958d 818 struct lttng_domain **domains)
9f19cc17
DG
819{
820 int ret;
cd80958d
DG
821 struct lttcomm_session_msg lsm;
822
330be774 823 if (session_name == NULL) {
cd80958d
DG
824 return -1;
825 }
9f19cc17 826
cd80958d
DG
827 lsm.cmd_type = LTTNG_LIST_DOMAINS;
828
330be774 829 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
830
831 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
832 if (ret < 0) {
833 return ret;
834 }
835
836 return ret / sizeof(struct lttng_domain);
837}
838
839/*
1c8d13c8
TD
840 * Ask the session daemon for all available channels of a session.
841 * Sets the contents of the channels array.
842 * Returns the number of lttng_channel entries in channels;
843 * on error, returns a negative value.
9f19cc17 844 */
cd80958d
DG
845int lttng_list_channels(struct lttng_handle *handle,
846 struct lttng_channel **channels)
9f19cc17
DG
847{
848 int ret;
cd80958d
DG
849 struct lttcomm_session_msg lsm;
850
9d697d3d 851 if (handle == NULL) {
cd80958d
DG
852 return -1;
853 }
854
855 lsm.cmd_type = LTTNG_LIST_CHANNELS;
856 copy_string(lsm.session.name, handle->session_name,
857 sizeof(lsm.session.name));
9f19cc17 858
cd80958d 859 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 860
cd80958d 861 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
862 if (ret < 0) {
863 return ret;
864 }
865
866 return ret / sizeof(struct lttng_channel);
867}
868
869/*
1c8d13c8
TD
870 * Ask the session daemon for all available events of a session channel.
871 * Sets the contents of the events array.
872 * Returns the number of lttng_event entries in events;
873 * on error, returns a negative value.
9f19cc17 874 */
cd80958d
DG
875int lttng_list_events(struct lttng_handle *handle,
876 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
877{
878 int ret;
cd80958d 879 struct lttcomm_session_msg lsm;
9f19cc17 880
9d697d3d
DG
881 /* Safety check. An handle and channel name are mandatory */
882 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
883 return -1;
884 }
885
886 lsm.cmd_type = LTTNG_LIST_EVENTS;
887 copy_string(lsm.session.name, handle->session_name,
888 sizeof(lsm.session.name));
889 copy_string(lsm.u.list.channel_name, channel_name,
890 sizeof(lsm.u.list.channel_name));
891
892 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 893
cd80958d 894 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
895 if (ret < 0) {
896 return ret;
897 }
898
899 return ret / sizeof(struct lttng_event);
900}
901
fac6795d 902/*
1c8d13c8
TD
903 * Sets the tracing_group variable with name.
904 * This function allocates memory pointed to by tracing_group.
905 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
906 */
907int lttng_set_tracing_group(const char *name)
908{
9d697d3d
DG
909 if (name == NULL) {
910 return -1;
911 }
912
fac6795d
DG
913 if (asprintf(&tracing_group, "%s", name) < 0) {
914 return -ENOMEM;
915 }
916
917 return 0;
918}
919
d0254c7c 920/*
af87c45a 921 * Returns size of returned session payload data or a negative error code.
d0254c7c 922 */
cd80958d 923int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
924 struct lttng_calibrate *calibrate)
925{
cd80958d 926 struct lttcomm_session_msg lsm;
d0254c7c 927
9d697d3d
DG
928 /* Safety check. NULL pointer are forbidden */
929 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
930 return -1;
931 }
d0254c7c 932
cd80958d
DG
933 lsm.cmd_type = LTTNG_CALIBRATE;
934 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 935
cd80958d
DG
936 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
937
938 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
939}
940
5edd7e09
DG
941/*
942 * Set default channel attributes.
441c16a7 943 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
944 */
945void lttng_channel_set_default_attr(struct lttng_domain *domain,
946 struct lttng_channel_attr *attr)
947{
948 /* Safety check */
949 if (attr == NULL || domain == NULL) {
950 return;
951 }
952
eacaa7a6
DS
953 memset(attr, 0, sizeof(struct lttng_channel_attr));
954
5edd7e09
DG
955 switch (domain->type) {
956 case LTTNG_DOMAIN_KERNEL:
957 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
958 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
959 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
960
961 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
962 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
963 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
964 break;
965 case LTTNG_DOMAIN_UST:
d78d6610 966#if 0
5edd7e09
DG
967 case LTTNG_DOMAIN_UST_EXEC_NAME:
968 case LTTNG_DOMAIN_UST_PID:
969 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 970#endif
5edd7e09
DG
971 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
972 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
973 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
974
975 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
976 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
977 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
978 break;
979 default:
441c16a7 980 /* Default behavior: leave set to 0. */
5edd7e09
DG
981 break;
982 }
983}
984
fac6795d 985/*
2269e89e 986 * Check if session daemon is alive.
fac6795d 987 *
2269e89e 988 * Return 1 if alive or 0 if not.
1c8d13c8 989 * On error returns a negative value.
fac6795d 990 */
947308c4 991int lttng_session_daemon_alive(void)
fac6795d
DG
992{
993 int ret;
994
995 ret = set_session_daemon_path();
996 if (ret < 0) {
947308c4 997 /* Error */
fac6795d
DG
998 return ret;
999 }
1000
2269e89e
DG
1001 if (strlen(sessiond_sock_path) == 0) {
1002 /* No socket path set. Weird error */
1003 return -1;
fac6795d
DG
1004 }
1005
2269e89e 1006 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1007 if (ret < 0) {
1008 /* Not alive */
1009 return 0;
1010 }
7d8234d9 1011
947308c4
DG
1012 /* Is alive */
1013 return 1;
fac6795d
DG
1014}
1015
00e2e675
DG
1016/*
1017 * Set URI for a consumer for a session and domain.
1018 *
1019 * Return 0 on success, else a negative value.
1020 */
1021int lttng_set_consumer_uri(struct lttng_handle *handle, struct lttng_uri *uri)
1022{
1023 struct lttcomm_session_msg lsm;
1024
1025 if (handle == NULL || uri == NULL) {
1026 return -1;
1027 }
1028
1029 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1030
1031 copy_string(lsm.session.name, handle->session_name,
1032 sizeof(lsm.session.name));
1033 copy_lttng_domain(&lsm.domain, &handle->domain);
1034
1035 memcpy(&lsm.u.uri, uri, sizeof(lsm.u.uri));
1036
1037 return ask_sessiond(&lsm, NULL);
1038}
1039
1040/*
1041 * Enable consumer for a session and domain.
1042 *
1043 * Return 0 on success, else a negative value.
1044 */
1045int lttng_enable_consumer(struct lttng_handle *handle)
1046{
1047 struct lttcomm_session_msg lsm;
1048
1049 if (handle == NULL) {
1050 return -1;
1051 }
1052
1053 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1054
1055 copy_string(lsm.session.name, handle->session_name,
1056 sizeof(lsm.session.name));
1057 copy_lttng_domain(&lsm.domain, &handle->domain);
1058
1059 return ask_sessiond(&lsm, NULL);
1060}
1061
1062/*
1063 * Disable consumer for a session and domain.
1064 *
1065 * Return 0 on success, else a negative value.
1066 */
1067int lttng_disable_consumer(struct lttng_handle *handle)
1068{
1069 struct lttcomm_session_msg lsm;
1070
1071 if (handle == NULL) {
1072 return -1;
1073 }
1074
1075 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1076
1077 copy_string(lsm.session.name, handle->session_name,
1078 sizeof(lsm.session.name));
1079 copy_lttng_domain(&lsm.domain, &handle->domain);
1080
1081 return ask_sessiond(&lsm, NULL);
1082}
1083
fac6795d
DG
1084/*
1085 * lib constructor
1086 */
1087static void __attribute__((constructor)) init()
1088{
1089 /* Set default session group */
bbccc3d2 1090 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
fac6795d 1091}
This page took 0.136903 seconds and 4 git commands to generate.