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/python-logging.h>
28 #include <lttng/log-level-rule-internal.hpp>
30 #include <urcu/rculist.h>
31 #include <urcu/uatomic.h>
33 using event_rule_logging_get_name_pattern
=
34 enum lttng_event_rule_status (*)(const struct lttng_event_rule
*, const char **);
35 using event_rule_logging_get_log_level_rule
= enum lttng_event_rule_status (*)(
36 const struct lttng_event_rule
*, const struct lttng_log_level_rule
**);
39 * Agent application context representation.
42 struct agent_app_ctx
{
46 /* agent_app_ctx are part of the agent app_ctx_list. */
47 struct cds_list_head list_node
;
49 /* For call_rcu teardown. */
50 struct rcu_head rcu_node
;
55 * Human readable agent return code.
57 static const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code
)
60 case AGENT_RET_CODE_SUCCESS
:
62 case AGENT_RET_CODE_INVALID
:
63 return "Invalid command";
64 case AGENT_RET_CODE_UNKNOWN_NAME
:
65 return "Unknown logger name";
67 return "Unknown code";
71 static void log_reply_code(uint32_t in_reply_ret_code
)
73 int level
= PRINT_DBG3
;
75 * reply_ret_code and in_reply_ret_code are kept separate to have a
76 * sanitized value (used to retrieve the human readable string) and the
77 * original value which is logged as-is.
79 uint32_t reply_ret_code
= in_reply_ret_code
;
81 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
|| reply_ret_code
>= AGENT_RET_CODE_NR
) {
82 reply_ret_code
= AGENT_RET_CODE_NR
;
87 "Agent replied with retcode: %s (%" PRIu32
")",
88 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code
) reply_ret_code
),
93 * Match function for the events hash table lookup by name.
95 static int ht_match_event_by_name(struct cds_lfht_node
*node
, const void *_key
)
97 struct agent_event
*event
;
98 const struct agent_ht_key
*key
;
103 event
= caa_container_of(node
, struct agent_event
, node
.node
);
104 key
= (agent_ht_key
*) _key
;
106 /* Match 1 elements of the key: name. */
109 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
120 * Match function for the events hash table lookup by name, log level and
123 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
125 struct agent_event
*event
;
126 const struct agent_ht_key
*key
;
132 event
= caa_container_of(node
, struct agent_event
, node
.node
);
133 key
= (agent_ht_key
*) _key
;
135 /* Match 2 elements of the key: name and loglevel. */
138 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
142 /* Event loglevel value and type. */
143 ll_match
= loglevels_match(event
->loglevel_type
,
144 event
->loglevel_value
,
147 LTTNG_EVENT_LOGLEVEL_ALL
);
153 /* Filter expression */
154 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
155 /* One has a filter expression, the other does not */
159 if (event
->filter_expression
) {
160 if (strncmp(event
->filter_expression
,
161 key
->filter_expression
,
162 strlen(event
->filter_expression
)) != 0) {
174 * Add unique agent event based on the event name and loglevel.
176 static void add_unique_agent_event(struct lttng_ht
*ht
, struct agent_event
*event
)
178 struct cds_lfht_node
*node_ptr
;
179 struct agent_ht_key key
;
182 LTTNG_ASSERT(ht
->ht
);
185 key
.name
= event
->name
;
186 key
.loglevel_value
= event
->loglevel_value
;
187 key
.loglevel_type
= event
->loglevel_type
;
188 key
.filter_expression
= event
->filter_expression
;
190 node_ptr
= cds_lfht_add_unique(ht
->ht
,
191 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
195 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
199 * URCU delayed agent event reclaim.
201 static void destroy_event_agent_rcu(struct rcu_head
*head
)
203 struct lttng_ht_node_str
*node
= lttng::utils::container_of(head
, <tng_ht_node_str::head
);
204 struct agent_event
*event
= lttng::utils::container_of(node
, &agent_event::node
);
206 agent_destroy_event(event
);
210 * URCU delayed agent app reclaim.
212 static void destroy_app_agent_rcu(struct rcu_head
*head
)
214 struct lttng_ht_node_ulong
*node
=
215 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
216 struct agent_app
*app
= lttng::utils::container_of(node
, &agent_app::node
);
222 * Communication with the agent. Send the message header to the given socket in
225 * Return 0 on success or else a negative errno message of sendmsg() op.
228 send_header(struct lttcomm_sock
*sock
, uint64_t data_size
, uint32_t cmd
, uint32_t cmd_version
)
232 struct lttcomm_agent_hdr msg
;
236 memset(&msg
, 0, sizeof(msg
));
237 msg
.data_size
= htobe64(data_size
);
238 msg
.cmd
= htobe32(cmd
);
239 msg
.cmd_version
= htobe32(cmd_version
);
241 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
242 if (size
< sizeof(msg
)) {
253 * Communication call with the agent. Send the payload to the given socket. The
254 * header MUST be sent prior to this call.
256 * Return 0 on success or else a negative errno value of sendmsg() op.
258 static int send_payload(struct lttcomm_sock
*sock
, const void *data
, size_t size
)
266 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
278 * Communication call with the agent. Receive reply from the agent using the
281 * Return 0 on success or else a negative errno value from recvmsg() op.
283 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
291 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
303 * Internal event listing for a given app. Populate events.
305 * Return number of element in the list or else a negative LTTNG_ERR* code.
306 * On success, the caller is responsible for freeing the memory
307 * allocated for "events".
309 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
311 int ret
, i
, len
= 0, offset
= 0;
314 uint32_t reply_ret_code
;
315 struct lttng_event
*tmp_events
= nullptr;
316 struct lttcomm_agent_list_reply
*reply
= nullptr;
317 struct lttcomm_agent_list_reply_hdr reply_hdr
;
320 LTTNG_ASSERT(app
->sock
);
321 LTTNG_ASSERT(events
);
323 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
, app
->sock
->fd
);
325 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
330 /* Get list header so we know how much we'll receive. */
331 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
336 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
337 log_reply_code(reply_ret_code
);
338 switch (reply_ret_code
) {
339 case AGENT_RET_CODE_SUCCESS
:
340 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
347 reply
= zmalloc
<lttcomm_agent_list_reply
>(data_size
);
349 ret
= LTTNG_ERR_NOMEM
;
353 /* Get the list with the appropriate data size. */
354 ret
= recv_reply(app
->sock
, reply
, data_size
);
359 nb_event
= be32toh(reply
->nb_event
);
360 tmp_events
= calloc
<lttng_event
>(nb_event
);
362 ret
= LTTNG_ERR_NOMEM
;
366 for (i
= 0; i
< nb_event
; i
++) {
368 if (lttng_strncpy(tmp_events
[i
].name
,
369 reply
->payload
+ offset
,
370 sizeof(tmp_events
[i
].name
))) {
371 ret
= LTTNG_ERR_INVALID
;
374 tmp_events
[i
].pid
= app
->pid
;
375 tmp_events
[i
].enabled
= -1;
376 len
= strlen(reply
->payload
+ offset
) + 1;
379 *events
= tmp_events
;
385 ret
= LTTNG_ERR_UST_LIST_FAIL
;
393 * Internal enable agent event on a agent application. This function
394 * communicates with the agent to enable a given event.
396 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
398 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
403 size_t filter_expression_length
;
404 uint32_t reply_ret_code
;
405 struct lttcomm_agent_enable_event msg
;
406 struct lttcomm_agent_generic_reply reply
;
409 LTTNG_ASSERT(app
->sock
);
412 DBG2("Agent enabling event %s for app pid: %d and socket %d",
418 * Calculate the payload's size, which is the fixed-size struct followed
419 * by the variable-length filter expression (+1 for the ending \0).
421 if (!event
->filter_expression
) {
422 filter_expression_length
= 0;
424 filter_expression_length
= strlen(event
->filter_expression
) + 1;
426 data_size
= sizeof(msg
) + filter_expression_length
;
428 memset(&msg
, 0, sizeof(msg
));
429 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
430 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
431 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
432 ret
= LTTNG_ERR_INVALID
;
435 msg
.filter_expression_length
= htobe32(filter_expression_length
);
437 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
442 bytes_to_send
= calloc
<char>(data_size
);
443 if (!bytes_to_send
) {
444 ret
= LTTNG_ERR_NOMEM
;
448 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
449 if (filter_expression_length
> 0) {
450 memcpy(bytes_to_send
+ sizeof(msg
),
451 event
->filter_expression
,
452 filter_expression_length
);
455 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
461 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
466 reply_ret_code
= be32toh(reply
.ret_code
);
467 log_reply_code(reply_ret_code
);
468 switch (reply_ret_code
) {
469 case AGENT_RET_CODE_SUCCESS
:
471 case AGENT_RET_CODE_UNKNOWN_NAME
:
472 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
482 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
488 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
490 static int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
495 len_be
= htobe32(len
);
496 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
501 ret
= send_payload(sock
, str
, len
);
510 * Internal enable application context on an agent application. This function
511 * communicates with the agent to enable a given application context.
513 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
515 static int app_context_op(const struct agent_app
*app
,
516 const struct agent_app_ctx
*ctx
,
517 enum lttcomm_agent_command cmd
)
520 uint32_t reply_ret_code
;
521 struct lttcomm_agent_generic_reply reply
;
522 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
525 LTTNG_ASSERT(app
->sock
);
527 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
|| cmd
== AGENT_CMD_APP_CTX_DISABLE
);
529 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
530 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
537 * Calculate the payload's size, which consists of the size (u32, BE)
538 * of the provider name, the NULL-terminated provider name string, the
539 * size (u32, BE) of the context name, followed by the NULL-terminated
540 * context name string.
542 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
543 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
545 sizeof(uint32_t) + app_ctx_provider_name_len
+ sizeof(uint32_t) + app_ctx_name_len
;
547 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
552 if (app_ctx_provider_name_len
> UINT32_MAX
|| app_ctx_name_len
> UINT32_MAX
) {
553 ERR("Application context name > MAX_UINT32");
554 ret
= LTTNG_ERR_INVALID
;
558 ret
= send_pstring(app
->sock
, ctx
->provider_name
, (uint32_t) app_ctx_provider_name_len
);
563 ret
= send_pstring(app
->sock
, ctx
->ctx_name
, (uint32_t) app_ctx_name_len
);
568 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
573 reply_ret_code
= be32toh(reply
.ret_code
);
574 log_reply_code(reply_ret_code
);
575 switch (reply_ret_code
) {
576 case AGENT_RET_CODE_SUCCESS
:
586 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
592 * Internal disable agent event call on a agent application. This function
593 * communicates with the agent to disable a given event.
595 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
597 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
601 uint32_t reply_ret_code
;
602 struct lttcomm_agent_disable_event msg
;
603 struct lttcomm_agent_generic_reply reply
;
606 LTTNG_ASSERT(app
->sock
);
609 DBG2("Agent disabling event %s for app pid: %d and socket %d",
614 data_size
= sizeof(msg
);
615 memset(&msg
, 0, sizeof(msg
));
616 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
617 ret
= LTTNG_ERR_INVALID
;
621 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
626 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
631 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
636 reply_ret_code
= be32toh(reply
.ret_code
);
637 log_reply_code(reply_ret_code
);
638 switch (reply_ret_code
) {
639 case AGENT_RET_CODE_SUCCESS
:
641 case AGENT_RET_CODE_UNKNOWN_NAME
:
642 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
652 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
658 * Send back the registration DONE command to a given agent application.
660 * Return 0 on success or else a negative value.
662 int agent_send_registration_done(struct agent_app
*app
)
665 LTTNG_ASSERT(app
->sock
);
667 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
669 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
673 * Enable agent event on every agent applications registered with the session
676 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
678 int agent_enable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
681 struct agent_app
*app
;
682 struct lttng_ht_iter iter
;
687 lttng::urcu::read_lock_guard read_lock
;
689 cds_lfht_for_each_entry (
690 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
691 if (app
->domain
!= domain
) {
695 /* Enable event on agent application through TCP socket. */
696 ret
= enable_event(app
, event
);
697 if (ret
!= LTTNG_OK
) {
703 event
->enabled_count
++;
710 static void destroy_app_ctx(struct agent_app_ctx
*ctx
)
712 free(ctx
->provider_name
);
717 static struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
719 struct agent_app_ctx
*agent_ctx
= nullptr;
725 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
726 agent_ctx
= zmalloc
<agent_app_ctx
>();
731 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
732 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
733 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
734 destroy_app_ctx(agent_ctx
);
742 * Enable agent context on every agent applications registered with the session
745 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
747 int agent_enable_context(const struct lttng_event_context
*ctx
, enum lttng_domain_type domain
)
750 struct agent_app
*app
;
751 struct lttng_ht_iter iter
;
754 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
755 ret
= LTTNG_ERR_INVALID
;
760 lttng::urcu::read_lock_guard read_lock
;
762 cds_lfht_for_each_entry (
763 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
764 struct agent_app_ctx
*agent_ctx
;
766 if (app
->domain
!= domain
) {
770 agent_ctx
= create_app_ctx(ctx
);
772 ret
= LTTNG_ERR_NOMEM
;
776 /* Enable event on agent application through TCP socket. */
777 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
778 destroy_app_ctx(agent_ctx
);
779 if (ret
!= LTTNG_OK
) {
793 * Disable agent event on every agent application registered with the session
796 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
798 int agent_disable_event(struct agent_event
*event
, enum lttng_domain_type domain
)
801 struct agent_app
*app
;
802 struct lttng_ht_iter iter
;
805 if (!AGENT_EVENT_IS_ENABLED(event
)) {
809 if (--event
->enabled_count
!= 0) {
811 * Agent event still enabled. Disable the agent event only when
812 * all "users" have disabled it (event notifiers, event rules,
820 lttng::urcu::read_lock_guard read_lock
;
822 cds_lfht_for_each_entry (
823 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
824 if (app
->domain
!= domain
) {
828 /* Enable event on agent application through TCP socket. */
829 ret
= disable_event(app
, event
);
830 if (ret
!= LTTNG_OK
) {
836 /* event->enabled_count is now 0. */
837 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event
));
845 * Disable agent context on every agent application registered with the session
848 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
850 static int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
853 struct agent_app
*app
;
854 struct lttng_ht_iter iter
;
857 DBG2("Disabling agent application context %s:%s", ctx
->provider_name
, ctx
->ctx_name
);
860 lttng::urcu::read_lock_guard read_lock
;
862 cds_lfht_for_each_entry (
863 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
864 if (app
->domain
!= domain
) {
868 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
869 if (ret
!= LTTNG_OK
) {
879 * Ask every agent for the list of possible event. Events is allocated with the
880 * events of every agent application.
882 * Return the number of events or else a negative value.
884 int agent_list_events(struct lttng_event
**events
, enum lttng_domain_type domain
)
887 size_t nbmem
, count
= 0;
888 struct agent_app
*app
;
889 struct lttng_event
*tmp_events
= nullptr;
890 struct lttng_ht_iter iter
;
892 LTTNG_ASSERT(events
);
894 DBG2("Agent listing events for domain %d", domain
);
896 nbmem
= UST_APP_EVENT_LIST_SIZE
;
897 tmp_events
= calloc
<lttng_event
>(nbmem
);
899 PERROR("zmalloc agent list events");
905 lttng::urcu::read_lock_guard read_lock
;
907 cds_lfht_for_each_entry (
908 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
, node
.node
) {
910 struct lttng_event
*agent_events
;
912 /* Skip domain not asked by the list. */
913 if (app
->domain
!= domain
) {
917 nb_ev
= list_events(app
, &agent_events
);
923 if (count
+ nb_ev
> nbmem
) {
924 /* In case the realloc fails, we free the memory */
925 struct lttng_event
*new_tmp_events
;
928 new_nbmem
= std::max(count
+ nb_ev
, nbmem
<< 1);
929 DBG2("Reallocating agent event list from %zu to %zu entries",
932 new_tmp_events
= (lttng_event
*) realloc(
933 tmp_events
, new_nbmem
* sizeof(*new_tmp_events
));
934 if (!new_tmp_events
) {
935 PERROR("realloc agent events");
941 /* Zero the new memory */
942 memset(new_tmp_events
+ nbmem
,
944 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
946 tmp_events
= new_tmp_events
;
948 memcpy(tmp_events
+ count
, agent_events
, nb_ev
* sizeof(*tmp_events
));
955 *events
= tmp_events
;
964 * Create a agent app object using the given PID.
966 * Return newly allocated object or else NULL on error.
969 agent_create_app(pid_t pid
, enum lttng_domain_type domain
, struct lttcomm_sock
*sock
)
971 struct agent_app
*app
;
975 app
= zmalloc
<agent_app
>();
977 PERROR("Failed to allocate agent application instance");
982 app
->domain
= domain
;
984 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
991 * Lookup agent app by socket in the global hash table.
993 * RCU read side lock MUST be acquired.
995 * Return object if found else NULL.
997 struct agent_app
*agent_find_app_by_sock(int sock
)
999 struct lttng_ht_node_ulong
*node
;
1000 struct lttng_ht_iter iter
;
1001 struct agent_app
*app
;
1003 LTTNG_ASSERT(sock
>= 0);
1004 ASSERT_RCU_READ_LOCKED();
1006 lttng_ht_lookup(the_agent_apps_ht_by_sock
, (void *) ((unsigned long) sock
), &iter
);
1007 node
= lttng_ht_iter_get_node
<lttng_ht_node_ulong
>(&iter
);
1008 if (node
== nullptr) {
1011 app
= lttng::utils::container_of(node
, &agent_app::node
);
1013 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1017 DBG3("Agent app NOT found by sock %d.", sock
);
1022 * Add agent application object to the global hash table.
1024 void agent_add_app(struct agent_app
*app
)
1028 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1029 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1033 * Delete agent application from the global hash table.
1035 * rcu_read_lock() must be held by the caller.
1037 void agent_delete_app(struct agent_app
*app
)
1040 struct lttng_ht_iter iter
;
1043 ASSERT_RCU_READ_LOCKED();
1045 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1047 iter
.iter
.node
= &app
->node
.node
;
1048 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1053 * Destroy an agent application object by detaching it from its corresponding
1054 * UST app if one is connected by closing the socket. Finally, perform a
1055 * delayed memory reclaim.
1057 void agent_destroy_app(struct agent_app
*app
)
1062 app
->sock
->ops
->close(app
->sock
);
1063 lttcomm_destroy_sock(app
->sock
);
1066 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1070 * Initialize an already allocated agent object.
1072 * Return 0 on success or else a negative errno value.
1074 int agent_init(struct agent
*agt
)
1080 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1085 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1087 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1095 * Add agent object to the given hash table.
1097 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1102 DBG3("Agent adding from domain %d", agt
->domain
);
1104 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1108 * Create an agent object for the given domain.
1110 * Return the allocated agent or NULL on error.
1112 struct agent
*agent_create(enum lttng_domain_type domain
)
1117 agt
= zmalloc
<agent
>();
1121 agt
->domain
= domain
;
1123 ret
= agent_init(agt
);
1135 * Create a newly allocated agent event data structure.
1136 * Ownership of filter_expression is taken.
1138 * Return a new object else NULL on error.
1140 struct agent_event
*agent_create_event(const char *name
,
1141 enum lttng_loglevel_type loglevel_type
,
1143 struct lttng_bytecode
*filter
,
1144 char *filter_expression
)
1146 struct agent_event
*event
= nullptr;
1148 DBG3("Agent create new event with name %s, loglevel type %d, \
1149 loglevel value %d and filter %s",
1153 filter_expression
? filter_expression
: "NULL");
1156 ERR("Failed to create agent event; no name provided.");
1160 event
= zmalloc
<agent_event
>();
1165 strncpy(event
->name
, name
, sizeof(event
->name
));
1166 event
->name
[sizeof(event
->name
) - 1] = '\0';
1167 lttng_ht_node_init_str(&event
->node
, event
->name
);
1169 event
->loglevel_value
= loglevel_value
;
1170 event
->loglevel_type
= loglevel_type
;
1171 event
->filter
= filter
;
1172 event
->filter_expression
= filter_expression
;
1178 * Unique add of a agent event to an agent object.
1180 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1182 LTTNG_ASSERT(event
);
1184 LTTNG_ASSERT(agt
->events
);
1186 DBG3("Agent adding event %s", event
->name
);
1187 add_unique_agent_event(agt
->events
, event
);
1188 agt
->being_used
= 1;
1192 * Unique add of a agent context to an agent object.
1194 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1197 struct agent_app_ctx
*agent_ctx
= nullptr;
1201 LTTNG_ASSERT(agt
->events
);
1202 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1204 agent_ctx
= create_app_ctx(ctx
);
1206 ret
= LTTNG_ERR_NOMEM
;
1210 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
, ctx
->u
.app_ctx
.ctx_name
);
1211 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1217 * Find multiple agent events sharing the given name.
1219 * RCU read side lock MUST be acquired. It must be held for the
1220 * duration of the iteration.
1222 * Sets the given iterator.
1224 void agent_find_events_by_name(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1226 struct lttng_ht
*ht
;
1227 struct agent_ht_key key
;
1231 LTTNG_ASSERT(agt
->events
);
1233 ASSERT_RCU_READ_LOCKED();
1238 cds_lfht_lookup(ht
->ht
,
1239 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1240 ht_match_event_by_name
,
1246 * Find the agent event matching a trigger.
1248 * RCU read side lock MUST be acquired. It must be held for as long as
1249 * the returned agent_event is used.
1251 * Return object if found else NULL.
1253 struct agent_event
*agent_find_event_by_trigger(const struct lttng_trigger
*trigger
,
1256 enum lttng_condition_status c_status
;
1257 enum lttng_event_rule_status er_status
;
1258 enum lttng_domain_type domain
;
1259 const struct lttng_condition
*condition
;
1260 const struct lttng_event_rule
*rule
;
1262 const char *filter_expression
;
1263 const struct lttng_log_level_rule
*log_level_rule
;
1264 /* Unused when loglevel_type is 'ALL'. */
1265 int loglevel_value
= 0;
1266 enum lttng_loglevel_type loglevel_type
;
1267 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1268 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1271 LTTNG_ASSERT(agt
->events
);
1272 ASSERT_RCU_READ_LOCKED();
1274 condition
= lttng_trigger_get_const_condition(trigger
);
1276 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1277 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1279 c_status
= lttng_condition_event_rule_matches_get_rule(condition
, &rule
);
1280 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1282 switch (lttng_event_rule_get_type(rule
)) {
1283 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1284 logging_get_name_pattern
= lttng_event_rule_jul_logging_get_name_pattern
;
1285 logging_get_log_level_rule
= lttng_event_rule_jul_logging_get_log_level_rule
;
1287 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1288 logging_get_name_pattern
= lttng_event_rule_log4j_logging_get_name_pattern
;
1289 logging_get_log_level_rule
= lttng_event_rule_log4j_logging_get_log_level_rule
;
1291 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1292 logging_get_name_pattern
= lttng_event_rule_python_logging_get_name_pattern
;
1293 logging_get_log_level_rule
= lttng_event_rule_python_logging_get_log_level_rule
;
1300 domain
= lttng_event_rule_get_domain_type(rule
);
1301 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1302 domain
== LTTNG_DOMAIN_PYTHON
);
1304 /* Get the event's pattern name ('name' in the legacy terminology). */
1305 er_status
= logging_get_name_pattern(rule
, &name
);
1306 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1308 /* Get the internal filter expression. */
1309 filter_expression
= lttng_event_rule_get_filter(rule
);
1311 /* Map log_level_rule to loglevel value. */
1312 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1313 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1314 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1316 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1317 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1322 return agent_find_event(name
, loglevel_type
, loglevel_value
, filter_expression
, agt
);
1326 * Get the next agent event duplicate by name. This should be called
1327 * after a call to agent_find_events_by_name() to iterate on events.
1329 * The RCU read lock must be held during the iteration and for as long
1330 * as the object the iterator points to remains in use.
1332 void agent_event_next_duplicate(const char *name
, struct agent
*agt
, struct lttng_ht_iter
*iter
)
1334 struct agent_ht_key key
;
1336 ASSERT_RCU_READ_LOCKED();
1340 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
, &key
, &iter
->iter
);
1344 * Find a agent event in the given agent using name, loglevel and filter.
1346 * RCU read side lock MUST be acquired. It must be kept for as long as
1347 * the returned agent_event is used.
1349 * Return object if found else NULL.
1351 struct agent_event
*agent_find_event(const char *name
,
1352 enum lttng_loglevel_type loglevel_type
,
1354 const char *filter_expression
,
1357 struct lttng_ht_node_str
*node
;
1358 struct lttng_ht_iter iter
;
1359 struct lttng_ht
*ht
;
1360 struct agent_ht_key key
;
1364 LTTNG_ASSERT(agt
->events
);
1365 ASSERT_RCU_READ_LOCKED();
1369 key
.loglevel_value
= loglevel_value
;
1370 key
.loglevel_type
= loglevel_type
;
1371 key
.filter_expression
= filter_expression
;
1373 cds_lfht_lookup(ht
->ht
,
1374 ht
->hash_fct((void *) name
, lttng_ht_seed
),
1378 node
= lttng_ht_iter_get_node
<lttng_ht_node_str
>(&iter
);
1379 if (node
== nullptr) {
1383 DBG3("Agent event found %s.", name
);
1384 return lttng::utils::container_of(node
, &agent_event::node
);
1387 DBG3("Agent event NOT found %s.", name
);
1392 * Free given agent event. This event must not be globally visible at this
1393 * point (only expected to be used on failure just after event creation). After
1394 * this call, the pointer is not usable anymore.
1396 void agent_destroy_event(struct agent_event
*event
)
1398 LTTNG_ASSERT(event
);
1400 free(event
->filter
);
1401 free(event
->filter_expression
);
1402 free(event
->exclusion
);
1406 static void destroy_app_ctx_rcu(struct rcu_head
*head
)
1408 struct agent_app_ctx
*ctx
= lttng::utils::container_of(head
, &agent_app_ctx::rcu_node
);
1410 destroy_app_ctx(ctx
);
1414 * Destroy an agent completely.
1416 void agent_destroy(struct agent
*agt
)
1418 struct lttng_ht_node_str
*node
;
1419 struct lttng_ht_iter iter
;
1420 struct agent_app_ctx
*ctx
;
1424 DBG3("Agent destroy");
1427 lttng::urcu::read_lock_guard read_lock
;
1429 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, node
, node
) {
1431 struct agent_event
*event
;
1434 * When destroying an event, we have to try to disable it on the
1435 * agent side so the event stops generating data. The return
1436 * value is not important since we have to continue anyway
1437 * destroying the object.
1439 event
= lttng::utils::container_of(node
, &agent_event::node
);
1440 (void) agent_disable_event(event
, agt
->domain
);
1442 ret
= lttng_ht_del(agt
->events
, &iter
);
1444 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1447 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1449 (void) disable_context(ctx
, agt
->domain
);
1450 cds_list_del(&ctx
->list_node
);
1451 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1455 lttng_ht_destroy(agt
->events
);
1460 * Allocate agent_apps_ht_by_sock.
1462 int agent_app_ht_alloc()
1464 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1465 return the_agent_apps_ht_by_sock
? 0 : -1;
1469 * Destroy a agent application by socket.
1471 void agent_destroy_app_by_sock(int sock
)
1473 struct agent_app
*app
;
1475 LTTNG_ASSERT(sock
>= 0);
1478 * Not finding an application is a very important error that should NEVER
1479 * happen. The hash table deletion is ONLY done through this call when the
1480 * main sessiond thread is torn down.
1482 lttng::urcu::read_lock_guard read_lock
;
1483 app
= agent_find_app_by_sock(sock
);
1486 /* RCU read side lock is assumed to be held by this function. */
1487 agent_delete_app(app
);
1489 /* The application is freed in a RCU call but the socket is closed here. */
1490 agent_destroy_app(app
);
1494 * Clean-up the agent app hash table and destroy it.
1496 void agent_app_ht_clean()
1498 struct lttng_ht_node_ulong
*node
;
1499 struct lttng_ht_iter iter
;
1501 if (!the_agent_apps_ht_by_sock
) {
1506 lttng::urcu::read_lock_guard read_lock
;
1508 cds_lfht_for_each_entry (the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1509 struct agent_app
*app
;
1511 app
= lttng::utils::container_of(node
, &agent_app::node
);
1512 agent_destroy_app_by_sock(app
->sock
->fd
);
1516 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1520 * Update a agent application (given socket) using the given agent.
1522 * Note that this function is most likely to be used with a tracing session
1523 * thus the caller should make sure to hold the appropriate lock(s).
1525 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1528 struct agent_event
*event
;
1529 struct lttng_ht_iter iter
;
1530 struct agent_app_ctx
*ctx
;
1535 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1538 * We are in the registration path thus if the application is gone,
1539 * there is a serious code flow error.
1542 lttng::urcu::read_lock_guard read_lock
;
1544 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1545 /* Skip event if disabled. */
1546 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1550 ret
= enable_event(app
, event
);
1551 if (ret
!= LTTNG_OK
) {
1552 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1556 /* Let's try the others here and don't assume the app is dead. */
1561 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
)
1563 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1564 if (ret
!= LTTNG_OK
) {
1565 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1577 * Allocate the per-event notifier domain agent hash table. It is lazily
1578 * populated as domains are used.
1580 int agent_by_event_notifier_domain_ht_create()
1582 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1583 return the_trigger_agents_ht_by_domain
? 0 : -1;
1587 * Clean-up the per-event notifier domain agent hash table and destroy it.
1589 void agent_by_event_notifier_domain_ht_destroy()
1591 struct lttng_ht_node_u64
*node
;
1592 struct lttng_ht_iter iter
;
1594 if (!the_trigger_agents_ht_by_domain
) {
1599 lttng::urcu::read_lock_guard read_lock
;
1601 cds_lfht_for_each_entry (
1602 the_trigger_agents_ht_by_domain
->ht
, &iter
.iter
, node
, node
) {
1603 struct agent
*agent
= lttng::utils::container_of(node
, &agent::node
);
1604 const int ret
= lttng_ht_del(the_trigger_agents_ht_by_domain
, &iter
);
1606 LTTNG_ASSERT(ret
== 0);
1607 agent_destroy(agent
);
1611 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1614 struct agent
*agent_find_by_event_notifier_domain(enum lttng_domain_type domain_type
)
1616 struct agent
*agt
= nullptr;
1617 struct lttng_ht_node_u64
*node
;
1618 struct lttng_ht_iter iter
;
1619 const uint64_t key
= (uint64_t) domain_type
;
1621 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1623 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1624 lttng_domain_type_str(domain_type
));
1626 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1627 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
1632 agt
= lttng::utils::container_of(node
, &agent::node
);