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