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