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> |
2001793c | 7 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 8 | * |
d14d33bf AM |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
11 | * as published by the Free Software Foundation. | |
82a3637f DG |
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 | * | |
d14d33bf AM |
18 | * You should have received a copy of the GNU Lesser General Public License |
19 | * along with this library; if not, write to the Free Software Foundation, | |
20 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
fac6795d DG |
21 | */ |
22 | ||
6c1c0768 | 23 | #define _LGPL_SOURCE |
44a5e5eb | 24 | #include <assert.h> |
fac6795d | 25 | #include <grp.h> |
1e307fab | 26 | #include <errno.h> |
fac6795d DG |
27 | #include <stdio.h> |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <unistd.h> | |
31 | ||
990570ed DG |
32 | #include <common/common.h> |
33 | #include <common/defaults.h> | |
db758600 | 34 | #include <common/sessiond-comm/sessiond-comm.h> |
a4b92340 | 35 | #include <common/uri.h> |
feb0f3e5 | 36 | #include <common/utils.h> |
1e307fab | 37 | #include <lttng/lttng.h> |
0c89d795 | 38 | #include <lttng/health-internal.h> |
fac6795d | 39 | |
d00c599e DG |
40 | #include "filter/filter-ast.h" |
41 | #include "filter/filter-parser.h" | |
42 | #include "filter/filter-bytecode.h" | |
43 | #include "filter/memstream.h" | |
cac3069d | 44 | #include "lttng-ctl-helper.h" |
53a80697 MD |
45 | |
46 | #ifdef DEBUG | |
d00c599e | 47 | static const int print_xml = 1; |
53a80697 MD |
48 | #define dbg_printf(fmt, args...) \ |
49 | printf("[debug liblttng-ctl] " fmt, ## args) | |
50 | #else | |
d00c599e | 51 | static const int print_xml = 0; |
53a80697 MD |
52 | #define dbg_printf(fmt, args...) \ |
53 | do { \ | |
54 | /* do nothing but check printf format */ \ | |
55 | if (0) \ | |
56 | printf("[debug liblttnctl] " fmt, ## args); \ | |
57 | } while (0) | |
58 | #endif | |
59 | ||
60 | ||
fac6795d DG |
61 | /* Socket to session daemon for communication */ |
62 | static int sessiond_socket; | |
63 | static char sessiond_sock_path[PATH_MAX]; | |
64 | ||
fac6795d DG |
65 | /* Variables */ |
66 | static char *tracing_group; | |
67 | static int connected; | |
68 | ||
97e19046 DG |
69 | /* Global */ |
70 | ||
71 | /* | |
72 | * Those two variables are used by error.h to silent or control the verbosity of | |
73 | * error message. They are global to the library so application linking with it | |
74 | * are able to compile correctly and also control verbosity of the library. | |
97e19046 DG |
75 | */ |
76 | int lttng_opt_quiet; | |
77 | int lttng_opt_verbose; | |
c7e35b03 | 78 | int lttng_opt_mi; |
97e19046 | 79 | |
99497cd0 MD |
80 | /* |
81 | * Copy string from src to dst and enforce null terminated byte. | |
82 | */ | |
cac3069d DG |
83 | LTTNG_HIDDEN |
84 | void lttng_ctl_copy_string(char *dst, const char *src, size_t len) | |
99497cd0 | 85 | { |
e7d6716d | 86 | if (src && dst) { |
99497cd0 MD |
87 | strncpy(dst, src, len); |
88 | /* Enforce the NULL terminated byte */ | |
89 | dst[len - 1] = '\0'; | |
cd80958d DG |
90 | } else if (dst) { |
91 | dst[0] = '\0'; | |
99497cd0 MD |
92 | } |
93 | } | |
94 | ||
fac6795d | 95 | /* |
cd80958d | 96 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 97 | * |
cd80958d DG |
98 | * If domain is unknown, default domain will be the kernel. |
99 | */ | |
cac3069d DG |
100 | LTTNG_HIDDEN |
101 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, | |
102 | struct lttng_domain *src) | |
cd80958d DG |
103 | { |
104 | if (src && dst) { | |
105 | switch (src->type) { | |
00e2e675 DG |
106 | case LTTNG_DOMAIN_KERNEL: |
107 | case LTTNG_DOMAIN_UST: | |
b9dfb167 | 108 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 109 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 110 | case LTTNG_DOMAIN_PYTHON: |
00e2e675 DG |
111 | memcpy(dst, src, sizeof(struct lttng_domain)); |
112 | break; | |
113 | default: | |
114 | memset(dst, 0, sizeof(struct lttng_domain)); | |
00e2e675 | 115 | break; |
cd80958d DG |
116 | } |
117 | } | |
118 | } | |
119 | ||
120 | /* | |
121 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 122 | * |
1c8d13c8 TD |
123 | * On success, returns the number of bytes sent (>=0) |
124 | * On error, returns -1 | |
fac6795d | 125 | */ |
cd80958d | 126 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
127 | { |
128 | int ret; | |
129 | ||
130 | if (!connected) { | |
2f70b271 | 131 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 132 | goto end; |
fac6795d DG |
133 | } |
134 | ||
a4b92340 DG |
135 | DBG("LSM cmd type : %d", lsm->cmd_type); |
136 | ||
be040666 | 137 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 138 | sizeof(struct lttcomm_session_msg)); |
2f70b271 DG |
139 | if (ret < 0) { |
140 | ret = -LTTNG_ERR_FATAL; | |
141 | } | |
e065084a DG |
142 | |
143 | end: | |
144 | return ret; | |
145 | } | |
146 | ||
53a80697 MD |
147 | /* |
148 | * Send var len data to the session daemon. | |
149 | * | |
150 | * On success, returns the number of bytes sent (>=0) | |
151 | * On error, returns -1 | |
152 | */ | |
c2d69327 | 153 | static int send_session_varlen(const void *data, size_t len) |
53a80697 MD |
154 | { |
155 | int ret; | |
156 | ||
157 | if (!connected) { | |
2f70b271 | 158 | ret = -LTTNG_ERR_NO_SESSIOND; |
53a80697 MD |
159 | goto end; |
160 | } | |
a4b92340 | 161 | |
53a80697 MD |
162 | if (!data || !len) { |
163 | ret = 0; | |
164 | goto end; | |
165 | } | |
166 | ||
167 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); | |
2f70b271 DG |
168 | if (ret < 0) { |
169 | ret = -LTTNG_ERR_FATAL; | |
170 | } | |
53a80697 MD |
171 | |
172 | end: | |
173 | return ret; | |
174 | } | |
175 | ||
e065084a | 176 | /* |
cd80958d | 177 | * Receive data from the sessiond socket. |
e065084a | 178 | * |
1c8d13c8 TD |
179 | * On success, returns the number of bytes received (>=0) |
180 | * On error, returns -1 (recvmsg() error) or -ENOTCONN | |
e065084a | 181 | */ |
ca95a216 | 182 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
183 | { |
184 | int ret; | |
185 | ||
186 | if (!connected) { | |
2f70b271 | 187 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 188 | goto end; |
fac6795d DG |
189 | } |
190 | ||
ca95a216 | 191 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
2f70b271 DG |
192 | if (ret < 0) { |
193 | ret = -LTTNG_ERR_FATAL; | |
194 | } | |
fac6795d | 195 | |
e065084a | 196 | end: |
fac6795d DG |
197 | return ret; |
198 | } | |
199 | ||
200 | /* | |
9ae110e2 | 201 | * Check if we are in the specified group. |
65beb5ff | 202 | * |
9ae110e2 | 203 | * If yes return 1, else return -1. |
947308c4 | 204 | */ |
6c71277b MD |
205 | LTTNG_HIDDEN |
206 | int lttng_check_tracing_group(void) | |
947308c4 DG |
207 | { |
208 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
209 | gid_t *grp_list; | |
210 | int grp_list_size, grp_id, i; | |
211 | int ret = -1; | |
6c71277b | 212 | const char *grp_name = tracing_group; |
947308c4 DG |
213 | |
214 | /* Get GID of group 'tracing' */ | |
215 | grp_tracing = getgrnam(grp_name); | |
b4d8603b MD |
216 | if (!grp_tracing) { |
217 | /* If grp_tracing is NULL, the group does not exist. */ | |
947308c4 DG |
218 | goto end; |
219 | } | |
220 | ||
221 | /* Get number of supplementary group IDs */ | |
222 | grp_list_size = getgroups(0, NULL); | |
223 | if (grp_list_size < 0) { | |
6f04ed72 | 224 | PERROR("getgroups"); |
947308c4 DG |
225 | goto end; |
226 | } | |
227 | ||
228 | /* Alloc group list of the right size */ | |
3f451dc0 | 229 | grp_list = zmalloc(grp_list_size * sizeof(gid_t)); |
00795392 | 230 | if (!grp_list) { |
6f04ed72 | 231 | PERROR("malloc"); |
00795392 MD |
232 | goto end; |
233 | } | |
947308c4 | 234 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 235 | if (grp_id < 0) { |
6f04ed72 | 236 | PERROR("getgroups"); |
947308c4 DG |
237 | goto free_list; |
238 | } | |
239 | ||
240 | for (i = 0; i < grp_list_size; i++) { | |
241 | if (grp_list[i] == grp_tracing->gr_gid) { | |
2269e89e | 242 | ret = 1; |
947308c4 DG |
243 | break; |
244 | } | |
245 | } | |
246 | ||
247 | free_list: | |
248 | free(grp_list); | |
249 | ||
250 | end: | |
251 | return ret; | |
252 | } | |
253 | ||
254 | /* | |
2269e89e DG |
255 | * Try connect to session daemon with sock_path. |
256 | * | |
257 | * Return 0 on success, else -1 | |
258 | */ | |
259 | static int try_connect_sessiond(const char *sock_path) | |
260 | { | |
261 | int ret; | |
262 | ||
263 | /* If socket exist, we check if the daemon listens for connect. */ | |
264 | ret = access(sock_path, F_OK); | |
265 | if (ret < 0) { | |
266 | /* Not alive */ | |
2f70b271 | 267 | goto error; |
2269e89e DG |
268 | } |
269 | ||
270 | ret = lttcomm_connect_unix_sock(sock_path); | |
271 | if (ret < 0) { | |
9ae110e2 | 272 | /* Not alive. */ |
2f70b271 | 273 | goto error; |
2269e89e DG |
274 | } |
275 | ||
276 | ret = lttcomm_close_unix_sock(ret); | |
277 | if (ret < 0) { | |
6f04ed72 | 278 | PERROR("lttcomm_close_unix_sock"); |
2269e89e DG |
279 | } |
280 | ||
281 | return 0; | |
2f70b271 DG |
282 | |
283 | error: | |
284 | return -1; | |
2269e89e DG |
285 | } |
286 | ||
287 | /* | |
2f70b271 DG |
288 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
289 | * variable. | |
290 | * | |
291 | * Returns 0 on success, negative value on failure (the sessiond socket path | |
292 | * is somehow too long or ENOMEM). | |
947308c4 DG |
293 | */ |
294 | static int set_session_daemon_path(void) | |
295 | { | |
9ae110e2 | 296 | int in_tgroup = 0; /* In tracing group. */ |
2269e89e DG |
297 | uid_t uid; |
298 | ||
299 | uid = getuid(); | |
947308c4 | 300 | |
2269e89e DG |
301 | if (uid != 0) { |
302 | /* Are we in the tracing group ? */ | |
6c71277b | 303 | in_tgroup = lttng_check_tracing_group(); |
2269e89e DG |
304 | } |
305 | ||
08a9c49f | 306 | if ((uid == 0) || in_tgroup) { |
cac3069d DG |
307 | lttng_ctl_copy_string(sessiond_sock_path, |
308 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path)); | |
08a9c49f | 309 | } |
2269e89e | 310 | |
08a9c49f | 311 | if (uid != 0) { |
c617c0c6 MD |
312 | int ret; |
313 | ||
08a9c49f | 314 | if (in_tgroup) { |
9ae110e2 | 315 | /* Tracing group. */ |
08a9c49f TD |
316 | ret = try_connect_sessiond(sessiond_sock_path); |
317 | if (ret >= 0) { | |
318 | goto end; | |
2269e89e | 319 | } |
08a9c49f | 320 | /* Global session daemon not available... */ |
2269e89e | 321 | } |
08a9c49f TD |
322 | /* ...or not in tracing group (and not root), default */ |
323 | ||
324 | /* | |
9ae110e2 JG |
325 | * With GNU C < 2.1, snprintf returns -1 if the target buffer |
326 | * is too small; | |
327 | * With GNU C >= 2.1, snprintf returns the required size | |
328 | * (excluding closing null) | |
08a9c49f TD |
329 | */ |
330 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
feb0f3e5 | 331 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
08a9c49f | 332 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
2f70b271 | 333 | goto error; |
947308c4 | 334 | } |
947308c4 | 335 | } |
08a9c49f | 336 | end: |
947308c4 | 337 | return 0; |
2f70b271 DG |
338 | |
339 | error: | |
340 | return -1; | |
947308c4 DG |
341 | } |
342 | ||
65beb5ff | 343 | /* |
9ae110e2 | 344 | * Connect to the LTTng session daemon. |
65beb5ff | 345 | * |
9ae110e2 | 346 | * On success, return 0. On error, return -1. |
65beb5ff DG |
347 | */ |
348 | static int connect_sessiond(void) | |
349 | { | |
350 | int ret; | |
351 | ||
da3c9ec1 DG |
352 | /* Don't try to connect if already connected. */ |
353 | if (connected) { | |
354 | return 0; | |
355 | } | |
356 | ||
65beb5ff DG |
357 | ret = set_session_daemon_path(); |
358 | if (ret < 0) { | |
2f70b271 | 359 | goto error; |
65beb5ff DG |
360 | } |
361 | ||
9ae110e2 | 362 | /* Connect to the sesssion daemon. */ |
65beb5ff DG |
363 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
364 | if (ret < 0) { | |
2f70b271 | 365 | goto error; |
65beb5ff DG |
366 | } |
367 | ||
368 | sessiond_socket = ret; | |
369 | connected = 1; | |
370 | ||
371 | return 0; | |
2f70b271 DG |
372 | |
373 | error: | |
374 | return -1; | |
65beb5ff DG |
375 | } |
376 | ||
377 | /* | |
1c8d13c8 | 378 | * Clean disconnect from the session daemon. |
9ae110e2 | 379 | * |
1c8d13c8 | 380 | * On success, return 0. On error, return -1. |
65beb5ff DG |
381 | */ |
382 | static int disconnect_sessiond(void) | |
383 | { | |
384 | int ret = 0; | |
385 | ||
386 | if (connected) { | |
387 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
388 | sessiond_socket = 0; | |
389 | connected = 0; | |
390 | } | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
795a978d PP |
395 | static int recv_sessiond_optional_data(size_t len, void **user_buf, |
396 | size_t *user_len) | |
397 | { | |
398 | int ret = 0; | |
399 | void *buf = NULL; | |
400 | ||
401 | if (len) { | |
402 | if (!user_len) { | |
403 | ret = -LTTNG_ERR_INVALID; | |
404 | goto end; | |
405 | } | |
406 | ||
407 | buf = zmalloc(len); | |
408 | if (!buf) { | |
409 | ret = -ENOMEM; | |
410 | goto end; | |
411 | } | |
412 | ||
413 | ret = recv_data_sessiond(buf, len); | |
414 | if (ret < 0) { | |
415 | goto end; | |
416 | } | |
417 | ||
418 | if (!user_buf) { | |
419 | ret = -LTTNG_ERR_INVALID; | |
420 | goto end; | |
421 | } | |
422 | ||
423 | /* Move ownership of command header buffer to user. */ | |
424 | *user_buf = buf; | |
425 | buf = NULL; | |
426 | *user_len = len; | |
427 | } else { | |
428 | /* No command header. */ | |
429 | if (user_len) { | |
430 | *user_len = 0; | |
431 | } | |
432 | ||
433 | if (user_buf) { | |
434 | *user_buf = NULL; | |
435 | } | |
436 | } | |
437 | ||
438 | end: | |
439 | free(buf); | |
440 | return ret; | |
441 | } | |
442 | ||
35a6fdb7 | 443 | /* |
cd80958d | 444 | * Ask the session daemon a specific command and put the data into buf. |
53a80697 | 445 | * Takes extra var. len. data as input to send to the session daemon. |
65beb5ff | 446 | * |
af87c45a | 447 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 448 | */ |
cac3069d DG |
449 | LTTNG_HIDDEN |
450 | int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm, | |
795a978d PP |
451 | const void *vardata, size_t vardata_len, |
452 | void **user_payload_buf, void **user_cmd_header_buf, | |
453 | size_t *user_cmd_header_len) | |
65beb5ff DG |
454 | { |
455 | int ret; | |
795a978d | 456 | size_t payload_len; |
cd80958d | 457 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
458 | |
459 | ret = connect_sessiond(); | |
460 | if (ret < 0) { | |
2f70b271 | 461 | ret = -LTTNG_ERR_NO_SESSIOND; |
65beb5ff DG |
462 | goto end; |
463 | } | |
464 | ||
65beb5ff | 465 | /* Send command to session daemon */ |
cd80958d | 466 | ret = send_session_msg(lsm); |
65beb5ff | 467 | if (ret < 0) { |
2f70b271 | 468 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
469 | goto end; |
470 | } | |
53a80697 | 471 | /* Send var len data */ |
795a978d | 472 | ret = send_session_varlen(vardata, vardata_len); |
53a80697 | 473 | if (ret < 0) { |
2f70b271 | 474 | /* Ret value is a valid lttng error code. */ |
53a80697 MD |
475 | goto end; |
476 | } | |
65beb5ff DG |
477 | |
478 | /* Get header from data transmission */ | |
479 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
480 | if (ret < 0) { | |
2f70b271 | 481 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
482 | goto end; |
483 | } | |
484 | ||
485 | /* Check error code if OK */ | |
f73fabfd | 486 | if (llm.ret_code != LTTNG_OK) { |
65beb5ff DG |
487 | ret = -llm.ret_code; |
488 | goto end; | |
489 | } | |
490 | ||
795a978d PP |
491 | /* Get command header from data transmission */ |
492 | ret = recv_sessiond_optional_data(llm.cmd_header_size, | |
493 | user_cmd_header_buf, user_cmd_header_len); | |
65beb5ff | 494 | if (ret < 0) { |
65beb5ff DG |
495 | goto end; |
496 | } | |
497 | ||
795a978d PP |
498 | /* Get payload from data transmission */ |
499 | ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, | |
500 | &payload_len); | |
501 | if (ret < 0) { | |
83009e5e DG |
502 | goto end; |
503 | } | |
504 | ||
795a978d | 505 | ret = llm.data_size; |
65beb5ff DG |
506 | |
507 | end: | |
508 | disconnect_sessiond(); | |
509 | return ret; | |
510 | } | |
511 | ||
9f19cc17 | 512 | /* |
cd80958d | 513 | * Create lttng handle and return pointer. |
9ae110e2 | 514 | * |
1c8d13c8 | 515 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 516 | */ |
cd80958d DG |
517 | struct lttng_handle *lttng_create_handle(const char *session_name, |
518 | struct lttng_domain *domain) | |
9f19cc17 | 519 | { |
2f70b271 DG |
520 | struct lttng_handle *handle = NULL; |
521 | ||
3f451dc0 | 522 | handle = zmalloc(sizeof(struct lttng_handle)); |
cd80958d | 523 | if (handle == NULL) { |
2f70b271 | 524 | PERROR("malloc handle"); |
cd80958d DG |
525 | goto end; |
526 | } | |
527 | ||
528 | /* Copy session name */ | |
cac3069d | 529 | lttng_ctl_copy_string(handle->session_name, session_name, |
cd80958d DG |
530 | sizeof(handle->session_name)); |
531 | ||
95681498 JG |
532 | /* Copy lttng domain or leave initialized to 0. */ |
533 | if (domain) { | |
534 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); | |
535 | } | |
cd80958d DG |
536 | |
537 | end: | |
538 | return handle; | |
539 | } | |
540 | ||
541 | /* | |
542 | * Destroy handle by free(3) the pointer. | |
543 | */ | |
544 | void lttng_destroy_handle(struct lttng_handle *handle) | |
545 | { | |
0e428499 | 546 | free(handle); |
eb354453 DG |
547 | } |
548 | ||
d9800920 DG |
549 | /* |
550 | * Register an outside consumer. | |
9ae110e2 | 551 | * |
1c8d13c8 | 552 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
553 | */ |
554 | int lttng_register_consumer(struct lttng_handle *handle, | |
555 | const char *socket_path) | |
556 | { | |
557 | struct lttcomm_session_msg lsm; | |
558 | ||
2f70b271 DG |
559 | if (handle == NULL || socket_path == NULL) { |
560 | return -LTTNG_ERR_INVALID; | |
561 | } | |
562 | ||
53efb85a | 563 | memset(&lsm, 0, sizeof(lsm)); |
d9800920 | 564 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
cac3069d | 565 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
d9800920 | 566 | sizeof(lsm.session.name)); |
cac3069d | 567 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
d9800920 | 568 | |
9ae110e2 JG |
569 | lttng_ctl_copy_string(lsm.u.reg.path, socket_path, |
570 | sizeof(lsm.u.reg.path)); | |
d9800920 | 571 | |
cac3069d | 572 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d9800920 DG |
573 | } |
574 | ||
1df4dedd | 575 | /* |
9ae110e2 JG |
576 | * Start tracing for all traces of the session. |
577 | * | |
578 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 579 | */ |
6a4f824d | 580 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 581 | { |
cd80958d DG |
582 | struct lttcomm_session_msg lsm; |
583 | ||
6a4f824d | 584 | if (session_name == NULL) { |
2f70b271 | 585 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
586 | } |
587 | ||
53efb85a | 588 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 589 | lsm.cmd_type = LTTNG_START_TRACE; |
6a4f824d | 590 | |
cac3069d DG |
591 | lttng_ctl_copy_string(lsm.session.name, session_name, |
592 | sizeof(lsm.session.name)); | |
cd80958d | 593 | |
cac3069d | 594 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
f3ed775e | 595 | } |
1df4dedd DG |
596 | |
597 | /* | |
38ee087f | 598 | * Stop tracing for all traces of the session. |
f3ed775e | 599 | */ |
38ee087f | 600 | static int _lttng_stop_tracing(const char *session_name, int wait) |
f3ed775e | 601 | { |
38ee087f | 602 | int ret, data_ret; |
cd80958d DG |
603 | struct lttcomm_session_msg lsm; |
604 | ||
6a4f824d | 605 | if (session_name == NULL) { |
2f70b271 | 606 | return -LTTNG_ERR_INVALID; |
6a4f824d DG |
607 | } |
608 | ||
53efb85a | 609 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 610 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d | 611 | |
cac3069d DG |
612 | lttng_ctl_copy_string(lsm.session.name, session_name, |
613 | sizeof(lsm.session.name)); | |
cd80958d | 614 | |
cac3069d | 615 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
38ee087f DG |
616 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
617 | goto error; | |
618 | } | |
619 | ||
620 | if (!wait) { | |
621 | goto end; | |
622 | } | |
623 | ||
38ee087f DG |
624 | /* Check for data availability */ |
625 | do { | |
6d805429 | 626 | data_ret = lttng_data_pending(session_name); |
38ee087f DG |
627 | if (data_ret < 0) { |
628 | /* Return the data available call error. */ | |
629 | ret = data_ret; | |
630 | goto error; | |
631 | } | |
632 | ||
633 | /* | |
9ae110e2 JG |
634 | * Data sleep time before retrying (in usec). Don't sleep if the |
635 | * call returned value indicates availability. | |
38ee087f | 636 | */ |
6d805429 | 637 | if (data_ret) { |
38ee087f | 638 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); |
38ee087f | 639 | } |
6d805429 | 640 | } while (data_ret != 0); |
38ee087f | 641 | |
38ee087f DG |
642 | end: |
643 | error: | |
644 | return ret; | |
645 | } | |
646 | ||
647 | /* | |
648 | * Stop tracing and wait for data availability. | |
649 | */ | |
650 | int lttng_stop_tracing(const char *session_name) | |
651 | { | |
652 | return _lttng_stop_tracing(session_name, 1); | |
653 | } | |
654 | ||
655 | /* | |
656 | * Stop tracing but _don't_ wait for data availability. | |
657 | */ | |
658 | int lttng_stop_tracing_no_wait(const char *session_name) | |
659 | { | |
660 | return _lttng_stop_tracing(session_name, 0); | |
f3ed775e DG |
661 | } |
662 | ||
663 | /* | |
601d5acf DG |
664 | * Add context to a channel. |
665 | * | |
666 | * If the given channel is NULL, add the contexts to all channels. | |
667 | * The event_name param is ignored. | |
af87c45a DG |
668 | * |
669 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 670 | */ |
cd80958d | 671 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
672 | struct lttng_event_context *ctx, const char *event_name, |
673 | const char *channel_name) | |
d65106b1 | 674 | { |
2001793c JG |
675 | int ret; |
676 | size_t len = 0; | |
677 | char *buf = NULL; | |
cd80958d DG |
678 | struct lttcomm_session_msg lsm; |
679 | ||
9ae110e2 | 680 | /* Safety check. Both are mandatory. */ |
9d697d3d | 681 | if (handle == NULL || ctx == NULL) { |
2001793c JG |
682 | ret = -LTTNG_ERR_INVALID; |
683 | goto end; | |
cd80958d DG |
684 | } |
685 | ||
441c16a7 | 686 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
687 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
688 | ||
85076754 MD |
689 | /* If no channel name, send empty string. */ |
690 | if (channel_name == NULL) { | |
691 | lttng_ctl_copy_string(lsm.u.context.channel_name, "", | |
692 | sizeof(lsm.u.context.channel_name)); | |
693 | } else { | |
694 | lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name, | |
695 | sizeof(lsm.u.context.channel_name)); | |
696 | } | |
cd80958d | 697 | |
cac3069d | 698 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
cac3069d | 699 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
700 | sizeof(lsm.session.name)); |
701 | ||
2001793c JG |
702 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
703 | size_t provider_len, ctx_len; | |
704 | const char *provider_name = ctx->u.app_ctx.provider_name; | |
705 | const char *ctx_name = ctx->u.app_ctx.ctx_name; | |
706 | ||
707 | if (!provider_name || !ctx_name) { | |
708 | ret = -LTTNG_ERR_INVALID; | |
709 | goto end; | |
710 | } | |
711 | ||
712 | provider_len = strlen(provider_name); | |
713 | if (provider_len == 0) { | |
714 | ret = -LTTNG_ERR_INVALID; | |
715 | goto end; | |
716 | } | |
717 | lsm.u.context.provider_name_len = provider_len; | |
718 | ||
719 | ctx_len = strlen(ctx_name); | |
720 | if (ctx_len == 0) { | |
721 | ret = -LTTNG_ERR_INVALID; | |
722 | goto end; | |
723 | } | |
724 | lsm.u.context.context_name_len = ctx_len; | |
725 | ||
726 | len = provider_len + ctx_len; | |
727 | buf = zmalloc(len); | |
728 | if (!buf) { | |
729 | ret = -LTTNG_ERR_NOMEM; | |
730 | goto end; | |
731 | } | |
732 | ||
733 | memcpy(buf, provider_name, provider_len); | |
734 | memcpy(buf + provider_len, ctx_name, ctx_len); | |
735 | } | |
736 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); | |
46f44e2a JR |
737 | |
738 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
739 | /* | |
740 | * Don't leak application addresses to the sessiond. | |
741 | * This is only necessary when ctx is for an app ctx otherwise | |
742 | * the values inside the union (type & config) are overwritten. | |
743 | */ | |
744 | lsm.u.context.ctx.u.app_ctx.provider_name = NULL; | |
745 | lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; | |
746 | } | |
2001793c | 747 | |
795a978d | 748 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); |
2001793c JG |
749 | end: |
750 | free(buf); | |
751 | return ret; | |
d65106b1 DG |
752 | } |
753 | ||
f3ed775e | 754 | /* |
9ae110e2 JG |
755 | * Enable event(s) for a channel. |
756 | * | |
757 | * If no event name is specified, all events are enabled. | |
758 | * If no channel name is specified, the default 'channel0' is used. | |
759 | * | |
760 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 761 | */ |
cd80958d | 762 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 763 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 764 | { |
f8a96544 JI |
765 | return lttng_enable_event_with_exclusions(handle, ev, channel_name, |
766 | NULL, 0, NULL); | |
1df4dedd DG |
767 | } |
768 | ||
53a80697 | 769 | /* |
025faf73 | 770 | * Create or enable an event with a filter expression. |
178191b3 | 771 | * |
53a80697 MD |
772 | * Return negative error value on error. |
773 | * Return size of returned session payload data if OK. | |
774 | */ | |
025faf73 | 775 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
178191b3 | 776 | struct lttng_event *event, const char *channel_name, |
53a80697 MD |
777 | const char *filter_expression) |
778 | { | |
f8a96544 JI |
779 | return lttng_enable_event_with_exclusions(handle, event, channel_name, |
780 | filter_expression, 0, NULL); | |
53a80697 MD |
781 | } |
782 | ||
347c5ab5 | 783 | /* |
0e115563 | 784 | * Depending on the event, return a newly allocated agent filter expression or |
347c5ab5 DG |
785 | * NULL if not applicable. |
786 | * | |
787 | * An event with NO loglevel and the name is * will return NULL. | |
788 | */ | |
0e115563 | 789 | static char *set_agent_filter(const char *filter, struct lttng_event *ev) |
347c5ab5 DG |
790 | { |
791 | int err; | |
0e115563 | 792 | char *agent_filter = NULL; |
347c5ab5 DG |
793 | |
794 | assert(ev); | |
795 | ||
796 | /* Don't add filter for the '*' event. */ | |
797 | if (ev->name[0] != '*') { | |
798 | if (filter) { | |
0e115563 | 799 | err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, |
347c5ab5 DG |
800 | ev->name); |
801 | } else { | |
0e115563 | 802 | err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); |
347c5ab5 DG |
803 | } |
804 | if (err < 0) { | |
805 | PERROR("asprintf"); | |
6a556f7b | 806 | goto error; |
347c5ab5 DG |
807 | } |
808 | } | |
809 | ||
810 | /* Add loglevel filtering if any for the JUL domain. */ | |
811 | if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { | |
812 | char *op; | |
813 | ||
814 | if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) { | |
815 | op = ">="; | |
816 | } else { | |
817 | op = "=="; | |
818 | } | |
819 | ||
0e115563 | 820 | if (filter || agent_filter) { |
6a556f7b JG |
821 | char *new_filter; |
822 | ||
fb0edb23 | 823 | err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", |
0e115563 | 824 | agent_filter ? agent_filter : filter, op, |
347c5ab5 | 825 | ev->loglevel); |
0e115563 DG |
826 | if (agent_filter) { |
827 | free(agent_filter); | |
6a556f7b | 828 | } |
0e115563 | 829 | agent_filter = new_filter; |
347c5ab5 | 830 | } else { |
0e115563 | 831 | err = asprintf(&agent_filter, "int_loglevel %s %d", op, |
347c5ab5 DG |
832 | ev->loglevel); |
833 | } | |
834 | if (err < 0) { | |
835 | PERROR("asprintf"); | |
6a556f7b | 836 | goto error; |
347c5ab5 DG |
837 | } |
838 | } | |
839 | ||
0e115563 | 840 | return agent_filter; |
6a556f7b | 841 | error: |
0e115563 | 842 | free(agent_filter); |
6a556f7b | 843 | return NULL; |
347c5ab5 DG |
844 | } |
845 | ||
137b9942 | 846 | /* |
ec166985 | 847 | * Generate the filter bytecode from a given filter expression string. Put the |
137b9942 DG |
848 | * newly allocated parser context in ctxp and populate the lsm object with the |
849 | * expression len. | |
850 | * | |
851 | * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched. | |
852 | */ | |
853 | static int generate_filter(char *filter_expression, | |
854 | struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp) | |
855 | { | |
856 | int ret; | |
857 | struct filter_parser_ctx *ctx = NULL; | |
858 | FILE *fmem = NULL; | |
859 | ||
860 | assert(filter_expression); | |
861 | assert(lsm); | |
862 | assert(ctxp); | |
863 | ||
864 | /* | |
865 | * Casting const to non-const, as the underlying function will use it in | |
866 | * read-only mode. | |
867 | */ | |
868 | fmem = lttng_fmemopen((void *) filter_expression, | |
869 | strlen(filter_expression), "r"); | |
870 | if (!fmem) { | |
871 | fprintf(stderr, "Error opening memory as stream\n"); | |
872 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
873 | goto error; | |
874 | } | |
875 | ctx = filter_parser_ctx_alloc(fmem); | |
876 | if (!ctx) { | |
877 | fprintf(stderr, "Error allocating parser\n"); | |
878 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
879 | goto filter_alloc_error; | |
880 | } | |
881 | ret = filter_parser_ctx_append_ast(ctx); | |
882 | if (ret) { | |
883 | fprintf(stderr, "Parse error\n"); | |
884 | ret = -LTTNG_ERR_FILTER_INVAL; | |
885 | goto parse_error; | |
886 | } | |
887 | ret = filter_visitor_set_parent(ctx); | |
888 | if (ret) { | |
889 | fprintf(stderr, "Set parent error\n"); | |
890 | ret = -LTTNG_ERR_FILTER_INVAL; | |
891 | goto parse_error; | |
892 | } | |
893 | if (print_xml) { | |
894 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
895 | if (ret) { | |
896 | fflush(stdout); | |
897 | fprintf(stderr, "XML print error\n"); | |
898 | ret = -LTTNG_ERR_FILTER_INVAL; | |
899 | goto parse_error; | |
900 | } | |
901 | } | |
902 | ||
903 | dbg_printf("Generating IR... "); | |
904 | fflush(stdout); | |
905 | ret = filter_visitor_ir_generate(ctx); | |
906 | if (ret) { | |
907 | fprintf(stderr, "Generate IR error\n"); | |
908 | ret = -LTTNG_ERR_FILTER_INVAL; | |
909 | goto parse_error; | |
910 | } | |
911 | dbg_printf("done\n"); | |
912 | ||
913 | dbg_printf("Validating IR... "); | |
914 | fflush(stdout); | |
915 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
916 | if (ret) { | |
917 | ret = -LTTNG_ERR_FILTER_INVAL; | |
918 | goto parse_error; | |
919 | } | |
9ae110e2 | 920 | /* Validate strings used as literals in the expression. */ |
dcd5daf2 JG |
921 | ret = filter_visitor_ir_validate_string(ctx); |
922 | if (ret) { | |
923 | ret = -LTTNG_ERR_FILTER_INVAL; | |
924 | goto parse_error; | |
925 | } | |
137b9942 DG |
926 | dbg_printf("done\n"); |
927 | ||
928 | dbg_printf("Generating bytecode... "); | |
929 | fflush(stdout); | |
930 | ret = filter_visitor_bytecode_generate(ctx); | |
931 | if (ret) { | |
932 | fprintf(stderr, "Generate bytecode error\n"); | |
933 | ret = -LTTNG_ERR_FILTER_INVAL; | |
934 | goto parse_error; | |
935 | } | |
936 | dbg_printf("done\n"); | |
937 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
938 | bytecode_get_len(&ctx->bytecode->b)); | |
939 | ||
940 | lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b) | |
941 | + bytecode_get_len(&ctx->bytecode->b); | |
942 | lsm->u.enable.expression_len = strlen(filter_expression) + 1; | |
943 | ||
944 | /* No need to keep the memory stream. */ | |
945 | if (fclose(fmem) != 0) { | |
6f04ed72 | 946 | PERROR("fclose"); |
137b9942 DG |
947 | } |
948 | ||
949 | *ctxp = ctx; | |
950 | return 0; | |
951 | ||
952 | parse_error: | |
137b9942 DG |
953 | filter_ir_free(ctx); |
954 | filter_parser_ctx_free(ctx); | |
955 | filter_alloc_error: | |
956 | if (fclose(fmem) != 0) { | |
6f04ed72 | 957 | PERROR("fclose"); |
137b9942 DG |
958 | } |
959 | error: | |
960 | return ret; | |
961 | } | |
962 | ||
93deb080 JI |
963 | /* |
964 | * Enable event(s) for a channel, possibly with exclusions and a filter. | |
965 | * If no event name is specified, all events are enabled. | |
966 | * If no channel name is specified, the default name is used. | |
967 | * If filter expression is not NULL, the filter is set for the event. | |
968 | * If exclusion count is not zero, the exclusions are set for the event. | |
969 | * Returns size of returned session payload data or a negative error code. | |
970 | */ | |
971 | int lttng_enable_event_with_exclusions(struct lttng_handle *handle, | |
972 | struct lttng_event *ev, const char *channel_name, | |
e9efbcd3 | 973 | const char *original_filter_expression, |
93deb080 JI |
974 | int exclusion_count, char **exclusion_list) |
975 | { | |
976 | struct lttcomm_session_msg lsm; | |
64226865 | 977 | char *varlen_data; |
93deb080 | 978 | int ret = 0; |
137b9942 | 979 | unsigned int free_filter_expression = 0; |
93deb080 | 980 | struct filter_parser_ctx *ctx = NULL; |
e9efbcd3 JG |
981 | /* |
982 | * Cast as non-const since we may replace the filter expression | |
983 | * by a dynamically allocated string. Otherwise, the original | |
984 | * string is not modified. | |
985 | */ | |
986 | char *filter_expression = (char *) original_filter_expression; | |
93deb080 JI |
987 | |
988 | if (handle == NULL || ev == NULL) { | |
137b9942 DG |
989 | ret = -LTTNG_ERR_INVALID; |
990 | goto error; | |
93deb080 JI |
991 | } |
992 | ||
9ae110e2 JG |
993 | /* |
994 | * Empty filter string will always be rejected by the parser | |
93deb080 JI |
995 | * anyway, so treat this corner-case early to eliminate |
996 | * lttng_fmemopen error for 0-byte allocation. | |
997 | */ | |
998 | if (filter_expression && filter_expression[0] == '\0') { | |
137b9942 DG |
999 | ret = -LTTNG_ERR_INVALID; |
1000 | goto error; | |
93deb080 JI |
1001 | } |
1002 | ||
1003 | memset(&lsm, 0, sizeof(lsm)); | |
1004 | ||
1005 | /* If no channel name, send empty string. */ | |
1006 | if (channel_name == NULL) { | |
1007 | lttng_ctl_copy_string(lsm.u.enable.channel_name, "", | |
1008 | sizeof(lsm.u.enable.channel_name)); | |
1009 | } else { | |
1010 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, | |
1011 | sizeof(lsm.u.enable.channel_name)); | |
1012 | } | |
1013 | ||
18a720cd MD |
1014 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
1015 | if (ev->name[0] == '\0') { | |
1016 | /* Enable all events */ | |
1017 | lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name)); | |
6565421f | 1018 | } |
93deb080 | 1019 | |
6565421f | 1020 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
6e911cad | 1021 | /* FIXME: copying non-packed struct to packed struct. */ |
93deb080 JI |
1022 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
1023 | ||
1024 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, | |
1025 | sizeof(lsm.session.name)); | |
1026 | lsm.u.enable.exclusion_count = exclusion_count; | |
1027 | lsm.u.enable.bytecode_len = 0; | |
1028 | ||
9b21e6d5 DG |
1029 | /* |
1030 | * For the JUL domain, a filter is enforced except for the enable all | |
1031 | * event. This is done to avoid having the event in all sessions thus | |
1032 | * filtering by logger name. | |
1033 | */ | |
1034 | if (exclusion_count == 0 && filter_expression == NULL && | |
5cdb6027 | 1035 | (handle->domain.type != LTTNG_DOMAIN_JUL && |
0e115563 DG |
1036 | handle->domain.type != LTTNG_DOMAIN_LOG4J && |
1037 | handle->domain.type != LTTNG_DOMAIN_PYTHON)) { | |
3e1c9ff7 | 1038 | goto ask_sessiond; |
93deb080 JI |
1039 | } |
1040 | ||
1041 | /* | |
1042 | * We have either a filter or some exclusions, so we need to set up | |
9ae110e2 | 1043 | * a variable-length memory block from where to send the data. |
93deb080 JI |
1044 | */ |
1045 | ||
9ae110e2 | 1046 | /* Parse filter expression. */ |
5cdb6027 | 1047 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
0e115563 DG |
1048 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1049 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
5cdb6027 | 1050 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1051 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1052 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1053 | char *agent_filter; | |
64226865 | 1054 | |
347c5ab5 | 1055 | /* Setup JUL filter if needed. */ |
0e115563 DG |
1056 | agent_filter = set_agent_filter(filter_expression, ev); |
1057 | if (!agent_filter) { | |
137b9942 | 1058 | if (!filter_expression) { |
9ae110e2 JG |
1059 | /* |
1060 | * No JUL and no filter, just skip | |
1061 | * everything below. | |
1062 | */ | |
137b9942 DG |
1063 | goto ask_sessiond; |
1064 | } | |
64226865 DG |
1065 | } else { |
1066 | /* | |
9ae110e2 JG |
1067 | * With an agent filter, the original filter has |
1068 | * been added to it thus replace the filter | |
1069 | * expression. | |
64226865 | 1070 | */ |
0e115563 | 1071 | filter_expression = agent_filter; |
e9efbcd3 | 1072 | free_filter_expression = 1; |
9b21e6d5 | 1073 | } |
9b21e6d5 | 1074 | } |
93deb080 | 1075 | |
137b9942 | 1076 | ret = generate_filter(filter_expression, &lsm, &ctx); |
93deb080 | 1077 | if (ret) { |
137b9942 | 1078 | goto filter_error; |
93deb080 | 1079 | } |
6b453b5e JG |
1080 | } |
1081 | ||
1082 | varlen_data = zmalloc(lsm.u.enable.bytecode_len | |
137b9942 DG |
1083 | + lsm.u.enable.expression_len |
1084 | + LTTNG_SYMBOL_NAME_LEN * exclusion_count); | |
6b453b5e JG |
1085 | if (!varlen_data) { |
1086 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; | |
7ca1dc6f | 1087 | goto mem_error; |
6b453b5e | 1088 | } |
137b9942 | 1089 | |
9ae110e2 | 1090 | /* Put exclusion names first in the data. */ |
6b453b5e JG |
1091 | while (exclusion_count--) { |
1092 | strncpy(varlen_data + LTTNG_SYMBOL_NAME_LEN * exclusion_count, | |
0c82ac62 PP |
1093 | *(exclusion_list + exclusion_count), |
1094 | LTTNG_SYMBOL_NAME_LEN - 1); | |
6b453b5e | 1095 | } |
9ae110e2 | 1096 | /* Add filter expression next. */ |
6b453b5e JG |
1097 | if (lsm.u.enable.expression_len != 0) { |
1098 | memcpy(varlen_data | |
1099 | + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count, | |
1100 | filter_expression, | |
1101 | lsm.u.enable.expression_len); | |
1102 | } | |
9ae110e2 | 1103 | /* Add filter bytecode next. */ |
137b9942 | 1104 | if (ctx && lsm.u.enable.bytecode_len != 0) { |
6b453b5e JG |
1105 | memcpy(varlen_data |
1106 | + LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count | |
1107 | + lsm.u.enable.expression_len, | |
1108 | &ctx->bytecode->b, | |
1109 | lsm.u.enable.bytecode_len); | |
93deb080 JI |
1110 | } |
1111 | ||
795a978d | 1112 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, |
67676bd8 | 1113 | (LTTNG_SYMBOL_NAME_LEN * lsm.u.enable.exclusion_count) + |
9ae110e2 JG |
1114 | lsm.u.enable.bytecode_len + lsm.u.enable.expression_len, |
1115 | NULL); | |
6b453b5e | 1116 | free(varlen_data); |
93deb080 | 1117 | |
7ca1dc6f DG |
1118 | mem_error: |
1119 | if (filter_expression && ctx) { | |
93deb080 JI |
1120 | filter_bytecode_free(ctx); |
1121 | filter_ir_free(ctx); | |
1122 | filter_parser_ctx_free(ctx); | |
7ca1dc6f DG |
1123 | } |
1124 | filter_error: | |
1125 | if (free_filter_expression) { | |
1126 | /* | |
9ae110e2 JG |
1127 | * The filter expression has been replaced and must be freed as |
1128 | * it is not the original filter expression received as a | |
1129 | * parameter. | |
7ca1dc6f DG |
1130 | */ |
1131 | free(filter_expression); | |
93deb080 | 1132 | } |
137b9942 DG |
1133 | error: |
1134 | /* | |
9ae110e2 JG |
1135 | * Return directly to the caller and don't ask the sessiond since |
1136 | * something went wrong in the parsing of data above. | |
137b9942 | 1137 | */ |
93deb080 | 1138 | return ret; |
3e1c9ff7 DG |
1139 | |
1140 | ask_sessiond: | |
1141 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1142 | return ret; | |
93deb080 JI |
1143 | } |
1144 | ||
6e911cad MD |
1145 | int lttng_disable_event_ext(struct lttng_handle *handle, |
1146 | struct lttng_event *ev, const char *channel_name, | |
1147 | const char *original_filter_expression) | |
1df4dedd | 1148 | { |
cd80958d | 1149 | struct lttcomm_session_msg lsm; |
6e911cad MD |
1150 | char *varlen_data; |
1151 | int ret = 0; | |
1152 | unsigned int free_filter_expression = 0; | |
1153 | struct filter_parser_ctx *ctx = NULL; | |
1154 | /* | |
1155 | * Cast as non-const since we may replace the filter expression | |
1156 | * by a dynamically allocated string. Otherwise, the original | |
1157 | * string is not modified. | |
1158 | */ | |
1159 | char *filter_expression = (char *) original_filter_expression; | |
1df4dedd | 1160 | |
6e911cad MD |
1161 | if (handle == NULL || ev == NULL) { |
1162 | ret = -LTTNG_ERR_INVALID; | |
1163 | goto error; | |
1164 | } | |
1165 | ||
9ae110e2 JG |
1166 | /* |
1167 | * Empty filter string will always be rejected by the parser | |
6e911cad MD |
1168 | * anyway, so treat this corner-case early to eliminate |
1169 | * lttng_fmemopen error for 0-byte allocation. | |
1170 | */ | |
1171 | if (filter_expression && filter_expression[0] == '\0') { | |
1172 | ret = -LTTNG_ERR_INVALID; | |
1173 | goto error; | |
cd80958d DG |
1174 | } |
1175 | ||
441c16a7 MD |
1176 | memset(&lsm, 0, sizeof(lsm)); |
1177 | ||
85076754 MD |
1178 | /* If no channel name, send empty string. */ |
1179 | if (channel_name == NULL) { | |
1180 | lttng_ctl_copy_string(lsm.u.disable.channel_name, "", | |
cd80958d | 1181 | sizeof(lsm.u.disable.channel_name)); |
f3ed775e | 1182 | } else { |
85076754 | 1183 | lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name, |
cd80958d | 1184 | sizeof(lsm.u.disable.channel_name)); |
eb354453 DG |
1185 | } |
1186 | ||
18a720cd | 1187 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
f3ed775e | 1188 | |
6e911cad MD |
1189 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
1190 | /* FIXME: copying non-packed struct to packed struct. */ | |
1191 | memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event)); | |
1192 | ||
cac3069d | 1193 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1194 | sizeof(lsm.session.name)); |
6e911cad | 1195 | lsm.u.disable.bytecode_len = 0; |
cd80958d | 1196 | |
6e911cad MD |
1197 | /* |
1198 | * For the JUL domain, a filter is enforced except for the | |
1199 | * disable all event. This is done to avoid having the event in | |
1200 | * all sessions thus filtering by logger name. | |
1201 | */ | |
1202 | if (filter_expression == NULL && | |
1203 | (handle->domain.type != LTTNG_DOMAIN_JUL && | |
0e115563 DG |
1204 | handle->domain.type != LTTNG_DOMAIN_LOG4J && |
1205 | handle->domain.type != LTTNG_DOMAIN_PYTHON)) { | |
6e911cad MD |
1206 | goto ask_sessiond; |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * We have a filter, so we need to set up a variable-length | |
1211 | * memory block from where to send the data. | |
1212 | */ | |
1213 | ||
1214 | /* Parse filter expression */ | |
1215 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL | |
0e115563 DG |
1216 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1217 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
6e911cad | 1218 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1219 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1220 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1221 | char *agent_filter; | |
6e911cad MD |
1222 | |
1223 | /* Setup JUL filter if needed. */ | |
0e115563 DG |
1224 | agent_filter = set_agent_filter(filter_expression, ev); |
1225 | if (!agent_filter) { | |
6e911cad | 1226 | if (!filter_expression) { |
9ae110e2 JG |
1227 | /* |
1228 | * No JUL and no filter, just skip | |
1229 | * everything below. | |
1230 | */ | |
6e911cad MD |
1231 | goto ask_sessiond; |
1232 | } | |
1233 | } else { | |
1234 | /* | |
9ae110e2 JG |
1235 | * With a JUL filter, the original filter has |
1236 | * been added to it thus replace the filter | |
1237 | * expression. | |
6e911cad | 1238 | */ |
0e115563 | 1239 | filter_expression = agent_filter; |
6e911cad MD |
1240 | free_filter_expression = 1; |
1241 | } | |
1242 | } | |
1243 | ||
1244 | ret = generate_filter(filter_expression, &lsm, &ctx); | |
1245 | if (ret) { | |
1246 | goto filter_error; | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | varlen_data = zmalloc(lsm.u.disable.bytecode_len | |
1251 | + lsm.u.disable.expression_len); | |
1252 | if (!varlen_data) { | |
1253 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; | |
1254 | goto mem_error; | |
1255 | } | |
1256 | ||
9ae110e2 | 1257 | /* Add filter expression. */ |
6e911cad MD |
1258 | if (lsm.u.disable.expression_len != 0) { |
1259 | memcpy(varlen_data, | |
1260 | filter_expression, | |
1261 | lsm.u.disable.expression_len); | |
1262 | } | |
9ae110e2 | 1263 | /* Add filter bytecode next. */ |
6e911cad MD |
1264 | if (ctx && lsm.u.disable.bytecode_len != 0) { |
1265 | memcpy(varlen_data | |
1266 | + lsm.u.disable.expression_len, | |
1267 | &ctx->bytecode->b, | |
1268 | lsm.u.disable.bytecode_len); | |
1269 | } | |
1270 | ||
795a978d | 1271 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, |
6e911cad MD |
1272 | lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL); |
1273 | free(varlen_data); | |
1274 | ||
1275 | mem_error: | |
1276 | if (filter_expression && ctx) { | |
1277 | filter_bytecode_free(ctx); | |
1278 | filter_ir_free(ctx); | |
1279 | filter_parser_ctx_free(ctx); | |
1280 | } | |
1281 | filter_error: | |
1282 | if (free_filter_expression) { | |
1283 | /* | |
9ae110e2 JG |
1284 | * The filter expression has been replaced and must be freed as |
1285 | * it is not the original filter expression received as a | |
1286 | * parameter. | |
6e911cad MD |
1287 | */ |
1288 | free(filter_expression); | |
1289 | } | |
1290 | error: | |
1291 | /* | |
9ae110e2 JG |
1292 | * Return directly to the caller and don't ask the sessiond since |
1293 | * something went wrong in the parsing of data above. | |
6e911cad MD |
1294 | */ |
1295 | return ret; | |
1296 | ||
1297 | ask_sessiond: | |
1298 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1299 | return ret; | |
1300 | } | |
1301 | ||
1302 | /* | |
9ae110e2 JG |
1303 | * Disable event(s) of a channel and domain. |
1304 | * If no event name is specified, all events are disabled. | |
1305 | * If no channel name is specified, the default 'channel0' is used. | |
1306 | * Returns size of returned session payload data or a negative error code. | |
6e911cad MD |
1307 | */ |
1308 | int lttng_disable_event(struct lttng_handle *handle, const char *name, | |
1309 | const char *channel_name) | |
1310 | { | |
1311 | struct lttng_event ev; | |
1312 | ||
1313 | memset(&ev, 0, sizeof(ev)); | |
9b7431cf | 1314 | ev.loglevel = -1; |
6e911cad MD |
1315 | ev.type = LTTNG_EVENT_ALL; |
1316 | lttng_ctl_copy_string(ev.name, name, sizeof(ev.name)); | |
1317 | return lttng_disable_event_ext(handle, &ev, channel_name, NULL); | |
1df4dedd DG |
1318 | } |
1319 | ||
1320 | /* | |
9ae110e2 JG |
1321 | * Enable channel per domain |
1322 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 1323 | */ |
cd80958d | 1324 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 1325 | struct lttng_channel *chan) |
a5c5a2bd | 1326 | { |
cd80958d DG |
1327 | struct lttcomm_session_msg lsm; |
1328 | ||
9ae110e2 | 1329 | /* NULL arguments are forbidden. No default values. */ |
5117eeec | 1330 | if (handle == NULL || chan == NULL) { |
2f70b271 | 1331 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1332 | } |
1333 | ||
441c16a7 MD |
1334 | memset(&lsm, 0, sizeof(lsm)); |
1335 | ||
5117eeec | 1336 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 1337 | |
cd80958d DG |
1338 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
1339 | ||
cac3069d | 1340 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
7d29a247 | 1341 | |
cac3069d | 1342 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1343 | sizeof(lsm.session.name)); |
1344 | ||
cac3069d | 1345 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
8c0faa1d | 1346 | } |
1df4dedd | 1347 | |
2ef84c95 | 1348 | /* |
9ae110e2 JG |
1349 | * All tracing will be stopped for registered events of the channel. |
1350 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 1351 | */ |
cd80958d | 1352 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 1353 | { |
cd80958d DG |
1354 | struct lttcomm_session_msg lsm; |
1355 | ||
9ae110e2 | 1356 | /* Safety check. Both are mandatory. */ |
9d697d3d | 1357 | if (handle == NULL || name == NULL) { |
2f70b271 | 1358 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1359 | } |
1360 | ||
441c16a7 MD |
1361 | memset(&lsm, 0, sizeof(lsm)); |
1362 | ||
cd80958d | 1363 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 1364 | |
cac3069d | 1365 | lttng_ctl_copy_string(lsm.u.disable.channel_name, name, |
9d697d3d DG |
1366 | sizeof(lsm.u.disable.channel_name)); |
1367 | ||
cac3069d | 1368 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
cd80958d | 1369 | |
cac3069d | 1370 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1371 | sizeof(lsm.session.name)); |
1372 | ||
cac3069d | 1373 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
ca95a216 DG |
1374 | } |
1375 | ||
ccf10263 | 1376 | /* |
9ae110e2 JG |
1377 | * Add PID to session tracker. |
1378 | * Return 0 on success else a negative LTTng error code. | |
ccf10263 MD |
1379 | */ |
1380 | int lttng_track_pid(struct lttng_handle *handle, int pid) | |
1381 | { | |
1382 | struct lttcomm_session_msg lsm; | |
1383 | ||
9ae110e2 | 1384 | /* NULL arguments are forbidden. No default values. */ |
ccf10263 MD |
1385 | if (handle == NULL) { |
1386 | return -LTTNG_ERR_INVALID; | |
1387 | } | |
1388 | ||
1389 | memset(&lsm, 0, sizeof(lsm)); | |
1390 | ||
1391 | lsm.cmd_type = LTTNG_TRACK_PID; | |
1392 | lsm.u.pid_tracker.pid = pid; | |
1393 | ||
1394 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); | |
1395 | ||
1396 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, | |
1397 | sizeof(lsm.session.name)); | |
1398 | ||
1399 | return lttng_ctl_ask_sessiond(&lsm, NULL); | |
1400 | } | |
1401 | ||
1402 | /* | |
9ae110e2 JG |
1403 | * Remove PID from session tracker. |
1404 | * Return 0 on success else a negative LTTng error code. | |
ccf10263 MD |
1405 | */ |
1406 | int lttng_untrack_pid(struct lttng_handle *handle, int pid) | |
1407 | { | |
1408 | struct lttcomm_session_msg lsm; | |
1409 | ||
9ae110e2 | 1410 | /* NULL arguments are forbidden. No default values. */ |
ccf10263 MD |
1411 | if (handle == NULL) { |
1412 | return -LTTNG_ERR_INVALID; | |
1413 | } | |
1414 | ||
1415 | memset(&lsm, 0, sizeof(lsm)); | |
1416 | ||
1417 | lsm.cmd_type = LTTNG_UNTRACK_PID; | |
1418 | lsm.u.pid_tracker.pid = pid; | |
1419 | ||
1420 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); | |
1421 | ||
1422 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, | |
1423 | sizeof(lsm.session.name)); | |
1424 | ||
1425 | return lttng_ctl_ask_sessiond(&lsm, NULL); | |
1426 | } | |
1427 | ||
fac6795d | 1428 | /* |
9ae110e2 JG |
1429 | * Lists all available tracepoints of domain. |
1430 | * Sets the contents of the events array. | |
1431 | * Returns the number of lttng_event entries in events; | |
1432 | * on error, returns a negative value. | |
fac6795d | 1433 | */ |
cd80958d | 1434 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 1435 | struct lttng_event **events) |
fac6795d | 1436 | { |
052da939 | 1437 | int ret; |
cd80958d DG |
1438 | struct lttcomm_session_msg lsm; |
1439 | ||
9d697d3d | 1440 | if (handle == NULL) { |
2f70b271 | 1441 | return -LTTNG_ERR_INVALID; |
cd80958d | 1442 | } |
fac6795d | 1443 | |
53efb85a | 1444 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1445 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
cac3069d | 1446 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
2a71efd5 | 1447 | |
cac3069d | 1448 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
052da939 DG |
1449 | if (ret < 0) { |
1450 | return ret; | |
eb354453 | 1451 | } |
fac6795d | 1452 | |
9f19cc17 | 1453 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
1454 | } |
1455 | ||
f37d259d | 1456 | /* |
9ae110e2 JG |
1457 | * Lists all available tracepoint fields of domain. |
1458 | * Sets the contents of the event field array. | |
1459 | * Returns the number of lttng_event_field entries in events; | |
1460 | * on error, returns a negative value. | |
f37d259d MD |
1461 | */ |
1462 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
1463 | struct lttng_event_field **fields) | |
1464 | { | |
1465 | int ret; | |
1466 | struct lttcomm_session_msg lsm; | |
1467 | ||
1468 | if (handle == NULL) { | |
2f70b271 | 1469 | return -LTTNG_ERR_INVALID; |
f37d259d MD |
1470 | } |
1471 | ||
53efb85a | 1472 | memset(&lsm, 0, sizeof(lsm)); |
f37d259d | 1473 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
cac3069d | 1474 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
f37d259d | 1475 | |
cac3069d | 1476 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
f37d259d MD |
1477 | if (ret < 0) { |
1478 | return ret; | |
1479 | } | |
1480 | ||
1481 | return ret / sizeof(struct lttng_event_field); | |
1482 | } | |
1483 | ||
834978fd | 1484 | /* |
9ae110e2 JG |
1485 | * Lists all available kernel system calls. Allocates and sets the contents of |
1486 | * the events array. | |
834978fd | 1487 | * |
9ae110e2 JG |
1488 | * Returns the number of lttng_event entries in events; on error, returns a |
1489 | * negative value. | |
834978fd DG |
1490 | */ |
1491 | int lttng_list_syscalls(struct lttng_event **events) | |
1492 | { | |
1493 | int ret; | |
1494 | struct lttcomm_session_msg lsm; | |
1495 | ||
1496 | if (!events) { | |
1497 | return -LTTNG_ERR_INVALID; | |
1498 | } | |
1499 | ||
1500 | memset(&lsm, 0, sizeof(lsm)); | |
1501 | lsm.cmd_type = LTTNG_LIST_SYSCALLS; | |
1502 | /* Force kernel domain for system calls. */ | |
1503 | lsm.domain.type = LTTNG_DOMAIN_KERNEL; | |
1504 | ||
1505 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); | |
1506 | if (ret < 0) { | |
1507 | return ret; | |
1508 | } | |
1509 | ||
1510 | return ret / sizeof(struct lttng_event); | |
1511 | } | |
1512 | ||
1657e9bb | 1513 | /* |
9ae110e2 JG |
1514 | * Returns a human readable string describing |
1515 | * the error code (a negative value). | |
1657e9bb | 1516 | */ |
9a745bc7 | 1517 | const char *lttng_strerror(int code) |
1657e9bb | 1518 | { |
f73fabfd | 1519 | return error_get_str(code); |
1657e9bb DG |
1520 | } |
1521 | ||
aaf97519 | 1522 | /* |
a4b92340 DG |
1523 | * Create a brand new session using name and url for destination. |
1524 | * | |
f73fabfd | 1525 | * Returns LTTNG_OK on success or a negative error code. |
00e2e675 | 1526 | */ |
a4b92340 | 1527 | int lttng_create_session(const char *name, const char *url) |
00e2e675 | 1528 | { |
3dd05a85 | 1529 | int ret; |
a4b92340 | 1530 | ssize_t size; |
00e2e675 | 1531 | struct lttcomm_session_msg lsm; |
a4b92340 | 1532 | struct lttng_uri *uris = NULL; |
00e2e675 | 1533 | |
a4b92340 | 1534 | if (name == NULL) { |
2f70b271 | 1535 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
1536 | } |
1537 | ||
a4b92340 | 1538 | memset(&lsm, 0, sizeof(lsm)); |
00e2e675 | 1539 | |
a4b92340 | 1540 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
cac3069d | 1541 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
a4b92340 DG |
1542 | |
1543 | /* There should never be a data URL */ | |
bc894455 | 1544 | size = uri_parse_str_urls(url, NULL, &uris); |
a4b92340 | 1545 | if (size < 0) { |
2f70b271 | 1546 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
1547 | } |
1548 | ||
a4b92340 DG |
1549 | lsm.u.uri.size = size; |
1550 | ||
795a978d | 1551 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 1552 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
1553 | |
1554 | free(uris); | |
1555 | return ret; | |
00e2e675 DG |
1556 | } |
1557 | ||
8028d920 | 1558 | /* |
9ae110e2 JG |
1559 | * Destroy session using name. |
1560 | * Returns size of returned session payload data or a negative error code. | |
8028d920 | 1561 | */ |
8fd2c92c | 1562 | static |
e20ee7c2 | 1563 | int _lttng_destroy_session(const char *session_name) |
8028d920 | 1564 | { |
cd80958d DG |
1565 | struct lttcomm_session_msg lsm; |
1566 | ||
843f5df9 | 1567 | if (session_name == NULL) { |
2f70b271 | 1568 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1569 | } |
1570 | ||
53efb85a | 1571 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1572 | lsm.cmd_type = LTTNG_DESTROY_SESSION; |
843f5df9 | 1573 | |
cac3069d DG |
1574 | lttng_ctl_copy_string(lsm.session.name, session_name, |
1575 | sizeof(lsm.session.name)); | |
cd80958d | 1576 | |
cac3069d | 1577 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
aaf97519 DG |
1578 | } |
1579 | ||
e20ee7c2 JD |
1580 | /* |
1581 | * Stop the session and wait for the data before destroying it | |
1582 | */ | |
1583 | int lttng_destroy_session(const char *session_name) | |
1584 | { | |
1585 | int ret; | |
1586 | ||
1587 | /* | |
1588 | * Stop the tracing and wait for the data. | |
1589 | */ | |
1590 | ret = _lttng_stop_tracing(session_name, 1); | |
1591 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
1592 | goto end; | |
1593 | } | |
1594 | ||
1595 | ret = _lttng_destroy_session(session_name); | |
1596 | end: | |
1597 | return ret; | |
1598 | } | |
1599 | ||
1600 | /* | |
1601 | * Destroy the session without waiting for the data. | |
1602 | */ | |
1603 | int lttng_destroy_session_no_wait(const char *session_name) | |
1604 | { | |
1605 | int ret; | |
1606 | ||
1607 | /* | |
1608 | * Stop the tracing without waiting for the data. | |
1609 | * The session might already have been stopped, so just | |
1610 | * skip this error. | |
1611 | */ | |
1612 | ret = _lttng_stop_tracing(session_name, 0); | |
1613 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
1614 | goto end; | |
1615 | } | |
1616 | ||
1617 | ret = _lttng_destroy_session(session_name); | |
1618 | end: | |
1619 | return ret; | |
1620 | } | |
1621 | ||
57167058 | 1622 | /* |
9ae110e2 JG |
1623 | * Ask the session daemon for all available sessions. |
1624 | * Sets the contents of the sessions array. | |
1625 | * Returns the number of lttng_session entries in sessions; | |
1626 | * on error, returns a negative value. | |
57167058 | 1627 | */ |
ca95a216 | 1628 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 1629 | { |
ca95a216 | 1630 | int ret; |
cd80958d | 1631 | struct lttcomm_session_msg lsm; |
57167058 | 1632 | |
53efb85a | 1633 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1634 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
cac3069d | 1635 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions); |
57167058 | 1636 | if (ret < 0) { |
ca95a216 | 1637 | return ret; |
57167058 DG |
1638 | } |
1639 | ||
ca95a216 | 1640 | return ret / sizeof(struct lttng_session); |
57167058 DG |
1641 | } |
1642 | ||
d7ba1388 MD |
1643 | int lttng_set_session_shm_path(const char *session_name, |
1644 | const char *shm_path) | |
1645 | { | |
1646 | struct lttcomm_session_msg lsm; | |
1647 | ||
1648 | if (session_name == NULL) { | |
1649 | return -LTTNG_ERR_INVALID; | |
1650 | } | |
1651 | ||
1652 | memset(&lsm, 0, sizeof(lsm)); | |
1653 | lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; | |
1654 | ||
1655 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
1656 | sizeof(lsm.session.name)); | |
1657 | lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path, | |
1658 | sizeof(lsm.u.set_shm_path.shm_path)); | |
1659 | ||
1660 | return lttng_ctl_ask_sessiond(&lsm, NULL); | |
1661 | } | |
1662 | ||
9f19cc17 | 1663 | /* |
9ae110e2 JG |
1664 | * Ask the session daemon for all available domains of a session. |
1665 | * Sets the contents of the domains array. | |
1666 | * Returns the number of lttng_domain entries in domains; | |
1667 | * on error, returns a negative value. | |
9f19cc17 | 1668 | */ |
330be774 | 1669 | int lttng_list_domains(const char *session_name, |
cd80958d | 1670 | struct lttng_domain **domains) |
9f19cc17 DG |
1671 | { |
1672 | int ret; | |
cd80958d DG |
1673 | struct lttcomm_session_msg lsm; |
1674 | ||
330be774 | 1675 | if (session_name == NULL) { |
2f70b271 | 1676 | return -LTTNG_ERR_INVALID; |
cd80958d | 1677 | } |
9f19cc17 | 1678 | |
53efb85a | 1679 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
1680 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
1681 | ||
cac3069d DG |
1682 | lttng_ctl_copy_string(lsm.session.name, session_name, |
1683 | sizeof(lsm.session.name)); | |
cd80958d | 1684 | |
cac3069d | 1685 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
9f19cc17 DG |
1686 | if (ret < 0) { |
1687 | return ret; | |
1688 | } | |
1689 | ||
1690 | return ret / sizeof(struct lttng_domain); | |
1691 | } | |
1692 | ||
1693 | /* | |
9ae110e2 JG |
1694 | * Ask the session daemon for all available channels of a session. |
1695 | * Sets the contents of the channels array. | |
1696 | * Returns the number of lttng_channel entries in channels; | |
1697 | * on error, returns a negative value. | |
9f19cc17 | 1698 | */ |
cd80958d DG |
1699 | int lttng_list_channels(struct lttng_handle *handle, |
1700 | struct lttng_channel **channels) | |
9f19cc17 DG |
1701 | { |
1702 | int ret; | |
53e367f9 JG |
1703 | size_t channel_count, i; |
1704 | const size_t channel_size = sizeof(struct lttng_channel) + | |
1705 | sizeof(struct lttcomm_channel_extended); | |
cd80958d | 1706 | struct lttcomm_session_msg lsm; |
53e367f9 | 1707 | void *extended_at; |
cd80958d | 1708 | |
9d697d3d | 1709 | if (handle == NULL) { |
53e367f9 JG |
1710 | ret = -LTTNG_ERR_INVALID; |
1711 | goto end; | |
cd80958d DG |
1712 | } |
1713 | ||
53efb85a | 1714 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1715 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
cac3069d | 1716 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1717 | sizeof(lsm.session.name)); |
9f19cc17 | 1718 | |
cac3069d | 1719 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 1720 | |
cac3069d | 1721 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); |
9f19cc17 | 1722 | if (ret < 0) { |
53e367f9 JG |
1723 | goto end; |
1724 | } | |
1725 | ||
1726 | if (ret % channel_size) { | |
1727 | ret = -LTTNG_ERR_UNK; | |
1728 | free(*channels); | |
1729 | *channels = NULL; | |
1730 | goto end; | |
9f19cc17 | 1731 | } |
53e367f9 | 1732 | channel_count = (size_t) ret / channel_size; |
9f19cc17 | 1733 | |
53e367f9 JG |
1734 | /* Set extended info pointers */ |
1735 | extended_at = ((void *) *channels) + | |
1736 | channel_count * sizeof(struct lttng_channel); | |
1737 | for (i = 0; i < channel_count; i++) { | |
1738 | struct lttng_channel *chan = &(*channels)[i]; | |
1739 | ||
1740 | chan->attr.extended.ptr = extended_at; | |
1741 | extended_at += sizeof(struct lttcomm_channel_extended); | |
1742 | } | |
1743 | ||
1744 | ret = (int) channel_count; | |
1745 | end: | |
1746 | return ret; | |
9f19cc17 DG |
1747 | } |
1748 | ||
1749 | /* | |
9ae110e2 JG |
1750 | * Ask the session daemon for all available events of a session channel. |
1751 | * Sets the contents of the events array. | |
1752 | * Returns the number of lttng_event entries in events; | |
1753 | * on error, returns a negative value. | |
9f19cc17 | 1754 | */ |
cd80958d DG |
1755 | int lttng_list_events(struct lttng_handle *handle, |
1756 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
1757 | { |
1758 | int ret; | |
cd80958d | 1759 | struct lttcomm_session_msg lsm; |
b4e3ceb9 PP |
1760 | struct lttcomm_event_command_header *cmd_header = NULL; |
1761 | size_t cmd_header_len; | |
1762 | uint32_t nb_events, i; | |
1763 | void *extended_at; | |
9f19cc17 | 1764 | |
9d697d3d DG |
1765 | /* Safety check. An handle and channel name are mandatory */ |
1766 | if (handle == NULL || channel_name == NULL) { | |
2f70b271 | 1767 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1768 | } |
1769 | ||
53efb85a | 1770 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1771 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
cac3069d | 1772 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1773 | sizeof(lsm.session.name)); |
cac3069d | 1774 | lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name, |
cd80958d | 1775 | sizeof(lsm.u.list.channel_name)); |
cac3069d | 1776 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 1777 | |
b4e3ceb9 PP |
1778 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, NULL, 0, (void **) events, |
1779 | (void **) &cmd_header, &cmd_header_len); | |
9f19cc17 | 1780 | if (ret < 0) { |
b4e3ceb9 | 1781 | goto error; |
9f19cc17 DG |
1782 | } |
1783 | ||
b4e3ceb9 PP |
1784 | /* Set number of events and free command header */ |
1785 | nb_events = cmd_header->nb_events; | |
1786 | if (nb_events > INT_MAX) { | |
1787 | ret = -EOVERFLOW; | |
1788 | goto error; | |
1789 | } | |
1790 | ret = (int) nb_events; | |
1791 | free(cmd_header); | |
1792 | cmd_header = NULL; | |
1793 | ||
1794 | /* Set extended info pointers */ | |
1795 | extended_at = ((void*) (*events)) + | |
1796 | nb_events * sizeof(struct lttng_event); | |
1797 | ||
1798 | for (i = 0; i < nb_events; i++) { | |
1799 | struct lttcomm_event_extended_header *ext_header; | |
1800 | struct lttng_event *event = &(*events)[i]; | |
1801 | ||
1802 | event->extended.ptr = extended_at; | |
1803 | ext_header = | |
1804 | (struct lttcomm_event_extended_header *) extended_at; | |
1805 | extended_at += sizeof(*ext_header); | |
1806 | extended_at += ext_header->filter_len; | |
795d57ce PP |
1807 | extended_at += |
1808 | ext_header->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
b4e3ceb9 PP |
1809 | } |
1810 | ||
1811 | return ret; | |
1812 | error: | |
1813 | free(cmd_header); | |
1814 | free(*events); | |
1815 | return ret; | |
9f19cc17 DG |
1816 | } |
1817 | ||
134e72ed JG |
1818 | int lttng_event_get_filter_expression(struct lttng_event *event, |
1819 | const char **filter_expression) | |
d31d3e8c PP |
1820 | { |
1821 | int ret = 0; | |
1822 | struct lttcomm_event_extended_header *ext_header; | |
1823 | ||
134e72ed | 1824 | if (!event || !filter_expression) { |
d31d3e8c PP |
1825 | ret = -LTTNG_ERR_INVALID; |
1826 | goto end; | |
1827 | } | |
1828 | ||
1829 | ext_header = event->extended.ptr; | |
1830 | ||
1831 | if (!ext_header) { | |
1832 | /* | |
1833 | * This can happen since the lttng_event structure is | |
1834 | * used for other tasks where this pointer is never set. | |
1835 | */ | |
134e72ed | 1836 | *filter_expression = NULL; |
d31d3e8c PP |
1837 | goto end; |
1838 | } | |
1839 | ||
1840 | if (ext_header->filter_len) { | |
134e72ed JG |
1841 | *filter_expression = ((const char *) (ext_header)) + |
1842 | sizeof(*ext_header); | |
d31d3e8c | 1843 | } else { |
134e72ed | 1844 | *filter_expression = NULL; |
d31d3e8c PP |
1845 | } |
1846 | ||
1847 | end: | |
1848 | return ret; | |
1849 | } | |
b4e3ceb9 | 1850 | |
f086e50e PP |
1851 | int lttng_event_get_exclusion_name_count(struct lttng_event *event) |
1852 | { | |
1853 | int ret; | |
1854 | struct lttcomm_event_extended_header *ext_header; | |
1855 | ||
1856 | if (!event) { | |
1857 | ret = -LTTNG_ERR_INVALID; | |
1858 | goto end; | |
1859 | } | |
1860 | ||
1861 | ext_header = event->extended.ptr; | |
1862 | if (!ext_header) { | |
1863 | /* | |
1864 | * This can happen since the lttng_event structure is | |
1865 | * used for other tasks where this pointer is never set. | |
1866 | */ | |
1867 | ret = 0; | |
1868 | goto end; | |
1869 | } | |
1870 | ||
1871 | if (ext_header->nb_exclusions > INT_MAX) { | |
1872 | ret = -LTTNG_ERR_OVERFLOW; | |
1873 | goto end; | |
1874 | } | |
1875 | ret = (int) ext_header->nb_exclusions; | |
1876 | end: | |
1877 | return ret; | |
1878 | } | |
1879 | ||
1880 | int lttng_event_get_exclusion_name(struct lttng_event *event, | |
1881 | size_t index, const char **exclusion_name) | |
1882 | { | |
1883 | int ret = 0; | |
1884 | struct lttcomm_event_extended_header *ext_header; | |
1885 | void *at; | |
1886 | ||
1887 | if (!event || !exclusion_name) { | |
1888 | ret = -LTTNG_ERR_INVALID; | |
1889 | goto end; | |
1890 | } | |
1891 | ||
1892 | ext_header = event->extended.ptr; | |
1893 | if (!ext_header) { | |
1894 | ret = -LTTNG_ERR_INVALID; | |
1895 | goto end; | |
1896 | } | |
1897 | ||
1898 | if (index >= ext_header->nb_exclusions) { | |
1899 | ret = -LTTNG_ERR_INVALID; | |
1900 | goto end; | |
1901 | } | |
1902 | ||
1903 | at = (void *) ext_header + sizeof(*ext_header); | |
1904 | at += ext_header->filter_len; | |
1905 | at += index * LTTNG_SYMBOL_NAME_LEN; | |
1906 | *exclusion_name = at; | |
1907 | ||
1908 | end: | |
1909 | return ret; | |
1910 | } | |
1911 | ||
fac6795d | 1912 | /* |
1c8d13c8 TD |
1913 | * Sets the tracing_group variable with name. |
1914 | * This function allocates memory pointed to by tracing_group. | |
1915 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
1916 | */ |
1917 | int lttng_set_tracing_group(const char *name) | |
1918 | { | |
9d697d3d | 1919 | if (name == NULL) { |
2f70b271 | 1920 | return -LTTNG_ERR_INVALID; |
9d697d3d DG |
1921 | } |
1922 | ||
fac6795d | 1923 | if (asprintf(&tracing_group, "%s", name) < 0) { |
2f70b271 | 1924 | return -LTTNG_ERR_FATAL; |
fac6795d DG |
1925 | } |
1926 | ||
1927 | return 0; | |
1928 | } | |
1929 | ||
d0254c7c | 1930 | /* |
af87c45a | 1931 | * Returns size of returned session payload data or a negative error code. |
d0254c7c | 1932 | */ |
cd80958d | 1933 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
1934 | struct lttng_calibrate *calibrate) |
1935 | { | |
cd80958d | 1936 | struct lttcomm_session_msg lsm; |
d0254c7c | 1937 | |
9d697d3d DG |
1938 | /* Safety check. NULL pointer are forbidden */ |
1939 | if (handle == NULL || calibrate == NULL) { | |
2f70b271 | 1940 | return -LTTNG_ERR_INVALID; |
cd80958d | 1941 | } |
d0254c7c | 1942 | |
53efb85a | 1943 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1944 | lsm.cmd_type = LTTNG_CALIBRATE; |
cac3069d | 1945 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
d0254c7c | 1946 | |
cd80958d DG |
1947 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
1948 | ||
cac3069d | 1949 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d0254c7c MD |
1950 | } |
1951 | ||
5edd7e09 DG |
1952 | /* |
1953 | * Set default channel attributes. | |
441c16a7 | 1954 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
1955 | */ |
1956 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
1957 | struct lttng_channel_attr *attr) | |
1958 | { | |
1959 | /* Safety check */ | |
1960 | if (attr == NULL || domain == NULL) { | |
1961 | return; | |
1962 | } | |
1963 | ||
eacaa7a6 DS |
1964 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
1965 | ||
0a9c6494 DG |
1966 | /* Same for all domains. */ |
1967 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
1968 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; | |
1969 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; | |
1970 | ||
5edd7e09 DG |
1971 | switch (domain->type) { |
1972 | case LTTNG_DOMAIN_KERNEL: | |
9ae110e2 JG |
1973 | attr->switch_timer_interval = |
1974 | DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; | |
d92ff3ef | 1975 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
3e230f92 | 1976 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
5edd7e09 DG |
1977 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
1978 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
1979 | break; | |
1980 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
1981 | switch (domain->buf_type) { |
1982 | case LTTNG_BUFFER_PER_UID: | |
1983 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); | |
1984 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; | |
1985 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
1986 | attr->switch_timer_interval = |
1987 | DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; | |
1988 | attr->read_timer_interval = | |
1989 | DEFAULT_UST_UID_CHANNEL_READ_TIMER; | |
0a9c6494 DG |
1990 | break; |
1991 | case LTTNG_BUFFER_PER_PID: | |
1992 | default: | |
1993 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); | |
1994 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; | |
1995 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
1996 | attr->switch_timer_interval = |
1997 | DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; | |
1998 | attr->read_timer_interval = | |
1999 | DEFAULT_UST_PID_CHANNEL_READ_TIMER; | |
0a9c6494 DG |
2000 | break; |
2001 | } | |
5edd7e09 | 2002 | default: |
441c16a7 | 2003 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
2004 | break; |
2005 | } | |
2006 | } | |
2007 | ||
5ba3702f JG |
2008 | int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, |
2009 | uint64_t *discarded_events) | |
2010 | { | |
2011 | int ret = 0; | |
2012 | struct lttcomm_channel_extended *chan_ext; | |
2013 | ||
2014 | if (!channel || !discarded_events) { | |
2015 | ret = -LTTNG_ERR_INVALID; | |
2016 | goto end; | |
2017 | } | |
2018 | ||
2019 | chan_ext = channel->attr.extended.ptr; | |
2020 | if (!chan_ext) { | |
2021 | /* | |
2022 | * This can happen since the lttng_channel structure is | |
2023 | * used for other tasks where this pointer is never set. | |
2024 | */ | |
2025 | *discarded_events = 0; | |
2026 | goto end; | |
2027 | } | |
2028 | ||
2029 | *discarded_events = chan_ext->discarded_events; | |
2030 | end: | |
2031 | return ret; | |
2032 | } | |
2033 | ||
2034 | int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, | |
2035 | uint64_t *lost_packets) | |
2036 | { | |
2037 | int ret = 0; | |
2038 | struct lttcomm_channel_extended *chan_ext; | |
2039 | ||
2040 | if (!channel || !lost_packets) { | |
2041 | ret = -LTTNG_ERR_INVALID; | |
2042 | goto end; | |
2043 | } | |
2044 | ||
2045 | chan_ext = channel->attr.extended.ptr; | |
2046 | if (!chan_ext) { | |
2047 | /* | |
2048 | * This can happen since the lttng_channel structure is | |
2049 | * used for other tasks where this pointer is never set. | |
2050 | */ | |
2051 | *lost_packets = 0; | |
2052 | goto end; | |
2053 | } | |
2054 | ||
2055 | *lost_packets = chan_ext->lost_packets; | |
2056 | end: | |
2057 | return ret; | |
2058 | } | |
2059 | ||
fac6795d | 2060 | /* |
2269e89e | 2061 | * Check if session daemon is alive. |
fac6795d | 2062 | * |
2269e89e | 2063 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 2064 | * On error returns a negative value. |
fac6795d | 2065 | */ |
947308c4 | 2066 | int lttng_session_daemon_alive(void) |
fac6795d DG |
2067 | { |
2068 | int ret; | |
2069 | ||
2070 | ret = set_session_daemon_path(); | |
2071 | if (ret < 0) { | |
9ae110e2 | 2072 | /* Error. */ |
fac6795d DG |
2073 | return ret; |
2074 | } | |
2075 | ||
9d035200 | 2076 | if (*sessiond_sock_path == '\0') { |
2f70b271 | 2077 | /* |
9ae110e2 JG |
2078 | * No socket path set. Weird error which means the constructor |
2079 | * was not called. | |
2f70b271 DG |
2080 | */ |
2081 | assert(0); | |
fac6795d DG |
2082 | } |
2083 | ||
2269e89e | 2084 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 | 2085 | if (ret < 0) { |
9ae110e2 | 2086 | /* Not alive. */ |
7d8234d9 MD |
2087 | return 0; |
2088 | } | |
7d8234d9 | 2089 | |
9ae110e2 | 2090 | /* Is alive. */ |
947308c4 | 2091 | return 1; |
fac6795d DG |
2092 | } |
2093 | ||
00e2e675 | 2094 | /* |
a4b92340 | 2095 | * Set URL for a consumer for a session and domain. |
00e2e675 DG |
2096 | * |
2097 | * Return 0 on success, else a negative value. | |
2098 | */ | |
a4b92340 DG |
2099 | int lttng_set_consumer_url(struct lttng_handle *handle, |
2100 | const char *control_url, const char *data_url) | |
00e2e675 | 2101 | { |
3dd05a85 | 2102 | int ret; |
a4b92340 | 2103 | ssize_t size; |
00e2e675 | 2104 | struct lttcomm_session_msg lsm; |
a4b92340 | 2105 | struct lttng_uri *uris = NULL; |
00e2e675 | 2106 | |
a4b92340 | 2107 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
2f70b271 | 2108 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
2109 | } |
2110 | ||
a4b92340 DG |
2111 | memset(&lsm, 0, sizeof(lsm)); |
2112 | ||
00e2e675 DG |
2113 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
2114 | ||
cac3069d | 2115 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
00e2e675 | 2116 | sizeof(lsm.session.name)); |
cac3069d | 2117 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
00e2e675 | 2118 | |
bc894455 | 2119 | size = uri_parse_str_urls(control_url, data_url, &uris); |
a4b92340 | 2120 | if (size < 0) { |
2f70b271 | 2121 | return -LTTNG_ERR_INVALID; |
a4b92340 DG |
2122 | } |
2123 | ||
2124 | lsm.u.uri.size = size; | |
00e2e675 | 2125 | |
795a978d | 2126 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 2127 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
2128 | |
2129 | free(uris); | |
2130 | return ret; | |
00e2e675 DG |
2131 | } |
2132 | ||
2133 | /* | |
9c6bda17 | 2134 | * [OBSOLETE] |
00e2e675 DG |
2135 | */ |
2136 | int lttng_enable_consumer(struct lttng_handle *handle) | |
2137 | { | |
785d2d0d | 2138 | return -ENOSYS; |
00e2e675 DG |
2139 | } |
2140 | ||
2141 | /* | |
9c6bda17 | 2142 | * [OBSOLETE] |
00e2e675 DG |
2143 | */ |
2144 | int lttng_disable_consumer(struct lttng_handle *handle) | |
2145 | { | |
785d2d0d | 2146 | return -ENOSYS; |
00e2e675 DG |
2147 | } |
2148 | ||
07424f16 DG |
2149 | /* |
2150 | * This is an extension of create session that is ONLY and SHOULD only be used | |
2151 | * by the lttng command line program. It exists to avoid using URI parsing in | |
2152 | * the lttng client. | |
2153 | * | |
2154 | * We need the date and time for the trace path subdirectory for the case where | |
2155 | * the user does NOT define one using either -o or -U. Using the normal | |
2156 | * lttng_create_session API call, we have no clue on the session daemon side if | |
2157 | * the URL was generated automatically by the client or define by the user. | |
2158 | * | |
2159 | * So this function "wrapper" is hidden from the public API, takes the datetime | |
2160 | * string and appends it if necessary to the URI subdirectory before sending it | |
2161 | * to the session daemon. | |
2162 | * | |
2163 | * With this extra function, the lttng_create_session call behavior is not | |
2164 | * changed and the timestamp is appended to the URI on the session daemon side | |
2165 | * if necessary. | |
2166 | */ | |
2167 | int _lttng_create_session_ext(const char *name, const char *url, | |
2168 | const char *datetime) | |
2169 | { | |
3dd05a85 | 2170 | int ret; |
07424f16 DG |
2171 | ssize_t size; |
2172 | struct lttcomm_session_msg lsm; | |
2173 | struct lttng_uri *uris = NULL; | |
2174 | ||
2bba9e53 | 2175 | if (name == NULL || datetime == NULL) { |
2f70b271 | 2176 | return -LTTNG_ERR_INVALID; |
07424f16 DG |
2177 | } |
2178 | ||
2179 | memset(&lsm, 0, sizeof(lsm)); | |
2180 | ||
2181 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
cac3069d | 2182 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
07424f16 | 2183 | |
9ae110e2 | 2184 | /* There should never be a data URL. */ |
bc894455 | 2185 | size = uri_parse_str_urls(url, NULL, &uris); |
07424f16 | 2186 | if (size < 0) { |
3dd05a85 DG |
2187 | ret = -LTTNG_ERR_INVALID; |
2188 | goto error; | |
07424f16 DG |
2189 | } |
2190 | ||
2191 | lsm.u.uri.size = size; | |
2192 | ||
4b35a6b3 | 2193 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) { |
da4aa2b8 DG |
2194 | /* Don't append datetime if the name was automatically created. */ |
2195 | if (strncmp(name, DEFAULT_SESSION_NAME "-", | |
2196 | strlen(DEFAULT_SESSION_NAME) + 1)) { | |
2197 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", | |
2198 | name, datetime); | |
2199 | } else { | |
2200 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name); | |
2201 | } | |
07424f16 DG |
2202 | if (ret < 0) { |
2203 | PERROR("snprintf uri subdir"); | |
3dd05a85 DG |
2204 | ret = -LTTNG_ERR_FATAL; |
2205 | goto error; | |
07424f16 DG |
2206 | } |
2207 | } | |
2208 | ||
795a978d | 2209 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 2210 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
2211 | |
2212 | error: | |
2213 | free(uris); | |
2214 | return ret; | |
07424f16 DG |
2215 | } |
2216 | ||
806e2684 DG |
2217 | /* |
2218 | * For a given session name, this call checks if the data is ready to be read | |
2219 | * or is still being extracted by the consumer(s) hence not ready to be used by | |
2220 | * any readers. | |
2221 | */ | |
6d805429 | 2222 | int lttng_data_pending(const char *session_name) |
806e2684 DG |
2223 | { |
2224 | int ret; | |
2225 | struct lttcomm_session_msg lsm; | |
f6151c55 | 2226 | uint8_t *pending = NULL; |
806e2684 DG |
2227 | |
2228 | if (session_name == NULL) { | |
2229 | return -LTTNG_ERR_INVALID; | |
2230 | } | |
2231 | ||
53efb85a | 2232 | memset(&lsm, 0, sizeof(lsm)); |
6d805429 | 2233 | lsm.cmd_type = LTTNG_DATA_PENDING; |
806e2684 | 2234 | |
cac3069d DG |
2235 | lttng_ctl_copy_string(lsm.session.name, session_name, |
2236 | sizeof(lsm.session.name)); | |
806e2684 | 2237 | |
f6151c55 JG |
2238 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending); |
2239 | if (ret < 0) { | |
2240 | goto end; | |
2241 | } else if (ret != 1) { | |
2242 | /* Unexpected payload size */ | |
2243 | ret = -LTTNG_ERR_INVALID; | |
2244 | goto end; | |
806e2684 DG |
2245 | } |
2246 | ||
f6151c55 JG |
2247 | ret = (int) *pending; |
2248 | end: | |
2249 | free(pending); | |
806e2684 DG |
2250 | return ret; |
2251 | } | |
2252 | ||
27babd3a DG |
2253 | /* |
2254 | * Create a session exclusively used for snapshot. | |
2255 | * | |
2256 | * Returns LTTNG_OK on success or a negative error code. | |
2257 | */ | |
2258 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) | |
2259 | { | |
2260 | int ret; | |
2261 | ssize_t size; | |
2262 | struct lttcomm_session_msg lsm; | |
2263 | struct lttng_uri *uris = NULL; | |
2264 | ||
2265 | if (name == NULL) { | |
2266 | return -LTTNG_ERR_INVALID; | |
2267 | } | |
2268 | ||
2269 | memset(&lsm, 0, sizeof(lsm)); | |
2270 | ||
2271 | lsm.cmd_type = LTTNG_CREATE_SESSION_SNAPSHOT; | |
2272 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
2273 | ||
2274 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); | |
2275 | if (size < 0) { | |
2276 | return -LTTNG_ERR_INVALID; | |
2277 | } | |
2278 | ||
2279 | lsm.u.uri.size = size; | |
2280 | ||
795a978d | 2281 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
27babd3a DG |
2282 | sizeof(struct lttng_uri) * size, NULL); |
2283 | ||
2284 | free(uris); | |
2285 | return ret; | |
2286 | } | |
2287 | ||
ecc48a90 JD |
2288 | /* |
2289 | * Create a session exclusively used for live. | |
2290 | * | |
2291 | * Returns LTTNG_OK on success or a negative error code. | |
2292 | */ | |
2293 | int lttng_create_session_live(const char *name, const char *url, | |
2294 | unsigned int timer_interval) | |
2295 | { | |
2296 | int ret; | |
2297 | ssize_t size; | |
2298 | struct lttcomm_session_msg lsm; | |
2299 | struct lttng_uri *uris = NULL; | |
2300 | ||
92805ee4 | 2301 | if (name == NULL || timer_interval == 0) { |
ecc48a90 JD |
2302 | return -LTTNG_ERR_INVALID; |
2303 | } | |
2304 | ||
2305 | memset(&lsm, 0, sizeof(lsm)); | |
2306 | ||
2307 | lsm.cmd_type = LTTNG_CREATE_SESSION_LIVE; | |
2308 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
2309 | ||
1a241656 DG |
2310 | if (url) { |
2311 | size = uri_parse_str_urls(url, NULL, &uris); | |
2312 | if (size <= 0) { | |
2313 | ret = -LTTNG_ERR_INVALID; | |
2314 | goto end; | |
2315 | } | |
ecc48a90 | 2316 | |
1a241656 DG |
2317 | /* file:// is not accepted for live session. */ |
2318 | if (uris[0].dtype == LTTNG_DST_PATH) { | |
2319 | ret = -LTTNG_ERR_INVALID; | |
2320 | goto end; | |
2321 | } | |
2322 | } else { | |
2323 | size = 0; | |
ecc48a90 JD |
2324 | } |
2325 | ||
2326 | lsm.u.session_live.nb_uri = size; | |
2327 | lsm.u.session_live.timer_interval = timer_interval; | |
2328 | ||
795a978d | 2329 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
ecc48a90 JD |
2330 | sizeof(struct lttng_uri) * size, NULL); |
2331 | ||
2332 | end: | |
2333 | free(uris); | |
2334 | return ret; | |
2335 | } | |
2336 | ||
a5dfbb9d MD |
2337 | /* |
2338 | * List PIDs in the tracker. | |
2339 | * | |
1b18ce93 JG |
2340 | * enabled is set to whether the PID tracker is enabled. |
2341 | * pids is set to an allocated array of PIDs currently tracked. On | |
2342 | * success, pids must be freed by the caller. | |
2343 | * nr_pids is set to the number of entries contained by the pids array. | |
a5dfbb9d MD |
2344 | * |
2345 | * Returns 0 on success, else a negative LTTng error code. | |
2346 | */ | |
2347 | int lttng_list_tracker_pids(struct lttng_handle *handle, | |
2348 | int *_enabled, int32_t **_pids, size_t *_nr_pids) | |
2349 | { | |
ebbf5ab7 JR |
2350 | int ret; |
2351 | int enabled = 1; | |
a5dfbb9d MD |
2352 | struct lttcomm_session_msg lsm; |
2353 | size_t nr_pids; | |
2354 | int32_t *pids; | |
2355 | ||
2356 | if (handle == NULL) { | |
2357 | return -LTTNG_ERR_INVALID; | |
2358 | } | |
2359 | ||
2360 | memset(&lsm, 0, sizeof(lsm)); | |
2361 | lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS; | |
2362 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, | |
2363 | sizeof(lsm.session.name)); | |
2364 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); | |
2365 | ||
2366 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids); | |
2367 | if (ret < 0) { | |
2368 | return ret; | |
2369 | } | |
2370 | nr_pids = ret / sizeof(int32_t); | |
2371 | if (nr_pids == 1 && pids[0] == -1) { | |
2372 | free(pids); | |
2373 | pids = NULL; | |
2374 | enabled = 0; | |
2375 | nr_pids = 0; | |
2376 | } | |
2377 | *_enabled = enabled; | |
2378 | *_pids = pids; | |
2379 | *_nr_pids = nr_pids; | |
2380 | return 0; | |
2381 | } | |
2382 | ||
93ec662e JD |
2383 | /* |
2384 | * Regenerate the metadata for a session. | |
2385 | * Return 0 on success, a negative error code on error. | |
2386 | */ | |
eded6438 | 2387 | int lttng_regenerate_metadata(const char *session_name) |
93ec662e JD |
2388 | { |
2389 | int ret; | |
2390 | struct lttcomm_session_msg lsm; | |
2391 | ||
2392 | if (!session_name) { | |
2393 | ret = -LTTNG_ERR_INVALID; | |
2394 | goto end; | |
2395 | } | |
2396 | ||
2397 | memset(&lsm, 0, sizeof(lsm)); | |
eded6438 | 2398 | lsm.cmd_type = LTTNG_REGENERATE_METADATA; |
93ec662e JD |
2399 | |
2400 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
2401 | sizeof(lsm.session.name)); | |
2402 | ||
2403 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
2404 | if (ret < 0) { | |
2405 | goto end; | |
2406 | } | |
2407 | ||
2408 | ret = 0; | |
2409 | end: | |
2410 | return ret; | |
2411 | } | |
2412 | ||
eded6438 JD |
2413 | /* |
2414 | * Deprecated, replaced by lttng_regenerate_metadata. | |
2415 | */ | |
2416 | int lttng_metadata_regenerate(const char *session_name) | |
2417 | { | |
2418 | return lttng_regenerate_metadata(session_name); | |
2419 | } | |
2420 | ||
fac6795d | 2421 | /* |
9ae110e2 | 2422 | * lib constructor. |
fac6795d | 2423 | */ |
b2b89e8a | 2424 | static void __attribute__((constructor)) init(void) |
fac6795d DG |
2425 | { |
2426 | /* Set default session group */ | |
bbccc3d2 | 2427 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 2428 | } |
49cca668 DG |
2429 | |
2430 | /* | |
9ae110e2 | 2431 | * lib destructor. |
49cca668 | 2432 | */ |
b2b89e8a | 2433 | static void __attribute__((destructor)) lttng_ctl_exit(void) |
49cca668 DG |
2434 | { |
2435 | free(tracing_group); | |
2436 | } |