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