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