2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/domain.h>
9 #include <lttng/lttng-error.h>
11 #include "lttng-ctl-helper.hpp"
12 #include <common/sessiond-comm/sessiond-comm.hpp>
13 #include <common/tracker.hpp>
14 #include <lttng/tracker.h>
15 #include <type_traits>
17 struct lttng_process_attr_tracker_handle
{
19 enum lttng_domain_type domain
;
20 enum lttng_process_attr process_attr
;
21 struct lttng_process_attr_values
*inclusion_set
;
24 void lttng_process_attr_tracker_handle_destroy(
25 struct lttng_process_attr_tracker_handle
*tracker
)
31 lttng_process_attr_values_destroy(tracker
->inclusion_set
);
32 free(tracker
->session_name
);
36 enum lttng_error_code
lttng_session_get_tracker_handle(const char *session_name
,
37 enum lttng_domain_type domain
,
38 enum lttng_process_attr process_attr
,
39 struct lttng_process_attr_tracker_handle
**out_tracker_handle
)
41 enum lttng_error_code ret_code
= LTTNG_OK
;
42 struct lttng_process_attr_tracker_handle
*handle
= NULL
;
43 enum lttng_process_attr_tracker_handle_status status
;
44 enum lttng_tracking_policy policy
;
46 if (!session_name
|| !out_tracker_handle
) {
47 ret_code
= LTTNG_ERR_INVALID
;
51 if (domain
!= LTTNG_DOMAIN_KERNEL
&& domain
!= LTTNG_DOMAIN_UST
) {
52 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
56 handle
= zmalloc
<lttng_process_attr_tracker_handle
>();
58 ret_code
= LTTNG_ERR_NOMEM
;
62 handle
->session_name
= strdup(session_name
);
63 if (!handle
->session_name
) {
64 ret_code
= LTTNG_ERR_NOMEM
;
68 handle
->domain
= domain
;
69 handle
->process_attr
= process_attr
;
72 * Use the `get_tracking_policy` command to validate the tracker's
75 status
= lttng_process_attr_tracker_handle_get_tracking_policy(
78 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
:
80 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
:
81 ret_code
= LTTNG_ERR_SESSION_NOT_EXIST
;
84 ret_code
= LTTNG_ERR_UNK
;
88 *out_tracker_handle
= handle
;
91 lttng_process_attr_tracker_handle_destroy(handle
);
95 enum lttng_process_attr_tracker_handle_status
96 lttng_process_attr_tracker_handle_get_tracking_policy(
97 const struct lttng_process_attr_tracker_handle
*tracker
,
98 enum lttng_tracking_policy
*policy
)
101 int reply_ret
, copy_ret
;
102 enum lttng_process_attr_tracker_handle_status status
=
103 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
104 struct lttcomm_session_msg lsm
= {
105 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
,
112 if (!tracker
|| !policy
) {
113 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
117 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
118 sizeof(lsm
.session
.name
));
120 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
124 lsm
.domain
.type
= tracker
->domain
;
125 lsm
.u
.process_attr_tracker_get_tracking_policy
.process_attr
=
126 (int32_t) tracker
->process_attr
;
128 /* Command returns a session descriptor on success. */
129 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
130 &lsm
, NULL
, 0, &reply
);
131 if (reply_ret
!= sizeof(uint32_t)) {
132 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
||
133 reply_ret
== -LTTNG_ERR_SESS_NOT_FOUND
) {
134 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
136 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
141 *policy
= (enum lttng_tracking_policy
)(*((const uint32_t *) reply
));
147 enum lttng_process_attr_tracker_handle_status
148 lttng_process_attr_tracker_handle_set_tracking_policy(
149 const struct lttng_process_attr_tracker_handle
*tracker
,
150 enum lttng_tracking_policy policy
)
152 int reply_ret
, copy_ret
;
153 enum lttng_process_attr_tracker_handle_status status
=
154 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
155 struct lttcomm_session_msg lsm
= {
156 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
,
164 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
168 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
169 sizeof(lsm
.session
.name
));
171 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
175 lsm
.domain
.type
= tracker
->domain
;
176 lsm
.u
.process_attr_tracker_set_tracking_policy
.process_attr
=
177 (int32_t) tracker
->process_attr
;
178 lsm
.u
.process_attr_tracker_set_tracking_policy
.tracking_policy
=
181 /* Command returns a session descriptor on success. */
182 reply_ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
184 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
) {
185 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
187 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
195 #define DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(command_upper, \
196 command_lower, process_attr_name, value_type_name, \
197 value_type_c, value_type_enum) \
198 enum lttng_process_attr_tracker_handle_status \
199 lttng_process_attr_##process_attr_name##_tracker_handle_##command_lower##_##value_type_name( \
200 const struct lttng_process_attr_tracker_handle \
202 value_type_c value) \
205 enum lttng_process_attr_tracker_handle_status status = \
206 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK; \
207 struct lttcomm_session_msg lsm = { \
208 .cmd_type = LTTNG_PROCESS_ATTR_TRACKER_##command_upper##_INCLUDE_VALUE, \
216 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
220 ret = lttng_strncpy(lsm.session.name, tracker->session_name, \
221 sizeof(lsm.session.name)); \
223 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
227 lsm.domain.type = tracker->domain; \
228 lsm.u.process_attr_tracker_add_remove_include_value \
230 (int32_t) tracker->process_attr; \
231 lsm.u.process_attr_tracker_add_remove_include_value \
232 .value_type = (uint32_t) \
233 LTTNG_PROCESS_ATTR_VALUE_TYPE_##value_type_enum; \
235 if (std::is_signed<value_type_c>::value) { \
236 lsm.u.process_attr_tracker_add_remove_include_value \
237 .integral_value.u._signed = value; \
239 lsm.u.process_attr_tracker_add_remove_include_value \
240 .integral_value.u._unsigned = value; \
243 ret = lttng_ctl_ask_sessiond(&lsm, NULL); \
246 case LTTNG_ERR_PROCESS_ATTR_EXISTS: \
247 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS; \
249 case LTTNG_ERR_PROCESS_ATTR_MISSING: \
250 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING; \
252 case LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY: \
253 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY; \
256 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR; \
263 #define DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(command_upper, \
264 command_lower, process_attr_name, value_type_name, \
266 enum lttng_process_attr_tracker_handle_status \
267 lttng_process_attr_##process_attr_name##_tracker_handle_##command_lower##_##value_type_name( \
268 const struct lttng_process_attr_tracker_handle \
273 enum lttng_process_attr_tracker_handle_status status = \
274 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK; \
275 struct lttcomm_session_msg lsm = { \
276 .cmd_type = LTTNG_PROCESS_ATTR_TRACKER_##command_upper##_INCLUDE_VALUE, \
282 const size_t len = value ? strlen(value) + 1 : 0; \
284 if (!tracker || !value) { \
285 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
289 ret = lttng_strncpy(lsm.session.name, tracker->session_name, \
290 sizeof(lsm.session.name)); \
292 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
296 lsm.domain.type = tracker->domain; \
297 lsm.u.process_attr_tracker_add_remove_include_value \
299 (int32_t) tracker->process_attr; \
300 lsm.u.process_attr_tracker_add_remove_include_value.name_len = \
302 lsm.u.process_attr_tracker_add_remove_include_value \
303 .value_type = (uint32_t) \
304 LTTNG_PROCESS_ATTR_VALUE_TYPE_##value_type_enum; \
306 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( \
307 &lsm, value, len, NULL); \
310 case LTTNG_ERR_PROCESS_ATTR_EXISTS: \
311 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS; \
313 case LTTNG_ERR_PROCESS_ATTR_MISSING: \
314 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING; \
316 case LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY: \
317 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY; \
319 case LTTNG_ERR_USER_NOT_FOUND: \
320 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_USER_NOT_FOUND; \
322 case LTTNG_ERR_GROUP_NOT_FOUND: \
323 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_GROUP_NOT_FOUND; \
326 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR; \
334 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
335 ADD
, add
, process_id
, pid
, pid_t
, PID
);
336 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
337 REMOVE
, remove
, process_id
, pid
, pid_t
, PID
);
340 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
341 ADD
, add
, virtual_process_id
, pid
, pid_t
, PID
);
342 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
343 REMOVE
, remove
, virtual_process_id
, pid
, pid_t
, PID
);
346 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
347 ADD
, add
, user_id
, uid
, uid_t
, UID
);
348 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
349 REMOVE
, remove
, user_id
, uid
, uid_t
, UID
);
350 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
351 ADD
, add
, user_id
, user_name
, USER_NAME
);
352 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
353 REMOVE
, remove
, user_id
, user_name
, USER_NAME
);
356 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
357 ADD
, add
, virtual_user_id
, uid
, uid_t
, UID
);
358 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
359 REMOVE
, remove
, virtual_user_id
, uid
, uid_t
, UID
);
360 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
361 ADD
, add
, virtual_user_id
, user_name
, USER_NAME
);
362 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
363 REMOVE
, remove
, virtual_user_id
, user_name
, USER_NAME
);
366 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
367 ADD
, add
, group_id
, gid
, gid_t
, GID
);
368 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
369 REMOVE
, remove
, group_id
, gid
, gid_t
, GID
);
370 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
371 ADD
, add
, group_id
, group_name
, GROUP_NAME
);
372 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
373 REMOVE
, remove
, group_id
, group_name
, GROUP_NAME
);
376 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
377 ADD
, add
, virtual_group_id
, gid
, gid_t
, GID
);
378 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
379 REMOVE
, remove
, virtual_group_id
, gid
, gid_t
, GID
);
380 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
381 ADD
, add
, virtual_group_id
, group_name
, GROUP_NAME
);
382 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
383 REMOVE
, remove
, virtual_group_id
, group_name
, GROUP_NAME
);
385 enum lttng_process_attr_tracker_handle_status
386 lttng_process_attr_tracker_handle_get_inclusion_set(
387 struct lttng_process_attr_tracker_handle
*tracker
,
388 const struct lttng_process_attr_values
**values
)
391 int reply_ret
, copy_ret
;
392 enum lttng_process_attr_tracker_handle_status status
=
393 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
394 struct lttcomm_session_msg lsm
= {
395 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
,
401 struct lttng_buffer_view inclusion_set_view
;
402 ssize_t inclusion_set_ret
;
404 if (!tracker
|| !values
) {
405 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
409 lttng_process_attr_values_destroy(tracker
->inclusion_set
);
410 tracker
->inclusion_set
= NULL
;
412 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
413 sizeof(lsm
.session
.name
));
415 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
419 lsm
.domain
.type
= tracker
->domain
;
420 lsm
.u
.process_attr_tracker_get_tracking_policy
.process_attr
=
421 (int32_t) tracker
->process_attr
;
423 /* Command returns a session descriptor on success. */
424 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
425 &lsm
, NULL
, 0, (void **) &reply
);
427 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
) {
428 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
429 } else if (reply_ret
==
430 -LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
) {
431 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
;
433 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
436 } else if (reply_ret
== 0) {
437 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
441 inclusion_set_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
442 if (!inclusion_set_view
.data
) {
443 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
447 inclusion_set_ret
= lttng_process_attr_values_create_from_buffer(
448 tracker
->domain
, tracker
->process_attr
,
449 &inclusion_set_view
, &tracker
->inclusion_set
);
450 if (inclusion_set_ret
< 0) {
451 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
454 *values
= tracker
->inclusion_set
;
460 enum lttng_process_attr_values_status
lttng_process_attr_values_get_count(
461 const struct lttng_process_attr_values
*values
,
464 enum lttng_process_attr_values_status status
=
465 LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
;
467 if (!values
|| !count
) {
468 status
= LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID
;
472 *count
= _lttng_process_attr_values_get_count(values
);
477 enum lttng_process_attr_value_type
lttng_process_attr_values_get_type_at_index(
478 const struct lttng_process_attr_values
*values
,
481 enum lttng_process_attr_value_type type
;
482 const struct process_attr_value
*value
;
485 type
= LTTNG_PROCESS_ATTR_VALUE_TYPE_INVALID
;
489 if (_lttng_process_attr_values_get_count(values
) <= index
) {
490 type
= LTTNG_PROCESS_ATTR_VALUE_TYPE_INVALID
;
494 value
= lttng_process_attr_tracker_values_get_at_index(values
, index
);
500 #define DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER( \
501 value_type_name, value_type, expected_value_type) \
502 enum lttng_process_attr_values_status \
503 lttng_process_attr_values_get_##value_type_name##_at_index( \
504 const struct lttng_process_attr_values \
506 unsigned int index, \
507 value_type *out_value) \
509 enum lttng_process_attr_values_status status = \
510 LTTNG_PROCESS_ATTR_VALUES_STATUS_OK; \
511 const struct process_attr_value *value; \
514 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID; \
518 if (_lttng_process_attr_values_get_count(values) <= index) { \
519 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID; \
523 value = lttng_process_attr_tracker_values_get_at_index( \
526 LTTNG_PROCESS_ATTR_VALUE_TYPE_##expected_value_type) { \
527 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID_TYPE; \
530 *out_value = value->value.value_type_name; \
535 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(pid
, pid_t
, PID
);
536 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(uid
, uid_t
, UID
);
537 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(gid
, gid_t
, GID
);
538 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(user_name
, const char *, USER_NAME
);
539 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(group_name
, const char *, GROUP_NAME
);
541 static enum lttng_error_code
handle_status_to_error_code(
542 enum lttng_process_attr_tracker_handle_status handle_status
)
544 switch (handle_status
) {
545 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
:
546 return LTTNG_ERR_INVALID
;
547 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
:
548 return LTTNG_ERR_SESSION_NOT_EXIST
;
549 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
:
550 return LTTNG_ERR_INVALID_PROTOCOL
;
551 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS
:
552 return LTTNG_ERR_PID_TRACKED
;
553 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING
:
554 return LTTNG_ERR_PID_NOT_TRACKED
;
555 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
:
557 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
:
560 return LTTNG_ERR_UNK
;
565 * Add PID to session tracker.
566 * Return 0 on success else a negative LTTng error code.
568 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
570 enum lttng_error_code ret_code
;
571 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
572 enum lttng_process_attr_tracker_handle_status handle_status
;
573 enum lttng_tracking_policy policy
;
574 enum lttng_process_attr process_attr
;
577 ret_code
= LTTNG_ERR_INVALID
;
581 process_attr
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
582 LTTNG_PROCESS_ATTR_PROCESS_ID
:
583 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
;
585 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
587 process_attr
, &tracker_handle
);
588 if (ret_code
!= LTTNG_OK
) {
593 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
595 LTTNG_TRACKING_POLICY_INCLUDE_ALL
);
596 ret_code
= handle_status_to_error_code(handle_status
);
600 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
601 tracker_handle
, &policy
);
602 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
603 ret_code
= handle_status_to_error_code(handle_status
);
607 if (policy
!= LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
608 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
610 LTTNG_TRACKING_POLICY_INCLUDE_SET
);
611 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
612 ret_code
= handle_status_to_error_code(handle_status
);
617 handle_status
= process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
?
618 lttng_process_attr_process_id_tracker_handle_add_pid(
621 lttng_process_attr_virtual_process_id_tracker_handle_add_pid(
624 ret_code
= handle_status_to_error_code(handle_status
);
626 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
627 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
631 * Remove PID from session tracker.
632 * Return 0 on success else a negative LTTng error code.
634 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
636 enum lttng_error_code ret_code
;
637 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
638 enum lttng_process_attr_tracker_handle_status handle_status
;
639 enum lttng_tracking_policy policy
;
640 enum lttng_process_attr process_attr
;
643 ret_code
= LTTNG_ERR_INVALID
;
647 process_attr
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
648 LTTNG_PROCESS_ATTR_PROCESS_ID
:
649 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
;
650 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
651 handle
->domain
.type
, process_attr
, &tracker_handle
);
652 if (ret_code
!= LTTNG_OK
) {
657 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
659 LTTNG_TRACKING_POLICY_EXCLUDE_ALL
);
660 ret_code
= handle_status_to_error_code(handle_status
);
664 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
665 tracker_handle
, &policy
);
666 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
667 ret_code
= handle_status_to_error_code(handle_status
);
671 if (policy
== LTTNG_TRACKING_POLICY_EXCLUDE_ALL
) {
672 ret_code
= LTTNG_ERR_PID_NOT_TRACKED
;
674 } else if (policy
== LTTNG_TRACKING_POLICY_INCLUDE_ALL
) {
675 ret_code
= LTTNG_ERR_INVALID
;
679 handle_status
= process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
?
680 lttng_process_attr_process_id_tracker_handle_remove_pid(
683 lttng_process_attr_virtual_process_id_tracker_handle_remove_pid(
686 if (handle_status
== LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
) {
687 ret_code
= LTTNG_ERR_PID_NOT_TRACKED
;
690 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
691 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
695 * List PIDs in the tracker.
697 * enabled is set to whether the PID tracker is enabled.
698 * pids is set to an allocated array of PIDs currently tracked. On
699 * success, pids must be freed by the caller.
700 * nr_pids is set to the number of entries contained by the pids array.
702 * Returns 0 on success, else a negative LTTng error code.
704 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
709 enum lttng_error_code ret_code
;
710 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
711 enum lttng_process_attr_tracker_handle_status handle_status
;
712 const struct lttng_process_attr_values
*values
;
713 enum lttng_tracking_policy policy
;
714 unsigned int pid_count
, i
;
715 int32_t *pid_array
= NULL
;
717 if (!handle
|| !_enabled
|| !_pids
|| !_nr_pids
) {
718 ret_code
= LTTNG_ERR_INVALID
;
722 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
724 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, &tracker_handle
);
725 if (ret_code
!= LTTNG_OK
) {
730 handle_status
= lttng_process_attr_tracker_handle_get_inclusion_set(
731 tracker_handle
, &values
);
733 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
734 policy
= LTTNG_TRACKING_POLICY_INCLUDE_SET
;
736 } else if (handle_status
!=
737 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
) {
738 ret_code
= handle_status_to_error_code(handle_status
);
742 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
743 tracker_handle
, &policy
);
745 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
746 ret_code
= handle_status_to_error_code(handle_status
);
750 /* Tracking policy changed in the meantime, retry. */
751 if (policy
== LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
758 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
761 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
765 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
767 const enum lttng_process_attr_values_status values_status
=
768 lttng_process_attr_values_get_count(
771 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
772 ret_code
= LTTNG_ERR_UNK
;
778 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
782 pid_array
= calloc
<int32_t>(pid_count
);
784 ret_code
= LTTNG_ERR_NOMEM
;
788 /* Extract values to a raw array. */
789 for (i
= 0; i
< pid_count
; i
++) {
791 const enum lttng_process_attr_values_status values_status
=
792 lttng_process_attr_values_get_pid_at_index(
795 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
796 ret_code
= LTTNG_ERR_UNK
;
799 pid_array
[i
] = (int32_t) pid
;
801 *_nr_pids
= (size_t) pid_count
;
805 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
807 return ret_code
== LTTNG_OK
? 0 : -ret_code
;