Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
eb5c4f4e | 2 | * lttng-ctl.c |
82a3637f DG |
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 | 32 | #include <common/common.h> |
15e37663 | 33 | #include <common/compat/string.h> |
990570ed | 34 | #include <common/defaults.h> |
db758600 | 35 | #include <common/sessiond-comm/sessiond-comm.h> |
a4b92340 | 36 | #include <common/uri.h> |
feb0f3e5 | 37 | #include <common/utils.h> |
de453daa | 38 | #include <common/dynamic-buffer.h> |
1e307fab | 39 | #include <lttng/lttng.h> |
0c89d795 | 40 | #include <lttng/health-internal.h> |
a58c490f JG |
41 | #include <lttng/trigger/trigger-internal.h> |
42 | #include <lttng/endpoint.h> | |
43 | #include <lttng/channel-internal.h> | |
de453daa | 44 | #include <lttng/event-internal.h> |
15e37663 | 45 | #include <lttng/userspace-probe-internal.h> |
5feee503 JG |
46 | #include <lttng/session-internal.h> |
47 | #include <lttng/session-descriptor-internal.h> | |
823a1989 | 48 | #include <lttng/destruction-handle.h> |
fac6795d | 49 | |
d00c599e DG |
50 | #include "filter/filter-ast.h" |
51 | #include "filter/filter-parser.h" | |
52 | #include "filter/filter-bytecode.h" | |
53 | #include "filter/memstream.h" | |
cac3069d | 54 | #include "lttng-ctl-helper.h" |
53a80697 MD |
55 | |
56 | #ifdef DEBUG | |
d00c599e | 57 | static const int print_xml = 1; |
53a80697 MD |
58 | #define dbg_printf(fmt, args...) \ |
59 | printf("[debug liblttng-ctl] " fmt, ## args) | |
60 | #else | |
d00c599e | 61 | static const int print_xml = 0; |
53a80697 MD |
62 | #define dbg_printf(fmt, args...) \ |
63 | do { \ | |
64 | /* do nothing but check printf format */ \ | |
65 | if (0) \ | |
66 | printf("[debug liblttnctl] " fmt, ## args); \ | |
67 | } while (0) | |
68 | #endif | |
69 | ||
67040e31 JG |
70 | #define COPY_DOMAIN_PACKED(dst, src) \ |
71 | do { \ | |
72 | struct lttng_domain _tmp_domain; \ | |
73 | \ | |
74 | lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \ | |
75 | dst = _tmp_domain; \ | |
76 | } while (0) | |
53a80697 | 77 | |
fac6795d | 78 | /* Socket to session daemon for communication */ |
823a1989 | 79 | static int sessiond_socket = -1; |
fac6795d DG |
80 | static char sessiond_sock_path[PATH_MAX]; |
81 | ||
fac6795d DG |
82 | /* Variables */ |
83 | static char *tracing_group; | |
84 | static int connected; | |
85 | ||
97e19046 DG |
86 | /* Global */ |
87 | ||
88 | /* | |
89 | * Those two variables are used by error.h to silent or control the verbosity of | |
90 | * error message. They are global to the library so application linking with it | |
91 | * are able to compile correctly and also control verbosity of the library. | |
97e19046 DG |
92 | */ |
93 | int lttng_opt_quiet; | |
94 | int lttng_opt_verbose; | |
c7e35b03 | 95 | int lttng_opt_mi; |
97e19046 | 96 | |
fac6795d | 97 | /* |
cd80958d | 98 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 99 | * |
cd80958d DG |
100 | * If domain is unknown, default domain will be the kernel. |
101 | */ | |
cac3069d DG |
102 | LTTNG_HIDDEN |
103 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, | |
104 | struct lttng_domain *src) | |
cd80958d DG |
105 | { |
106 | if (src && dst) { | |
107 | switch (src->type) { | |
00e2e675 DG |
108 | case LTTNG_DOMAIN_KERNEL: |
109 | case LTTNG_DOMAIN_UST: | |
b9dfb167 | 110 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 111 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 112 | case LTTNG_DOMAIN_PYTHON: |
00e2e675 DG |
113 | memcpy(dst, src, sizeof(struct lttng_domain)); |
114 | break; | |
115 | default: | |
116 | memset(dst, 0, sizeof(struct lttng_domain)); | |
00e2e675 | 117 | break; |
cd80958d DG |
118 | } |
119 | } | |
120 | } | |
121 | ||
122 | /* | |
123 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 124 | * |
1c8d13c8 TD |
125 | * On success, returns the number of bytes sent (>=0) |
126 | * On error, returns -1 | |
fac6795d | 127 | */ |
cd80958d | 128 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
129 | { |
130 | int ret; | |
131 | ||
132 | if (!connected) { | |
2f70b271 | 133 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 134 | goto end; |
fac6795d DG |
135 | } |
136 | ||
a4b92340 DG |
137 | DBG("LSM cmd type : %d", lsm->cmd_type); |
138 | ||
be040666 | 139 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 140 | sizeof(struct lttcomm_session_msg)); |
2f70b271 DG |
141 | if (ret < 0) { |
142 | ret = -LTTNG_ERR_FATAL; | |
143 | } | |
e065084a DG |
144 | |
145 | end: | |
146 | return ret; | |
147 | } | |
148 | ||
53a80697 MD |
149 | /* |
150 | * Send var len data to the session daemon. | |
151 | * | |
152 | * On success, returns the number of bytes sent (>=0) | |
153 | * On error, returns -1 | |
154 | */ | |
c2d69327 | 155 | static int send_session_varlen(const void *data, size_t len) |
53a80697 MD |
156 | { |
157 | int ret; | |
158 | ||
159 | if (!connected) { | |
2f70b271 | 160 | ret = -LTTNG_ERR_NO_SESSIOND; |
53a80697 MD |
161 | goto end; |
162 | } | |
a4b92340 | 163 | |
53a80697 MD |
164 | if (!data || !len) { |
165 | ret = 0; | |
166 | goto end; | |
167 | } | |
168 | ||
169 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); | |
2f70b271 DG |
170 | if (ret < 0) { |
171 | ret = -LTTNG_ERR_FATAL; | |
172 | } | |
53a80697 MD |
173 | |
174 | end: | |
175 | return ret; | |
176 | } | |
177 | ||
a04d53fc FD |
178 | /* |
179 | * Send file descriptors to the session daemon. | |
180 | * | |
181 | * On success, returns the number of bytes sent (>=0) | |
182 | * On error, returns -1 | |
183 | */ | |
184 | static int send_session_fds(const int *fds, size_t nb_fd) | |
185 | { | |
186 | int ret; | |
187 | ||
188 | if (!connected) { | |
189 | ret = -LTTNG_ERR_NO_SESSIOND; | |
190 | goto end; | |
191 | } | |
192 | ||
193 | if (!fds || !nb_fd) { | |
194 | ret = 0; | |
195 | goto end; | |
196 | } | |
197 | ||
198 | ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd); | |
199 | if (ret < 0) { | |
200 | ret = -LTTNG_ERR_FATAL; | |
201 | } | |
202 | ||
203 | end: | |
204 | return ret; | |
205 | } | |
206 | ||
e065084a | 207 | /* |
cd80958d | 208 | * Receive data from the sessiond socket. |
e065084a | 209 | * |
1c8d13c8 TD |
210 | * On success, returns the number of bytes received (>=0) |
211 | * On error, returns -1 (recvmsg() error) or -ENOTCONN | |
e065084a | 212 | */ |
ca95a216 | 213 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
214 | { |
215 | int ret; | |
216 | ||
217 | if (!connected) { | |
2f70b271 | 218 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 219 | goto end; |
fac6795d DG |
220 | } |
221 | ||
ca95a216 | 222 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
2f70b271 DG |
223 | if (ret < 0) { |
224 | ret = -LTTNG_ERR_FATAL; | |
225 | } | |
fac6795d | 226 | |
e065084a | 227 | end: |
fac6795d DG |
228 | return ret; |
229 | } | |
230 | ||
231 | /* | |
9ae110e2 | 232 | * Check if we are in the specified group. |
65beb5ff | 233 | * |
9ae110e2 | 234 | * If yes return 1, else return -1. |
947308c4 | 235 | */ |
6c71277b MD |
236 | LTTNG_HIDDEN |
237 | int lttng_check_tracing_group(void) | |
947308c4 | 238 | { |
6710e5aa | 239 | gid_t *grp_list, tracing_gid; |
947308c4 DG |
240 | int grp_list_size, grp_id, i; |
241 | int ret = -1; | |
6c71277b | 242 | const char *grp_name = tracing_group; |
947308c4 DG |
243 | |
244 | /* Get GID of group 'tracing' */ | |
6710e5aa | 245 | if (utils_get_group_id(grp_name, false, &tracing_gid)) { |
b4d8603b | 246 | /* If grp_tracing is NULL, the group does not exist. */ |
947308c4 DG |
247 | goto end; |
248 | } | |
249 | ||
250 | /* Get number of supplementary group IDs */ | |
251 | grp_list_size = getgroups(0, NULL); | |
252 | if (grp_list_size < 0) { | |
6f04ed72 | 253 | PERROR("getgroups"); |
947308c4 DG |
254 | goto end; |
255 | } | |
256 | ||
257 | /* Alloc group list of the right size */ | |
3f451dc0 | 258 | grp_list = zmalloc(grp_list_size * sizeof(gid_t)); |
00795392 | 259 | if (!grp_list) { |
6f04ed72 | 260 | PERROR("malloc"); |
00795392 MD |
261 | goto end; |
262 | } | |
947308c4 | 263 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 264 | if (grp_id < 0) { |
6f04ed72 | 265 | PERROR("getgroups"); |
947308c4 DG |
266 | goto free_list; |
267 | } | |
268 | ||
269 | for (i = 0; i < grp_list_size; i++) { | |
6710e5aa | 270 | if (grp_list[i] == tracing_gid) { |
2269e89e | 271 | ret = 1; |
947308c4 DG |
272 | break; |
273 | } | |
274 | } | |
275 | ||
276 | free_list: | |
277 | free(grp_list); | |
278 | ||
279 | end: | |
280 | return ret; | |
281 | } | |
282 | ||
e3091d6a FD |
283 | static int check_enough_available_memory(size_t num_bytes_requested_per_cpu) |
284 | { | |
285 | int ret; | |
286 | long num_cpu; | |
287 | size_t best_mem_info; | |
288 | size_t num_bytes_requested_total; | |
289 | ||
290 | /* | |
291 | * Get the number of CPU currently online to compute the amount of | |
292 | * memory needed to create a buffer for every CPU. | |
293 | */ | |
294 | num_cpu = sysconf(_SC_NPROCESSORS_ONLN); | |
295 | if (num_cpu == -1) { | |
296 | goto error; | |
297 | } | |
298 | ||
299 | num_bytes_requested_total = num_bytes_requested_per_cpu * num_cpu; | |
300 | ||
301 | /* | |
302 | * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most | |
303 | * reliable estimate we can get but it is only exposed by the kernel | |
304 | * since 3.14. (See Linux kernel commit: | |
305 | * 34e431b0ae398fc54ea69ff85ec700722c9da773) | |
306 | */ | |
307 | ret = utils_get_memory_available(&best_mem_info); | |
308 | if (ret >= 0) { | |
309 | goto success; | |
310 | } | |
311 | ||
312 | /* | |
313 | * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This | |
314 | * is a sanity check for obvious user error. | |
315 | */ | |
316 | ret = utils_get_memory_total(&best_mem_info); | |
317 | if (ret >= 0) { | |
318 | goto success; | |
319 | } | |
320 | ||
321 | error: | |
322 | return -1; | |
323 | success: | |
324 | return best_mem_info >= num_bytes_requested_total; | |
325 | } | |
326 | ||
947308c4 | 327 | /* |
2269e89e DG |
328 | * Try connect to session daemon with sock_path. |
329 | * | |
330 | * Return 0 on success, else -1 | |
331 | */ | |
332 | static int try_connect_sessiond(const char *sock_path) | |
333 | { | |
334 | int ret; | |
335 | ||
336 | /* If socket exist, we check if the daemon listens for connect. */ | |
337 | ret = access(sock_path, F_OK); | |
338 | if (ret < 0) { | |
339 | /* Not alive */ | |
2f70b271 | 340 | goto error; |
2269e89e DG |
341 | } |
342 | ||
343 | ret = lttcomm_connect_unix_sock(sock_path); | |
344 | if (ret < 0) { | |
9ae110e2 | 345 | /* Not alive. */ |
2f70b271 | 346 | goto error; |
2269e89e DG |
347 | } |
348 | ||
349 | ret = lttcomm_close_unix_sock(ret); | |
350 | if (ret < 0) { | |
6f04ed72 | 351 | PERROR("lttcomm_close_unix_sock"); |
2269e89e DG |
352 | } |
353 | ||
354 | return 0; | |
2f70b271 DG |
355 | |
356 | error: | |
357 | return -1; | |
2269e89e DG |
358 | } |
359 | ||
360 | /* | |
2f70b271 DG |
361 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
362 | * variable. | |
363 | * | |
364 | * Returns 0 on success, negative value on failure (the sessiond socket path | |
365 | * is somehow too long or ENOMEM). | |
947308c4 DG |
366 | */ |
367 | static int set_session_daemon_path(void) | |
368 | { | |
9ae110e2 | 369 | int in_tgroup = 0; /* In tracing group. */ |
2269e89e DG |
370 | uid_t uid; |
371 | ||
372 | uid = getuid(); | |
947308c4 | 373 | |
2269e89e DG |
374 | if (uid != 0) { |
375 | /* Are we in the tracing group ? */ | |
6c71277b | 376 | in_tgroup = lttng_check_tracing_group(); |
2269e89e DG |
377 | } |
378 | ||
08a9c49f | 379 | if ((uid == 0) || in_tgroup) { |
55fb8091 JG |
380 | const int ret = lttng_strncpy(sessiond_sock_path, |
381 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
382 | sizeof(sessiond_sock_path)); | |
383 | ||
384 | if (ret) { | |
385 | goto error; | |
386 | } | |
08a9c49f | 387 | } |
2269e89e | 388 | |
08a9c49f | 389 | if (uid != 0) { |
c617c0c6 MD |
390 | int ret; |
391 | ||
08a9c49f | 392 | if (in_tgroup) { |
9ae110e2 | 393 | /* Tracing group. */ |
08a9c49f TD |
394 | ret = try_connect_sessiond(sessiond_sock_path); |
395 | if (ret >= 0) { | |
396 | goto end; | |
2269e89e | 397 | } |
08a9c49f | 398 | /* Global session daemon not available... */ |
2269e89e | 399 | } |
08a9c49f TD |
400 | /* ...or not in tracing group (and not root), default */ |
401 | ||
402 | /* | |
9ae110e2 JG |
403 | * With GNU C < 2.1, snprintf returns -1 if the target buffer |
404 | * is too small; | |
405 | * With GNU C >= 2.1, snprintf returns the required size | |
406 | * (excluding closing null) | |
08a9c49f TD |
407 | */ |
408 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
feb0f3e5 | 409 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
08a9c49f | 410 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
2f70b271 | 411 | goto error; |
947308c4 | 412 | } |
947308c4 | 413 | } |
08a9c49f | 414 | end: |
947308c4 | 415 | return 0; |
2f70b271 DG |
416 | |
417 | error: | |
418 | return -1; | |
947308c4 DG |
419 | } |
420 | ||
65beb5ff | 421 | /* |
9ae110e2 | 422 | * Connect to the LTTng session daemon. |
65beb5ff | 423 | * |
823a1989 | 424 | * On success, return the socket's file descriptor. On error, return -1. |
65beb5ff | 425 | */ |
823a1989 | 426 | LTTNG_HIDDEN int connect_sessiond(void) |
65beb5ff DG |
427 | { |
428 | int ret; | |
429 | ||
430 | ret = set_session_daemon_path(); | |
431 | if (ret < 0) { | |
2f70b271 | 432 | goto error; |
65beb5ff DG |
433 | } |
434 | ||
9ae110e2 | 435 | /* Connect to the sesssion daemon. */ |
65beb5ff DG |
436 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
437 | if (ret < 0) { | |
2f70b271 | 438 | goto error; |
65beb5ff DG |
439 | } |
440 | ||
823a1989 | 441 | return ret; |
2f70b271 DG |
442 | |
443 | error: | |
444 | return -1; | |
65beb5ff DG |
445 | } |
446 | ||
823a1989 JG |
447 | static void reset_global_sessiond_connection_state(void) |
448 | { | |
449 | sessiond_socket = -1; | |
450 | connected = 0; | |
451 | } | |
452 | ||
65beb5ff | 453 | /* |
1c8d13c8 | 454 | * Clean disconnect from the session daemon. |
9ae110e2 | 455 | * |
1c8d13c8 | 456 | * On success, return 0. On error, return -1. |
65beb5ff DG |
457 | */ |
458 | static int disconnect_sessiond(void) | |
459 | { | |
460 | int ret = 0; | |
461 | ||
462 | if (connected) { | |
463 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
823a1989 | 464 | reset_global_sessiond_connection_state(); |
65beb5ff DG |
465 | } |
466 | ||
467 | return ret; | |
468 | } | |
469 | ||
795a978d PP |
470 | static int recv_sessiond_optional_data(size_t len, void **user_buf, |
471 | size_t *user_len) | |
472 | { | |
473 | int ret = 0; | |
474 | void *buf = NULL; | |
475 | ||
476 | if (len) { | |
477 | if (!user_len) { | |
478 | ret = -LTTNG_ERR_INVALID; | |
479 | goto end; | |
480 | } | |
481 | ||
482 | buf = zmalloc(len); | |
483 | if (!buf) { | |
484 | ret = -ENOMEM; | |
485 | goto end; | |
486 | } | |
487 | ||
488 | ret = recv_data_sessiond(buf, len); | |
489 | if (ret < 0) { | |
490 | goto end; | |
491 | } | |
492 | ||
493 | if (!user_buf) { | |
494 | ret = -LTTNG_ERR_INVALID; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | /* Move ownership of command header buffer to user. */ | |
499 | *user_buf = buf; | |
500 | buf = NULL; | |
501 | *user_len = len; | |
502 | } else { | |
503 | /* No command header. */ | |
504 | if (user_len) { | |
505 | *user_len = 0; | |
506 | } | |
507 | ||
508 | if (user_buf) { | |
509 | *user_buf = NULL; | |
510 | } | |
511 | } | |
512 | ||
513 | end: | |
514 | free(buf); | |
515 | return ret; | |
516 | } | |
517 | ||
35a6fdb7 | 518 | /* |
cd80958d | 519 | * Ask the session daemon a specific command and put the data into buf. |
a04d53fc FD |
520 | * Takes extra var. len. data and file descriptors as input to send to the |
521 | * session daemon. | |
65beb5ff | 522 | * |
af87c45a | 523 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 524 | */ |
cac3069d | 525 | LTTNG_HIDDEN |
a04d53fc FD |
526 | int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, |
527 | const int *fds, size_t nb_fd, const void *vardata, | |
528 | size_t vardata_len, void **user_payload_buf, | |
529 | void **user_cmd_header_buf, size_t *user_cmd_header_len) | |
65beb5ff DG |
530 | { |
531 | int ret; | |
795a978d | 532 | size_t payload_len; |
cd80958d | 533 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
534 | |
535 | ret = connect_sessiond(); | |
536 | if (ret < 0) { | |
2f70b271 | 537 | ret = -LTTNG_ERR_NO_SESSIOND; |
65beb5ff | 538 | goto end; |
823a1989 JG |
539 | } else { |
540 | sessiond_socket = ret; | |
541 | connected = 1; | |
65beb5ff DG |
542 | } |
543 | ||
65beb5ff | 544 | /* Send command to session daemon */ |
cd80958d | 545 | ret = send_session_msg(lsm); |
65beb5ff | 546 | if (ret < 0) { |
2f70b271 | 547 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
548 | goto end; |
549 | } | |
53a80697 | 550 | /* Send var len data */ |
795a978d | 551 | ret = send_session_varlen(vardata, vardata_len); |
53a80697 | 552 | if (ret < 0) { |
2f70b271 | 553 | /* Ret value is a valid lttng error code. */ |
53a80697 MD |
554 | goto end; |
555 | } | |
65beb5ff | 556 | |
a04d53fc FD |
557 | /* Send fds */ |
558 | ret = send_session_fds(fds, nb_fd); | |
559 | if (ret < 0) { | |
560 | /* Ret value is a valid lttng error code. */ | |
561 | goto end; | |
562 | } | |
563 | ||
65beb5ff DG |
564 | /* Get header from data transmission */ |
565 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
566 | if (ret < 0) { | |
2f70b271 | 567 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
568 | goto end; |
569 | } | |
570 | ||
571 | /* Check error code if OK */ | |
f73fabfd | 572 | if (llm.ret_code != LTTNG_OK) { |
65beb5ff DG |
573 | ret = -llm.ret_code; |
574 | goto end; | |
575 | } | |
576 | ||
795a978d PP |
577 | /* Get command header from data transmission */ |
578 | ret = recv_sessiond_optional_data(llm.cmd_header_size, | |
579 | user_cmd_header_buf, user_cmd_header_len); | |
65beb5ff | 580 | if (ret < 0) { |
65beb5ff DG |
581 | goto end; |
582 | } | |
583 | ||
795a978d PP |
584 | /* Get payload from data transmission */ |
585 | ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, | |
586 | &payload_len); | |
587 | if (ret < 0) { | |
83009e5e DG |
588 | goto end; |
589 | } | |
590 | ||
795a978d | 591 | ret = llm.data_size; |
65beb5ff DG |
592 | |
593 | end: | |
594 | disconnect_sessiond(); | |
595 | return ret; | |
596 | } | |
597 | ||
9f19cc17 | 598 | /* |
cd80958d | 599 | * Create lttng handle and return pointer. |
9ae110e2 | 600 | * |
1c8d13c8 | 601 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 602 | */ |
cd80958d DG |
603 | struct lttng_handle *lttng_create_handle(const char *session_name, |
604 | struct lttng_domain *domain) | |
9f19cc17 | 605 | { |
55fb8091 | 606 | int ret; |
2f70b271 DG |
607 | struct lttng_handle *handle = NULL; |
608 | ||
3f451dc0 | 609 | handle = zmalloc(sizeof(struct lttng_handle)); |
cd80958d | 610 | if (handle == NULL) { |
2f70b271 | 611 | PERROR("malloc handle"); |
cd80958d DG |
612 | goto end; |
613 | } | |
614 | ||
615 | /* Copy session name */ | |
55fb8091 JG |
616 | ret = lttng_strncpy(handle->session_name, session_name ? : "", |
617 | sizeof(handle->session_name)); | |
618 | if (ret) { | |
619 | goto error; | |
620 | } | |
cd80958d | 621 | |
95681498 JG |
622 | /* Copy lttng domain or leave initialized to 0. */ |
623 | if (domain) { | |
624 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); | |
625 | } | |
cd80958d DG |
626 | |
627 | end: | |
628 | return handle; | |
55fb8091 JG |
629 | error: |
630 | free(handle); | |
631 | return NULL; | |
cd80958d DG |
632 | } |
633 | ||
634 | /* | |
635 | * Destroy handle by free(3) the pointer. | |
636 | */ | |
637 | void lttng_destroy_handle(struct lttng_handle *handle) | |
638 | { | |
0e428499 | 639 | free(handle); |
eb354453 DG |
640 | } |
641 | ||
d9800920 DG |
642 | /* |
643 | * Register an outside consumer. | |
9ae110e2 | 644 | * |
1c8d13c8 | 645 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
646 | */ |
647 | int lttng_register_consumer(struct lttng_handle *handle, | |
648 | const char *socket_path) | |
649 | { | |
55fb8091 | 650 | int ret; |
d9800920 DG |
651 | struct lttcomm_session_msg lsm; |
652 | ||
2f70b271 | 653 | if (handle == NULL || socket_path == NULL) { |
55fb8091 JG |
654 | ret = -LTTNG_ERR_INVALID; |
655 | goto end; | |
2f70b271 DG |
656 | } |
657 | ||
53efb85a | 658 | memset(&lsm, 0, sizeof(lsm)); |
d9800920 | 659 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
55fb8091 | 660 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
d9800920 | 661 | sizeof(lsm.session.name)); |
55fb8091 JG |
662 | if (ret) { |
663 | ret = -LTTNG_ERR_INVALID; | |
664 | goto end; | |
665 | } | |
666 | ||
67040e31 | 667 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
d9800920 | 668 | |
55fb8091 JG |
669 | ret = lttng_strncpy(lsm.u.reg.path, socket_path, |
670 | sizeof(lsm.u.reg.path)); | |
671 | if (ret) { | |
672 | ret = -LTTNG_ERR_INVALID; | |
673 | goto end; | |
674 | } | |
d9800920 | 675 | |
55fb8091 JG |
676 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
677 | end: | |
678 | return ret; | |
d9800920 DG |
679 | } |
680 | ||
1df4dedd | 681 | /* |
9ae110e2 JG |
682 | * Start tracing for all traces of the session. |
683 | * | |
684 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 685 | */ |
6a4f824d | 686 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 687 | { |
55fb8091 | 688 | int ret; |
cd80958d DG |
689 | struct lttcomm_session_msg lsm; |
690 | ||
6a4f824d | 691 | if (session_name == NULL) { |
55fb8091 JG |
692 | ret = -LTTNG_ERR_INVALID; |
693 | goto end; | |
cd80958d DG |
694 | } |
695 | ||
53efb85a | 696 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 697 | lsm.cmd_type = LTTNG_START_TRACE; |
6a4f824d | 698 | |
55fb8091 JG |
699 | ret = lttng_strncpy(lsm.session.name, session_name, |
700 | sizeof(lsm.session.name)); | |
701 | if (ret) { | |
702 | ret = -LTTNG_ERR_INVALID; | |
703 | goto end; | |
704 | } | |
cd80958d | 705 | |
55fb8091 JG |
706 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
707 | end: | |
708 | return ret; | |
f3ed775e | 709 | } |
1df4dedd DG |
710 | |
711 | /* | |
38ee087f | 712 | * Stop tracing for all traces of the session. |
f3ed775e | 713 | */ |
38ee087f | 714 | static int _lttng_stop_tracing(const char *session_name, int wait) |
f3ed775e | 715 | { |
38ee087f | 716 | int ret, data_ret; |
cd80958d DG |
717 | struct lttcomm_session_msg lsm; |
718 | ||
6a4f824d | 719 | if (session_name == NULL) { |
55fb8091 JG |
720 | ret = -LTTNG_ERR_INVALID; |
721 | goto error; | |
6a4f824d DG |
722 | } |
723 | ||
53efb85a | 724 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 725 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d | 726 | |
55fb8091 JG |
727 | ret = lttng_strncpy(lsm.session.name, session_name, |
728 | sizeof(lsm.session.name)); | |
729 | if (ret) { | |
730 | ret = -LTTNG_ERR_INVALID; | |
731 | goto error; | |
732 | } | |
cd80958d | 733 | |
cac3069d | 734 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
38ee087f DG |
735 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
736 | goto error; | |
737 | } | |
738 | ||
739 | if (!wait) { | |
740 | goto end; | |
741 | } | |
742 | ||
38ee087f DG |
743 | /* Check for data availability */ |
744 | do { | |
6d805429 | 745 | data_ret = lttng_data_pending(session_name); |
38ee087f DG |
746 | if (data_ret < 0) { |
747 | /* Return the data available call error. */ | |
748 | ret = data_ret; | |
749 | goto error; | |
750 | } | |
751 | ||
752 | /* | |
9ae110e2 JG |
753 | * Data sleep time before retrying (in usec). Don't sleep if the |
754 | * call returned value indicates availability. | |
38ee087f | 755 | */ |
6d805429 | 756 | if (data_ret) { |
ea0f4f56 | 757 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
38ee087f | 758 | } |
6d805429 | 759 | } while (data_ret != 0); |
38ee087f | 760 | |
38ee087f DG |
761 | end: |
762 | error: | |
763 | return ret; | |
764 | } | |
765 | ||
766 | /* | |
767 | * Stop tracing and wait for data availability. | |
768 | */ | |
769 | int lttng_stop_tracing(const char *session_name) | |
770 | { | |
771 | return _lttng_stop_tracing(session_name, 1); | |
772 | } | |
773 | ||
774 | /* | |
775 | * Stop tracing but _don't_ wait for data availability. | |
776 | */ | |
777 | int lttng_stop_tracing_no_wait(const char *session_name) | |
778 | { | |
779 | return _lttng_stop_tracing(session_name, 0); | |
f3ed775e DG |
780 | } |
781 | ||
782 | /* | |
601d5acf DG |
783 | * Add context to a channel. |
784 | * | |
785 | * If the given channel is NULL, add the contexts to all channels. | |
786 | * The event_name param is ignored. | |
af87c45a DG |
787 | * |
788 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 789 | */ |
cd80958d | 790 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
791 | struct lttng_event_context *ctx, const char *event_name, |
792 | const char *channel_name) | |
d65106b1 | 793 | { |
2001793c JG |
794 | int ret; |
795 | size_t len = 0; | |
796 | char *buf = NULL; | |
cd80958d DG |
797 | struct lttcomm_session_msg lsm; |
798 | ||
9ae110e2 | 799 | /* Safety check. Both are mandatory. */ |
9d697d3d | 800 | if (handle == NULL || ctx == NULL) { |
2001793c JG |
801 | ret = -LTTNG_ERR_INVALID; |
802 | goto end; | |
cd80958d DG |
803 | } |
804 | ||
441c16a7 | 805 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
806 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
807 | ||
85076754 | 808 | /* If no channel name, send empty string. */ |
55fb8091 JG |
809 | ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "", |
810 | sizeof(lsm.u.context.channel_name)); | |
811 | if (ret) { | |
812 | ret = -LTTNG_ERR_INVALID; | |
813 | goto end; | |
85076754 | 814 | } |
cd80958d | 815 | |
67040e31 | 816 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
55fb8091 | 817 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 818 | sizeof(lsm.session.name)); |
55fb8091 JG |
819 | if (ret) { |
820 | ret = -LTTNG_ERR_INVALID; | |
821 | goto end; | |
822 | } | |
cd80958d | 823 | |
2001793c JG |
824 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
825 | size_t provider_len, ctx_len; | |
826 | const char *provider_name = ctx->u.app_ctx.provider_name; | |
827 | const char *ctx_name = ctx->u.app_ctx.ctx_name; | |
828 | ||
829 | if (!provider_name || !ctx_name) { | |
830 | ret = -LTTNG_ERR_INVALID; | |
831 | goto end; | |
832 | } | |
833 | ||
834 | provider_len = strlen(provider_name); | |
835 | if (provider_len == 0) { | |
836 | ret = -LTTNG_ERR_INVALID; | |
837 | goto end; | |
838 | } | |
839 | lsm.u.context.provider_name_len = provider_len; | |
840 | ||
841 | ctx_len = strlen(ctx_name); | |
842 | if (ctx_len == 0) { | |
843 | ret = -LTTNG_ERR_INVALID; | |
844 | goto end; | |
845 | } | |
846 | lsm.u.context.context_name_len = ctx_len; | |
847 | ||
848 | len = provider_len + ctx_len; | |
849 | buf = zmalloc(len); | |
850 | if (!buf) { | |
851 | ret = -LTTNG_ERR_NOMEM; | |
852 | goto end; | |
853 | } | |
854 | ||
855 | memcpy(buf, provider_name, provider_len); | |
856 | memcpy(buf + provider_len, ctx_name, ctx_len); | |
857 | } | |
858 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); | |
46f44e2a JR |
859 | |
860 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
861 | /* | |
862 | * Don't leak application addresses to the sessiond. | |
863 | * This is only necessary when ctx is for an app ctx otherwise | |
864 | * the values inside the union (type & config) are overwritten. | |
865 | */ | |
866 | lsm.u.context.ctx.u.app_ctx.provider_name = NULL; | |
867 | lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; | |
868 | } | |
2001793c | 869 | |
795a978d | 870 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); |
2001793c JG |
871 | end: |
872 | free(buf); | |
873 | return ret; | |
d65106b1 DG |
874 | } |
875 | ||
f3ed775e | 876 | /* |
9ae110e2 JG |
877 | * Enable event(s) for a channel. |
878 | * | |
879 | * If no event name is specified, all events are enabled. | |
880 | * If no channel name is specified, the default 'channel0' is used. | |
881 | * | |
882 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 883 | */ |
cd80958d | 884 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 885 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 886 | { |
f8a96544 JI |
887 | return lttng_enable_event_with_exclusions(handle, ev, channel_name, |
888 | NULL, 0, NULL); | |
1df4dedd DG |
889 | } |
890 | ||
53a80697 | 891 | /* |
025faf73 | 892 | * Create or enable an event with a filter expression. |
178191b3 | 893 | * |
53a80697 MD |
894 | * Return negative error value on error. |
895 | * Return size of returned session payload data if OK. | |
896 | */ | |
025faf73 | 897 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
178191b3 | 898 | struct lttng_event *event, const char *channel_name, |
53a80697 MD |
899 | const char *filter_expression) |
900 | { | |
f8a96544 JI |
901 | return lttng_enable_event_with_exclusions(handle, event, channel_name, |
902 | filter_expression, 0, NULL); | |
53a80697 MD |
903 | } |
904 | ||
347c5ab5 | 905 | /* |
0e115563 | 906 | * Depending on the event, return a newly allocated agent filter expression or |
347c5ab5 DG |
907 | * NULL if not applicable. |
908 | * | |
909 | * An event with NO loglevel and the name is * will return NULL. | |
910 | */ | |
0e115563 | 911 | static char *set_agent_filter(const char *filter, struct lttng_event *ev) |
347c5ab5 DG |
912 | { |
913 | int err; | |
0e115563 | 914 | char *agent_filter = NULL; |
347c5ab5 DG |
915 | |
916 | assert(ev); | |
917 | ||
918 | /* Don't add filter for the '*' event. */ | |
9f449915 | 919 | if (strcmp(ev->name, "*") != 0) { |
347c5ab5 | 920 | if (filter) { |
0e115563 | 921 | err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, |
347c5ab5 DG |
922 | ev->name); |
923 | } else { | |
0e115563 | 924 | err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); |
347c5ab5 DG |
925 | } |
926 | if (err < 0) { | |
927 | PERROR("asprintf"); | |
6a556f7b | 928 | goto error; |
347c5ab5 DG |
929 | } |
930 | } | |
931 | ||
932 | /* Add loglevel filtering if any for the JUL domain. */ | |
933 | if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { | |
934 | char *op; | |
935 | ||
936 | if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) { | |
937 | op = ">="; | |
938 | } else { | |
939 | op = "=="; | |
940 | } | |
941 | ||
0e115563 | 942 | if (filter || agent_filter) { |
6a556f7b JG |
943 | char *new_filter; |
944 | ||
fb0edb23 | 945 | err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", |
0e115563 | 946 | agent_filter ? agent_filter : filter, op, |
347c5ab5 | 947 | ev->loglevel); |
0e115563 DG |
948 | if (agent_filter) { |
949 | free(agent_filter); | |
6a556f7b | 950 | } |
0e115563 | 951 | agent_filter = new_filter; |
347c5ab5 | 952 | } else { |
0e115563 | 953 | err = asprintf(&agent_filter, "int_loglevel %s %d", op, |
347c5ab5 DG |
954 | ev->loglevel); |
955 | } | |
956 | if (err < 0) { | |
957 | PERROR("asprintf"); | |
6a556f7b | 958 | goto error; |
347c5ab5 DG |
959 | } |
960 | } | |
961 | ||
0e115563 | 962 | return agent_filter; |
6a556f7b | 963 | error: |
0e115563 | 964 | free(agent_filter); |
6a556f7b | 965 | return NULL; |
347c5ab5 DG |
966 | } |
967 | ||
137b9942 | 968 | /* |
ec166985 | 969 | * Generate the filter bytecode from a given filter expression string. Put the |
137b9942 DG |
970 | * newly allocated parser context in ctxp and populate the lsm object with the |
971 | * expression len. | |
972 | * | |
973 | * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched. | |
974 | */ | |
975 | static int generate_filter(char *filter_expression, | |
976 | struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp) | |
977 | { | |
978 | int ret; | |
979 | struct filter_parser_ctx *ctx = NULL; | |
980 | FILE *fmem = NULL; | |
981 | ||
982 | assert(filter_expression); | |
983 | assert(lsm); | |
984 | assert(ctxp); | |
985 | ||
986 | /* | |
987 | * Casting const to non-const, as the underlying function will use it in | |
988 | * read-only mode. | |
989 | */ | |
990 | fmem = lttng_fmemopen((void *) filter_expression, | |
991 | strlen(filter_expression), "r"); | |
992 | if (!fmem) { | |
993 | fprintf(stderr, "Error opening memory as stream\n"); | |
994 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
995 | goto error; | |
996 | } | |
997 | ctx = filter_parser_ctx_alloc(fmem); | |
998 | if (!ctx) { | |
999 | fprintf(stderr, "Error allocating parser\n"); | |
1000 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
1001 | goto filter_alloc_error; | |
1002 | } | |
1003 | ret = filter_parser_ctx_append_ast(ctx); | |
1004 | if (ret) { | |
1005 | fprintf(stderr, "Parse error\n"); | |
1006 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1007 | goto parse_error; | |
1008 | } | |
137b9942 DG |
1009 | if (print_xml) { |
1010 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
1011 | if (ret) { | |
1012 | fflush(stdout); | |
1013 | fprintf(stderr, "XML print error\n"); | |
1014 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1015 | goto parse_error; | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | dbg_printf("Generating IR... "); | |
1020 | fflush(stdout); | |
1021 | ret = filter_visitor_ir_generate(ctx); | |
1022 | if (ret) { | |
1023 | fprintf(stderr, "Generate IR error\n"); | |
1024 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1025 | goto parse_error; | |
1026 | } | |
1027 | dbg_printf("done\n"); | |
1028 | ||
1029 | dbg_printf("Validating IR... "); | |
1030 | fflush(stdout); | |
1031 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
1032 | if (ret) { | |
1033 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1034 | goto parse_error; | |
1035 | } | |
9f449915 PP |
1036 | |
1037 | /* Normalize globbing patterns in the expression. */ | |
1038 | ret = filter_visitor_ir_normalize_glob_patterns(ctx); | |
1039 | if (ret) { | |
1040 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1041 | goto parse_error; | |
1042 | } | |
1043 | ||
9ae110e2 | 1044 | /* Validate strings used as literals in the expression. */ |
dcd5daf2 JG |
1045 | ret = filter_visitor_ir_validate_string(ctx); |
1046 | if (ret) { | |
1047 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1048 | goto parse_error; | |
1049 | } | |
9f449915 PP |
1050 | |
1051 | /* Validate globbing patterns in the expression. */ | |
1052 | ret = filter_visitor_ir_validate_globbing(ctx); | |
1053 | if (ret) { | |
1054 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1055 | goto parse_error; | |
1056 | } | |
1057 | ||
137b9942 DG |
1058 | dbg_printf("done\n"); |
1059 | ||
1060 | dbg_printf("Generating bytecode... "); | |
1061 | fflush(stdout); | |
1062 | ret = filter_visitor_bytecode_generate(ctx); | |
1063 | if (ret) { | |
1064 | fprintf(stderr, "Generate bytecode error\n"); | |
1065 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1066 | goto parse_error; | |
1067 | } | |
1068 | dbg_printf("done\n"); | |
1069 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
1070 | bytecode_get_len(&ctx->bytecode->b)); | |
1071 | ||
1072 | lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b) | |
1073 | + bytecode_get_len(&ctx->bytecode->b); | |
1074 | lsm->u.enable.expression_len = strlen(filter_expression) + 1; | |
1075 | ||
1076 | /* No need to keep the memory stream. */ | |
1077 | if (fclose(fmem) != 0) { | |
6f04ed72 | 1078 | PERROR("fclose"); |
137b9942 DG |
1079 | } |
1080 | ||
1081 | *ctxp = ctx; | |
1082 | return 0; | |
1083 | ||
1084 | parse_error: | |
137b9942 DG |
1085 | filter_ir_free(ctx); |
1086 | filter_parser_ctx_free(ctx); | |
1087 | filter_alloc_error: | |
1088 | if (fclose(fmem) != 0) { | |
6f04ed72 | 1089 | PERROR("fclose"); |
137b9942 DG |
1090 | } |
1091 | error: | |
1092 | return ret; | |
1093 | } | |
1094 | ||
93deb080 JI |
1095 | /* |
1096 | * Enable event(s) for a channel, possibly with exclusions and a filter. | |
1097 | * If no event name is specified, all events are enabled. | |
1098 | * If no channel name is specified, the default name is used. | |
1099 | * If filter expression is not NULL, the filter is set for the event. | |
1100 | * If exclusion count is not zero, the exclusions are set for the event. | |
1101 | * Returns size of returned session payload data or a negative error code. | |
1102 | */ | |
1103 | int lttng_enable_event_with_exclusions(struct lttng_handle *handle, | |
1104 | struct lttng_event *ev, const char *channel_name, | |
e9efbcd3 | 1105 | const char *original_filter_expression, |
93deb080 JI |
1106 | int exclusion_count, char **exclusion_list) |
1107 | { | |
1108 | struct lttcomm_session_msg lsm; | |
15e37663 JG |
1109 | struct lttng_dynamic_buffer send_buffer; |
1110 | int ret = 0, i, fd_to_send = -1; | |
1111 | bool send_fd = false; | |
137b9942 | 1112 | unsigned int free_filter_expression = 0; |
93deb080 | 1113 | struct filter_parser_ctx *ctx = NULL; |
ec005ec6 | 1114 | |
6ea91205 YL |
1115 | /* |
1116 | * We have either a filter or some exclusions, so we need to set up | |
1117 | * a variable-length memory block from where to send the data. | |
1118 | */ | |
1119 | lttng_dynamic_buffer_init(&send_buffer); | |
ec005ec6 | 1120 | |
e9efbcd3 JG |
1121 | /* |
1122 | * Cast as non-const since we may replace the filter expression | |
1123 | * by a dynamically allocated string. Otherwise, the original | |
1124 | * string is not modified. | |
1125 | */ | |
1126 | char *filter_expression = (char *) original_filter_expression; | |
93deb080 JI |
1127 | |
1128 | if (handle == NULL || ev == NULL) { | |
137b9942 DG |
1129 | ret = -LTTNG_ERR_INVALID; |
1130 | goto error; | |
93deb080 JI |
1131 | } |
1132 | ||
9ae110e2 JG |
1133 | /* |
1134 | * Empty filter string will always be rejected by the parser | |
93deb080 JI |
1135 | * anyway, so treat this corner-case early to eliminate |
1136 | * lttng_fmemopen error for 0-byte allocation. | |
1137 | */ | |
1138 | if (filter_expression && filter_expression[0] == '\0') { | |
137b9942 DG |
1139 | ret = -LTTNG_ERR_INVALID; |
1140 | goto error; | |
93deb080 JI |
1141 | } |
1142 | ||
1143 | memset(&lsm, 0, sizeof(lsm)); | |
1144 | ||
1145 | /* If no channel name, send empty string. */ | |
55fb8091 JG |
1146 | ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "", |
1147 | sizeof(lsm.u.enable.channel_name)); | |
1148 | if (ret) { | |
1149 | ret = -LTTNG_ERR_INVALID; | |
1150 | goto error; | |
93deb080 JI |
1151 | } |
1152 | ||
18a720cd MD |
1153 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
1154 | if (ev->name[0] == '\0') { | |
55fb8091 JG |
1155 | /* Enable all events. */ |
1156 | ret = lttng_strncpy(ev->name, "*", sizeof(ev->name)); | |
1157 | assert(ret == 0); | |
6565421f | 1158 | } |
93deb080 | 1159 | |
67040e31 | 1160 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
93deb080 JI |
1161 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
1162 | ||
55fb8091 | 1163 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
93deb080 | 1164 | sizeof(lsm.session.name)); |
55fb8091 JG |
1165 | if (ret) { |
1166 | ret = -LTTNG_ERR_INVALID; | |
1167 | goto error; | |
1168 | } | |
1169 | ||
93deb080 JI |
1170 | lsm.u.enable.exclusion_count = exclusion_count; |
1171 | lsm.u.enable.bytecode_len = 0; | |
1172 | ||
9ae110e2 | 1173 | /* Parse filter expression. */ |
5cdb6027 | 1174 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
0e115563 DG |
1175 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1176 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
5cdb6027 | 1177 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1178 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1179 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1180 | char *agent_filter; | |
64226865 | 1181 | |
347c5ab5 | 1182 | /* Setup JUL filter if needed. */ |
0e115563 DG |
1183 | agent_filter = set_agent_filter(filter_expression, ev); |
1184 | if (!agent_filter) { | |
137b9942 | 1185 | if (!filter_expression) { |
9ae110e2 JG |
1186 | /* |
1187 | * No JUL and no filter, just skip | |
1188 | * everything below. | |
1189 | */ | |
137b9942 DG |
1190 | goto ask_sessiond; |
1191 | } | |
64226865 DG |
1192 | } else { |
1193 | /* | |
9ae110e2 JG |
1194 | * With an agent filter, the original filter has |
1195 | * been added to it thus replace the filter | |
1196 | * expression. | |
64226865 | 1197 | */ |
0e115563 | 1198 | filter_expression = agent_filter; |
e9efbcd3 | 1199 | free_filter_expression = 1; |
9b21e6d5 | 1200 | } |
9b21e6d5 | 1201 | } |
93deb080 | 1202 | |
137b9942 | 1203 | ret = generate_filter(filter_expression, &lsm, &ctx); |
93deb080 | 1204 | if (ret) { |
137b9942 | 1205 | goto filter_error; |
93deb080 | 1206 | } |
6b453b5e JG |
1207 | } |
1208 | ||
15e37663 JG |
1209 | ret = lttng_dynamic_buffer_set_capacity(&send_buffer, |
1210 | lsm.u.enable.bytecode_len | |
137b9942 DG |
1211 | + lsm.u.enable.expression_len |
1212 | + LTTNG_SYMBOL_NAME_LEN * exclusion_count); | |
15e37663 | 1213 | if (ret) { |
6b453b5e | 1214 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; |
7ca1dc6f | 1215 | goto mem_error; |
6b453b5e | 1216 | } |
137b9942 | 1217 | |
9ae110e2 | 1218 | /* Put exclusion names first in the data. */ |
15e37663 JG |
1219 | for (i = 0; i < exclusion_count; i++) { |
1220 | size_t exclusion_len; | |
1221 | ||
1222 | exclusion_len = lttng_strnlen(*(exclusion_list + i), | |
1223 | LTTNG_SYMBOL_NAME_LEN); | |
1224 | if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) { | |
1225 | /* Exclusion is not NULL-terminated. */ | |
1226 | ret = -LTTNG_ERR_INVALID; | |
1227 | goto mem_error; | |
1228 | } | |
1229 | ||
1230 | ret = lttng_dynamic_buffer_append(&send_buffer, | |
1231 | *(exclusion_list + i), | |
1232 | LTTNG_SYMBOL_NAME_LEN); | |
1233 | if (ret) { | |
1234 | goto mem_error; | |
1235 | } | |
6b453b5e | 1236 | } |
15e37663 | 1237 | |
9ae110e2 | 1238 | /* Add filter expression next. */ |
15e37663 JG |
1239 | if (filter_expression) { |
1240 | ret = lttng_dynamic_buffer_append(&send_buffer, | |
1241 | filter_expression, lsm.u.enable.expression_len); | |
1242 | if (ret) { | |
1243 | goto mem_error; | |
1244 | } | |
6b453b5e | 1245 | } |
9ae110e2 | 1246 | /* Add filter bytecode next. */ |
137b9942 | 1247 | if (ctx && lsm.u.enable.bytecode_len != 0) { |
15e37663 JG |
1248 | ret = lttng_dynamic_buffer_append(&send_buffer, |
1249 | &ctx->bytecode->b, lsm.u.enable.bytecode_len); | |
1250 | if (ret) { | |
1251 | goto mem_error; | |
1252 | } | |
1253 | } | |
1254 | if (ev->extended.ptr) { | |
1255 | struct lttng_event_extended *ev_ext = | |
1256 | (struct lttng_event_extended *) ev->extended.ptr; | |
1257 | ||
1258 | if (ev_ext->probe_location) { | |
1259 | /* | |
1260 | * lttng_userspace_probe_location_serialize returns the | |
1261 | * number of bytes that was appended to the buffer. | |
1262 | */ | |
1263 | ret = lttng_userspace_probe_location_serialize( | |
1264 | ev_ext->probe_location, &send_buffer, | |
1265 | &fd_to_send); | |
56f0bc67 | 1266 | if (ret < 0) { |
15e37663 JG |
1267 | goto mem_error; |
1268 | } | |
1269 | ||
1270 | send_fd = fd_to_send >= 0; | |
1271 | /* | |
1272 | * Set the size of the userspace probe location element | |
1273 | * of the buffer so that the receiving side knows where | |
1274 | * to split it. | |
1275 | */ | |
1276 | lsm.u.enable.userspace_probe_location_len = ret; | |
1277 | } | |
93deb080 JI |
1278 | } |
1279 | ||
15e37663 JG |
1280 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, |
1281 | send_fd ? &fd_to_send : NULL, | |
1282 | send_fd ? 1 : 0, | |
1283 | send_buffer.size ? send_buffer.data : NULL, | |
1284 | send_buffer.size, NULL, NULL, 0); | |
93deb080 | 1285 | |
7ca1dc6f DG |
1286 | mem_error: |
1287 | if (filter_expression && ctx) { | |
93deb080 JI |
1288 | filter_bytecode_free(ctx); |
1289 | filter_ir_free(ctx); | |
1290 | filter_parser_ctx_free(ctx); | |
7ca1dc6f DG |
1291 | } |
1292 | filter_error: | |
1293 | if (free_filter_expression) { | |
1294 | /* | |
9ae110e2 JG |
1295 | * The filter expression has been replaced and must be freed as |
1296 | * it is not the original filter expression received as a | |
1297 | * parameter. | |
7ca1dc6f DG |
1298 | */ |
1299 | free(filter_expression); | |
93deb080 | 1300 | } |
137b9942 DG |
1301 | error: |
1302 | /* | |
9ae110e2 JG |
1303 | * Return directly to the caller and don't ask the sessiond since |
1304 | * something went wrong in the parsing of data above. | |
137b9942 | 1305 | */ |
15e37663 | 1306 | lttng_dynamic_buffer_reset(&send_buffer); |
93deb080 | 1307 | return ret; |
3e1c9ff7 DG |
1308 | |
1309 | ask_sessiond: | |
1310 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1311 | return ret; | |
93deb080 JI |
1312 | } |
1313 | ||
6e911cad MD |
1314 | int lttng_disable_event_ext(struct lttng_handle *handle, |
1315 | struct lttng_event *ev, const char *channel_name, | |
1316 | const char *original_filter_expression) | |
1df4dedd | 1317 | { |
cd80958d | 1318 | struct lttcomm_session_msg lsm; |
6e911cad MD |
1319 | char *varlen_data; |
1320 | int ret = 0; | |
1321 | unsigned int free_filter_expression = 0; | |
1322 | struct filter_parser_ctx *ctx = NULL; | |
1323 | /* | |
1324 | * Cast as non-const since we may replace the filter expression | |
1325 | * by a dynamically allocated string. Otherwise, the original | |
1326 | * string is not modified. | |
1327 | */ | |
1328 | char *filter_expression = (char *) original_filter_expression; | |
1df4dedd | 1329 | |
6e911cad MD |
1330 | if (handle == NULL || ev == NULL) { |
1331 | ret = -LTTNG_ERR_INVALID; | |
1332 | goto error; | |
1333 | } | |
1334 | ||
9ae110e2 JG |
1335 | /* |
1336 | * Empty filter string will always be rejected by the parser | |
6e911cad MD |
1337 | * anyway, so treat this corner-case early to eliminate |
1338 | * lttng_fmemopen error for 0-byte allocation. | |
1339 | */ | |
1340 | if (filter_expression && filter_expression[0] == '\0') { | |
1341 | ret = -LTTNG_ERR_INVALID; | |
1342 | goto error; | |
cd80958d DG |
1343 | } |
1344 | ||
441c16a7 MD |
1345 | memset(&lsm, 0, sizeof(lsm)); |
1346 | ||
85076754 | 1347 | /* If no channel name, send empty string. */ |
55fb8091 JG |
1348 | ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "", |
1349 | sizeof(lsm.u.disable.channel_name)); | |
1350 | if (ret) { | |
1351 | ret = -LTTNG_ERR_INVALID; | |
1352 | goto error; | |
eb354453 DG |
1353 | } |
1354 | ||
18a720cd | 1355 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
f3ed775e | 1356 | |
67040e31 | 1357 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
6e911cad MD |
1358 | memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event)); |
1359 | ||
55fb8091 | 1360 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 1361 | sizeof(lsm.session.name)); |
55fb8091 JG |
1362 | if (ret) { |
1363 | ret = -LTTNG_ERR_INVALID; | |
1364 | goto error; | |
1365 | } | |
1366 | ||
6e911cad | 1367 | lsm.u.disable.bytecode_len = 0; |
cd80958d | 1368 | |
6e911cad MD |
1369 | /* |
1370 | * For the JUL domain, a filter is enforced except for the | |
1371 | * disable all event. This is done to avoid having the event in | |
1372 | * all sessions thus filtering by logger name. | |
1373 | */ | |
1374 | if (filter_expression == NULL && | |
1375 | (handle->domain.type != LTTNG_DOMAIN_JUL && | |
0e115563 DG |
1376 | handle->domain.type != LTTNG_DOMAIN_LOG4J && |
1377 | handle->domain.type != LTTNG_DOMAIN_PYTHON)) { | |
6e911cad MD |
1378 | goto ask_sessiond; |
1379 | } | |
1380 | ||
1381 | /* | |
1382 | * We have a filter, so we need to set up a variable-length | |
1383 | * memory block from where to send the data. | |
1384 | */ | |
1385 | ||
1386 | /* Parse filter expression */ | |
1387 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL | |
0e115563 DG |
1388 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1389 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
6e911cad | 1390 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1391 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1392 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1393 | char *agent_filter; | |
6e911cad MD |
1394 | |
1395 | /* Setup JUL filter if needed. */ | |
0e115563 DG |
1396 | agent_filter = set_agent_filter(filter_expression, ev); |
1397 | if (!agent_filter) { | |
6e911cad | 1398 | if (!filter_expression) { |
9ae110e2 JG |
1399 | /* |
1400 | * No JUL and no filter, just skip | |
1401 | * everything below. | |
1402 | */ | |
6e911cad MD |
1403 | goto ask_sessiond; |
1404 | } | |
1405 | } else { | |
1406 | /* | |
9ae110e2 JG |
1407 | * With a JUL filter, the original filter has |
1408 | * been added to it thus replace the filter | |
1409 | * expression. | |
6e911cad | 1410 | */ |
0e115563 | 1411 | filter_expression = agent_filter; |
6e911cad MD |
1412 | free_filter_expression = 1; |
1413 | } | |
1414 | } | |
1415 | ||
1416 | ret = generate_filter(filter_expression, &lsm, &ctx); | |
1417 | if (ret) { | |
1418 | goto filter_error; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | varlen_data = zmalloc(lsm.u.disable.bytecode_len | |
1423 | + lsm.u.disable.expression_len); | |
1424 | if (!varlen_data) { | |
1425 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; | |
1426 | goto mem_error; | |
1427 | } | |
1428 | ||
9ae110e2 | 1429 | /* Add filter expression. */ |
6e911cad MD |
1430 | if (lsm.u.disable.expression_len != 0) { |
1431 | memcpy(varlen_data, | |
1432 | filter_expression, | |
1433 | lsm.u.disable.expression_len); | |
1434 | } | |
9ae110e2 | 1435 | /* Add filter bytecode next. */ |
6e911cad MD |
1436 | if (ctx && lsm.u.disable.bytecode_len != 0) { |
1437 | memcpy(varlen_data | |
1438 | + lsm.u.disable.expression_len, | |
1439 | &ctx->bytecode->b, | |
1440 | lsm.u.disable.bytecode_len); | |
1441 | } | |
1442 | ||
795a978d | 1443 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, |
6e911cad MD |
1444 | lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL); |
1445 | free(varlen_data); | |
1446 | ||
1447 | mem_error: | |
1448 | if (filter_expression && ctx) { | |
1449 | filter_bytecode_free(ctx); | |
1450 | filter_ir_free(ctx); | |
1451 | filter_parser_ctx_free(ctx); | |
1452 | } | |
1453 | filter_error: | |
1454 | if (free_filter_expression) { | |
1455 | /* | |
9ae110e2 JG |
1456 | * The filter expression has been replaced and must be freed as |
1457 | * it is not the original filter expression received as a | |
1458 | * parameter. | |
6e911cad MD |
1459 | */ |
1460 | free(filter_expression); | |
1461 | } | |
1462 | error: | |
1463 | /* | |
9ae110e2 JG |
1464 | * Return directly to the caller and don't ask the sessiond since |
1465 | * something went wrong in the parsing of data above. | |
6e911cad MD |
1466 | */ |
1467 | return ret; | |
1468 | ||
1469 | ask_sessiond: | |
1470 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1471 | return ret; | |
1472 | } | |
1473 | ||
1474 | /* | |
9ae110e2 JG |
1475 | * Disable event(s) of a channel and domain. |
1476 | * If no event name is specified, all events are disabled. | |
1477 | * If no channel name is specified, the default 'channel0' is used. | |
1478 | * Returns size of returned session payload data or a negative error code. | |
6e911cad MD |
1479 | */ |
1480 | int lttng_disable_event(struct lttng_handle *handle, const char *name, | |
1481 | const char *channel_name) | |
1482 | { | |
55fb8091 | 1483 | int ret; |
6e911cad MD |
1484 | struct lttng_event ev; |
1485 | ||
1486 | memset(&ev, 0, sizeof(ev)); | |
9b7431cf | 1487 | ev.loglevel = -1; |
6e911cad | 1488 | ev.type = LTTNG_EVENT_ALL; |
55fb8091 JG |
1489 | ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name)); |
1490 | if (ret) { | |
1491 | ret = -LTTNG_ERR_INVALID; | |
1492 | goto end; | |
1493 | } | |
1494 | ||
1495 | ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL); | |
1496 | end: | |
1497 | return ret; | |
1df4dedd DG |
1498 | } |
1499 | ||
cf0bcb51 JG |
1500 | struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) |
1501 | { | |
1502 | struct lttng_channel *channel = NULL; | |
1503 | struct lttng_channel_extended *extended = NULL; | |
1504 | ||
1505 | if (!domain) { | |
1506 | goto error; | |
1507 | } | |
1508 | ||
1509 | /* Validate domain. */ | |
1510 | switch (domain->type) { | |
1511 | case LTTNG_DOMAIN_UST: | |
1512 | switch (domain->buf_type) { | |
1513 | case LTTNG_BUFFER_PER_UID: | |
1514 | case LTTNG_BUFFER_PER_PID: | |
1515 | break; | |
1516 | default: | |
1517 | goto error; | |
1518 | } | |
1519 | break; | |
1520 | case LTTNG_DOMAIN_KERNEL: | |
1521 | if (domain->buf_type != LTTNG_BUFFER_GLOBAL) { | |
1522 | goto error; | |
1523 | } | |
1524 | break; | |
1525 | default: | |
1526 | goto error; | |
1527 | } | |
1528 | ||
1529 | channel = zmalloc(sizeof(*channel)); | |
1530 | if (!channel) { | |
1531 | goto error; | |
1532 | } | |
1533 | ||
1534 | extended = zmalloc(sizeof(*extended)); | |
1535 | if (!extended) { | |
1536 | goto error; | |
1537 | } | |
1538 | ||
1539 | channel->attr.extended.ptr = extended; | |
1540 | ||
1541 | lttng_channel_set_default_attr(domain, &channel->attr); | |
1542 | return channel; | |
1543 | error: | |
1544 | free(channel); | |
1545 | free(extended); | |
1546 | return NULL; | |
1547 | } | |
1548 | ||
1549 | void lttng_channel_destroy(struct lttng_channel *channel) | |
1550 | { | |
1551 | if (!channel) { | |
1552 | return; | |
1553 | } | |
1554 | ||
1555 | if (channel->attr.extended.ptr) { | |
1556 | free(channel->attr.extended.ptr); | |
1557 | } | |
1558 | free(channel); | |
1559 | } | |
1560 | ||
1df4dedd | 1561 | /* |
9ae110e2 JG |
1562 | * Enable channel per domain |
1563 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 1564 | */ |
cd80958d | 1565 | int lttng_enable_channel(struct lttng_handle *handle, |
cf0bcb51 | 1566 | struct lttng_channel *in_chan) |
a5c5a2bd | 1567 | { |
55fb8091 | 1568 | int ret; |
cd80958d | 1569 | struct lttcomm_session_msg lsm; |
e3091d6a | 1570 | size_t total_buffer_size_needed_per_cpu = 0; |
cd80958d | 1571 | |
9ae110e2 | 1572 | /* NULL arguments are forbidden. No default values. */ |
cf0bcb51 | 1573 | if (handle == NULL || in_chan == NULL) { |
2f70b271 | 1574 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1575 | } |
1576 | ||
441c16a7 | 1577 | memset(&lsm, 0, sizeof(lsm)); |
cf0bcb51 JG |
1578 | memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan)); |
1579 | lsm.u.channel.chan.attr.extended.ptr = NULL; | |
441c16a7 | 1580 | |
cf0bcb51 JG |
1581 | if (!in_chan->attr.extended.ptr) { |
1582 | struct lttng_channel *channel; | |
1583 | struct lttng_channel_extended *extended; | |
7d29a247 | 1584 | |
cf0bcb51 JG |
1585 | channel = lttng_channel_create(&handle->domain); |
1586 | if (!channel) { | |
1587 | return -LTTNG_ERR_NOMEM; | |
1588 | } | |
1589 | ||
1590 | /* | |
1591 | * Create a new channel in order to use default extended | |
1592 | * attribute values. | |
1593 | */ | |
1594 | extended = (struct lttng_channel_extended *) | |
1595 | channel->attr.extended.ptr; | |
1596 | memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); | |
1597 | lttng_channel_destroy(channel); | |
1598 | } else { | |
1599 | struct lttng_channel_extended *extended; | |
1600 | ||
1601 | extended = (struct lttng_channel_extended *) | |
1602 | in_chan->attr.extended.ptr; | |
1603 | memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); | |
1604 | } | |
cd80958d | 1605 | |
e3091d6a FD |
1606 | /* |
1607 | * Verify that the amount of memory required to create the requested | |
1608 | * buffer is available on the system at the moment. | |
1609 | */ | |
1610 | total_buffer_size_needed_per_cpu = lsm.u.channel.chan.attr.num_subbuf * | |
1611 | lsm.u.channel.chan.attr.subbuf_size; | |
1612 | if (!check_enough_available_memory(total_buffer_size_needed_per_cpu)) { | |
1613 | return -LTTNG_ERR_NOMEM; | |
1614 | } | |
1615 | ||
cf0bcb51 | 1616 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
67040e31 | 1617 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
7d29a247 | 1618 | |
55fb8091 JG |
1619 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
1620 | sizeof(lsm.session.name)); | |
1621 | if (ret) { | |
1622 | ret = -LTTNG_ERR_INVALID; | |
1623 | goto end; | |
1624 | } | |
cd80958d | 1625 | |
55fb8091 JG |
1626 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
1627 | end: | |
1628 | return ret; | |
8c0faa1d | 1629 | } |
1df4dedd | 1630 | |
2ef84c95 | 1631 | /* |
9ae110e2 JG |
1632 | * All tracing will be stopped for registered events of the channel. |
1633 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 1634 | */ |
cd80958d | 1635 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 1636 | { |
55fb8091 | 1637 | int ret; |
cd80958d DG |
1638 | struct lttcomm_session_msg lsm; |
1639 | ||
9ae110e2 | 1640 | /* Safety check. Both are mandatory. */ |
9d697d3d | 1641 | if (handle == NULL || name == NULL) { |
2f70b271 | 1642 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1643 | } |
1644 | ||
441c16a7 MD |
1645 | memset(&lsm, 0, sizeof(lsm)); |
1646 | ||
cd80958d | 1647 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 1648 | |
55fb8091 | 1649 | ret = lttng_strncpy(lsm.u.disable.channel_name, name, |
9d697d3d | 1650 | sizeof(lsm.u.disable.channel_name)); |
55fb8091 JG |
1651 | if (ret) { |
1652 | ret = -LTTNG_ERR_INVALID; | |
1653 | goto end; | |
1654 | } | |
9d697d3d | 1655 | |
67040e31 | 1656 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
cd80958d | 1657 | |
55fb8091 JG |
1658 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
1659 | sizeof(lsm.session.name)); | |
1660 | if (ret) { | |
1661 | ret = -LTTNG_ERR_INVALID; | |
1662 | goto end; | |
1663 | } | |
cd80958d | 1664 | |
55fb8091 JG |
1665 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
1666 | end: | |
1667 | return ret; | |
ca95a216 DG |
1668 | } |
1669 | ||
ccf10263 | 1670 | /* |
9ae110e2 JG |
1671 | * Add PID to session tracker. |
1672 | * Return 0 on success else a negative LTTng error code. | |
ccf10263 MD |
1673 | */ |
1674 | int lttng_track_pid(struct lttng_handle *handle, int pid) | |
1675 | { | |
55fb8091 | 1676 | int ret; |
ccf10263 MD |
1677 | struct lttcomm_session_msg lsm; |
1678 | ||
9ae110e2 | 1679 | /* NULL arguments are forbidden. No default values. */ |
ccf10263 MD |
1680 | if (handle == NULL) { |
1681 | return -LTTNG_ERR_INVALID; | |
1682 | } | |
1683 | ||
1684 | memset(&lsm, 0, sizeof(lsm)); | |
1685 | ||
1686 | lsm.cmd_type = LTTNG_TRACK_PID; | |
1687 | lsm.u.pid_tracker.pid = pid; | |
1688 | ||
67040e31 | 1689 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
ccf10263 | 1690 | |
55fb8091 | 1691 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
ccf10263 | 1692 | sizeof(lsm.session.name)); |
55fb8091 JG |
1693 | if (ret) { |
1694 | ret = -LTTNG_ERR_INVALID; | |
1695 | goto end; | |
1696 | } | |
ccf10263 | 1697 | |
55fb8091 JG |
1698 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
1699 | end: | |
1700 | return ret; | |
ccf10263 MD |
1701 | } |
1702 | ||
1703 | /* | |
9ae110e2 JG |
1704 | * Remove PID from session tracker. |
1705 | * Return 0 on success else a negative LTTng error code. | |
ccf10263 MD |
1706 | */ |
1707 | int lttng_untrack_pid(struct lttng_handle *handle, int pid) | |
1708 | { | |
55fb8091 | 1709 | int ret; |
ccf10263 MD |
1710 | struct lttcomm_session_msg lsm; |
1711 | ||
9ae110e2 | 1712 | /* NULL arguments are forbidden. No default values. */ |
ccf10263 MD |
1713 | if (handle == NULL) { |
1714 | return -LTTNG_ERR_INVALID; | |
1715 | } | |
1716 | ||
1717 | memset(&lsm, 0, sizeof(lsm)); | |
1718 | ||
1719 | lsm.cmd_type = LTTNG_UNTRACK_PID; | |
1720 | lsm.u.pid_tracker.pid = pid; | |
1721 | ||
67040e31 | 1722 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
ccf10263 | 1723 | |
55fb8091 JG |
1724 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
1725 | sizeof(lsm.session.name)); | |
1726 | if (ret) { | |
1727 | ret = -LTTNG_ERR_INVALID; | |
1728 | goto end; | |
1729 | } | |
ccf10263 | 1730 | |
55fb8091 JG |
1731 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
1732 | end: | |
1733 | return ret; | |
ccf10263 MD |
1734 | } |
1735 | ||
fac6795d | 1736 | /* |
9ae110e2 JG |
1737 | * Lists all available tracepoints of domain. |
1738 | * Sets the contents of the events array. | |
1739 | * Returns the number of lttng_event entries in events; | |
1740 | * on error, returns a negative value. | |
fac6795d | 1741 | */ |
cd80958d | 1742 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 1743 | struct lttng_event **events) |
fac6795d | 1744 | { |
052da939 | 1745 | int ret; |
cd80958d DG |
1746 | struct lttcomm_session_msg lsm; |
1747 | ||
9d697d3d | 1748 | if (handle == NULL) { |
2f70b271 | 1749 | return -LTTNG_ERR_INVALID; |
cd80958d | 1750 | } |
fac6795d | 1751 | |
53efb85a | 1752 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1753 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
67040e31 | 1754 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
2a71efd5 | 1755 | |
cac3069d | 1756 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
052da939 DG |
1757 | if (ret < 0) { |
1758 | return ret; | |
eb354453 | 1759 | } |
fac6795d | 1760 | |
9f19cc17 | 1761 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
1762 | } |
1763 | ||
f37d259d | 1764 | /* |
9ae110e2 JG |
1765 | * Lists all available tracepoint fields of domain. |
1766 | * Sets the contents of the event field array. | |
1767 | * Returns the number of lttng_event_field entries in events; | |
1768 | * on error, returns a negative value. | |
f37d259d MD |
1769 | */ |
1770 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
1771 | struct lttng_event_field **fields) | |
1772 | { | |
1773 | int ret; | |
1774 | struct lttcomm_session_msg lsm; | |
1775 | ||
1776 | if (handle == NULL) { | |
2f70b271 | 1777 | return -LTTNG_ERR_INVALID; |
f37d259d MD |
1778 | } |
1779 | ||
53efb85a | 1780 | memset(&lsm, 0, sizeof(lsm)); |
f37d259d | 1781 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
67040e31 | 1782 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
f37d259d | 1783 | |
cac3069d | 1784 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
f37d259d MD |
1785 | if (ret < 0) { |
1786 | return ret; | |
1787 | } | |
1788 | ||
1789 | return ret / sizeof(struct lttng_event_field); | |
1790 | } | |
1791 | ||
834978fd | 1792 | /* |
9ae110e2 JG |
1793 | * Lists all available kernel system calls. Allocates and sets the contents of |
1794 | * the events array. | |
834978fd | 1795 | * |
9ae110e2 JG |
1796 | * Returns the number of lttng_event entries in events; on error, returns a |
1797 | * negative value. | |
834978fd DG |
1798 | */ |
1799 | int lttng_list_syscalls(struct lttng_event **events) | |
1800 | { | |
1801 | int ret; | |
1802 | struct lttcomm_session_msg lsm; | |
1803 | ||
1804 | if (!events) { | |
1805 | return -LTTNG_ERR_INVALID; | |
1806 | } | |
1807 | ||
1808 | memset(&lsm, 0, sizeof(lsm)); | |
1809 | lsm.cmd_type = LTTNG_LIST_SYSCALLS; | |
1810 | /* Force kernel domain for system calls. */ | |
1811 | lsm.domain.type = LTTNG_DOMAIN_KERNEL; | |
1812 | ||
1813 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); | |
1814 | if (ret < 0) { | |
1815 | return ret; | |
1816 | } | |
1817 | ||
1818 | return ret / sizeof(struct lttng_event); | |
1819 | } | |
1820 | ||
1657e9bb | 1821 | /* |
9ae110e2 JG |
1822 | * Returns a human readable string describing |
1823 | * the error code (a negative value). | |
1657e9bb | 1824 | */ |
9a745bc7 | 1825 | const char *lttng_strerror(int code) |
1657e9bb | 1826 | { |
f73fabfd | 1827 | return error_get_str(code); |
1657e9bb DG |
1828 | } |
1829 | ||
5feee503 JG |
1830 | enum lttng_error_code lttng_create_session_ext( |
1831 | struct lttng_session_descriptor *session_descriptor) | |
1832 | { | |
1833 | enum lttng_error_code ret_code; | |
1834 | struct lttcomm_session_msg lsm = { | |
1835 | .cmd_type = LTTNG_CREATE_SESSION_EXT, | |
1836 | }; | |
1837 | void *reply = NULL; | |
1838 | struct lttng_buffer_view reply_view; | |
1839 | int reply_ret; | |
1840 | bool sessiond_must_generate_ouput; | |
1841 | struct lttng_dynamic_buffer payload; | |
1842 | int ret; | |
1843 | size_t descriptor_size; | |
1844 | struct lttng_session_descriptor *descriptor_reply = NULL; | |
1845 | ||
1846 | lttng_dynamic_buffer_init(&payload); | |
1847 | if (!session_descriptor) { | |
1848 | ret_code = LTTNG_ERR_INVALID; | |
1849 | goto end; | |
1850 | } | |
1851 | ||
1852 | sessiond_must_generate_ouput = | |
1853 | !lttng_session_descriptor_is_output_destination_initialized( | |
1854 | session_descriptor); | |
1855 | if (sessiond_must_generate_ouput) { | |
1856 | const char *home_dir = utils_get_home_dir(); | |
1857 | size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0; | |
1858 | ||
1859 | if (!home_dir || home_dir_len > LTTNG_PATH_MAX) { | |
1860 | ret_code = LTTNG_ERR_FATAL; | |
1861 | goto end; | |
1862 | } | |
1863 | ||
1864 | lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; | |
1865 | ret = lttng_dynamic_buffer_append(&payload, home_dir, | |
1866 | home_dir_len); | |
1867 | if (ret) { | |
1868 | ret_code = LTTNG_ERR_NOMEM; | |
1869 | goto end; | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | descriptor_size = payload.size; | |
1874 | ret = lttng_session_descriptor_serialize(session_descriptor, | |
1875 | &payload); | |
1876 | if (ret) { | |
1877 | ret_code = LTTNG_ERR_INVALID; | |
1878 | goto end; | |
1879 | } | |
1880 | descriptor_size = payload.size - descriptor_size; | |
1881 | lsm.u.create_session.session_descriptor_size = descriptor_size; | |
1882 | ||
1883 | /* Command returns a session descriptor on success. */ | |
1884 | reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, | |
1885 | payload.size, &reply); | |
1886 | if (reply_ret < 0) { | |
1887 | ret_code = -reply_ret; | |
1888 | goto end; | |
1889 | } else if (reply_ret == 0) { | |
1890 | /* Socket unexpectedly closed by the session daemon. */ | |
1891 | ret_code = LTTNG_ERR_FATAL; | |
1892 | goto end; | |
1893 | } | |
1894 | ||
1895 | reply_view = lttng_buffer_view_init(reply, 0, reply_ret); | |
1896 | ret = lttng_session_descriptor_create_from_buffer(&reply_view, | |
1897 | &descriptor_reply); | |
1898 | if (ret < 0) { | |
1899 | ret_code = LTTNG_ERR_FATAL; | |
1900 | goto end; | |
1901 | } | |
1902 | ret_code = LTTNG_OK; | |
1903 | lttng_session_descriptor_assign(session_descriptor, descriptor_reply); | |
1904 | end: | |
1905 | free(reply); | |
1906 | lttng_dynamic_buffer_reset(&payload); | |
1907 | lttng_session_descriptor_destroy(descriptor_reply); | |
1908 | return ret_code; | |
1909 | } | |
1910 | ||
aaf97519 | 1911 | /* |
5feee503 | 1912 | * Create a new session using name and url for destination. |
a4b92340 | 1913 | * |
b3459096 | 1914 | * Return 0 on success else a negative LTTng error code. |
00e2e675 | 1915 | */ |
a4b92340 | 1916 | int lttng_create_session(const char *name, const char *url) |
00e2e675 | 1917 | { |
3dd05a85 | 1918 | int ret; |
a4b92340 | 1919 | ssize_t size; |
a4b92340 | 1920 | struct lttng_uri *uris = NULL; |
5feee503 JG |
1921 | struct lttng_session_descriptor *descriptor = NULL; |
1922 | enum lttng_error_code ret_code; | |
00e2e675 | 1923 | |
5feee503 JG |
1924 | if (!name) { |
1925 | ret = -LTTNG_ERR_INVALID; | |
1926 | goto end; | |
00e2e675 DG |
1927 | } |
1928 | ||
bc894455 | 1929 | size = uri_parse_str_urls(url, NULL, &uris); |
a4b92340 | 1930 | if (size < 0) { |
5feee503 JG |
1931 | ret = -LTTNG_ERR_INVALID; |
1932 | goto end; | |
1933 | } | |
1934 | switch (size) { | |
1935 | case 0: | |
1936 | descriptor = lttng_session_descriptor_create(name); | |
1937 | break; | |
1938 | case 1: | |
1939 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
1940 | ret = -LTTNG_ERR_INVALID; | |
1941 | goto end; | |
1942 | } | |
1943 | descriptor = lttng_session_descriptor_local_create(name, | |
1944 | uris[0].dst.path); | |
1945 | break; | |
1946 | case 2: | |
1947 | descriptor = lttng_session_descriptor_network_create(name, url, | |
1948 | NULL); | |
1949 | break; | |
1950 | default: | |
1951 | ret = -LTTNG_ERR_INVALID; | |
1952 | goto end; | |
1953 | } | |
1954 | if (!descriptor) { | |
1955 | ret = -LTTNG_ERR_INVALID; | |
1956 | goto end; | |
00e2e675 | 1957 | } |
5feee503 JG |
1958 | ret_code = lttng_create_session_ext(descriptor); |
1959 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
1960 | end: | |
1961 | lttng_session_descriptor_destroy(descriptor); | |
1962 | free(uris); | |
1963 | return ret; | |
1964 | } | |
00e2e675 | 1965 | |
5feee503 JG |
1966 | /* |
1967 | * Create a session exclusively used for snapshot. | |
1968 | * | |
b3459096 | 1969 | * Return 0 on success else a negative LTTng error code. |
5feee503 JG |
1970 | */ |
1971 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) | |
1972 | { | |
1973 | int ret; | |
1974 | enum lttng_error_code ret_code; | |
1975 | ssize_t size; | |
1976 | struct lttng_uri *uris = NULL; | |
1977 | struct lttng_session_descriptor *descriptor = NULL; | |
a4b92340 | 1978 | |
5feee503 JG |
1979 | if (!name) { |
1980 | ret = -LTTNG_ERR_INVALID; | |
1981 | goto end; | |
1982 | } | |
1983 | ||
1984 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); | |
1985 | if (size < 0) { | |
1986 | ret = -LTTNG_ERR_INVALID; | |
1987 | goto end; | |
1988 | } | |
1989 | /* | |
1990 | * If the user does not specify a custom subdir, use the session name. | |
1991 | */ | |
1992 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && | |
1993 | strlen(uris[0].subdir) == 0) { | |
1994 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", | |
1995 | name); | |
1996 | if (ret < 0) { | |
1997 | PERROR("Failed to set session name as network destination sub-directory"); | |
1998 | ret = -LTTNG_ERR_FATAL; | |
1999 | goto end; | |
2000 | } else if (ret >= sizeof(uris[0].subdir)) { | |
2001 | /* Truncated output. */ | |
2002 | ret = -LTTNG_ERR_INVALID; | |
2003 | goto end; | |
2004 | } | |
2005 | } | |
3dd05a85 | 2006 | |
5feee503 JG |
2007 | switch (size) { |
2008 | case 0: | |
2009 | descriptor = lttng_session_descriptor_snapshot_create(name); | |
2010 | break; | |
2011 | case 1: | |
2012 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
2013 | ret = -LTTNG_ERR_INVALID; | |
2014 | goto end; | |
2015 | } | |
2016 | descriptor = lttng_session_descriptor_snapshot_local_create( | |
2017 | name, | |
2018 | uris[0].dst.path); | |
2019 | break; | |
2020 | case 2: | |
2021 | descriptor = lttng_session_descriptor_snapshot_network_create( | |
2022 | name, | |
2023 | snapshot_url, | |
2024 | NULL); | |
2025 | break; | |
2026 | default: | |
2027 | ret = -LTTNG_ERR_INVALID; | |
2028 | goto end; | |
2029 | } | |
2030 | if (!descriptor) { | |
2031 | ret = -LTTNG_ERR_INVALID; | |
2032 | goto end; | |
2033 | } | |
2034 | ret_code = lttng_create_session_ext(descriptor); | |
2035 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
2036 | end: | |
2037 | lttng_session_descriptor_destroy(descriptor); | |
3dd05a85 DG |
2038 | free(uris); |
2039 | return ret; | |
00e2e675 DG |
2040 | } |
2041 | ||
5feee503 JG |
2042 | /* |
2043 | * Create a session exclusively used for live. | |
2044 | * | |
b3459096 | 2045 | * Return 0 on success else a negative LTTng error code. |
5feee503 JG |
2046 | */ |
2047 | int lttng_create_session_live(const char *name, const char *url, | |
2048 | unsigned int timer_interval) | |
2049 | { | |
2050 | int ret; | |
2051 | enum lttng_error_code ret_code; | |
2052 | struct lttng_session_descriptor *descriptor = NULL; | |
2053 | ||
2054 | if (!name) { | |
2055 | ret = -LTTNG_ERR_INVALID; | |
2056 | goto end; | |
2057 | } | |
2058 | ||
2059 | if (url) { | |
2060 | descriptor = lttng_session_descriptor_live_network_create( | |
2061 | name, url, NULL, timer_interval); | |
2062 | } else { | |
2063 | descriptor = lttng_session_descriptor_live_create( | |
2064 | name, timer_interval); | |
2065 | } | |
2066 | if (!descriptor) { | |
2067 | ret = -LTTNG_ERR_INVALID; | |
2068 | goto end; | |
2069 | } | |
2070 | ret_code = lttng_create_session_ext(descriptor); | |
2071 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
2072 | end: | |
2073 | lttng_session_descriptor_destroy(descriptor); | |
2074 | return ret; | |
2075 | } | |
2076 | ||
e20ee7c2 JD |
2077 | /* |
2078 | * Stop the session and wait for the data before destroying it | |
b3459096 MD |
2079 | * |
2080 | * Return 0 on success else a negative LTTng error code. | |
e20ee7c2 JD |
2081 | */ |
2082 | int lttng_destroy_session(const char *session_name) | |
2083 | { | |
2084 | int ret; | |
823a1989 JG |
2085 | enum lttng_error_code ret_code; |
2086 | enum lttng_destruction_handle_status status; | |
2087 | struct lttng_destruction_handle *handle = NULL; | |
e20ee7c2 JD |
2088 | |
2089 | /* | |
823a1989 JG |
2090 | * Stop the tracing and wait for the data to be |
2091 | * consumed. | |
e20ee7c2 JD |
2092 | */ |
2093 | ret = _lttng_stop_tracing(session_name, 1); | |
2094 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
2095 | goto end; | |
2096 | } | |
2097 | ||
823a1989 JG |
2098 | ret_code = lttng_destroy_session_ext(session_name, &handle); |
2099 | if (ret_code != LTTNG_OK) { | |
2100 | ret = (int) -ret_code; | |
2101 | goto end; | |
2102 | } | |
2103 | assert(handle); | |
2104 | ||
2105 | /* Block until the completion of the destruction of the session. */ | |
2106 | status = lttng_destruction_handle_wait_for_completion(handle, -1); | |
2107 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) { | |
2108 | ret = -LTTNG_ERR_UNK; | |
2109 | goto end; | |
2110 | } | |
2111 | ||
2112 | status = lttng_destruction_handle_get_result(handle, &ret_code); | |
2113 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { | |
2114 | ret = -LTTNG_ERR_UNK; | |
2115 | goto end; | |
2116 | } | |
b3459096 | 2117 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
e20ee7c2 | 2118 | end: |
823a1989 | 2119 | lttng_destruction_handle_destroy(handle); |
e20ee7c2 JD |
2120 | return ret; |
2121 | } | |
2122 | ||
2123 | /* | |
2124 | * Destroy the session without waiting for the data. | |
2125 | */ | |
2126 | int lttng_destroy_session_no_wait(const char *session_name) | |
2127 | { | |
823a1989 | 2128 | enum lttng_error_code ret_code; |
e20ee7c2 | 2129 | |
823a1989 JG |
2130 | ret_code = lttng_destroy_session_ext(session_name, NULL); |
2131 | return ret_code == LTTNG_OK ? ret_code : -ret_code; | |
e20ee7c2 JD |
2132 | } |
2133 | ||
57167058 | 2134 | /* |
9ae110e2 JG |
2135 | * Ask the session daemon for all available sessions. |
2136 | * Sets the contents of the sessions array. | |
2137 | * Returns the number of lttng_session entries in sessions; | |
2138 | * on error, returns a negative value. | |
57167058 | 2139 | */ |
5feee503 | 2140 | int lttng_list_sessions(struct lttng_session **out_sessions) |
57167058 | 2141 | { |
ca95a216 | 2142 | int ret; |
cd80958d | 2143 | struct lttcomm_session_msg lsm; |
5feee503 JG |
2144 | const size_t session_size = sizeof(struct lttng_session) + |
2145 | sizeof(struct lttng_session_extended); | |
2146 | size_t session_count, i; | |
2147 | struct lttng_session_extended *sessions_extended_begin; | |
2148 | struct lttng_session *sessions = NULL; | |
57167058 | 2149 | |
53efb85a | 2150 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2151 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
5feee503 JG |
2152 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); |
2153 | if (ret <= 0) { | |
5feee503 JG |
2154 | goto end; |
2155 | } | |
2156 | if (!sessions) { | |
2157 | ret = -LTTNG_ERR_FATAL; | |
2158 | goto end; | |
2159 | } | |
2160 | ||
2161 | if (ret % session_size) { | |
2162 | ret = -LTTNG_ERR_UNK; | |
2163 | free(sessions); | |
2164 | *out_sessions = NULL; | |
2165 | goto end; | |
57167058 | 2166 | } |
5feee503 JG |
2167 | session_count = (size_t) ret / session_size; |
2168 | sessions_extended_begin = (struct lttng_session_extended *) | |
2169 | (&sessions[session_count]); | |
57167058 | 2170 | |
5feee503 JG |
2171 | /* Set extended session info pointers. */ |
2172 | for (i = 0; i < session_count; i++) { | |
2173 | struct lttng_session *session = &sessions[i]; | |
2174 | struct lttng_session_extended *extended = | |
2175 | &(sessions_extended_begin[i]); | |
2176 | ||
2177 | session->extended.ptr = extended; | |
2178 | } | |
2179 | ||
2180 | ret = (int) session_count; | |
2181 | *out_sessions = sessions; | |
2182 | end: | |
2183 | return ret; | |
2184 | } | |
2185 | ||
2186 | enum lttng_error_code lttng_session_get_creation_time( | |
2187 | const struct lttng_session *session, uint64_t *creation_time) | |
2188 | { | |
2189 | enum lttng_error_code ret = LTTNG_OK; | |
2190 | struct lttng_session_extended *extended; | |
2191 | ||
2192 | if (!session || !creation_time || !session->extended.ptr) { | |
2193 | ret = LTTNG_ERR_INVALID; | |
2194 | goto end; | |
2195 | } | |
2196 | ||
2197 | extended = session->extended.ptr; | |
2198 | if (!extended->creation_time.is_set) { | |
2199 | /* Not created on the session daemon yet. */ | |
2200 | ret = LTTNG_ERR_SESSION_NOT_EXIST; | |
2201 | goto end; | |
2202 | } | |
2203 | *creation_time = extended->creation_time.value; | |
2204 | end: | |
2205 | return ret; | |
57167058 DG |
2206 | } |
2207 | ||
d7ba1388 MD |
2208 | int lttng_set_session_shm_path(const char *session_name, |
2209 | const char *shm_path) | |
2210 | { | |
55fb8091 | 2211 | int ret; |
d7ba1388 MD |
2212 | struct lttcomm_session_msg lsm; |
2213 | ||
2214 | if (session_name == NULL) { | |
2215 | return -LTTNG_ERR_INVALID; | |
2216 | } | |
2217 | ||
2218 | memset(&lsm, 0, sizeof(lsm)); | |
2219 | lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; | |
2220 | ||
55fb8091 | 2221 | ret = lttng_strncpy(lsm.session.name, session_name, |
d7ba1388 | 2222 | sizeof(lsm.session.name)); |
55fb8091 JG |
2223 | if (ret) { |
2224 | ret = -LTTNG_ERR_INVALID; | |
2225 | goto end; | |
2226 | } | |
2227 | ||
2228 | ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "", | |
d7ba1388 | 2229 | sizeof(lsm.u.set_shm_path.shm_path)); |
55fb8091 JG |
2230 | if (ret) { |
2231 | ret = -LTTNG_ERR_INVALID; | |
2232 | goto end; | |
2233 | } | |
d7ba1388 | 2234 | |
55fb8091 JG |
2235 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
2236 | end: | |
2237 | return ret; | |
d7ba1388 MD |
2238 | } |
2239 | ||
9f19cc17 | 2240 | /* |
9ae110e2 JG |
2241 | * Ask the session daemon for all available domains of a session. |
2242 | * Sets the contents of the domains array. | |
2243 | * Returns the number of lttng_domain entries in domains; | |
2244 | * on error, returns a negative value. | |
9f19cc17 | 2245 | */ |
330be774 | 2246 | int lttng_list_domains(const char *session_name, |
cd80958d | 2247 | struct lttng_domain **domains) |
9f19cc17 DG |
2248 | { |
2249 | int ret; | |
cd80958d DG |
2250 | struct lttcomm_session_msg lsm; |
2251 | ||
330be774 | 2252 | if (session_name == NULL) { |
55fb8091 JG |
2253 | ret = -LTTNG_ERR_INVALID; |
2254 | goto error; | |
cd80958d | 2255 | } |
9f19cc17 | 2256 | |
53efb85a | 2257 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
2258 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
2259 | ||
55fb8091 | 2260 | ret = lttng_strncpy(lsm.session.name, session_name, |
cac3069d | 2261 | sizeof(lsm.session.name)); |
55fb8091 JG |
2262 | if (ret) { |
2263 | ret = -LTTNG_ERR_INVALID; | |
2264 | goto error; | |
2265 | } | |
cd80958d | 2266 | |
cac3069d | 2267 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
9f19cc17 | 2268 | if (ret < 0) { |
55fb8091 | 2269 | goto error; |
9f19cc17 DG |
2270 | } |
2271 | ||
2272 | return ret / sizeof(struct lttng_domain); | |
55fb8091 JG |
2273 | error: |
2274 | return ret; | |
9f19cc17 DG |
2275 | } |
2276 | ||
2277 | /* | |
9ae110e2 JG |
2278 | * Ask the session daemon for all available channels of a session. |
2279 | * Sets the contents of the channels array. | |
2280 | * Returns the number of lttng_channel entries in channels; | |
2281 | * on error, returns a negative value. | |
9f19cc17 | 2282 | */ |
cd80958d DG |
2283 | int lttng_list_channels(struct lttng_handle *handle, |
2284 | struct lttng_channel **channels) | |
9f19cc17 DG |
2285 | { |
2286 | int ret; | |
53e367f9 JG |
2287 | size_t channel_count, i; |
2288 | const size_t channel_size = sizeof(struct lttng_channel) + | |
cf0bcb51 | 2289 | sizeof(struct lttng_channel_extended); |
cd80958d | 2290 | struct lttcomm_session_msg lsm; |
53e367f9 | 2291 | void *extended_at; |
cd80958d | 2292 | |
9d697d3d | 2293 | if (handle == NULL) { |
53e367f9 JG |
2294 | ret = -LTTNG_ERR_INVALID; |
2295 | goto end; | |
cd80958d DG |
2296 | } |
2297 | ||
53efb85a | 2298 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2299 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
55fb8091 | 2300 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 2301 | sizeof(lsm.session.name)); |
55fb8091 JG |
2302 | if (ret) { |
2303 | ret = -LTTNG_ERR_INVALID; | |
2304 | goto end; | |
2305 | } | |
9f19cc17 | 2306 | |
67040e31 | 2307 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2308 | |
cac3069d | 2309 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); |
9f19cc17 | 2310 | if (ret < 0) { |
53e367f9 JG |
2311 | goto end; |
2312 | } | |
2313 | ||
2314 | if (ret % channel_size) { | |
2315 | ret = -LTTNG_ERR_UNK; | |
2316 | free(*channels); | |
2317 | *channels = NULL; | |
2318 | goto end; | |
9f19cc17 | 2319 | } |
53e367f9 | 2320 | channel_count = (size_t) ret / channel_size; |
9f19cc17 | 2321 | |
53e367f9 JG |
2322 | /* Set extended info pointers */ |
2323 | extended_at = ((void *) *channels) + | |
2324 | channel_count * sizeof(struct lttng_channel); | |
2325 | for (i = 0; i < channel_count; i++) { | |
2326 | struct lttng_channel *chan = &(*channels)[i]; | |
2327 | ||
2328 | chan->attr.extended.ptr = extended_at; | |
cf0bcb51 | 2329 | extended_at += sizeof(struct lttng_channel_extended); |
53e367f9 JG |
2330 | } |
2331 | ||
2332 | ret = (int) channel_count; | |
2333 | end: | |
2334 | return ret; | |
9f19cc17 DG |
2335 | } |
2336 | ||
2337 | /* | |
9ae110e2 JG |
2338 | * Ask the session daemon for all available events of a session channel. |
2339 | * Sets the contents of the events array. | |
2340 | * Returns the number of lttng_event entries in events; | |
2341 | * on error, returns a negative value. | |
9f19cc17 | 2342 | */ |
cd80958d DG |
2343 | int lttng_list_events(struct lttng_handle *handle, |
2344 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
2345 | { |
2346 | int ret; | |
cd80958d | 2347 | struct lttcomm_session_msg lsm; |
b4e3ceb9 PP |
2348 | struct lttcomm_event_command_header *cmd_header = NULL; |
2349 | size_t cmd_header_len; | |
2350 | uint32_t nb_events, i; | |
de453daa | 2351 | void *comm_ext_at; |
de453daa | 2352 | char *reception_buffer = NULL; |
56f0bc67 | 2353 | struct lttng_dynamic_buffer listing; |
de453daa | 2354 | size_t storage_req; |
9f19cc17 | 2355 | |
9d697d3d DG |
2356 | /* Safety check. An handle and channel name are mandatory */ |
2357 | if (handle == NULL || channel_name == NULL) { | |
2f70b271 | 2358 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
2359 | } |
2360 | ||
53efb85a | 2361 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2362 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
55fb8091 | 2363 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 2364 | sizeof(lsm.session.name)); |
55fb8091 JG |
2365 | if (ret) { |
2366 | ret = -LTTNG_ERR_INVALID; | |
2367 | goto end; | |
2368 | } | |
2369 | ||
2370 | ret = lttng_strncpy(lsm.u.list.channel_name, channel_name, | |
cd80958d | 2371 | sizeof(lsm.u.list.channel_name)); |
55fb8091 JG |
2372 | if (ret) { |
2373 | ret = -LTTNG_ERR_INVALID; | |
2374 | goto end; | |
2375 | } | |
2376 | ||
67040e31 | 2377 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2378 | |
a04d53fc | 2379 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, |
de453daa JG |
2380 | (void **) &reception_buffer, (void **) &cmd_header, |
2381 | &cmd_header_len); | |
9f19cc17 | 2382 | if (ret < 0) { |
de453daa | 2383 | goto end; |
9f19cc17 DG |
2384 | } |
2385 | ||
d9f484bc JG |
2386 | if (!cmd_header) { |
2387 | ret = -LTTNG_ERR_UNK; | |
2388 | goto end; | |
2389 | } | |
2390 | ||
b4e3ceb9 PP |
2391 | /* Set number of events and free command header */ |
2392 | nb_events = cmd_header->nb_events; | |
2393 | if (nb_events > INT_MAX) { | |
2394 | ret = -EOVERFLOW; | |
de453daa | 2395 | goto end; |
b4e3ceb9 | 2396 | } |
b4e3ceb9 PP |
2397 | free(cmd_header); |
2398 | cmd_header = NULL; | |
2399 | ||
de453daa JG |
2400 | /* |
2401 | * The buffer that is returned must contain a "flat" version of | |
2402 | * the events that are returned. In other words, all pointers | |
2403 | * within an lttng_event must point to a location within the returned | |
56f0bc67 JG |
2404 | * buffer so that the user may free everything by simply calling free() |
2405 | * on the returned buffer. This is needed in order to maintain API | |
de453daa JG |
2406 | * compatibility. |
2407 | * | |
56f0bc67 JG |
2408 | * A first pass is performed to compute the size of the buffer that |
2409 | * must be allocated. A second pass is then performed to setup | |
de453daa JG |
2410 | * the returned events so that their members always point within the |
2411 | * buffer. | |
2412 | * | |
2413 | * The layout of the returned buffer is as follows: | |
2414 | * - struct lttng_event[nb_events], | |
2415 | * - nb_events times the following: | |
2416 | * - struct lttng_event_extended, | |
2417 | * - flattened version of userspace_probe_location | |
2418 | * - filter_expression | |
2419 | * - exclusions | |
2420 | * - padding to align to 64-bits | |
2421 | */ | |
2422 | comm_ext_at = reception_buffer + | |
2423 | (nb_events * sizeof(struct lttng_event)); | |
2424 | storage_req = nb_events * sizeof(struct lttng_event); | |
b4e3ceb9 PP |
2425 | |
2426 | for (i = 0; i < nb_events; i++) { | |
de453daa JG |
2427 | struct lttcomm_event_extended_header *ext_comm = |
2428 | (struct lttcomm_event_extended_header *) comm_ext_at; | |
56f0bc67 | 2429 | int probe_storage_req = 0; |
b4e3ceb9 | 2430 | |
de453daa JG |
2431 | comm_ext_at += sizeof(*ext_comm); |
2432 | comm_ext_at += ext_comm->filter_len; | |
2433 | comm_ext_at += | |
2434 | ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
2435 | ||
56f0bc67 JG |
2436 | if (ext_comm->userspace_probe_location_len) { |
2437 | struct lttng_userspace_probe_location *probe_location = NULL; | |
2438 | struct lttng_buffer_view probe_location_view; | |
2439 | ||
2440 | probe_location_view = lttng_buffer_view_init( | |
2441 | comm_ext_at, 0, | |
2442 | ext_comm->userspace_probe_location_len); | |
2443 | ||
2444 | /* | |
2445 | * Create a temporary userspace probe location to | |
2446 | * determine the size needed by a "flattened" version | |
2447 | * of that same probe location. | |
2448 | */ | |
2449 | ret = lttng_userspace_probe_location_create_from_buffer( | |
2450 | &probe_location_view, &probe_location); | |
2451 | if (ret < 0) { | |
2452 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2453 | goto end; | |
2454 | } | |
2455 | ||
2456 | ret = lttng_userspace_probe_location_flatten( | |
2457 | probe_location, NULL); | |
2458 | lttng_userspace_probe_location_destroy(probe_location); | |
2459 | if (ret < 0) { | |
2460 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2461 | goto end; | |
2462 | } | |
2463 | ||
2464 | probe_storage_req = ret; | |
2465 | comm_ext_at += ext_comm->userspace_probe_location_len; | |
56f0bc67 JG |
2466 | } |
2467 | ||
de453daa | 2468 | storage_req += sizeof(struct lttng_event_extended); |
de453daa JG |
2469 | storage_req += ext_comm->filter_len; |
2470 | storage_req += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
56f0bc67 JG |
2471 | /* Padding to ensure the flat probe is aligned. */ |
2472 | storage_req = ALIGN_TO(storage_req, sizeof(uint64_t)); | |
2473 | storage_req += probe_storage_req; | |
b4e3ceb9 PP |
2474 | } |
2475 | ||
56f0bc67 JG |
2476 | lttng_dynamic_buffer_init(&listing); |
2477 | /* | |
2478 | * We must ensure that "listing" is never resized so as to preserve | |
2479 | * the validity of the flattened objects. | |
2480 | */ | |
2481 | ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req); | |
2482 | if (ret) { | |
2483 | ret = -LTTNG_ERR_NOMEM; | |
de453daa JG |
2484 | goto end; |
2485 | } | |
56f0bc67 JG |
2486 | |
2487 | ret = lttng_dynamic_buffer_append(&listing, reception_buffer, | |
2488 | nb_events * sizeof(struct lttng_event)); | |
2489 | if (ret) { | |
2490 | ret = -LTTNG_ERR_NOMEM; | |
2491 | goto free_dynamic_buffer; | |
2492 | } | |
de453daa JG |
2493 | |
2494 | comm_ext_at = reception_buffer + | |
56f0bc67 | 2495 | (nb_events * sizeof(struct lttng_event)); |
de453daa JG |
2496 | for (i = 0; i < nb_events; i++) { |
2497 | struct lttng_event *event = (struct lttng_event *) | |
56f0bc67 | 2498 | (listing.data + (sizeof(struct lttng_event) * i)); |
de453daa JG |
2499 | struct lttcomm_event_extended_header *ext_comm = |
2500 | (struct lttcomm_event_extended_header *) comm_ext_at; | |
2501 | struct lttng_event_extended *event_extended = | |
56f0bc67 JG |
2502 | (struct lttng_event_extended *) |
2503 | (listing.data + listing.size); | |
de453daa | 2504 | |
56f0bc67 JG |
2505 | /* Insert struct lttng_event_extended. */ |
2506 | ret = lttng_dynamic_buffer_set_size(&listing, | |
2507 | listing.size + sizeof(*event_extended)); | |
2508 | if (ret) { | |
2509 | ret = -LTTNG_ERR_NOMEM; | |
2510 | goto free_dynamic_buffer; | |
2511 | } | |
de453daa | 2512 | event->extended.ptr = event_extended; |
56f0bc67 | 2513 | |
de453daa JG |
2514 | comm_ext_at += sizeof(*ext_comm); |
2515 | ||
56f0bc67 JG |
2516 | /* Insert filter expression. */ |
2517 | if (ext_comm->filter_len) { | |
2518 | event_extended->filter_expression = listing.data + | |
2519 | listing.size; | |
2520 | ret = lttng_dynamic_buffer_append(&listing, comm_ext_at, | |
2521 | ext_comm->filter_len); | |
2522 | if (ret) { | |
2523 | ret = -LTTNG_ERR_NOMEM; | |
2524 | goto free_dynamic_buffer; | |
2525 | } | |
2526 | comm_ext_at += ext_comm->filter_len; | |
2527 | } | |
2528 | ||
2529 | /* Insert exclusions. */ | |
2530 | if (ext_comm->nb_exclusions) { | |
2531 | event_extended->exclusions.count = | |
2532 | ext_comm->nb_exclusions; | |
2533 | event_extended->exclusions.strings = | |
2534 | listing.data + listing.size; | |
2535 | ||
2536 | ret = lttng_dynamic_buffer_append(&listing, | |
2537 | comm_ext_at, | |
2538 | ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN); | |
2539 | if (ret) { | |
2540 | ret = -LTTNG_ERR_NOMEM; | |
5e4f4898 | 2541 | goto free_dynamic_buffer; |
56f0bc67 JG |
2542 | } |
2543 | comm_ext_at += ext_comm->nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
2544 | } | |
2545 | ||
2546 | /* Insert padding to align to 64-bits. */ | |
2547 | ret = lttng_dynamic_buffer_set_size(&listing, | |
2548 | ALIGN_TO(listing.size, sizeof(uint64_t))); | |
2549 | if (ret) { | |
2550 | ret = -LTTNG_ERR_NOMEM; | |
2551 | goto free_dynamic_buffer; | |
2552 | } | |
2553 | ||
2554 | /* Insert flattened userspace probe location. */ | |
2555 | if (ext_comm->userspace_probe_location_len) { | |
2556 | struct lttng_userspace_probe_location *probe_location = NULL; | |
2557 | struct lttng_buffer_view probe_location_view; | |
de453daa | 2558 | |
56f0bc67 JG |
2559 | probe_location_view = lttng_buffer_view_init( |
2560 | comm_ext_at, 0, | |
2561 | ext_comm->userspace_probe_location_len); | |
de453daa | 2562 | |
56f0bc67 JG |
2563 | ret = lttng_userspace_probe_location_create_from_buffer( |
2564 | &probe_location_view, &probe_location); | |
2565 | if (ret < 0) { | |
2566 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2567 | goto free_dynamic_buffer; | |
2568 | } | |
2569 | ||
2570 | event_extended->probe_location = (struct lttng_userspace_probe_location *) | |
2571 | (listing.data + listing.size); | |
2572 | ret = lttng_userspace_probe_location_flatten( | |
2573 | probe_location, &listing); | |
2574 | lttng_userspace_probe_location_destroy(probe_location); | |
2575 | if (ret < 0) { | |
2576 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2577 | goto free_dynamic_buffer; | |
2578 | } | |
2579 | ||
2580 | comm_ext_at += ext_comm->userspace_probe_location_len; | |
2581 | } | |
de453daa JG |
2582 | } |
2583 | ||
56f0bc67 JG |
2584 | /* Don't reset listing buffer as we return its content. */ |
2585 | *events = (struct lttng_event *) listing.data; | |
2586 | lttng_dynamic_buffer_init(&listing); | |
de453daa | 2587 | ret = (int) nb_events; |
56f0bc67 JG |
2588 | free_dynamic_buffer: |
2589 | lttng_dynamic_buffer_reset(&listing); | |
de453daa | 2590 | end: |
b4e3ceb9 | 2591 | free(cmd_header); |
de453daa | 2592 | free(reception_buffer); |
b4e3ceb9 | 2593 | return ret; |
9f19cc17 DG |
2594 | } |
2595 | ||
fac6795d | 2596 | /* |
1c8d13c8 TD |
2597 | * Sets the tracing_group variable with name. |
2598 | * This function allocates memory pointed to by tracing_group. | |
2599 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
2600 | */ |
2601 | int lttng_set_tracing_group(const char *name) | |
2602 | { | |
9d697d3d | 2603 | if (name == NULL) { |
2f70b271 | 2604 | return -LTTNG_ERR_INVALID; |
9d697d3d DG |
2605 | } |
2606 | ||
fac6795d | 2607 | if (asprintf(&tracing_group, "%s", name) < 0) { |
2f70b271 | 2608 | return -LTTNG_ERR_FATAL; |
fac6795d DG |
2609 | } |
2610 | ||
2611 | return 0; | |
2612 | } | |
2613 | ||
cd80958d | 2614 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
2615 | struct lttng_calibrate *calibrate) |
2616 | { | |
b812e5ca PP |
2617 | /* |
2618 | * This command was removed in LTTng 2.9. | |
2619 | */ | |
2620 | return -LTTNG_ERR_UND; | |
d0254c7c MD |
2621 | } |
2622 | ||
5edd7e09 DG |
2623 | /* |
2624 | * Set default channel attributes. | |
441c16a7 | 2625 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
2626 | */ |
2627 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
2628 | struct lttng_channel_attr *attr) | |
2629 | { | |
294851b0 | 2630 | struct lttng_channel_extended *extended; |
cf0bcb51 | 2631 | |
5edd7e09 DG |
2632 | /* Safety check */ |
2633 | if (attr == NULL || domain == NULL) { | |
2634 | return; | |
2635 | } | |
2636 | ||
294851b0 | 2637 | extended = (struct lttng_channel_extended *) attr->extended.ptr; |
eacaa7a6 DS |
2638 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
2639 | ||
0a9c6494 DG |
2640 | /* Same for all domains. */ |
2641 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
2642 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; | |
2643 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; | |
2644 | ||
5edd7e09 DG |
2645 | switch (domain->type) { |
2646 | case LTTNG_DOMAIN_KERNEL: | |
9ae110e2 JG |
2647 | attr->switch_timer_interval = |
2648 | DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; | |
d92ff3ef | 2649 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
3e230f92 | 2650 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
5edd7e09 DG |
2651 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
2652 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
cf0bcb51 JG |
2653 | if (extended) { |
2654 | extended->monitor_timer_interval = | |
2655 | DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2656 | extended->blocking_timeout = |
2657 | DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2658 | } |
5edd7e09 DG |
2659 | break; |
2660 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
2661 | switch (domain->buf_type) { |
2662 | case LTTNG_BUFFER_PER_UID: | |
2663 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); | |
2664 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; | |
2665 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2666 | attr->switch_timer_interval = |
2667 | DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; | |
2668 | attr->read_timer_interval = | |
2669 | DEFAULT_UST_UID_CHANNEL_READ_TIMER; | |
cf0bcb51 JG |
2670 | if (extended) { |
2671 | extended->monitor_timer_interval = | |
2672 | DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2673 | extended->blocking_timeout = |
2674 | DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2675 | } |
0a9c6494 DG |
2676 | break; |
2677 | case LTTNG_BUFFER_PER_PID: | |
2678 | default: | |
2679 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); | |
2680 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; | |
2681 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2682 | attr->switch_timer_interval = |
2683 | DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; | |
2684 | attr->read_timer_interval = | |
2685 | DEFAULT_UST_PID_CHANNEL_READ_TIMER; | |
cf0bcb51 JG |
2686 | if (extended) { |
2687 | extended->monitor_timer_interval = | |
2688 | DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2689 | extended->blocking_timeout = |
2690 | DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2691 | } |
0a9c6494 DG |
2692 | break; |
2693 | } | |
5edd7e09 | 2694 | default: |
441c16a7 | 2695 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
2696 | break; |
2697 | } | |
cf0bcb51 JG |
2698 | |
2699 | attr->extended.ptr = extended; | |
5edd7e09 DG |
2700 | } |
2701 | ||
5ba3702f JG |
2702 | int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, |
2703 | uint64_t *discarded_events) | |
2704 | { | |
2705 | int ret = 0; | |
cf0bcb51 | 2706 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2707 | |
2708 | if (!channel || !discarded_events) { | |
2709 | ret = -LTTNG_ERR_INVALID; | |
2710 | goto end; | |
2711 | } | |
2712 | ||
2713 | chan_ext = channel->attr.extended.ptr; | |
2714 | if (!chan_ext) { | |
2715 | /* | |
2716 | * This can happen since the lttng_channel structure is | |
2717 | * used for other tasks where this pointer is never set. | |
2718 | */ | |
2719 | *discarded_events = 0; | |
2720 | goto end; | |
2721 | } | |
2722 | ||
2723 | *discarded_events = chan_ext->discarded_events; | |
2724 | end: | |
2725 | return ret; | |
2726 | } | |
2727 | ||
2728 | int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, | |
2729 | uint64_t *lost_packets) | |
2730 | { | |
2731 | int ret = 0; | |
cf0bcb51 | 2732 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2733 | |
2734 | if (!channel || !lost_packets) { | |
2735 | ret = -LTTNG_ERR_INVALID; | |
2736 | goto end; | |
2737 | } | |
2738 | ||
2739 | chan_ext = channel->attr.extended.ptr; | |
2740 | if (!chan_ext) { | |
2741 | /* | |
2742 | * This can happen since the lttng_channel structure is | |
2743 | * used for other tasks where this pointer is never set. | |
2744 | */ | |
2745 | *lost_packets = 0; | |
2746 | goto end; | |
2747 | } | |
2748 | ||
2749 | *lost_packets = chan_ext->lost_packets; | |
2750 | end: | |
2751 | return ret; | |
2752 | } | |
2753 | ||
cf0bcb51 JG |
2754 | int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, |
2755 | uint64_t *monitor_timer_interval) | |
2756 | { | |
2757 | int ret = 0; | |
2758 | ||
2759 | if (!chan || !monitor_timer_interval) { | |
2760 | ret = -LTTNG_ERR_INVALID; | |
2761 | goto end; | |
2762 | } | |
2763 | ||
2764 | if (!chan->attr.extended.ptr) { | |
2765 | ret = -LTTNG_ERR_INVALID; | |
2766 | goto end; | |
2767 | } | |
2768 | ||
2769 | *monitor_timer_interval = ((struct lttng_channel_extended *) | |
2770 | chan->attr.extended.ptr)->monitor_timer_interval; | |
2771 | end: | |
2772 | return ret; | |
2773 | } | |
2774 | ||
2775 | int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, | |
2776 | uint64_t monitor_timer_interval) | |
2777 | { | |
2778 | int ret = 0; | |
2779 | ||
2780 | if (!chan || !chan->attr.extended.ptr) { | |
2781 | ret = -LTTNG_ERR_INVALID; | |
2782 | goto end; | |
2783 | } | |
2784 | ||
2785 | ((struct lttng_channel_extended *) | |
2786 | chan->attr.extended.ptr)->monitor_timer_interval = | |
2787 | monitor_timer_interval; | |
2788 | end: | |
2789 | return ret; | |
2790 | } | |
2791 | ||
491d1539 MD |
2792 | int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, |
2793 | int64_t *blocking_timeout) | |
2794 | { | |
2795 | int ret = 0; | |
2796 | ||
2797 | if (!chan || !blocking_timeout) { | |
2798 | ret = -LTTNG_ERR_INVALID; | |
2799 | goto end; | |
2800 | } | |
2801 | ||
2802 | if (!chan->attr.extended.ptr) { | |
2803 | ret = -LTTNG_ERR_INVALID; | |
2804 | goto end; | |
2805 | } | |
2806 | ||
2807 | *blocking_timeout = ((struct lttng_channel_extended *) | |
2808 | chan->attr.extended.ptr)->blocking_timeout; | |
2809 | end: | |
2810 | return ret; | |
2811 | } | |
2812 | ||
2813 | int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, | |
2814 | int64_t blocking_timeout) | |
2815 | { | |
2816 | int ret = 0; | |
2817 | int64_t msec_timeout; | |
2818 | ||
2819 | if (!chan || !chan->attr.extended.ptr) { | |
2820 | ret = -LTTNG_ERR_INVALID; | |
2821 | goto end; | |
2822 | } | |
2823 | ||
2824 | if (blocking_timeout < 0 && blocking_timeout != -1) { | |
2825 | ret = -LTTNG_ERR_INVALID; | |
2826 | goto end; | |
2827 | } | |
2828 | ||
2829 | /* | |
2830 | * LTTng-ust's use of poll() to implement this timeout mechanism forces | |
2831 | * us to accept a narrower range of values (msecs expressed as a signed | |
2832 | * 32-bit integer). | |
2833 | */ | |
2834 | msec_timeout = blocking_timeout / 1000; | |
2835 | if (msec_timeout != (int32_t) msec_timeout) { | |
2836 | ret = -LTTNG_ERR_INVALID; | |
2837 | goto end; | |
2838 | } | |
2839 | ||
2840 | ((struct lttng_channel_extended *) | |
2841 | chan->attr.extended.ptr)->blocking_timeout = | |
2842 | blocking_timeout; | |
2843 | end: | |
2844 | return ret; | |
2845 | } | |
2846 | ||
fac6795d | 2847 | /* |
2269e89e | 2848 | * Check if session daemon is alive. |
fac6795d | 2849 | * |
2269e89e | 2850 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 2851 | * On error returns a negative value. |
fac6795d | 2852 | */ |
947308c4 | 2853 | int lttng_session_daemon_alive(void) |
fac6795d DG |
2854 | { |
2855 | int ret; | |
2856 | ||
2857 | ret = set_session_daemon_path(); | |
2858 | if (ret < 0) { | |
9ae110e2 | 2859 | /* Error. */ |
fac6795d DG |
2860 | return ret; |
2861 | } | |
2862 | ||
9d035200 | 2863 | if (*sessiond_sock_path == '\0') { |
2f70b271 | 2864 | /* |
9ae110e2 JG |
2865 | * No socket path set. Weird error which means the constructor |
2866 | * was not called. | |
2f70b271 DG |
2867 | */ |
2868 | assert(0); | |
fac6795d DG |
2869 | } |
2870 | ||
2269e89e | 2871 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 | 2872 | if (ret < 0) { |
9ae110e2 | 2873 | /* Not alive. */ |
7d8234d9 MD |
2874 | return 0; |
2875 | } | |
7d8234d9 | 2876 | |
9ae110e2 | 2877 | /* Is alive. */ |
947308c4 | 2878 | return 1; |
fac6795d DG |
2879 | } |
2880 | ||
00e2e675 | 2881 | /* |
a4b92340 | 2882 | * Set URL for a consumer for a session and domain. |
00e2e675 DG |
2883 | * |
2884 | * Return 0 on success, else a negative value. | |
2885 | */ | |
a4b92340 DG |
2886 | int lttng_set_consumer_url(struct lttng_handle *handle, |
2887 | const char *control_url, const char *data_url) | |
00e2e675 | 2888 | { |
3dd05a85 | 2889 | int ret; |
a4b92340 | 2890 | ssize_t size; |
00e2e675 | 2891 | struct lttcomm_session_msg lsm; |
a4b92340 | 2892 | struct lttng_uri *uris = NULL; |
00e2e675 | 2893 | |
a4b92340 | 2894 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
55fb8091 JG |
2895 | ret = -LTTNG_ERR_INVALID; |
2896 | goto error; | |
00e2e675 DG |
2897 | } |
2898 | ||
a4b92340 DG |
2899 | memset(&lsm, 0, sizeof(lsm)); |
2900 | ||
00e2e675 DG |
2901 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
2902 | ||
55fb8091 | 2903 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
00e2e675 | 2904 | sizeof(lsm.session.name)); |
55fb8091 JG |
2905 | if (ret) { |
2906 | ret = -LTTNG_ERR_INVALID; | |
2907 | goto error; | |
2908 | } | |
2909 | ||
67040e31 | 2910 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
00e2e675 | 2911 | |
bc894455 | 2912 | size = uri_parse_str_urls(control_url, data_url, &uris); |
a4b92340 | 2913 | if (size < 0) { |
55fb8091 JG |
2914 | ret = -LTTNG_ERR_INVALID; |
2915 | goto error; | |
a4b92340 DG |
2916 | } |
2917 | ||
2918 | lsm.u.uri.size = size; | |
00e2e675 | 2919 | |
795a978d | 2920 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 2921 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
2922 | |
2923 | free(uris); | |
55fb8091 | 2924 | error: |
3dd05a85 | 2925 | return ret; |
00e2e675 DG |
2926 | } |
2927 | ||
2928 | /* | |
9c6bda17 | 2929 | * [OBSOLETE] |
00e2e675 DG |
2930 | */ |
2931 | int lttng_enable_consumer(struct lttng_handle *handle) | |
2932 | { | |
785d2d0d | 2933 | return -ENOSYS; |
00e2e675 DG |
2934 | } |
2935 | ||
2936 | /* | |
9c6bda17 | 2937 | * [OBSOLETE] |
00e2e675 DG |
2938 | */ |
2939 | int lttng_disable_consumer(struct lttng_handle *handle) | |
2940 | { | |
785d2d0d | 2941 | return -ENOSYS; |
00e2e675 DG |
2942 | } |
2943 | ||
07424f16 | 2944 | /* |
5feee503 | 2945 | * [OBSOLETE] |
07424f16 DG |
2946 | */ |
2947 | int _lttng_create_session_ext(const char *name, const char *url, | |
2948 | const char *datetime) | |
2949 | { | |
5feee503 | 2950 | return -ENOSYS; |
07424f16 DG |
2951 | } |
2952 | ||
806e2684 DG |
2953 | /* |
2954 | * For a given session name, this call checks if the data is ready to be read | |
2955 | * or is still being extracted by the consumer(s) hence not ready to be used by | |
2956 | * any readers. | |
2957 | */ | |
6d805429 | 2958 | int lttng_data_pending(const char *session_name) |
806e2684 DG |
2959 | { |
2960 | int ret; | |
2961 | struct lttcomm_session_msg lsm; | |
f6151c55 | 2962 | uint8_t *pending = NULL; |
806e2684 DG |
2963 | |
2964 | if (session_name == NULL) { | |
2965 | return -LTTNG_ERR_INVALID; | |
2966 | } | |
2967 | ||
53efb85a | 2968 | memset(&lsm, 0, sizeof(lsm)); |
6d805429 | 2969 | lsm.cmd_type = LTTNG_DATA_PENDING; |
806e2684 | 2970 | |
55fb8091 | 2971 | ret = lttng_strncpy(lsm.session.name, session_name, |
cac3069d | 2972 | sizeof(lsm.session.name)); |
55fb8091 JG |
2973 | if (ret) { |
2974 | ret = -LTTNG_ERR_INVALID; | |
2975 | goto end; | |
2976 | } | |
806e2684 | 2977 | |
f6151c55 JG |
2978 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending); |
2979 | if (ret < 0) { | |
2980 | goto end; | |
2981 | } else if (ret != 1) { | |
2982 | /* Unexpected payload size */ | |
2983 | ret = -LTTNG_ERR_INVALID; | |
2984 | goto end; | |
203cfe5a JG |
2985 | } else if (!pending) { |
2986 | /* Internal error. */ | |
2987 | ret = -LTTNG_ERR_UNK; | |
2988 | goto end; | |
806e2684 DG |
2989 | } |
2990 | ||
f6151c55 JG |
2991 | ret = (int) *pending; |
2992 | end: | |
2993 | free(pending); | |
806e2684 DG |
2994 | return ret; |
2995 | } | |
2996 | ||
a5dfbb9d MD |
2997 | /* |
2998 | * List PIDs in the tracker. | |
2999 | * | |
1b18ce93 JG |
3000 | * enabled is set to whether the PID tracker is enabled. |
3001 | * pids is set to an allocated array of PIDs currently tracked. On | |
3002 | * success, pids must be freed by the caller. | |
3003 | * nr_pids is set to the number of entries contained by the pids array. | |
a5dfbb9d MD |
3004 | * |
3005 | * Returns 0 on success, else a negative LTTng error code. | |
3006 | */ | |
3007 | int lttng_list_tracker_pids(struct lttng_handle *handle, | |
3008 | int *_enabled, int32_t **_pids, size_t *_nr_pids) | |
3009 | { | |
ebbf5ab7 JR |
3010 | int ret; |
3011 | int enabled = 1; | |
a5dfbb9d MD |
3012 | struct lttcomm_session_msg lsm; |
3013 | size_t nr_pids; | |
ff75c11e | 3014 | int32_t *pids = NULL; |
a5dfbb9d MD |
3015 | |
3016 | if (handle == NULL) { | |
3017 | return -LTTNG_ERR_INVALID; | |
3018 | } | |
3019 | ||
3020 | memset(&lsm, 0, sizeof(lsm)); | |
3021 | lsm.cmd_type = LTTNG_LIST_TRACKER_PIDS; | |
55fb8091 | 3022 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
a5dfbb9d | 3023 | sizeof(lsm.session.name)); |
55fb8091 JG |
3024 | if (ret) { |
3025 | return -LTTNG_ERR_INVALID; | |
3026 | } | |
67040e31 | 3027 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
a5dfbb9d MD |
3028 | |
3029 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pids); | |
3030 | if (ret < 0) { | |
3031 | return ret; | |
3032 | } | |
3033 | nr_pids = ret / sizeof(int32_t); | |
de8218d4 JG |
3034 | if (nr_pids > 0 && !pids) { |
3035 | return -LTTNG_ERR_UNK; | |
3036 | } | |
a5dfbb9d MD |
3037 | if (nr_pids == 1 && pids[0] == -1) { |
3038 | free(pids); | |
3039 | pids = NULL; | |
3040 | enabled = 0; | |
3041 | nr_pids = 0; | |
3042 | } | |
3043 | *_enabled = enabled; | |
3044 | *_pids = pids; | |
3045 | *_nr_pids = nr_pids; | |
3046 | return 0; | |
3047 | } | |
3048 | ||
93ec662e JD |
3049 | /* |
3050 | * Regenerate the metadata for a session. | |
3051 | * Return 0 on success, a negative error code on error. | |
3052 | */ | |
eded6438 | 3053 | int lttng_regenerate_metadata(const char *session_name) |
93ec662e JD |
3054 | { |
3055 | int ret; | |
3056 | struct lttcomm_session_msg lsm; | |
3057 | ||
3058 | if (!session_name) { | |
3059 | ret = -LTTNG_ERR_INVALID; | |
3060 | goto end; | |
3061 | } | |
3062 | ||
3063 | memset(&lsm, 0, sizeof(lsm)); | |
eded6438 | 3064 | lsm.cmd_type = LTTNG_REGENERATE_METADATA; |
93ec662e | 3065 | |
55fb8091 | 3066 | ret = lttng_strncpy(lsm.session.name, session_name, |
93ec662e | 3067 | sizeof(lsm.session.name)); |
55fb8091 JG |
3068 | if (ret) { |
3069 | ret = -LTTNG_ERR_INVALID; | |
3070 | goto end; | |
3071 | } | |
93ec662e JD |
3072 | |
3073 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
3074 | if (ret < 0) { | |
3075 | goto end; | |
3076 | } | |
3077 | ||
3078 | ret = 0; | |
3079 | end: | |
3080 | return ret; | |
3081 | } | |
3082 | ||
eded6438 JD |
3083 | /* |
3084 | * Deprecated, replaced by lttng_regenerate_metadata. | |
3085 | */ | |
3086 | int lttng_metadata_regenerate(const char *session_name) | |
3087 | { | |
3088 | return lttng_regenerate_metadata(session_name); | |
3089 | } | |
3090 | ||
c2561365 JD |
3091 | /* |
3092 | * Regenerate the statedump of a session. | |
3093 | * Return 0 on success, a negative error code on error. | |
3094 | */ | |
3095 | int lttng_regenerate_statedump(const char *session_name) | |
3096 | { | |
3097 | int ret; | |
3098 | struct lttcomm_session_msg lsm; | |
3099 | ||
3100 | if (!session_name) { | |
3101 | ret = -LTTNG_ERR_INVALID; | |
3102 | goto end; | |
3103 | } | |
3104 | ||
3105 | memset(&lsm, 0, sizeof(lsm)); | |
3106 | lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP; | |
3107 | ||
55fb8091 | 3108 | ret = lttng_strncpy(lsm.session.name, session_name, |
c2561365 | 3109 | sizeof(lsm.session.name)); |
55fb8091 JG |
3110 | if (ret) { |
3111 | ret = -LTTNG_ERR_INVALID; | |
3112 | goto end; | |
3113 | } | |
c2561365 JD |
3114 | |
3115 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
3116 | if (ret < 0) { | |
3117 | goto end; | |
3118 | } | |
3119 | ||
3120 | ret = 0; | |
3121 | end: | |
3122 | return ret; | |
3123 | } | |
3124 | ||
a58c490f JG |
3125 | int lttng_register_trigger(struct lttng_trigger *trigger) |
3126 | { | |
3127 | int ret; | |
3128 | struct lttcomm_session_msg lsm; | |
3647288f | 3129 | struct lttng_dynamic_buffer buffer; |
a58c490f | 3130 | |
3647288f | 3131 | lttng_dynamic_buffer_init(&buffer); |
a58c490f JG |
3132 | if (!trigger) { |
3133 | ret = -LTTNG_ERR_INVALID; | |
3134 | goto end; | |
3135 | } | |
3136 | ||
3137 | if (!lttng_trigger_validate(trigger)) { | |
eac4828d | 3138 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a58c490f JG |
3139 | goto end; |
3140 | } | |
3141 | ||
3647288f JG |
3142 | ret = lttng_trigger_serialize(trigger, &buffer); |
3143 | if (ret < 0) { | |
a58c490f JG |
3144 | ret = -LTTNG_ERR_UNK; |
3145 | goto end; | |
3146 | } | |
3147 | ||
a58c490f JG |
3148 | memset(&lsm, 0, sizeof(lsm)); |
3149 | lsm.cmd_type = LTTNG_REGISTER_TRIGGER; | |
3647288f JG |
3150 | lsm.u.trigger.length = (uint32_t) buffer.size; |
3151 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, | |
3152 | buffer.size, NULL); | |
a58c490f | 3153 | end: |
3647288f | 3154 | lttng_dynamic_buffer_reset(&buffer); |
a58c490f JG |
3155 | return ret; |
3156 | } | |
3157 | ||
3158 | int lttng_unregister_trigger(struct lttng_trigger *trigger) | |
3159 | { | |
3160 | int ret; | |
3161 | struct lttcomm_session_msg lsm; | |
3647288f | 3162 | struct lttng_dynamic_buffer buffer; |
a58c490f | 3163 | |
3647288f | 3164 | lttng_dynamic_buffer_init(&buffer); |
a58c490f JG |
3165 | if (!trigger) { |
3166 | ret = -LTTNG_ERR_INVALID; | |
3167 | goto end; | |
3168 | } | |
3169 | ||
3170 | if (!lttng_trigger_validate(trigger)) { | |
3647288f | 3171 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a58c490f JG |
3172 | goto end; |
3173 | } | |
3174 | ||
3647288f JG |
3175 | ret = lttng_trigger_serialize(trigger, &buffer); |
3176 | if (ret < 0) { | |
a58c490f JG |
3177 | ret = -LTTNG_ERR_UNK; |
3178 | goto end; | |
3179 | } | |
3180 | ||
a58c490f JG |
3181 | memset(&lsm, 0, sizeof(lsm)); |
3182 | lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; | |
3647288f JG |
3183 | lsm.u.trigger.length = (uint32_t) buffer.size; |
3184 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data, | |
3185 | buffer.size, NULL); | |
a58c490f | 3186 | end: |
3647288f | 3187 | lttng_dynamic_buffer_reset(&buffer); |
a58c490f JG |
3188 | return ret; |
3189 | } | |
3190 | ||
fac6795d | 3191 | /* |
9ae110e2 | 3192 | * lib constructor. |
fac6795d | 3193 | */ |
b2b89e8a | 3194 | static void __attribute__((constructor)) init(void) |
fac6795d DG |
3195 | { |
3196 | /* Set default session group */ | |
bbccc3d2 | 3197 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 3198 | } |
49cca668 DG |
3199 | |
3200 | /* | |
9ae110e2 | 3201 | * lib destructor. |
49cca668 | 3202 | */ |
b2b89e8a | 3203 | static void __attribute__((destructor)) lttng_ctl_exit(void) |
49cca668 DG |
3204 | { |
3205 | free(tracing_group); | |
3206 | } |