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