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