NULL ptr validation across liblttngctl
[lttng-tools.git] / liblttngctl / lttngctl.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
e88129fc 31#include <lttng-sessiond-comm.h>
1e307fab
DG
32#include <lttng-share.h>
33#include <lttng/lttng.h>
34#include <lttngerr.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:
69 case LTTNG_DOMAIN_UST_EXEC_NAME:
70 case LTTNG_DOMAIN_UST_PID:
71 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
72 memcpy(dst, src, sizeof(struct lttng_domain));
73 break;
74 default:
75 dst->type = LTTNG_DOMAIN_KERNEL;
76 break;
77 }
78 }
79}
80
81/*
82 * Send lttcomm_session_msg to the session daemon.
fac6795d 83 *
cd80958d
DG
84 * On success, return 0
85 * On error, return error code
fac6795d 86 */
cd80958d 87static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
88{
89 int ret;
90
91 if (!connected) {
e065084a
DG
92 ret = -ENOTCONN;
93 goto end;
fac6795d
DG
94 }
95
cd80958d
DG
96 ret = lttcomm_send_unix_sock(sessiond_socket, lsm,
97 sizeof(struct lttcomm_session_msg));
e065084a
DG
98
99end:
100 return ret;
101}
102
103/*
cd80958d 104 * Receive data from the sessiond socket.
e065084a 105 *
cd80958d
DG
106 * On success, return 0
107 * On error, return recv() error code
e065084a 108 */
ca95a216 109static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
110{
111 int ret;
112
113 if (!connected) {
114 ret = -ENOTCONN;
115 goto end;
fac6795d
DG
116 }
117
ca95a216 118 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 119
e065084a 120end:
fac6795d
DG
121 return ret;
122}
123
124/*
947308c4 125 * Check if the specified group name exist.
65beb5ff 126 *
2269e89e 127 * If yes return 1, else return -1.
947308c4
DG
128 */
129static int check_tracing_group(const char *grp_name)
130{
131 struct group *grp_tracing; /* no free(). See getgrnam(3) */
132 gid_t *grp_list;
133 int grp_list_size, grp_id, i;
134 int ret = -1;
135
136 /* Get GID of group 'tracing' */
137 grp_tracing = getgrnam(grp_name);
138 if (grp_tracing == NULL) {
139 /* NULL means not found also. getgrnam(3) */
140 if (errno != 0) {
141 perror("getgrnam");
142 }
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
MD
155 if (!grp_list) {
156 ret = -1;
157 goto end;
158 }
947308c4
DG
159 grp_id = getgroups(grp_list_size, grp_list);
160 if (grp_id < -1) {
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/*
210 * Set sessiond socket path by putting it in the global sessiond_sock_path
211 * variable.
947308c4
DG
212 */
213static int set_session_daemon_path(void)
214{
215 int ret;
2269e89e
DG
216 int in_tgroup = 0; /* In tracing group */
217 uid_t uid;
218
219 uid = getuid();
947308c4 220
2269e89e
DG
221 if (uid != 0) {
222 /* Are we in the tracing group ? */
223 in_tgroup = check_tracing_group(tracing_group);
224 }
225
226 if (uid == 0) {
227 /* Root */
228 copy_string(sessiond_sock_path,
229 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
230 sizeof(sessiond_sock_path));
231 } else if (in_tgroup) {
232 /* Tracing group */
233 copy_string(sessiond_sock_path,
234 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
235 sizeof(sessiond_sock_path));
236
237 ret = try_connect_sessiond(sessiond_sock_path);
238 if (ret < 0) {
239 /* Global session daemon not available */
240 if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
241 DEFAULT_HOME_CLIENT_UNIX_SOCK,
242 getenv("HOME")) < 0) {
243 return -ENOMEM;
244 }
245 }
246 } else {
247 /* Not in tracing group and not root, default */
99497cd0 248 if (snprintf(sessiond_sock_path, PATH_MAX,
cd80958d
DG
249 DEFAULT_HOME_CLIENT_UNIX_SOCK,
250 getenv("HOME")) < 0) {
947308c4
DG
251 return -ENOMEM;
252 }
947308c4
DG
253 }
254
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) {
269 return ret;
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/*
285 * Clean disconnect the session daemon.
286 */
287static int disconnect_sessiond(void)
288{
289 int ret = 0;
290
291 if (connected) {
292 ret = lttcomm_close_unix_sock(sessiond_socket);
293 sessiond_socket = 0;
294 connected = 0;
295 }
296
297 return ret;
298}
299
35a6fdb7 300/*
cd80958d 301 * Ask the session daemon a specific command and put the data into buf.
65beb5ff 302 *
cd80958d 303 * Return size of data (only payload, not header).
65beb5ff 304 */
cd80958d 305static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
65beb5ff
DG
306{
307 int ret;
308 size_t size;
309 void *data = NULL;
cd80958d 310 struct lttcomm_lttng_msg llm;
65beb5ff
DG
311
312 ret = connect_sessiond();
313 if (ret < 0) {
314 goto end;
315 }
316
65beb5ff 317 /* Send command to session daemon */
cd80958d 318 ret = send_session_msg(lsm);
65beb5ff
DG
319 if (ret < 0) {
320 goto end;
321 }
322
323 /* Get header from data transmission */
324 ret = recv_data_sessiond(&llm, sizeof(llm));
325 if (ret < 0) {
326 goto end;
327 }
328
329 /* Check error code if OK */
330 if (llm.ret_code != LTTCOMM_OK) {
331 ret = -llm.ret_code;
332 goto end;
333 }
334
335 size = llm.data_size;
336 if (size == 0) {
874d3f84 337 /* If client free with size 0 */
a45d5536
DG
338 if (buf != NULL) {
339 *buf = NULL;
340 }
7d29a247 341 ret = 0;
65beb5ff
DG
342 goto end;
343 }
344
345 data = (void*) malloc(size);
346
347 /* Get payload data */
348 ret = recv_data_sessiond(data, size);
349 if (ret < 0) {
350 free(data);
351 goto end;
352 }
353
83009e5e
DG
354 /*
355 * Extra protection not to dereference a NULL pointer. If buf is NULL at
356 * this point, an error is returned and data is freed.
357 */
358 if (buf == NULL) {
359 ret = -1;
360 free(data);
361 goto end;
362 }
363
65beb5ff
DG
364 *buf = data;
365 ret = size;
366
367end:
368 disconnect_sessiond();
369 return ret;
370}
371
9f19cc17 372/*
cd80958d 373 * Create lttng handle and return pointer.
9f19cc17 374 */
cd80958d
DG
375struct lttng_handle *lttng_create_handle(const char *session_name,
376 struct lttng_domain *domain)
9f19cc17 377{
cd80958d
DG
378 struct lttng_handle *handle;
379
380 handle = malloc(sizeof(struct lttng_handle));
381 if (handle == NULL) {
382 perror("malloc handle");
383 goto end;
384 }
385
386 /* Copy session name */
387 copy_string(handle->session_name, session_name,
388 sizeof(handle->session_name));
389
390 /* Copy lttng domain */
391 copy_lttng_domain(&handle->domain, domain);
392
393end:
394 return handle;
395}
396
397/*
398 * Destroy handle by free(3) the pointer.
399 */
400void lttng_destroy_handle(struct lttng_handle *handle)
401{
402 if (handle) {
403 free(handle);
eb354453
DG
404 }
405}
406
d9800920
DG
407/*
408 * Register an outside consumer.
409 */
410int lttng_register_consumer(struct lttng_handle *handle,
411 const char *socket_path)
412{
413 struct lttcomm_session_msg lsm;
414
415 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
416 copy_string(lsm.session.name, handle->session_name,
417 sizeof(lsm.session.name));
418 copy_lttng_domain(&lsm.domain, &handle->domain);
419
420 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
421
422 return ask_sessiond(&lsm, NULL);
423}
424
1df4dedd 425/*
f3ed775e 426 * Start tracing for all trace of the session.
1df4dedd 427 */
cd80958d 428int lttng_start_tracing(struct lttng_handle *handle)
f3ed775e 429{
cd80958d
DG
430 struct lttcomm_session_msg lsm;
431
9d697d3d 432 if (handle == NULL) {
cd80958d
DG
433 return -1;
434 }
435
436 lsm.cmd_type = LTTNG_START_TRACE;
437 copy_string(lsm.session.name, handle->session_name,
438 sizeof(lsm.session.name));
439
440 return ask_sessiond(&lsm, NULL);
f3ed775e 441}
1df4dedd
DG
442
443/*
f3ed775e
DG
444 * Stop tracing for all trace of the session.
445 */
cd80958d 446int lttng_stop_tracing(struct lttng_handle *handle)
f3ed775e 447{
cd80958d
DG
448 struct lttcomm_session_msg lsm;
449
450 lsm.cmd_type = LTTNG_STOP_TRACE;
451 copy_string(lsm.session.name, handle->session_name,
452 sizeof(lsm.session.name));
453
454 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
455}
456
457/*
cd80958d 458 * Add context to event or/and channel.
1df4dedd 459 */
cd80958d 460int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
461 struct lttng_event_context *ctx, const char *event_name,
462 const char *channel_name)
d65106b1 463{
cd80958d
DG
464 struct lttcomm_session_msg lsm;
465
9d697d3d
DG
466 /* Safety check. Both are mandatory */
467 if (handle == NULL || ctx == NULL) {
cd80958d
DG
468 return -1;
469 }
470
471 lsm.cmd_type = LTTNG_ADD_CONTEXT;
472
473 /* Copy channel name */
474 copy_string(lsm.u.context.channel_name, channel_name,
475 sizeof(lsm.u.context.channel_name));
476 /* Copy event name */
477 copy_string(lsm.u.context.event_name, event_name,
478 sizeof(lsm.u.context.event_name));
479
480 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 481
9d697d3d 482 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 483
cd80958d
DG
484 copy_string(lsm.session.name, handle->session_name,
485 sizeof(lsm.session.name));
486
487 return ask_sessiond(&lsm, NULL);
d65106b1
DG
488}
489
f3ed775e 490/*
cd80958d 491 * Enable event
f3ed775e 492 */
cd80958d 493int lttng_enable_event(struct lttng_handle *handle,
38057ed1 494 struct lttng_event *ev, const char *channel_name)
1df4dedd 495{
cd80958d
DG
496 struct lttcomm_session_msg lsm;
497
5117eeec 498 if (handle == NULL || ev == NULL) {
cd80958d
DG
499 return -1;
500 }
33a2b854 501
5117eeec 502 /* If no channel name, we put the default name */
94cf3c47 503 if (channel_name == NULL) {
cd80958d
DG
504 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
505 sizeof(lsm.u.enable.channel_name));
33a2b854 506 } else {
cd80958d
DG
507 copy_string(lsm.u.enable.channel_name, channel_name,
508 sizeof(lsm.u.enable.channel_name));
eb354453
DG
509 }
510
cd80958d 511 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 512
8c9ae521 513 if (ev->name[0] != '\0') {
cd80958d 514 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 515 } else {
cd80958d 516 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 517 }
8c9ae521 518 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 519
cd80958d
DG
520 copy_string(lsm.session.name, handle->session_name,
521 sizeof(lsm.session.name));
522
523 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
524}
525
526/*
f5177a38 527 * Disable event of a channel and domain.
1df4dedd 528 */
cd80958d 529int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 530 const char *channel_name)
1df4dedd 531{
cd80958d 532 struct lttcomm_session_msg lsm;
1df4dedd 533
9d697d3d 534 if (handle == NULL) {
cd80958d
DG
535 return -1;
536 }
537
538 if (channel_name) {
539 copy_string(lsm.u.disable.channel_name, channel_name,
540 sizeof(lsm.u.disable.channel_name));
f3ed775e 541 } else {
cd80958d
DG
542 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
543 sizeof(lsm.u.disable.channel_name));
eb354453
DG
544 }
545
cd80958d 546 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 547
f84efadf 548 if (name != NULL) {
cd80958d
DG
549 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
550 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 551 } else {
cd80958d 552 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
553 }
554
cd80958d
DG
555 copy_string(lsm.session.name, handle->session_name,
556 sizeof(lsm.session.name));
557
558 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
559}
560
561/*
0d0c377a 562 * Enable channel per domain
a5c5a2bd 563 */
cd80958d 564int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 565 struct lttng_channel *chan)
a5c5a2bd 566{
cd80958d
DG
567 struct lttcomm_session_msg lsm;
568
5117eeec
DG
569 /*
570 * NULL arguments are forbidden. No default values.
571 */
572 if (handle == NULL || chan == NULL) {
cd80958d
DG
573 return -1;
574 }
575
5117eeec 576 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 577
cd80958d
DG
578 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
579
580 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 581
cd80958d
DG
582 copy_string(lsm.session.name, handle->session_name,
583 sizeof(lsm.session.name));
584
585 return ask_sessiond(&lsm, NULL);
8c0faa1d 586}
1df4dedd 587
2ef84c95 588/*
0b97ec54 589 * All tracing will be stopped for registered events of the channel.
2ef84c95 590 */
cd80958d 591int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 592{
cd80958d
DG
593 struct lttcomm_session_msg lsm;
594
9d697d3d
DG
595 /* Safety check. Both are mandatory */
596 if (handle == NULL || name == NULL) {
cd80958d
DG
597 return -1;
598 }
599
cd80958d 600 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 601
9d697d3d
DG
602 copy_string(lsm.u.disable.channel_name, name,
603 sizeof(lsm.u.disable.channel_name));
604
cd80958d
DG
605 copy_lttng_domain(&lsm.domain, &handle->domain);
606
607 copy_string(lsm.session.name, handle->session_name,
608 sizeof(lsm.session.name));
609
610 return ask_sessiond(&lsm, NULL);
ca95a216
DG
611}
612
fac6795d 613/*
2a71efd5 614 * List all available tracepoints of domain.
fac6795d 615 *
2a71efd5 616 * Return the size (bytes) of the list and set the events array.
7d29a247 617 * On error, return negative value.
fac6795d 618 */
cd80958d 619int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 620 struct lttng_event **events)
fac6795d 621{
052da939 622 int ret;
cd80958d
DG
623 struct lttcomm_session_msg lsm;
624
9d697d3d 625 if (handle == NULL) {
cd80958d
DG
626 return -1;
627 }
fac6795d 628
cd80958d
DG
629 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
630 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 631
cd80958d 632 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
633 if (ret < 0) {
634 return ret;
eb354453 635 }
fac6795d 636
9f19cc17 637 return ret / sizeof(struct lttng_event);
fac6795d
DG
638}
639
1657e9bb 640/*
7d29a247 641 * Return a human readable string of code
1657e9bb 642 */
9a745bc7 643const char *lttng_strerror(int code)
1657e9bb 644{
7d29a247
DG
645 if (code > -LTTCOMM_OK) {
646 return "Ended with errors";
1657e9bb
DG
647 }
648
7d29a247 649 return lttcomm_get_readable_code(code);
1657e9bb
DG
650}
651
aaf97519 652/*
894be886 653 * Create a brand new session using name.
aaf97519 654 */
38057ed1 655int lttng_create_session(const char *name, const char *path)
aaf97519 656{
cd80958d
DG
657 struct lttcomm_session_msg lsm;
658
659 lsm.cmd_type = LTTNG_CREATE_SESSION;
660 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
661 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
662
663 return ask_sessiond(&lsm, NULL);
8028d920
DG
664}
665
666/*
8028d920
DG
667 * Destroy session using name.
668 */
cd80958d 669int lttng_destroy_session(struct lttng_handle *handle)
8028d920 670{
cd80958d
DG
671 struct lttcomm_session_msg lsm;
672
9d697d3d 673 if (handle == NULL) {
cd80958d
DG
674 return -1;
675 }
676
677 lsm.cmd_type = LTTNG_DESTROY_SESSION;
678 copy_string(lsm.session.name, handle->session_name,
679 sizeof(lsm.session.name));
680
681 return ask_sessiond(&lsm, NULL);
aaf97519
DG
682}
683
57167058 684/*
57167058
DG
685 * Ask the session daemon for all available sessions.
686 *
ca95a216
DG
687 * Return number of session.
688 * On error, return negative value.
57167058 689 */
ca95a216 690int lttng_list_sessions(struct lttng_session **sessions)
57167058 691{
ca95a216 692 int ret;
cd80958d 693 struct lttcomm_session_msg lsm;
57167058 694
cd80958d
DG
695 lsm.cmd_type = LTTNG_LIST_SESSIONS;
696 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 697 if (ret < 0) {
ca95a216 698 return ret;
57167058
DG
699 }
700
ca95a216 701 return ret / sizeof(struct lttng_session);
57167058
DG
702}
703
9f19cc17
DG
704/*
705 * List domain of a session.
706 */
cd80958d
DG
707int lttng_list_domains(struct lttng_handle *handle,
708 struct lttng_domain **domains)
9f19cc17
DG
709{
710 int ret;
cd80958d
DG
711 struct lttcomm_session_msg lsm;
712
9d697d3d 713 if (handle == NULL) {
cd80958d
DG
714 return -1;
715 }
9f19cc17 716
cd80958d
DG
717 lsm.cmd_type = LTTNG_LIST_DOMAINS;
718
719 copy_string(lsm.session.name, handle->session_name,
720 sizeof(lsm.session.name));
721
722 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
723 if (ret < 0) {
724 return ret;
725 }
726
727 return ret / sizeof(struct lttng_domain);
728}
729
730/*
731 * List channels of a session
732 */
cd80958d
DG
733int lttng_list_channels(struct lttng_handle *handle,
734 struct lttng_channel **channels)
9f19cc17
DG
735{
736 int ret;
cd80958d
DG
737 struct lttcomm_session_msg lsm;
738
9d697d3d 739 if (handle == NULL) {
cd80958d
DG
740 return -1;
741 }
742
743 lsm.cmd_type = LTTNG_LIST_CHANNELS;
744 copy_string(lsm.session.name, handle->session_name,
745 sizeof(lsm.session.name));
9f19cc17 746
cd80958d 747 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 748
cd80958d 749 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
750 if (ret < 0) {
751 return ret;
752 }
753
754 return ret / sizeof(struct lttng_channel);
755}
756
757/*
758 * List events of a session channel.
759 */
cd80958d
DG
760int lttng_list_events(struct lttng_handle *handle,
761 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
762{
763 int ret;
cd80958d 764 struct lttcomm_session_msg lsm;
9f19cc17 765
9d697d3d
DG
766 /* Safety check. An handle and channel name are mandatory */
767 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
768 return -1;
769 }
770
771 lsm.cmd_type = LTTNG_LIST_EVENTS;
772 copy_string(lsm.session.name, handle->session_name,
773 sizeof(lsm.session.name));
774 copy_string(lsm.u.list.channel_name, channel_name,
775 sizeof(lsm.u.list.channel_name));
776
777 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 778
cd80958d 779 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
780 if (ret < 0) {
781 return ret;
782 }
783
784 return ret / sizeof(struct lttng_event);
785}
786
fac6795d 787/*
5edd7e09
DG
788 * Set tracing group variable with name. This function allocate memory pointed
789 * by tracing_group.
fac6795d
DG
790 */
791int lttng_set_tracing_group(const char *name)
792{
9d697d3d
DG
793 if (name == NULL) {
794 return -1;
795 }
796
fac6795d
DG
797 if (asprintf(&tracing_group, "%s", name) < 0) {
798 return -ENOMEM;
799 }
800
801 return 0;
802}
803
d0254c7c
MD
804/*
805 * lttng_calibrate
806 */
cd80958d 807int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
808 struct lttng_calibrate *calibrate)
809{
cd80958d 810 struct lttcomm_session_msg lsm;
d0254c7c 811
9d697d3d
DG
812 /* Safety check. NULL pointer are forbidden */
813 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
814 return -1;
815 }
d0254c7c 816
cd80958d
DG
817 lsm.cmd_type = LTTNG_CALIBRATE;
818 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 819
cd80958d
DG
820 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
821
822 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
823}
824
5edd7e09
DG
825/*
826 * Set default channel attributes.
827 */
828void lttng_channel_set_default_attr(struct lttng_domain *domain,
829 struct lttng_channel_attr *attr)
830{
831 /* Safety check */
832 if (attr == NULL || domain == NULL) {
833 return;
834 }
835
836 switch (domain->type) {
837 case LTTNG_DOMAIN_KERNEL:
838 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
839 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
840 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
841
842 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
843 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
844 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
845 break;
846 case LTTNG_DOMAIN_UST:
847 case LTTNG_DOMAIN_UST_EXEC_NAME:
848 case LTTNG_DOMAIN_UST_PID:
849 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
850 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
851 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
852 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
853
854 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
855 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
856 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
857 break;
858 default:
859 /* Default behavior */
860 memset(attr, 0, sizeof(struct lttng_channel_attr));
861 break;
862 }
863}
864
fac6795d 865/*
2269e89e 866 * Check if session daemon is alive.
fac6795d 867 *
2269e89e
DG
868 * Return 1 if alive or 0 if not.
869 * On error return -1
fac6795d 870 */
947308c4 871int lttng_session_daemon_alive(void)
fac6795d
DG
872{
873 int ret;
874
875 ret = set_session_daemon_path();
876 if (ret < 0) {
947308c4 877 /* Error */
fac6795d
DG
878 return ret;
879 }
880
2269e89e
DG
881 if (strlen(sessiond_sock_path) == 0) {
882 /* No socket path set. Weird error */
883 return -1;
fac6795d
DG
884 }
885
2269e89e 886 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
887 if (ret < 0) {
888 /* Not alive */
889 return 0;
890 }
7d8234d9 891
947308c4
DG
892 /* Is alive */
893 return 1;
fac6795d
DG
894}
895
896/*
897 * lib constructor
898 */
899static void __attribute__((constructor)) init()
900{
901 /* Set default session group */
64a23ac4 902 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 903}
This page took 0.070925 seconds and 4 git commands to generate.