2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "lttng-ctl-helper.h"
9 #include "lttng/domain.h"
10 #include "lttng/lttng-error.h"
11 #include <common/sessiond-comm/sessiond-comm.h>
12 #include <common/tracker.h>
13 #include <lttng/tracker.h>
15 struct lttng_process_attr_tracker_handle
{
17 enum lttng_domain_type domain
;
18 enum lttng_process_attr process_attr
;
19 struct lttng_process_attr_values
*inclusion_set
;
22 void lttng_process_attr_tracker_handle_destroy(
23 struct lttng_process_attr_tracker_handle
*tracker
)
29 lttng_process_attr_values_destroy(tracker
->inclusion_set
);
30 free(tracker
->session_name
);
34 enum lttng_error_code
lttng_session_get_tracker_handle(const char *session_name
,
35 enum lttng_domain_type domain
,
36 enum lttng_process_attr process_attr
,
37 struct lttng_process_attr_tracker_handle
**out_tracker_handle
)
39 enum lttng_error_code ret_code
= LTTNG_OK
;
40 struct lttng_process_attr_tracker_handle
*handle
= NULL
;
41 enum lttng_process_attr_tracker_handle_status status
;
42 enum lttng_tracking_policy policy
;
44 if (!session_name
|| !out_tracker_handle
) {
45 ret_code
= LTTNG_ERR_INVALID
;
49 if (domain
!= LTTNG_DOMAIN_KERNEL
&& domain
!= LTTNG_DOMAIN_UST
) {
50 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
54 handle
= zmalloc(sizeof(*handle
));
56 ret_code
= LTTNG_ERR_NOMEM
;
60 handle
->session_name
= strdup(session_name
);
61 if (!handle
->session_name
) {
62 ret_code
= LTTNG_ERR_NOMEM
;
66 handle
->domain
= domain
;
67 handle
->process_attr
= process_attr
;
70 * Use the `get_tracking_policy` command to validate the tracker's
73 status
= lttng_process_attr_tracker_handle_get_tracking_policy(
76 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
:
78 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
:
79 ret_code
= LTTNG_ERR_SESSION_NOT_EXIST
;
82 ret_code
= LTTNG_ERR_UNK
;
86 *out_tracker_handle
= handle
;
89 lttng_process_attr_tracker_handle_destroy(handle
);
93 enum lttng_process_attr_tracker_handle_status
94 lttng_process_attr_tracker_handle_get_tracking_policy(
95 const struct lttng_process_attr_tracker_handle
*tracker
,
96 enum lttng_tracking_policy
*policy
)
99 int reply_ret
, copy_ret
;
100 enum lttng_process_attr_tracker_handle_status status
=
101 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
102 struct lttcomm_session_msg lsm
= {
103 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
,
106 if (!tracker
|| !policy
) {
107 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
111 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
112 sizeof(lsm
.session
.name
));
114 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
118 lsm
.domain
.type
= tracker
->domain
;
119 lsm
.u
.process_attr_tracker_get_tracking_policy
.process_attr
=
120 (int32_t) tracker
->process_attr
;
122 /* Command returns a session descriptor on success. */
123 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
124 &lsm
, NULL
, 0, &reply
);
125 if (reply_ret
!= sizeof(uint32_t)) {
126 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
||
127 reply_ret
== -LTTNG_ERR_SESS_NOT_FOUND
) {
128 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
130 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
135 *policy
= (enum lttng_tracking_policy
)(*((const uint32_t *) reply
));
141 enum lttng_process_attr_tracker_handle_status
142 lttng_process_attr_tracker_handle_set_tracking_policy(
143 const struct lttng_process_attr_tracker_handle
*tracker
,
144 enum lttng_tracking_policy policy
)
146 int reply_ret
, copy_ret
;
147 enum lttng_process_attr_tracker_handle_status status
=
148 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
149 struct lttcomm_session_msg lsm
= {
150 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
,
154 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
158 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
159 sizeof(lsm
.session
.name
));
161 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
165 lsm
.domain
.type
= tracker
->domain
;
166 lsm
.u
.process_attr_tracker_set_tracking_policy
.process_attr
=
167 (int32_t) tracker
->process_attr
;
168 lsm
.u
.process_attr_tracker_set_tracking_policy
.tracking_policy
=
171 /* Command returns a session descriptor on success. */
172 reply_ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
174 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
) {
175 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
177 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
185 #define DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(command_upper, \
186 command_lower, process_attr_name, value_type_name, \
187 value_type_c, value_type_enum) \
188 enum lttng_process_attr_tracker_handle_status \
189 lttng_process_attr_##process_attr_name##_tracker_handle_##command_lower##_##value_type_name( \
190 const struct lttng_process_attr_tracker_handle \
192 value_type_c value) \
195 enum lttng_process_attr_tracker_handle_status status = \
196 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK; \
197 struct lttcomm_session_msg lsm = { \
198 .cmd_type = LTTNG_PROCESS_ATTR_TRACKER_##command_upper##_INCLUDE_VALUE}; \
201 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
205 ret = lttng_strncpy(lsm.session.name, tracker->session_name, \
206 sizeof(lsm.session.name)); \
208 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
212 lsm.domain.type = tracker->domain; \
213 lsm.u.process_attr_tracker_add_remove_include_value \
215 (int32_t) tracker->process_attr; \
216 lsm.u.process_attr_tracker_add_remove_include_value \
217 .value_type = (uint32_t) \
218 LTTNG_PROCESS_ATTR_VALUE_TYPE_##value_type_enum; \
220 if (is_signed(value_type_c)) { \
221 lsm.u.process_attr_tracker_add_remove_include_value \
222 .integral_value.u._signed = value; \
224 lsm.u.process_attr_tracker_add_remove_include_value \
225 .integral_value.u._unsigned = value; \
228 ret = lttng_ctl_ask_sessiond(&lsm, NULL); \
231 case LTTNG_ERR_PROCESS_ATTR_EXISTS: \
232 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS; \
234 case LTTNG_ERR_PROCESS_ATTR_MISSING: \
235 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING; \
237 case LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY: \
238 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY; \
241 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR; \
248 #define DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(command_upper, \
249 command_lower, process_attr_name, value_type_name, \
251 enum lttng_process_attr_tracker_handle_status \
252 lttng_process_attr_##process_attr_name##_tracker_handle_##command_lower##_##value_type_name( \
253 const struct lttng_process_attr_tracker_handle \
258 enum lttng_process_attr_tracker_handle_status status = \
259 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK; \
260 struct lttcomm_session_msg lsm = { \
261 .cmd_type = LTTNG_PROCESS_ATTR_TRACKER_##command_upper##_INCLUDE_VALUE}; \
262 const size_t len = value ? strlen(value) + 1 : 0; \
264 if (!tracker || !value) { \
265 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
269 ret = lttng_strncpy(lsm.session.name, tracker->session_name, \
270 sizeof(lsm.session.name)); \
272 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID; \
276 lsm.domain.type = tracker->domain; \
277 lsm.u.process_attr_tracker_add_remove_include_value \
279 (int32_t) tracker->process_attr; \
280 lsm.u.process_attr_tracker_add_remove_include_value.name_len = \
282 lsm.u.process_attr_tracker_add_remove_include_value \
283 .value_type = (uint32_t) \
284 LTTNG_PROCESS_ATTR_VALUE_TYPE_##value_type_enum; \
286 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( \
287 &lsm, value, len, NULL); \
290 case LTTNG_ERR_PROCESS_ATTR_EXISTS: \
291 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS; \
293 case LTTNG_ERR_PROCESS_ATTR_MISSING: \
294 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING; \
296 case LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY: \
297 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY; \
299 case LTTNG_ERR_USER_NOT_FOUND: \
300 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_USER_NOT_FOUND; \
302 case LTTNG_ERR_GROUP_NOT_FOUND: \
303 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_GROUP_NOT_FOUND; \
306 status = LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR; \
314 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
315 ADD
, add
, process_id
, pid
, pid_t
, PID
);
316 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
317 REMOVE
, remove
, process_id
, pid
, pid_t
, PID
);
320 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
321 ADD
, add
, virtual_process_id
, pid
, pid_t
, PID
);
322 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
323 REMOVE
, remove
, virtual_process_id
, pid
, pid_t
, PID
);
326 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
327 ADD
, add
, user_id
, uid
, uid_t
, UID
);
328 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
329 REMOVE
, remove
, user_id
, uid
, uid_t
, UID
);
330 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
331 ADD
, add
, user_id
, user_name
, USER_NAME
);
332 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
333 REMOVE
, remove
, user_id
, user_name
, USER_NAME
);
336 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
337 ADD
, add
, virtual_user_id
, uid
, uid_t
, UID
);
338 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
339 REMOVE
, remove
, virtual_user_id
, uid
, uid_t
, UID
);
340 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
341 ADD
, add
, virtual_user_id
, user_name
, USER_NAME
);
342 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
343 REMOVE
, remove
, virtual_user_id
, user_name
, USER_NAME
);
346 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
347 ADD
, add
, group_id
, gid
, gid_t
, GID
);
348 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
349 REMOVE
, remove
, group_id
, gid
, gid_t
, GID
);
350 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
351 ADD
, add
, group_id
, group_name
, GROUP_NAME
);
352 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
353 REMOVE
, remove
, group_id
, group_name
, GROUP_NAME
);
356 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
357 ADD
, add
, virtual_group_id
, gid
, gid_t
, GID
);
358 DEFINE_TRACKER_ADD_REMOVE_INTEGRAL_VALUE_FUNC(
359 REMOVE
, remove
, virtual_group_id
, gid
, gid_t
, GID
);
360 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
361 ADD
, add
, virtual_group_id
, group_name
, GROUP_NAME
);
362 DEFINE_TRACKER_ADD_REMOVE_STRING_VALUE_FUNC(
363 REMOVE
, remove
, virtual_group_id
, group_name
, GROUP_NAME
);
365 enum lttng_process_attr_tracker_handle_status
366 lttng_process_attr_tracker_handle_get_inclusion_set(
367 struct lttng_process_attr_tracker_handle
*tracker
,
368 const struct lttng_process_attr_values
**values
)
371 int reply_ret
, copy_ret
;
372 enum lttng_process_attr_tracker_handle_status status
=
373 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
;
374 struct lttcomm_session_msg lsm
= {
375 .cmd_type
= LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
,
377 struct lttng_buffer_view inclusion_set_view
;
378 ssize_t inclusion_set_ret
;
380 if (!tracker
|| !values
) {
381 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
385 lttng_process_attr_values_destroy(tracker
->inclusion_set
);
386 tracker
->inclusion_set
= NULL
;
388 copy_ret
= lttng_strncpy(lsm
.session
.name
, tracker
->session_name
,
389 sizeof(lsm
.session
.name
));
391 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID
;
395 lsm
.domain
.type
= tracker
->domain
;
396 lsm
.u
.process_attr_tracker_get_tracking_policy
.process_attr
=
397 (int32_t) tracker
->process_attr
;
399 /* Command returns a session descriptor on success. */
400 reply_ret
= lttng_ctl_ask_sessiond_varlen_no_cmd_header(
401 &lsm
, NULL
, 0, &reply
);
403 if (reply_ret
== -LTTNG_ERR_SESSION_NOT_EXIST
) {
404 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
;
405 } else if (reply_ret
==
406 -LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
) {
407 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
;
409 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
;
412 } else if (reply_ret
== 0) {
413 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
417 inclusion_set_view
= lttng_buffer_view_init(reply
, 0, reply_ret
);
418 if (!inclusion_set_view
.data
) {
419 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
423 inclusion_set_ret
= lttng_process_attr_values_create_from_buffer(
424 tracker
->domain
, tracker
->process_attr
,
425 &inclusion_set_view
, &tracker
->inclusion_set
);
426 if (inclusion_set_ret
< 0) {
427 status
= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
;
430 *values
= tracker
->inclusion_set
;
436 enum lttng_process_attr_values_status
lttng_process_attr_values_get_count(
437 const struct lttng_process_attr_values
*values
,
440 enum lttng_process_attr_values_status status
=
441 LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
;
443 if (!values
|| !count
) {
444 status
= LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID
;
448 *count
= _lttng_process_attr_values_get_count(values
);
453 enum lttng_process_attr_value_type
lttng_process_attr_values_get_type_at_index(
454 const struct lttng_process_attr_values
*values
,
457 enum lttng_process_attr_value_type type
;
458 const struct process_attr_value
*value
;
461 type
= LTTNG_PROCESS_ATTR_VALUE_TYPE_INVALID
;
465 if (_lttng_process_attr_values_get_count(values
) <= index
) {
466 type
= LTTNG_PROCESS_ATTR_VALUE_TYPE_INVALID
;
470 value
= lttng_process_attr_tracker_values_get_at_index(values
, index
);
476 #define DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER( \
477 value_type_name, value_type, expected_value_type) \
478 enum lttng_process_attr_values_status \
479 lttng_process_attr_values_get_##value_type_name##_at_index( \
480 const struct lttng_process_attr_values \
482 unsigned int index, \
483 value_type *out_value) \
485 enum lttng_process_attr_values_status status = \
486 LTTNG_PROCESS_ATTR_VALUES_STATUS_OK; \
487 const struct process_attr_value *value; \
490 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID; \
494 if (_lttng_process_attr_values_get_count(values) <= index) { \
495 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID; \
499 value = lttng_process_attr_tracker_values_get_at_index( \
502 LTTNG_PROCESS_ATTR_VALUE_TYPE_##expected_value_type) { \
503 status = LTTNG_PROCESS_ATTR_VALUES_STATUS_INVALID_TYPE; \
506 *out_value = value->value.value_type_name; \
511 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(pid
, pid_t
, PID
);
512 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(uid
, uid_t
, UID
);
513 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(gid
, gid_t
, GID
);
514 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(user_name
, const char *, USER_NAME
);
515 DEFINE_LTTNG_PROCESS_ATTR_VALUES_GETTER(group_name
, const char *, GROUP_NAME
);
517 static enum lttng_error_code
handle_status_to_error_code(
518 enum lttng_process_attr_tracker_handle_status handle_status
)
520 switch (handle_status
) {
521 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
:
522 return LTTNG_ERR_INVALID
;
523 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST
:
524 return LTTNG_ERR_SESSION_NOT_EXIST
;
525 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR
:
526 return LTTNG_ERR_INVALID_PROTOCOL
;
527 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS
:
528 return LTTNG_ERR_PID_TRACKED
;
529 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING
:
530 return LTTNG_ERR_PID_NOT_TRACKED
;
531 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
:
533 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR
:
536 return LTTNG_ERR_UNK
;
541 * Add PID to session tracker.
542 * Return 0 on success else a negative LTTng error code.
544 int lttng_track_pid(struct lttng_handle
*handle
, int pid
)
546 enum lttng_error_code ret_code
;
547 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
548 enum lttng_process_attr_tracker_handle_status handle_status
;
549 enum lttng_tracking_policy policy
;
550 enum lttng_process_attr process_attr
;
553 ret_code
= LTTNG_ERR_INVALID
;
557 process_attr
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
558 LTTNG_PROCESS_ATTR_PROCESS_ID
:
559 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
;
561 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
563 process_attr
, &tracker_handle
);
564 if (ret_code
!= LTTNG_OK
) {
569 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
571 LTTNG_TRACKING_POLICY_INCLUDE_ALL
);
572 ret_code
= handle_status_to_error_code(handle_status
);
576 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
577 tracker_handle
, &policy
);
578 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
579 ret_code
= handle_status_to_error_code(handle_status
);
583 if (policy
!= LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
584 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
586 LTTNG_TRACKING_POLICY_INCLUDE_SET
);
587 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
588 ret_code
= handle_status_to_error_code(handle_status
);
593 handle_status
= process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
?
594 lttng_process_attr_process_id_tracker_handle_add_pid(
597 lttng_process_attr_virtual_process_id_tracker_handle_add_pid(
600 ret_code
= handle_status_to_error_code(handle_status
);
602 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
603 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
607 * Remove PID from session tracker.
608 * Return 0 on success else a negative LTTng error code.
610 int lttng_untrack_pid(struct lttng_handle
*handle
, int pid
)
612 enum lttng_error_code ret_code
;
613 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
614 enum lttng_process_attr_tracker_handle_status handle_status
;
615 enum lttng_tracking_policy policy
;
616 enum lttng_process_attr process_attr
;
619 ret_code
= LTTNG_ERR_INVALID
;
623 process_attr
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
624 LTTNG_PROCESS_ATTR_PROCESS_ID
:
625 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
;
626 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
627 handle
->domain
.type
, process_attr
, &tracker_handle
);
628 if (ret_code
!= LTTNG_OK
) {
633 handle_status
= lttng_process_attr_tracker_handle_set_tracking_policy(
635 LTTNG_TRACKING_POLICY_EXCLUDE_ALL
);
636 ret_code
= handle_status_to_error_code(handle_status
);
640 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
641 tracker_handle
, &policy
);
642 if (handle_status
!= LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
643 ret_code
= handle_status_to_error_code(handle_status
);
647 if (policy
== LTTNG_TRACKING_POLICY_EXCLUDE_ALL
) {
648 ret_code
= LTTNG_ERR_PID_NOT_TRACKED
;
650 } else if (policy
== LTTNG_TRACKING_POLICY_INCLUDE_ALL
) {
651 ret_code
= LTTNG_ERR_INVALID
;
655 handle_status
= process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
?
656 lttng_process_attr_process_id_tracker_handle_remove_pid(
659 lttng_process_attr_virtual_process_id_tracker_handle_remove_pid(
662 if (handle_status
== LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
) {
663 ret_code
= LTTNG_ERR_PID_NOT_TRACKED
;
666 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
667 return ret_code
== LTTNG_OK
? 0 : -ret_code
;
671 * List PIDs in the tracker.
673 * enabled is set to whether the PID tracker is enabled.
674 * pids is set to an allocated array of PIDs currently tracked. On
675 * success, pids must be freed by the caller.
676 * nr_pids is set to the number of entries contained by the pids array.
678 * Returns 0 on success, else a negative LTTng error code.
680 int lttng_list_tracker_pids(struct lttng_handle
*handle
,
685 enum lttng_error_code ret_code
;
686 struct lttng_process_attr_tracker_handle
*tracker_handle
= NULL
;
687 enum lttng_process_attr_tracker_handle_status handle_status
;
688 const struct lttng_process_attr_values
*values
;
689 enum lttng_tracking_policy policy
;
690 unsigned int pid_count
, i
;
691 int32_t *pid_array
= NULL
;
693 if (!handle
|| !_enabled
|| !_pids
|| !_nr_pids
) {
694 ret_code
= LTTNG_ERR_INVALID
;
698 ret_code
= lttng_session_get_tracker_handle(handle
->session_name
,
700 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
, &tracker_handle
);
701 if (ret_code
!= LTTNG_OK
) {
706 handle_status
= lttng_process_attr_tracker_handle_get_inclusion_set(
707 tracker_handle
, &values
);
709 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
710 policy
= LTTNG_TRACKING_POLICY_INCLUDE_SET
;
712 } else if (handle_status
!=
713 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY
) {
714 ret_code
= handle_status_to_error_code(handle_status
);
718 handle_status
= lttng_process_attr_tracker_handle_get_tracking_policy(
719 tracker_handle
, &policy
);
721 LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK
) {
722 ret_code
= handle_status_to_error_code(handle_status
);
726 /* Tracking policy changed in the meantime, retry. */
727 if (policy
== LTTNG_TRACKING_POLICY_INCLUDE_SET
) {
734 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
737 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
741 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
743 const enum lttng_process_attr_values_status values_status
=
744 lttng_process_attr_values_get_count(
747 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
748 ret_code
= LTTNG_ERR_UNK
;
754 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
758 pid_array
= zmalloc(pid_count
* sizeof(int32_t));
760 ret_code
= LTTNG_ERR_NOMEM
;
764 /* Extract values to a raw array. */
765 for (i
= 0; i
< pid_count
; i
++) {
767 const enum lttng_process_attr_values_status values_status
=
768 lttng_process_attr_values_get_pid_at_index(
771 if (values_status
!= LTTNG_PROCESS_ATTR_VALUES_STATUS_OK
) {
772 ret_code
= LTTNG_ERR_UNK
;
775 pid_array
[i
] = (int32_t) pid
;
777 *_nr_pids
= (size_t) pid_count
;
781 lttng_process_attr_tracker_handle_destroy(tracker_handle
);
783 return ret_code
== LTTNG_OK
? 0 : -ret_code
;