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>
18 #include <common/urcu.hpp>
20 #include <lttng/condition/condition.h>
21 #include <lttng/condition/event-rule-matches.h>
22 #include <lttng/domain-internal.hpp>
23 #include <lttng/event-rule/event-rule-internal.hpp>
24 #include <lttng/event-rule/event-rule.h>
25 #include <lttng/event-rule/jul-logging.h>
26 #include <lttng/event-rule/log4j-logging.h>
27 #include <lttng/event-rule/log4j2-logging.h>
28 #include <lttng/event-rule/python-logging.h>
29 #include <lttng/log-level-rule-internal.hpp>
31 #include <urcu/rculist.h>
32 #include <urcu/uatomic.h>
34 using event_rule_logging_get_name_pattern
=
35 enum lttng_event_rule_status (*)(const struct lttng_event_rule
*, const char **);
36 using event_rule_logging_get_log_level_rule
= enum lttng_event_rule_status (*)(
37 const struct lttng_event_rule
*, const struct lttng_log_level_rule
**);
40 * Agent application context representation.
43 struct agent_app_ctx
{
47 /* agent_app_ctx are part of the agent app_ctx_list. */
48 struct cds_list_head list_node
;
50 /* For call_rcu teardown. */
51 struct rcu_head rcu_node
;
56 * Human readable agent return code.
58 static const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code
)
61 case AGENT_RET_CODE_SUCCESS
:
63 case AGENT_RET_CODE_INVALID
:
64 return "Invalid command";
65 case AGENT_RET_CODE_UNKNOWN_NAME
:
66 return "Unknown logger name";
68 return "Unknown code";
72 static void log_reply_code(uint32_t in_reply_ret_code
)
74 int level
= PRINT_DBG3
;
76 * reply_ret_code and in_reply_ret_code are kept separate to have a
77 * sanitized value (used to retrieve the human readable string) and the
78 * original value which is logged as-is.
80 uint32_t reply_ret_code
= in_reply_ret_code
;
82 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
|| reply_ret_code
>= AGENT_RET_CODE_NR
) {
83 reply_ret_code
= AGENT_RET_CODE_NR
;
88 "Agent replied with retcode: %s (%" PRIu32
")",
89 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code
) reply_ret_code
),
94 * Match function for the events hash table lookup by name.
96 static int ht_match_event_by_name(struct cds_lfht_node
*node
, const void *_key
)
101 auto *event
= lttng_ht_node_container_of(node
, &agent_event::node
);
102 const auto *key
= static_cast<const agent_ht_key
*>(_key
);
104 /* Match 1 elements of the key: name. */
107 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
118 * Match function for the events hash table lookup by name, log level and
121 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
126 const auto *event
= lttng_ht_node_container_of(node
, &agent_event::node
);
127 const auto *key
= (agent_ht_key
*) _key
;
130 /* Match 2 elements of the key: name and loglevel. */
133 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
137 /* Event loglevel value and type. */
138 ll_match
= loglevels_match(event
->loglevel_type
,
139 event
->loglevel_value
,
142 LTTNG_EVENT_LOGLEVEL_ALL
);
148 /* Filter expression */
149 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
150 /* One has a filter expression, the other does not */
154 if (event
->filter_expression
) {
155 if (strncmp(event
->filter_expression
,
156 key
->filter_expression
,
157 strlen(event
->filter_expression
)) != 0) {
169 * Add unique agent event based on the event name and loglevel.
171 static void add_unique_agent_event(struct lttng_ht
*ht
, struct agent_event
*event
)
173 struct cds_lfht_node
*node_ptr
;
174 struct agent_ht_key key
;
177 LTTNG_ASSERT(ht
->ht
);
180 key
.name
= event
->name
;
181 key
.loglevel_value
= event
->loglevel_value
;
182 key
.loglevel_type
= event
->loglevel_type
;
183 key
.filter_expression
= event
->filter_expression
;
185 node_ptr
= cds_lfht_add_unique(ht
->ht
,
186 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
190 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
194 * URCU delayed agent event reclaim.
196 static void destroy_event_agent_rcu(struct rcu_head
*head
)
198 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
199 struct agent_event
*event
= lttng::utils::container_of(node
, &agent_event::node
);
201 agent_destroy_event(event
);
205 * URCU delayed agent app reclaim.
207 static void destroy_app_agent_rcu(struct rcu_head
*head
)
209 struct lttng_ht_node_ulong
*node
=
210 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
211 struct agent_app
*app
= lttng::utils::container_of(node
, &agent_app::node
);
217 * Communication with the agent. Send the message header to the given socket in
220 * Return 0 on success or else a negative errno message of sendmsg() op.
223 send_header(struct lttcomm_sock
*sock
, uint64_t data_size
, uint32_t cmd
, uint32_t cmd_version
)
227 struct lttcomm_agent_hdr msg
;
231 memset(&msg
, 0, sizeof(msg
));
232 msg
.data_size
= htobe64(data_size
);
233 msg
.cmd
= htobe32(cmd
);
234 msg
.cmd_version
= htobe32(cmd_version
);
236 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
237 if (size
< sizeof(msg
)) {
248 * Communication call with the agent. Send the payload to the given socket. The
249 * header MUST be sent prior to this call.
251 * Return 0 on success or else a negative errno value of sendmsg() op.
253 static int send_payload(struct lttcomm_sock
*sock
, const void *data
, size_t size
)
261 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
273 * Communication call with the agent. Receive reply from the agent using the
276 * Return 0 on success or else a negative errno value from recvmsg() op.
278 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
286 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
298 * Internal event listing for a given app. Populate events.
300 * Return number of element in the list or else a negative LTTNG_ERR* code.
301 * On success, the caller is responsible for freeing the memory
302 * allocated for "events".
304 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
306 int ret
, i
, len
= 0, offset
= 0;
309 uint32_t reply_ret_code
;
310 struct lttng_event
*tmp_events
= nullptr;
311 struct lttcomm_agent_list_reply
*reply
= nullptr;
312 struct lttcomm_agent_list_reply_hdr reply_hdr
;
315 LTTNG_ASSERT(app
->sock
);
316 LTTNG_ASSERT(events
);
318 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
, app
->sock
->fd
);
320 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
325 /* Get list header so we know how much we'll receive. */
326 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
331 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
332 log_reply_code(reply_ret_code
);
333 switch (reply_ret_code
) {
334 case AGENT_RET_CODE_SUCCESS
:
335 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
342 reply
= zmalloc
<lttcomm_agent_list_reply
>(data_size
);
344 ret
= LTTNG_ERR_NOMEM
;
348 /* Get the list with the appropriate data size. */
349 ret
= recv_reply(app
->sock
, reply
, data_size
);
354 nb_event
= be32toh(reply
->nb_event
);
355 tmp_events
= calloc
<lttng_event
>(nb_event
);
357 ret
= LTTNG_ERR_NOMEM
;
361 for (i
= 0; i
< nb_event
; i
++) {
363 if (lttng_strncpy(tmp_events
[i
].name
,
364 reply
->payload
+ offset
,
365 sizeof(tmp_events
[i
].name
))) {
366 ret
= LTTNG_ERR_INVALID
;
369 tmp_events
[i
].pid
= app
->pid
;
370 tmp_events
[i
].enabled
= -1;
371 len
= strlen(reply
->payload
+ offset
) + 1;
374 *events
= tmp_events
;
380 ret
= LTTNG_ERR_UST_LIST_FAIL
;
388 * Internal enable agent event on a agent application. This function
389 * communicates with the agent to enable a given event.
391 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
393 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
398 size_t filter_expression_length
;
399 uint32_t reply_ret_code
;
400 struct lttcomm_agent_enable_event msg
;
401 struct lttcomm_agent_generic_reply reply
;
404 LTTNG_ASSERT(app
->sock
);
407 DBG2("Agent enabling event %s for app pid: %d and socket %d",
413 * Calculate the payload's size, which is the fixed-size struct followed
414 * by the variable-length filter expression (+1 for the ending \0).
416 if (!event
->filter_expression
) {
417 filter_expression_length
= 0;
419 filter_expression_length
= strlen(event
->filter_expression
) + 1;
421 data_size
= sizeof(msg
) + filter_expression_length
;
423 memset(&msg
, 0, sizeof(msg
));
424 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
425 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
426 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
427 ret
= LTTNG_ERR_INVALID
;
430 msg
.filter_expression_length
= htobe32(filter_expression_length
);
432 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
437 bytes_to_send
= calloc
<char>(data_size
);
438 if (!bytes_to_send
) {
439 ret
= LTTNG_ERR_NOMEM
;
443 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
444 if (filter_expression_length
> 0) {
445 memcpy(bytes_to_send
+ sizeof(msg
),
446 event
->filter_expression
,
447 filter_expression_length
);
450 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
456 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
461 reply_ret_code
= be32toh(reply
.ret_code
);
462 log_reply_code(reply_ret_code
);
463 switch (reply_ret_code
) {
464 case AGENT_RET_CODE_SUCCESS
:
466 case AGENT_RET_CODE_UNKNOWN_NAME
:
467 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
477 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
483 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
485 static int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
490 len_be
= htobe32(len
);
491 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
496 ret
= send_payload(sock
, str
, len
);
505 * Internal enable application context on an agent application. This function
506 * communicates with the agent to enable a given application context.
508 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
510 static int app_context_op(const struct agent_app
*app
,
511 const struct agent_app_ctx
*ctx
,
512 enum lttcomm_agent_command cmd
)
515 uint32_t reply_ret_code
;
516 struct lttcomm_agent_generic_reply reply
;
517 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
520 LTTNG_ASSERT(app
->sock
);
522 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
|| cmd
== AGENT_CMD_APP_CTX_DISABLE
);
524 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
525 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
532 * Calculate the payload's size, which consists of the size (u32, BE)
533 * of the provider name, the NULL-terminated provider name string, the
534 * size (u32, BE) of the context name, followed by the NULL-terminated
535 * context name string.
537 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
538 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
540 sizeof(uint32_t) + app_ctx_provider_name_len
+ sizeof(uint32_t) + app_ctx_name_len
;
542 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
547 if (app_ctx_provider_name_len
> UINT32_MAX
|| app_ctx_name_len
> UINT32_MAX
) {
548 ERR("Application context name > MAX_UINT32");
549 ret
= LTTNG_ERR_INVALID
;
553 ret
= send_pstring(app
->sock
, ctx
->provider_name
, (uint32_t) app_ctx_provider_name_len
);
558 ret
= send_pstring(app
->sock
, ctx
->ctx_name
, (uint32_t) app_ctx_name_len
);
563 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
568 reply_ret_code
= be32toh(reply
.ret_code
);
569 log_reply_code(reply_ret_code
);
570 switch (reply_ret_code
) {
571 case AGENT_RET_CODE_SUCCESS
:
581 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
587 * Internal disable agent event call on a agent application. This function
588 * communicates with the agent to disable a given event.
590 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
592 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
596 uint32_t reply_ret_code
;
597 struct lttcomm_agent_disable_event msg
;
598 struct lttcomm_agent_generic_reply reply
;
601 LTTNG_ASSERT(app
->sock
);
604 DBG2("Agent disabling event %s for app pid: %d and socket %d",
609 data_size
= sizeof(msg
);
610 memset(&msg
, 0, sizeof(msg
));
611 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
612 ret
= LTTNG_ERR_INVALID
;
616 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
621 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
626 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
631 reply_ret_code
= be32toh(reply
.ret_code
);
632 log_reply_code(reply_ret_code
);
633 switch (reply_ret_code
) {
634 case AGENT_RET_CODE_SUCCESS
:
636 case AGENT_RET_CODE_UNKNOWN_NAME
:
637 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
647 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
653 * Send back the registration DONE command to a given agent application.
655 * Return 0 on success or else a negative value.
657 int agent_send_registration_done(struct agent_app
*app
)
660 LTTNG_ASSERT(app
->sock
);
662 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
664 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
668 * Enable agent event on every agent applications registered with the session
671 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
673 int agent_enable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
679 for (auto *app
: lttng::urcu::
680 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
681 *the_agent_apps_ht_by_sock
->ht
)) {
682 if (app
->domain
!= domain
) {
686 /* Enable event on agent application through TCP socket. */
687 ret
= enable_event(app
, event
);
688 if (ret
!= LTTNG_OK
) {
693 event
->enabled_count
++;
700 static void destroy_app_ctx(struct agent_app_ctx
*ctx
)
702 free(ctx
->provider_name
);
707 static struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
709 struct agent_app_ctx
*agent_ctx
= nullptr;
715 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
716 agent_ctx
= zmalloc
<agent_app_ctx
>();
721 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
722 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
723 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
724 destroy_app_ctx(agent_ctx
);
732 * Enable agent context on every agent applications registered with the session
735 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
737 int agent_enable_context(const struct lttng_event_context
*ctx
, enum lttng_domain_type domain
)
742 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
743 ret
= LTTNG_ERR_INVALID
;
747 for (auto *app
: lttng::urcu::
748 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
749 *the_agent_apps_ht_by_sock
->ht
)) {
750 struct agent_app_ctx
*agent_ctx
;
752 if (app
->domain
!= domain
) {
756 agent_ctx
= create_app_ctx(ctx
);
758 ret
= LTTNG_ERR_NOMEM
;
762 /* Enable event on agent application through TCP socket. */
763 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
764 destroy_app_ctx(agent_ctx
);
765 if (ret
!= LTTNG_OK
) {
778 * Disable agent event on every agent application registered with the session
781 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
783 int agent_disable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
788 if (!AGENT_EVENT_IS_ENABLED(event
)) {
792 if (--event
->enabled_count
!= 0) {
794 * Agent event still enabled. Disable the agent event only when
795 * all "users" have disabled it (event notifiers, event rules,
802 for (auto *app
: lttng::urcu::
803 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
804 *the_agent_apps_ht_by_sock
->ht
)) {
805 if (app
->domain
!= domain
) {
809 /* Enable event on agent application through TCP socket. */
810 ret
= disable_event(app
, event
);
811 if (ret
!= LTTNG_OK
) {
816 /* event->enabled_count is now 0. */
817 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event
));
825 * Disable agent context on every agent application registered with the session
828 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
830 static int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
835 DBG2("Disabling agent application context %s:%s", ctx
->provider_name
, ctx
->ctx_name
);
837 for (auto *app
: lttng::urcu::
838 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
839 *the_agent_apps_ht_by_sock
->ht
)) {
840 if (app
->domain
!= domain
) {
844 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
845 if (ret
!= LTTNG_OK
) {
854 * Ask every agent for the list of possible event. Events is allocated with the
855 * events of every agent application.
857 * Return the number of events or else a negative value.
859 int agent_list_events(struct lttng_event
**events
, enum lttng_domain_type domain
)
862 size_t nbmem
, count
= 0;
863 struct lttng_event
*tmp_events
= nullptr;
865 LTTNG_ASSERT(events
);
867 DBG2("Agent listing events for domain %d", domain
);
869 nbmem
= UST_APP_EVENT_LIST_SIZE
;
870 tmp_events
= calloc
<lttng_event
>(nbmem
);
872 PERROR("zmalloc agent list events");
877 for (auto *app
: lttng::urcu::
878 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
879 *the_agent_apps_ht_by_sock
->ht
)) {
881 struct lttng_event
*agent_events
;
883 /* Skip domain not asked by the list. */
884 if (app
->domain
!= domain
) {
888 nb_ev
= list_events(app
, &agent_events
);
894 if (count
+ nb_ev
> nbmem
) {
895 /* In case the realloc fails, we free the memory */
896 struct lttng_event
*new_tmp_events
;
899 new_nbmem
= std::max(count
+ nb_ev
, nbmem
<< 1);
900 DBG2("Reallocating agent event list from %zu to %zu entries",
903 new_tmp_events
= (lttng_event
*) realloc(
904 tmp_events
, new_nbmem
* sizeof(*new_tmp_events
));
905 if (!new_tmp_events
) {
906 PERROR("realloc agent events");
912 /* Zero the new memory */
913 memset(new_tmp_events
+ nbmem
,
915 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
917 tmp_events
= new_tmp_events
;
919 memcpy(tmp_events
+ count
, agent_events
, nb_ev
* sizeof(*tmp_events
));
925 *events
= tmp_events
;
934 * Create a agent app object using the given PID.
936 * Return newly allocated object or else NULL on error.
939 agent_create_app(pid_t pid
, enum lttng_domain_type domain
, struct lttcomm_sock
*sock
)
941 struct agent_app
*app
;
945 app
= zmalloc
<agent_app
>();
947 PERROR("Failed to allocate agent application instance");
952 app
->domain
= domain
;
954 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
961 * Lookup agent app by socket in the global hash table.
963 * RCU read side lock MUST be acquired.
965 * Return object if found else NULL.
967 struct agent_app
*agent_find_app_by_sock(int sock
)
969 struct lttng_ht_node_ulong
*node
;
970 struct lttng_ht_iter iter
;
971 struct agent_app
*app
;
973 LTTNG_ASSERT(sock
>= 0);
974 ASSERT_RCU_READ_LOCKED();
976 lttng_ht_lookup(the_agent_apps_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
977 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
978 if (node
== nullptr) {
981 app
= lttng::utils::container_of(node
, &agent_app::node
);
983 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
987 DBG3("Agent app NOT found by sock %d.", sock
);
992 * Add agent application object to the global hash table.
994 void agent_add_app(struct agent_app
*app
)
998 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
999 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1003 * Delete agent application from the global hash table.
1005 * rcu_read_lock() must be held by the caller.
1007 void agent_delete_app(struct agent_app
*app
)
1010 struct lttng_ht_iter iter
;
1013 ASSERT_RCU_READ_LOCKED();
1015 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1017 iter
.iter
.node
= &app
->node
.node
;
1018 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1023 * Destroy an agent application object by detaching it from its corresponding
1024 * UST app if one is connected by closing the socket. Finally, perform a
1025 * delayed memory reclaim.
1027 void agent_destroy_app(struct agent_app
*app
)
1032 app
->sock
->ops
->close(app
->sock
);
1033 lttcomm_destroy_sock(app
->sock
);
1036 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1040 * Initialize an already allocated agent object.
1042 * Return 0 on success or else a negative errno value.
1044 int agent_init(struct agent
*agt
)
1050 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1055 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1057 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1065 * Add agent object to the given hash table.
1067 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1072 DBG3("Agent adding from domain %d", agt
->domain
);
1074 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1078 * Create an agent object for the given domain.
1080 * Return the allocated agent or NULL on error.
1082 struct agent
*agent_create(enum lttng_domain_type domain
)
1087 agt
= zmalloc
<agent
>();
1091 agt
->domain
= domain
;
1093 ret
= agent_init(agt
);
1105 * Create a newly allocated agent event data structure.
1106 * Ownership of filter_expression is taken.
1108 * Return a new object else NULL on error.
1110 struct agent_event
*agent_create_event(const char *name
,
1111 enum lttng_loglevel_type loglevel_type
,
1113 struct lttng_bytecode
*filter
,
1114 char *filter_expression
)
1116 struct agent_event
*event
= nullptr;
1118 DBG3("Agent create new event with name %s, loglevel type %d, \
1119 loglevel value %d and filter %s",
1123 filter_expression
? filter_expression
: "NULL");
1126 ERR("Failed to create agent event; no name provided.");
1130 event
= zmalloc
<agent_event
>();
1135 strncpy(event
->name
, name
, sizeof(event
->name
));
1136 event
->name
[sizeof(event
->name
) - 1] = '\0';
1137 lttng_ht_node_init_str(&event
->node
, event
->name
);
1139 event
->loglevel_value
= loglevel_value
;
1140 event
->loglevel_type
= loglevel_type
;
1141 event
->filter
= filter
;
1142 event
->filter_expression
= filter_expression
;
1148 * Unique add of a agent event to an agent object.
1150 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1152 LTTNG_ASSERT(event
);
1154 LTTNG_ASSERT(agt
->events
);
1156 DBG3("Agent adding event %s", event
->name
);
1157 add_unique_agent_event(agt
->events
, event
);
1158 agt
->being_used
= 1;
1162 * Unique add of a agent context to an agent object.
1164 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1167 struct agent_app_ctx
*agent_ctx
= nullptr;
1171 LTTNG_ASSERT(agt
->events
);
1172 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1174 agent_ctx
= create_app_ctx(ctx
);
1176 ret
= LTTNG_ERR_NOMEM
;
1180 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
, ctx
->u
.app_ctx
.ctx_name
);
1181 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1187 * Find multiple agent events sharing the given name.
1189 * RCU read side lock MUST be acquired. It must be held for the
1190 * duration of the iteration.
1192 * Sets the given iterator.
1194 void agent_find_events_by_name(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1196 struct lttng_ht
*ht
;
1197 struct agent_ht_key key
;
1201 LTTNG_ASSERT(agt
->events
);
1203 ASSERT_RCU_READ_LOCKED();
1208 cds_lfht_lookup(ht
->ht
,
1209 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1210 ht_match_event_by_name
,
1216 * Find the agent event matching a trigger.
1218 * RCU read side lock MUST be acquired. It must be held for as long as
1219 * the returned agent_event is used.
1221 * Return object if found else NULL.
1223 struct agent_event
*agent_find_event_by_trigger(const struct lttng_trigger
*trigger
,
1226 enum lttng_condition_status c_status
;
1227 enum lttng_event_rule_status er_status
;
1228 enum lttng_domain_type domain
;
1229 const struct lttng_condition
*condition
;
1230 const struct lttng_event_rule
*rule
;
1232 const char *filter_expression
;
1233 const struct lttng_log_level_rule
*log_level_rule
;
1234 /* Unused when loglevel_type is 'ALL'. */
1235 int loglevel_value
= 0;
1236 enum lttng_loglevel_type loglevel_type
;
1237 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1238 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1241 LTTNG_ASSERT(agt
->events
);
1242 ASSERT_RCU_READ_LOCKED();
1244 condition
= lttng_trigger_get_const_condition(trigger
);
1246 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1247 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1249 c_status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
1250 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1252 switch (lttng_event_rule_get_type(rule
)) {
1253 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1254 logging_get_name_pattern
= lttng_event_rule_jul_logging_get_name_pattern
;
1255 logging_get_log_level_rule
= lttng_event_rule_jul_logging_get_log_level_rule
;
1257 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1258 logging_get_name_pattern
= lttng_event_rule_log4j_logging_get_name_pattern
;
1259 logging_get_log_level_rule
= lttng_event_rule_log4j_logging_get_log_level_rule
;
1261 case LTTNG_EVENT_RULE_TYPE_LOG4J2_LOGGING
:
1262 logging_get_name_pattern
= lttng_event_rule_log4j2_logging_get_name_pattern
;
1263 logging_get_log_level_rule
= lttng_event_rule_log4j2_logging_get_log_level_rule
;
1265 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1266 logging_get_name_pattern
= lttng_event_rule_python_logging_get_name_pattern
;
1267 logging_get_log_level_rule
= lttng_event_rule_python_logging_get_log_level_rule
;
1274 domain
= lttng_event_rule_get_domain_type(rule
);
1275 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1276 domain
== LTTNG_DOMAIN_LOG4J2
|| domain
== LTTNG_DOMAIN_PYTHON
);
1278 /* Get the event's pattern name ('name' in the legacy terminology). */
1279 er_status
= logging_get_name_pattern(rule
, &name
);
1280 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1282 /* Get the internal filter expression. */
1283 filter_expression
= lttng_event_rule_get_filter(rule
);
1285 /* Map log_level_rule to loglevel value. */
1286 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1287 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1288 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1290 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1291 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1296 return agent_find_event(name
, loglevel_type
, loglevel_value
, filter_expression
, agt
);
1300 * Get the next agent event duplicate by name. This should be called
1301 * after a call to agent_find_events_by_name() to iterate on events.
1303 * The RCU read lock must be held during the iteration and for as long
1304 * as the object the iterator points to remains in use.
1306 void agent_event_next_duplicate(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1308 struct agent_ht_key key
;
1310 ASSERT_RCU_READ_LOCKED();
1314 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
, &key
, &iter
->iter
);
1318 * Find a agent event in the given agent using name, loglevel and filter.
1320 * RCU read side lock MUST be acquired. It must be kept for as long as
1321 * the returned agent_event is used.
1323 * Return object if found else NULL.
1325 struct agent_event
*agent_find_event(const char *name
,
1326 enum lttng_loglevel_type loglevel_type
,
1328 const char *filter_expression
,
1331 struct lttng_ht_node_str
*node
;
1332 struct lttng_ht_iter iter
;
1333 struct lttng_ht
*ht
;
1334 struct agent_ht_key key
;
1338 LTTNG_ASSERT(agt
->events
);
1339 ASSERT_RCU_READ_LOCKED();
1343 key
.loglevel_value
= loglevel_value
;
1344 key
.loglevel_type
= loglevel_type
;
1345 key
.filter_expression
= filter_expression
;
1347 cds_lfht_lookup(ht
->ht
,
1348 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1352 node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&iter
);
1353 if (node
== nullptr) {
1357 DBG3("Agent event found %s.", name
);
1358 return lttng::utils::container_of(node
, &agent_event::node
);
1361 DBG3("Agent event NOT found %s.", name
);
1366 * Free given agent event. This event must not be globally visible at this
1367 * point (only expected to be used on failure just after event creation). After
1368 * this call, the pointer is not usable anymore.
1370 void agent_destroy_event(struct agent_event
*event
)
1372 LTTNG_ASSERT(event
);
1374 free(event
->filter
);
1375 free(event
->filter_expression
);
1376 free(event
->exclusion
);
1380 static void destroy_app_ctx_rcu(struct rcu_head
*head
)
1382 struct agent_app_ctx
*ctx
= lttng::utils::container_of(head
, &agent_app_ctx::rcu_node
);
1384 destroy_app_ctx(ctx
);
1388 * Destroy an agent completely.
1390 void agent_destroy(struct agent
*agt
)
1394 DBG3("Agent destroy");
1397 lttng::urcu::lfht_iteration_adapter
<agent_event
,
1398 decltype(agent_event::node
),
1399 &agent_event::node
>(*agt
->events
->ht
)) {
1403 * When destroying an event, we have to try to disable it on the
1404 * agent side so the event stops generating data. The return
1405 * value is not important since we have to continue anyway
1406 * destroying the object.
1408 (void) agent_disable_event(event
, agt
->domain
);
1410 ret
= cds_lfht_del(agt
->events
->ht
, &event
->node
.node
);
1412 call_rcu(&event
->node
.head
, destroy_event_agent_rcu
);
1416 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1418 (void) disable_context(ctx
, agt
->domain
);
1419 cds_list_del(&ctx
->list_node
);
1420 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1423 lttng_ht_destroy(agt
->events
);
1428 * Allocate agent_apps_ht_by_sock.
1430 int agent_app_ht_alloc()
1432 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1433 return the_agent_apps_ht_by_sock
? 0 : -1;
1437 * Destroy a agent application by socket.
1439 void agent_destroy_app_by_sock(int sock
)
1441 struct agent_app
*app
;
1443 LTTNG_ASSERT(sock
>= 0);
1446 * Not finding an application is a very important error that should NEVER
1447 * happen. The hash table deletion is ONLY done through this call when the
1448 * main sessiond thread is torn down.
1450 const lttng::urcu::read_lock_guard read_lock
;
1451 app
= agent_find_app_by_sock(sock
);
1454 /* RCU read side lock is assumed to be held by this function. */
1455 agent_delete_app(app
);
1457 /* The application is freed in a RCU call but the socket is closed here. */
1458 agent_destroy_app(app
);
1462 * Clean-up the agent app hash table and destroy it.
1464 void agent_app_ht_clean()
1466 if (!the_agent_apps_ht_by_sock
) {
1470 for (auto *app
: lttng::urcu::
1471 lfht_iteration_adapter
<agent_app
, decltype(agent_app::node
), &agent_app::node
>(
1472 *the_agent_apps_ht_by_sock
->ht
)) {
1473 agent_destroy_app_by_sock(app
->sock
->fd
);
1476 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1480 * Update a agent application (given socket) using the given agent.
1482 * Note that this function is most likely to be used with a tracing session
1483 * thus the caller should make sure to hold the appropriate lock(s).
1485 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1488 struct agent_app_ctx
*ctx
;
1493 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1496 * We are in the registration path thus if the application is gone,
1497 * there is a serious code flow error.
1500 lttng::urcu::lfht_iteration_adapter
<agent_event
,
1501 decltype(agent_event::node
),
1502 &agent_event::node
>(*agt
->events
->ht
)) {
1503 /* Skip event if disabled. */
1504 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1508 ret
= enable_event(app
, event
);
1509 if (ret
!= LTTNG_OK
) {
1510 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1514 /* Let's try the others here and don't assume the app is dead. */
1519 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1521 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1522 if (ret
!= LTTNG_OK
) {
1523 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1534 * Allocate the per-event notifier domain agent hash table. It is lazily
1535 * populated as domains are used.
1537 int agent_by_event_notifier_domain_ht_create()
1539 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1540 return the_trigger_agents_ht_by_domain
? 0 : -1;
1544 * Clean-up the per-event notifier domain agent hash table and destroy it.
1546 void agent_by_event_notifier_domain_ht_destroy()
1548 if (!the_trigger_agents_ht_by_domain
) {
1552 for (struct agent
*agent
:
1553 lttng::urcu::lfht_iteration_adapter
<struct agent
, decltype(agent::node
), &agent::node
>(
1554 *the_trigger_agents_ht_by_domain
->ht
)) {
1556 cds_lfht_del(the_trigger_agents_ht_by_domain
->ht
, &agent
->node
.node
);
1558 LTTNG_ASSERT(ret
== 0);
1559 agent_destroy(agent
);
1562 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1565 struct agent
*agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type
)
1567 struct agent
*agt
= nullptr;
1568 struct lttng_ht_node_u64
*node
;
1569 struct lttng_ht_iter iter
;
1570 const uint64_t key
= (uint64_t) domain_type
;
1572 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1574 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1575 lttng_domain_type_str(domain_type
));
1577 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1578 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
1583 agt
= lttng::utils::container_of(node
, &agent::node
);