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