2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "common/error.hpp"
12 #include "ust-app.hpp"
15 #include <common/common.hpp>
16 #include <common/compat/endian.hpp>
17 #include <common/sessiond-comm/agent.hpp>
19 #include <lttng/condition/condition.h>
20 #include <lttng/condition/event-rule-matches.h>
21 #include <lttng/domain-internal.hpp>
22 #include <lttng/event-rule/event-rule-internal.hpp>
23 #include <lttng/event-rule/event-rule.h>
24 #include <lttng/event-rule/jul-logging.h>
25 #include <lttng/event-rule/log4j-logging.h>
26 #include <lttng/event-rule/python-logging.h>
27 #include <lttng/log-level-rule-internal.hpp>
29 #include <urcu/rculist.h>
30 #include <urcu/uatomic.h>
32 using event_rule_logging_get_name_pattern
=
33 enum lttng_event_rule_status (*)(const struct lttng_event_rule
*, const char **);
34 using event_rule_logging_get_log_level_rule
= enum lttng_event_rule_status (*)(
35 const struct lttng_event_rule
*, const struct lttng_log_level_rule
**);
38 * Agent application context representation.
41 struct agent_app_ctx
{
45 /* agent_app_ctx are part of the agent app_ctx_list. */
46 struct cds_list_head list_node
;
48 /* For call_rcu teardown. */
49 struct rcu_head rcu_node
;
54 * Human readable agent return code.
56 static const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code
)
59 case AGENT_RET_CODE_SUCCESS
:
61 case AGENT_RET_CODE_INVALID
:
62 return "Invalid command";
63 case AGENT_RET_CODE_UNKNOWN_NAME
:
64 return "Unknown logger name";
66 return "Unknown code";
70 static void log_reply_code(uint32_t in_reply_ret_code
)
72 int level
= PRINT_DBG3
;
74 * reply_ret_code and in_reply_ret_code are kept separate to have a
75 * sanitized value (used to retrieve the human readable string) and the
76 * original value which is logged as-is.
78 uint32_t reply_ret_code
= in_reply_ret_code
;
80 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
|| reply_ret_code
>= AGENT_RET_CODE_NR
) {
81 reply_ret_code
= AGENT_RET_CODE_NR
;
86 "Agent replied with retcode: %s (%" PRIu32
")",
87 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code
) reply_ret_code
),
92 * Match function for the events hash table lookup by name.
94 static int ht_match_event_by_name(struct cds_lfht_node
*node
, const void *_key
)
96 struct agent_event
*event
;
97 const struct agent_ht_key
*key
;
102 event
= caa_container_of(node
, struct agent_event
, node
.node
);
103 key
= (agent_ht_key
*) _key
;
105 /* Match 1 elements of the key: name. */
108 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
119 * Match function for the events hash table lookup by name, log level and
122 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
124 struct agent_event
*event
;
125 const struct agent_ht_key
*key
;
131 event
= caa_container_of(node
, struct agent_event
, node
.node
);
132 key
= (agent_ht_key
*) _key
;
134 /* Match 2 elements of the key: name and loglevel. */
137 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
141 /* Event loglevel value and type. */
142 ll_match
= loglevels_match(event
->loglevel_type
,
143 event
->loglevel_value
,
146 LTTNG_EVENT_LOGLEVEL_ALL
);
152 /* Filter expression */
153 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
154 /* One has a filter expression, the other does not */
158 if (event
->filter_expression
) {
159 if (strncmp(event
->filter_expression
,
160 key
->filter_expression
,
161 strlen(event
->filter_expression
)) != 0) {
173 * Add unique agent event based on the event name and loglevel.
175 static void add_unique_agent_event(struct lttng_ht
*ht
, struct agent_event
*event
)
177 struct cds_lfht_node
*node_ptr
;
178 struct agent_ht_key key
;
181 LTTNG_ASSERT(ht
->ht
);
184 key
.name
= event
->name
;
185 key
.loglevel_value
= event
->loglevel_value
;
186 key
.loglevel_type
= event
->loglevel_type
;
187 key
.filter_expression
= event
->filter_expression
;
189 node_ptr
= cds_lfht_add_unique(ht
->ht
,
190 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
194 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
198 * URCU delayed agent event reclaim.
200 static void destroy_event_agent_rcu(struct rcu_head
*head
)
202 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
203 struct agent_event
*event
= lttng::utils::container_of(node
, &agent_event::node
);
205 agent_destroy_event(event
);
209 * URCU delayed agent app reclaim.
211 static void destroy_app_agent_rcu(struct rcu_head
*head
)
213 struct lttng_ht_node_ulong
*node
=
214 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
215 struct agent_app
*app
= lttng::utils::container_of(node
, &agent_app::node
);
221 * Communication with the agent. Send the message header to the given socket in
224 * Return 0 on success or else a negative errno message of sendmsg() op.
227 send_header(struct lttcomm_sock
*sock
, uint64_t data_size
, uint32_t cmd
, uint32_t cmd_version
)
231 struct lttcomm_agent_hdr msg
;
235 memset(&msg
, 0, sizeof(msg
));
236 msg
.data_size
= htobe64(data_size
);
237 msg
.cmd
= htobe32(cmd
);
238 msg
.cmd_version
= htobe32(cmd_version
);
240 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
241 if (size
< sizeof(msg
)) {
252 * Communication call with the agent. Send the payload to the given socket. The
253 * header MUST be sent prior to this call.
255 * Return 0 on success or else a negative errno value of sendmsg() op.
257 static int send_payload(struct lttcomm_sock
*sock
, const void *data
, size_t size
)
265 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
277 * Communication call with the agent. Receive reply from the agent using the
280 * Return 0 on success or else a negative errno value from recvmsg() op.
282 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
290 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
302 * Internal event listing for a given app. Populate events.
304 * Return number of element in the list or else a negative LTTNG_ERR* code.
305 * On success, the caller is responsible for freeing the memory
306 * allocated for "events".
308 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
310 int ret
, i
, len
= 0, offset
= 0;
313 uint32_t reply_ret_code
;
314 struct lttng_event
*tmp_events
= nullptr;
315 struct lttcomm_agent_list_reply
*reply
= nullptr;
316 struct lttcomm_agent_list_reply_hdr reply_hdr
;
319 LTTNG_ASSERT(app
->sock
);
320 LTTNG_ASSERT(events
);
322 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
, app
->sock
->fd
);
324 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
329 /* Get list header so we know how much we'll receive. */
330 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
335 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
336 log_reply_code(reply_ret_code
);
337 switch (reply_ret_code
) {
338 case AGENT_RET_CODE_SUCCESS
:
339 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
346 reply
= zmalloc
<lttcomm_agent_list_reply
>(data_size
);
348 ret
= LTTNG_ERR_NOMEM
;
352 /* Get the list with the appropriate data size. */
353 ret
= recv_reply(app
->sock
, reply
, data_size
);
358 nb_event
= be32toh(reply
->nb_event
);
359 tmp_events
= calloc
<lttng_event
>(nb_event
);
361 ret
= LTTNG_ERR_NOMEM
;
365 for (i
= 0; i
< nb_event
; i
++) {
367 if (lttng_strncpy(tmp_events
[i
].name
,
368 reply
->payload
+ offset
,
369 sizeof(tmp_events
[i
].name
))) {
370 ret
= LTTNG_ERR_INVALID
;
373 tmp_events
[i
].pid
= app
->pid
;
374 tmp_events
[i
].enabled
= -1;
375 len
= strlen(reply
->payload
+ offset
) + 1;
378 *events
= tmp_events
;
384 ret
= LTTNG_ERR_UST_LIST_FAIL
;
392 * Internal enable agent event on a agent application. This function
393 * communicates with the agent to enable a given event.
395 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
397 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
402 size_t filter_expression_length
;
403 uint32_t reply_ret_code
;
404 struct lttcomm_agent_enable_event msg
;
405 struct lttcomm_agent_generic_reply reply
;
408 LTTNG_ASSERT(app
->sock
);
411 DBG2("Agent enabling event %s for app pid: %d and socket %d",
417 * Calculate the payload's size, which is the fixed-size struct followed
418 * by the variable-length filter expression (+1 for the ending \0).
420 if (!event
->filter_expression
) {
421 filter_expression_length
= 0;
423 filter_expression_length
= strlen(event
->filter_expression
) + 1;
425 data_size
= sizeof(msg
) + filter_expression_length
;
427 memset(&msg
, 0, sizeof(msg
));
428 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
429 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
430 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
431 ret
= LTTNG_ERR_INVALID
;
434 msg
.filter_expression_length
= htobe32(filter_expression_length
);
436 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
441 bytes_to_send
= calloc
<char>(data_size
);
442 if (!bytes_to_send
) {
443 ret
= LTTNG_ERR_NOMEM
;
447 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
448 if (filter_expression_length
> 0) {
449 memcpy(bytes_to_send
+ sizeof(msg
),
450 event
->filter_expression
,
451 filter_expression_length
);
454 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
460 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
465 reply_ret_code
= be32toh(reply
.ret_code
);
466 log_reply_code(reply_ret_code
);
467 switch (reply_ret_code
) {
468 case AGENT_RET_CODE_SUCCESS
:
470 case AGENT_RET_CODE_UNKNOWN_NAME
:
471 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
481 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
487 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
489 static int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
494 len_be
= htobe32(len
);
495 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
500 ret
= send_payload(sock
, str
, len
);
509 * Internal enable application context on an agent application. This function
510 * communicates with the agent to enable a given application context.
512 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
514 static int app_context_op(const struct agent_app
*app
,
515 const struct agent_app_ctx
*ctx
,
516 enum lttcomm_agent_command cmd
)
519 uint32_t reply_ret_code
;
520 struct lttcomm_agent_generic_reply reply
;
521 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
524 LTTNG_ASSERT(app
->sock
);
526 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
|| cmd
== AGENT_CMD_APP_CTX_DISABLE
);
528 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
529 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
536 * Calculate the payload's size, which consists of the size (u32, BE)
537 * of the provider name, the NULL-terminated provider name string, the
538 * size (u32, BE) of the context name, followed by the NULL-terminated
539 * context name string.
541 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
542 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
544 sizeof(uint32_t) + app_ctx_provider_name_len
+ sizeof(uint32_t) + app_ctx_name_len
;
546 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
551 if (app_ctx_provider_name_len
> UINT32_MAX
|| app_ctx_name_len
> UINT32_MAX
) {
552 ERR("Application context name > MAX_UINT32");
553 ret
= LTTNG_ERR_INVALID
;
557 ret
= send_pstring(app
->sock
, ctx
->provider_name
, (uint32_t) app_ctx_provider_name_len
);
562 ret
= send_pstring(app
->sock
, ctx
->ctx_name
, (uint32_t) app_ctx_name_len
);
567 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
572 reply_ret_code
= be32toh(reply
.ret_code
);
573 log_reply_code(reply_ret_code
);
574 switch (reply_ret_code
) {
575 case AGENT_RET_CODE_SUCCESS
:
585 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
591 * Internal disable agent event call on a agent application. This function
592 * communicates with the agent to disable a given event.
594 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
596 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
600 uint32_t reply_ret_code
;
601 struct lttcomm_agent_disable_event msg
;
602 struct lttcomm_agent_generic_reply reply
;
605 LTTNG_ASSERT(app
->sock
);
608 DBG2("Agent disabling event %s for app pid: %d and socket %d",
613 data_size
= sizeof(msg
);
614 memset(&msg
, 0, sizeof(msg
));
615 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
616 ret
= LTTNG_ERR_INVALID
;
620 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
625 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
630 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
635 reply_ret_code
= be32toh(reply
.ret_code
);
636 log_reply_code(reply_ret_code
);
637 switch (reply_ret_code
) {
638 case AGENT_RET_CODE_SUCCESS
:
640 case AGENT_RET_CODE_UNKNOWN_NAME
:
641 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
651 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
657 * Send back the registration DONE command to a given agent application.
659 * Return 0 on success or else a negative value.
661 int agent_send_registration_done(struct agent_app
*app
)
664 LTTNG_ASSERT(app
->sock
);
666 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
668 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
672 * Enable agent event on every agent applications registered with the session
675 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
677 int agent_enable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
680 struct agent_app
*app
;
681 struct lttng_ht_iter iter
;
687 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
688 if (app
->domain
!= domain
) {
692 /* Enable event on agent application through TCP socket. */
693 ret
= enable_event(app
, event
);
694 if (ret
!= LTTNG_OK
) {
699 event
->enabled_count
++;
707 static void destroy_app_ctx(struct agent_app_ctx
*ctx
)
709 free(ctx
->provider_name
);
714 static struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
716 struct agent_app_ctx
*agent_ctx
= nullptr;
722 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
723 agent_ctx
= zmalloc
<agent_app_ctx
>();
728 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
729 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
730 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
731 destroy_app_ctx(agent_ctx
);
739 * Enable agent context on every agent applications registered with the session
742 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
744 int agent_enable_context(const struct lttng_event_context
*ctx
, enum lttng_domain_type domain
)
747 struct agent_app
*app
;
748 struct lttng_ht_iter iter
;
751 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
752 ret
= LTTNG_ERR_INVALID
;
758 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
759 struct agent_app_ctx
*agent_ctx
;
761 if (app
->domain
!= domain
) {
765 agent_ctx
= create_app_ctx(ctx
);
767 ret
= LTTNG_ERR_NOMEM
;
771 /* Enable event on agent application through TCP socket. */
772 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
773 destroy_app_ctx(agent_ctx
);
774 if (ret
!= LTTNG_OK
) {
788 * Disable agent event on every agent application registered with the session
791 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
793 int agent_disable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
796 struct agent_app
*app
;
797 struct lttng_ht_iter iter
;
800 if (!AGENT_EVENT_IS_ENABLED(event
)) {
804 if (--event
->enabled_count
!= 0) {
806 * Agent event still enabled. Disable the agent event only when
807 * all "users" have disabled it (event notifiers, event rules,
816 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
817 if (app
->domain
!= domain
) {
821 /* Enable event on agent application through TCP socket. */
822 ret
= disable_event(app
, event
);
823 if (ret
!= LTTNG_OK
) {
828 /* event->enabled_count is now 0. */
829 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event
));
838 * Disable agent context on every agent application registered with the session
841 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
843 static int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
846 struct agent_app
*app
;
847 struct lttng_ht_iter iter
;
852 DBG2("Disabling agent application context %s:%s", ctx
->provider_name
, ctx
->ctx_name
);
853 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
854 if (app
->domain
!= domain
) {
858 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
859 if (ret
!= LTTNG_OK
) {
869 * Ask every agent for the list of possible event. Events is allocated with the
870 * events of every agent application.
872 * Return the number of events or else a negative value.
874 int agent_list_events(struct lttng_event
**events
, enum lttng_domain_type domain
)
877 size_t nbmem
, count
= 0;
878 struct agent_app
*app
;
879 struct lttng_event
*tmp_events
= nullptr;
880 struct lttng_ht_iter iter
;
882 LTTNG_ASSERT(events
);
884 DBG2("Agent listing events for domain %d", domain
);
886 nbmem
= UST_APP_EVENT_LIST_SIZE
;
887 tmp_events
= calloc
<lttng_event
>(nbmem
);
889 PERROR("zmalloc agent list events");
895 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
897 struct lttng_event
*agent_events
;
899 /* Skip domain not asked by the list. */
900 if (app
->domain
!= domain
) {
904 nb_ev
= list_events(app
, &agent_events
);
910 if (count
+ nb_ev
> nbmem
) {
911 /* In case the realloc fails, we free the memory */
912 struct lttng_event
*new_tmp_events
;
915 new_nbmem
= std::max(count
+ nb_ev
, nbmem
<< 1);
916 DBG2("Reallocating agent event list from %zu to %zu entries",
919 new_tmp_events
= (lttng_event
*) realloc(
920 tmp_events
, new_nbmem
* sizeof(*new_tmp_events
));
921 if (!new_tmp_events
) {
922 PERROR("realloc agent events");
927 /* Zero the new memory */
928 memset(new_tmp_events
+ nbmem
,
930 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
932 tmp_events
= new_tmp_events
;
934 memcpy(tmp_events
+ count
, agent_events
, nb_ev
* sizeof(*tmp_events
));
941 *events
= tmp_events
;
952 * Create a agent app object using the given PID.
954 * Return newly allocated object or else NULL on error.
957 agent_create_app(pid_t pid
, enum lttng_domain_type domain
, struct lttcomm_sock
*sock
)
959 struct agent_app
*app
;
963 app
= zmalloc
<agent_app
>();
965 PERROR("Failed to allocate agent application instance");
970 app
->domain
= domain
;
972 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
979 * Lookup agent app by socket in the global hash table.
981 * RCU read side lock MUST be acquired.
983 * Return object if found else NULL.
985 struct agent_app
*agent_find_app_by_sock(int sock
)
987 struct lttng_ht_node_ulong
*node
;
988 struct lttng_ht_iter iter
;
989 struct agent_app
*app
;
991 LTTNG_ASSERT(sock
>= 0);
992 ASSERT_RCU_READ_LOCKED();
994 lttng_ht_lookup(the_agent_apps_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
995 node
= lttng_ht_iter_get_node_ulong(&iter
);
996 if (node
== nullptr) {
999 app
= lttng::utils::container_of(node
, &agent_app::node
);
1001 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1005 DBG3("Agent app NOT found by sock %d.", sock
);
1010 * Add agent application object to the global hash table.
1012 void agent_add_app(struct agent_app
*app
)
1016 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1017 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1021 * Delete agent application from the global hash table.
1023 * rcu_read_lock() must be held by the caller.
1025 void agent_delete_app(struct agent_app
*app
)
1028 struct lttng_ht_iter iter
;
1031 ASSERT_RCU_READ_LOCKED();
1033 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1035 iter
.iter
.node
= &app
->node
.node
;
1036 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1041 * Destroy an agent application object by detaching it from its corresponding
1042 * UST app if one is connected by closing the socket. Finally, perform a
1043 * delayed memory reclaim.
1045 void agent_destroy_app(struct agent_app
*app
)
1050 app
->sock
->ops
->close(app
->sock
);
1051 lttcomm_destroy_sock(app
->sock
);
1054 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1058 * Initialize an already allocated agent object.
1060 * Return 0 on success or else a negative errno value.
1062 int agent_init(struct agent
*agt
)
1068 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1073 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1075 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1083 * Add agent object to the given hash table.
1085 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1090 DBG3("Agent adding from domain %d", agt
->domain
);
1092 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1096 * Create an agent object for the given domain.
1098 * Return the allocated agent or NULL on error.
1100 struct agent
*agent_create(enum lttng_domain_type domain
)
1105 agt
= zmalloc
<agent
>();
1109 agt
->domain
= domain
;
1111 ret
= agent_init(agt
);
1123 * Create a newly allocated agent event data structure.
1124 * Ownership of filter_expression is taken.
1126 * Return a new object else NULL on error.
1128 struct agent_event
*agent_create_event(const char *name
,
1129 enum lttng_loglevel_type loglevel_type
,
1131 struct lttng_bytecode
*filter
,
1132 char *filter_expression
)
1134 struct agent_event
*event
= nullptr;
1136 DBG3("Agent create new event with name %s, loglevel type %d, \
1137 loglevel value %d and filter %s",
1141 filter_expression
? filter_expression
: "NULL");
1144 ERR("Failed to create agent event; no name provided.");
1148 event
= zmalloc
<agent_event
>();
1153 strncpy(event
->name
, name
, sizeof(event
->name
));
1154 event
->name
[sizeof(event
->name
) - 1] = '\0';
1155 lttng_ht_node_init_str(&event
->node
, event
->name
);
1157 event
->loglevel_value
= loglevel_value
;
1158 event
->loglevel_type
= loglevel_type
;
1159 event
->filter
= filter
;
1160 event
->filter_expression
= filter_expression
;
1166 * Unique add of a agent event to an agent object.
1168 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1170 LTTNG_ASSERT(event
);
1172 LTTNG_ASSERT(agt
->events
);
1174 DBG3("Agent adding event %s", event
->name
);
1175 add_unique_agent_event(agt
->events
, event
);
1176 agt
->being_used
= 1;
1180 * Unique add of a agent context to an agent object.
1182 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1185 struct agent_app_ctx
*agent_ctx
= nullptr;
1189 LTTNG_ASSERT(agt
->events
);
1190 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1192 agent_ctx
= create_app_ctx(ctx
);
1194 ret
= LTTNG_ERR_NOMEM
;
1198 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
, ctx
->u
.app_ctx
.ctx_name
);
1199 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1205 * Find multiple agent events sharing the given name.
1207 * RCU read side lock MUST be acquired. It must be held for the
1208 * duration of the iteration.
1210 * Sets the given iterator.
1212 void agent_find_events_by_name(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1214 struct lttng_ht
*ht
;
1215 struct agent_ht_key key
;
1219 LTTNG_ASSERT(agt
->events
);
1221 ASSERT_RCU_READ_LOCKED();
1226 cds_lfht_lookup(ht
->ht
,
1227 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1228 ht_match_event_by_name
,
1234 * Find the agent event matching a trigger.
1236 * RCU read side lock MUST be acquired. It must be held for as long as
1237 * the returned agent_event is used.
1239 * Return object if found else NULL.
1241 struct agent_event
*agent_find_event_by_trigger(const struct lttng_trigger
*trigger
,
1244 enum lttng_condition_status c_status
;
1245 enum lttng_event_rule_status er_status
;
1246 enum lttng_domain_type domain
;
1247 const struct lttng_condition
*condition
;
1248 const struct lttng_event_rule
*rule
;
1250 const char *filter_expression
;
1251 const struct lttng_log_level_rule
*log_level_rule
;
1252 /* Unused when loglevel_type is 'ALL'. */
1253 int loglevel_value
= 0;
1254 enum lttng_loglevel_type loglevel_type
;
1255 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1256 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1259 LTTNG_ASSERT(agt
->events
);
1260 ASSERT_RCU_READ_LOCKED();
1262 condition
= lttng_trigger_get_const_condition(trigger
);
1264 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1265 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1267 c_status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
1268 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1270 switch (lttng_event_rule_get_type(rule
)) {
1271 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1272 logging_get_name_pattern
= lttng_event_rule_jul_logging_get_name_pattern
;
1273 logging_get_log_level_rule
= lttng_event_rule_jul_logging_get_log_level_rule
;
1275 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1276 logging_get_name_pattern
= lttng_event_rule_log4j_logging_get_name_pattern
;
1277 logging_get_log_level_rule
= lttng_event_rule_log4j_logging_get_log_level_rule
;
1279 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1280 logging_get_name_pattern
= lttng_event_rule_python_logging_get_name_pattern
;
1281 logging_get_log_level_rule
= lttng_event_rule_python_logging_get_log_level_rule
;
1288 domain
= lttng_event_rule_get_domain_type(rule
);
1289 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1290 domain
== LTTNG_DOMAIN_PYTHON
);
1292 /* Get the event's pattern name ('name' in the legacy terminology). */
1293 er_status
= logging_get_name_pattern(rule
, &name
);
1294 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1296 /* Get the internal filter expression. */
1297 filter_expression
= lttng_event_rule_get_filter(rule
);
1299 /* Map log_level_rule to loglevel value. */
1300 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1301 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1302 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1304 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1305 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1310 return agent_find_event(name
, loglevel_type
, loglevel_value
, filter_expression
, agt
);
1314 * Get the next agent event duplicate by name. This should be called
1315 * after a call to agent_find_events_by_name() to iterate on events.
1317 * The RCU read lock must be held during the iteration and for as long
1318 * as the object the iterator points to remains in use.
1320 void agent_event_next_duplicate(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1322 struct agent_ht_key key
;
1324 ASSERT_RCU_READ_LOCKED();
1328 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
, &key
, &iter
->iter
);
1332 * Find a agent event in the given agent using name, loglevel and filter.
1334 * RCU read side lock MUST be acquired. It must be kept for as long as
1335 * the returned agent_event is used.
1337 * Return object if found else NULL.
1339 struct agent_event
*agent_find_event(const char *name
,
1340 enum lttng_loglevel_type loglevel_type
,
1342 const char *filter_expression
,
1345 struct lttng_ht_node_str
*node
;
1346 struct lttng_ht_iter iter
;
1347 struct lttng_ht
*ht
;
1348 struct agent_ht_key key
;
1352 LTTNG_ASSERT(agt
->events
);
1353 ASSERT_RCU_READ_LOCKED();
1357 key
.loglevel_value
= loglevel_value
;
1358 key
.loglevel_type
= loglevel_type
;
1359 key
.filter_expression
= filter_expression
;
1361 cds_lfht_lookup(ht
->ht
,
1362 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1366 node
= lttng_ht_iter_get_node_str(&iter
);
1367 if (node
== nullptr) {
1371 DBG3("Agent event found %s.", name
);
1372 return lttng::utils::container_of(node
, &agent_event::node
);
1375 DBG3("Agent event NOT found %s.", name
);
1380 * Free given agent event. This event must not be globally visible at this
1381 * point (only expected to be used on failure just after event creation). After
1382 * this call, the pointer is not usable anymore.
1384 void agent_destroy_event(struct agent_event
*event
)
1386 LTTNG_ASSERT(event
);
1388 free(event
->filter
);
1389 free(event
->filter_expression
);
1390 free(event
->exclusion
);
1394 static void destroy_app_ctx_rcu(struct rcu_head
*head
)
1396 struct agent_app_ctx
*ctx
= lttng::utils::container_of(head
, &agent_app_ctx::rcu_node
);
1398 destroy_app_ctx(ctx
);
1402 * Destroy an agent completely.
1404 void agent_destroy(struct agent
*agt
)
1406 struct lttng_ht_node_str
*node
;
1407 struct lttng_ht_iter iter
;
1408 struct agent_app_ctx
*ctx
;
1412 DBG3("Agent destroy");
1415 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, node
, node
) {
1417 struct agent_event
*event
;
1420 * When destroying an event, we have to try to disable it on the
1421 * agent side so the event stops generating data. The return
1422 * value is not important since we have to continue anyway
1423 * destroying the object.
1425 event
= lttng::utils::container_of(node
, &agent_event::node
);
1426 (void) agent_disable_event(event
, agt
->domain
);
1428 ret
= lttng_ht_del(agt
->events
, &iter
);
1430 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1433 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1435 (void) disable_context(ctx
, agt
->domain
);
1436 cds_list_del(&ctx
->list_node
);
1437 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1440 lttng_ht_destroy(agt
->events
);
1445 * Allocate agent_apps_ht_by_sock.
1447 int agent_app_ht_alloc()
1449 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1450 return the_agent_apps_ht_by_sock
? 0 : -1;
1454 * Destroy a agent application by socket.
1456 void agent_destroy_app_by_sock(int sock
)
1458 struct agent_app
*app
;
1460 LTTNG_ASSERT(sock
>= 0);
1463 * Not finding an application is a very important error that should NEVER
1464 * happen. The hash table deletion is ONLY done through this call when the
1465 * main sessiond thread is torn down.
1468 app
= agent_find_app_by_sock(sock
);
1471 /* RCU read side lock is assumed to be held by this function. */
1472 agent_delete_app(app
);
1474 /* The application is freed in a RCU call but the socket is closed here. */
1475 agent_destroy_app(app
);
1480 * Clean-up the agent app hash table and destroy it.
1482 void agent_app_ht_clean()
1484 struct lttng_ht_node_ulong
*node
;
1485 struct lttng_ht_iter iter
;
1487 if (!the_agent_apps_ht_by_sock
) {
1491 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1492 struct agent_app
*app
;
1494 app
= lttng::utils::container_of(node
, &agent_app::node
);
1495 agent_destroy_app_by_sock(app
->sock
->fd
);
1499 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1503 * Update a agent application (given socket) using the given agent.
1505 * Note that this function is most likely to be used with a tracing session
1506 * thus the caller should make sure to hold the appropriate lock(s).
1508 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1511 struct agent_event
*event
;
1512 struct lttng_ht_iter iter
;
1513 struct agent_app_ctx
*ctx
;
1518 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1522 * We are in the registration path thus if the application is gone,
1523 * there is a serious code flow error.
1526 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1527 /* Skip event if disabled. */
1528 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1532 ret
= enable_event(app
, event
);
1533 if (ret
!= LTTNG_OK
) {
1534 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1538 /* Let's try the others here and don't assume the app is dead. */
1543 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1545 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1546 if (ret
!= LTTNG_OK
) {
1547 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1560 * Allocate the per-event notifier domain agent hash table. It is lazily
1561 * populated as domains are used.
1563 int agent_by_event_notifier_domain_ht_create()
1565 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1566 return the_trigger_agents_ht_by_domain
? 0 : -1;
1570 * Clean-up the per-event notifier domain agent hash table and destroy it.
1572 void agent_by_event_notifier_domain_ht_destroy()
1574 struct lttng_ht_node_u64
*node
;
1575 struct lttng_ht_iter iter
;
1577 if (!the_trigger_agents_ht_by_domain
) {
1582 cds_lfht_for_each_entry (the_trigger_agents_ht_by_domain
->ht
, &iter
.iter
, node
, node
) {
1583 struct agent
*agent
= lttng::utils::container_of(node
, &agent::node
);
1584 const int ret
= lttng_ht_del(the_trigger_agents_ht_by_domain
, &iter
);
1586 LTTNG_ASSERT(ret
== 0);
1587 agent_destroy(agent
);
1591 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1594 struct agent
*agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type
)
1596 struct agent
*agt
= nullptr;
1597 struct lttng_ht_node_u64
*node
;
1598 struct lttng_ht_iter iter
;
1599 const uint64_t key
= (uint64_t) domain_type
;
1601 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1603 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1604 lttng_domain_type_str(domain_type
));
1606 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1607 node
= lttng_ht_iter_get_node_u64(&iter
);
1612 agt
= lttng::utils::container_of(node
, &agent::node
);