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