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 <urcu/uatomic.h>
12 #include <urcu/rculist.h>
14 #include <lttng/event-rule/event-rule.h>
15 #include <lttng/event-rule/event-rule-internal.h>
16 #include <lttng/event-rule/jul-logging.h>
17 #include <lttng/event-rule/log4j-logging.h>
18 #include <lttng/event-rule/python-logging.h>
19 #include <lttng/condition/condition.h>
20 #include <lttng/condition/event-rule-matches.h>
21 #include <lttng/domain-internal.h>
22 #include <lttng/log-level-rule-internal.h>
24 #include <common/common.h>
25 #include <common/sessiond-comm/agent.h>
27 #include <common/compat/endian.h>
32 #include "common/error.h"
34 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
36 typedef enum lttng_event_rule_status (*event_rule_logging_get_name_pattern
)(
37 const struct lttng_event_rule
*rule
, const char **pattern
);
38 typedef enum lttng_event_rule_status (*event_rule_logging_get_log_level_rule
)(
39 const struct lttng_event_rule
*rule
,
40 const struct lttng_log_level_rule
**log_level_rule
);
43 * Agent application context representation.
45 struct agent_app_ctx
{
49 /* agent_app_ctx are part of the agent app_ctx_list. */
50 struct cds_list_head list_node
;
52 /* For call_rcu teardown. */
53 struct rcu_head rcu_node
;
57 * Human readable agent return code.
59 static const char *error_string_array
[] = {
60 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
61 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
62 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
65 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
69 void log_reply_code(uint32_t in_reply_ret_code
)
71 int level
= PRINT_DBG3
;
73 * reply_ret_code and in_reply_ret_code are kept separate to have a
74 * sanitized value (used to retrieve the human readable string) and the
75 * original value which is logged as-is.
77 uint32_t reply_ret_code
= in_reply_ret_code
;
79 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
80 reply_ret_code
>= AGENT_RET_CODE_NR
) {
81 reply_ret_code
= AGENT_RET_CODE_NR
;
85 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
86 error_string_array
[AGENT_RET_CODE_INDEX(
92 * Match function for the events hash table lookup by name.
94 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
97 struct agent_event
*event
;
98 const struct agent_ht_key
*key
;
103 event
= caa_container_of(node
, struct agent_event
, node
.node
);
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
,
126 struct agent_event
*event
;
127 const struct agent_ht_key
*key
;
133 event
= caa_container_of(node
, struct agent_event
, node
.node
);
136 /* Match 2 elements of the key: name and loglevel. */
139 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
143 /* Event loglevel value and type. */
144 ll_match
= loglevels_match(event
->loglevel_type
,
145 event
->loglevel_value
, key
->loglevel_type
,
146 key
->loglevel_value
, 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
, key
->filter_expression
,
160 strlen(event
->filter_expression
)) != 0) {
172 * Add unique agent event based on the event name and loglevel.
174 static void add_unique_agent_event(struct lttng_ht
*ht
,
175 struct agent_event
*event
)
177 struct cds_lfht_node
*node_ptr
;
178 struct agent_ht_key key
;
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
),
191 ht_match_event
, &key
, &event
->node
.node
);
192 assert(node_ptr
== &event
->node
.node
);
196 * URCU delayed agent event reclaim.
198 static void destroy_event_agent_rcu(struct rcu_head
*head
)
200 struct lttng_ht_node_str
*node
=
201 caa_container_of(head
, struct lttng_ht_node_str
, head
);
202 struct agent_event
*event
=
203 caa_container_of(node
, struct 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 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
215 struct agent_app
*app
=
216 caa_container_of(node
, struct 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.
227 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
228 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
,
267 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
279 * Communication call with the agent. Receive reply from the agent using the
282 * Return 0 on success or else a negative errno value from recvmsg() op.
284 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
292 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
304 * Internal event listing for a given app. Populate events.
306 * Return number of element in the list or else a negative LTTNG_ERR* code.
307 * On success, the caller is responsible for freeing the memory
308 * allocated for "events".
310 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
312 int ret
, i
, len
= 0, offset
= 0;
315 uint32_t reply_ret_code
;
316 struct lttng_event
*tmp_events
= NULL
;
317 struct lttcomm_agent_list_reply
*reply
= NULL
;
318 struct lttcomm_agent_list_reply_hdr reply_hdr
;
324 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
327 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
332 /* Get list header so we know how much we'll receive. */
333 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
338 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
339 log_reply_code(reply_ret_code
);
340 switch (reply_ret_code
) {
341 case AGENT_RET_CODE_SUCCESS
:
342 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
349 reply
= zmalloc(data_size
);
351 ret
= LTTNG_ERR_NOMEM
;
355 /* Get the list with the appropriate data size. */
356 ret
= recv_reply(app
->sock
, reply
, data_size
);
361 nb_event
= be32toh(reply
->nb_event
);
362 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
364 ret
= LTTNG_ERR_NOMEM
;
368 for (i
= 0; i
< nb_event
; i
++) {
370 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
371 sizeof(tmp_events
[i
].name
))) {
372 ret
= LTTNG_ERR_INVALID
;
375 tmp_events
[i
].pid
= app
->pid
;
376 tmp_events
[i
].enabled
= -1;
377 len
= strlen(reply
->payload
+ offset
) + 1;
380 *events
= tmp_events
;
386 ret
= LTTNG_ERR_UST_LIST_FAIL
;
395 * Internal enable agent event on a agent application. This function
396 * communicates with the agent to enable a given event.
398 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
400 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
405 size_t filter_expression_length
;
406 uint32_t reply_ret_code
;
407 struct lttcomm_agent_enable_event msg
;
408 struct lttcomm_agent_generic_reply reply
;
414 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
415 app
->pid
, app
->sock
->fd
);
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
= zmalloc(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
), 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.
490 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
, 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
;
526 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
527 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",
531 ctx
->provider_name
, ctx
->ctx_name
,
532 app
->pid
, app
->sock
->fd
);
535 * Calculate the payload's size, which consists of the size (u32, BE)
536 * of the provider name, the NULL-terminated provider name string, the
537 * size (u32, BE) of the context name, followed by the NULL-terminated
538 * context name string.
540 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
541 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
542 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
543 sizeof(uint32_t) + app_ctx_name_len
;
545 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
550 if (app_ctx_provider_name_len
> UINT32_MAX
||
551 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
,
558 (uint32_t) app_ctx_provider_name_len
);
563 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
564 (uint32_t) app_ctx_name_len
);
569 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
574 reply_ret_code
= be32toh(reply
.ret_code
);
575 log_reply_code(reply_ret_code
);
576 switch (reply_ret_code
) {
577 case AGENT_RET_CODE_SUCCESS
:
587 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
593 * Internal disable agent event call on a agent application. This function
594 * communicates with the agent to disable a given event.
596 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
598 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
602 uint32_t reply_ret_code
;
603 struct lttcomm_agent_disable_event msg
;
604 struct lttcomm_agent_generic_reply reply
;
610 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
611 app
->pid
, app
->sock
->fd
);
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
)
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
,
678 enum lttng_domain_type domain
)
681 struct agent_app
*app
;
682 struct lttng_ht_iter iter
;
688 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
690 if (app
->domain
!= domain
) {
694 /* Enable event on agent application through TCP socket. */
695 ret
= enable_event(app
, event
);
696 if (ret
!= LTTNG_OK
) {
701 event
->enabled_count
++;
710 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
712 free(ctx
->provider_name
);
718 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
720 struct agent_app_ctx
*agent_ctx
= NULL
;
726 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
727 agent_ctx
= zmalloc(sizeof(*ctx
));
732 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
733 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
734 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
735 destroy_app_ctx(agent_ctx
);
743 * Enable agent context on every agent applications registered with the session
746 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
748 int agent_enable_context(const struct lttng_event_context
*ctx
,
749 enum lttng_domain_type domain
)
752 struct agent_app
*app
;
753 struct lttng_ht_iter iter
;
756 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
757 ret
= LTTNG_ERR_INVALID
;
763 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
765 struct agent_app_ctx
*agent_ctx
;
767 if (app
->domain
!= domain
) {
771 agent_ctx
= create_app_ctx(ctx
);
773 ret
= LTTNG_ERR_NOMEM
;
777 /* Enable event on agent application through TCP socket. */
778 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
779 destroy_app_ctx(agent_ctx
);
780 if (ret
!= LTTNG_OK
) {
794 * Disable agent event on every agent application registered with the session
797 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
799 int agent_disable_event(struct agent_event
*event
,
800 enum lttng_domain_type domain
)
803 struct agent_app
*app
;
804 struct lttng_ht_iter iter
;
807 if (!AGENT_EVENT_IS_ENABLED(event
)) {
811 if (--event
->enabled_count
!= 0) {
813 * Agent event still enabled. Disable the agent event only when
814 * all "users" have disabled it (event notifiers, event rules,
823 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
825 if (app
->domain
!= domain
) {
829 /* Enable event on agent application through TCP socket. */
830 ret
= disable_event(app
, event
);
831 if (ret
!= LTTNG_OK
) {
836 /* event->enabled_count is now 0. */
837 assert(!AGENT_EVENT_IS_ENABLED(event
));
846 * Disable agent context on every agent application registered with the session
849 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
851 static int disable_context(struct agent_app_ctx
*ctx
,
852 enum lttng_domain_type domain
)
855 struct agent_app
*app
;
856 struct lttng_ht_iter iter
;
861 DBG2("Disabling agent application context %s:%s",
862 ctx
->provider_name
, ctx
->ctx_name
);
863 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
865 if (app
->domain
!= domain
) {
869 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
870 if (ret
!= LTTNG_OK
) {
880 * Ask every agent for the list of possible event. Events is allocated with the
881 * events of every agent application.
883 * Return the number of events or else a negative value.
885 int agent_list_events(struct lttng_event
**events
,
886 enum lttng_domain_type domain
)
889 size_t nbmem
, count
= 0;
890 struct agent_app
*app
;
891 struct lttng_event
*tmp_events
= NULL
;
892 struct lttng_ht_iter iter
;
896 DBG2("Agent listing events for domain %d", domain
);
898 nbmem
= UST_APP_EVENT_LIST_SIZE
;
899 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
901 PERROR("zmalloc agent list events");
907 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
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
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
929 DBG2("Reallocating agent event list from %zu to %zu entries",
931 new_tmp_events
= realloc(tmp_events
,
932 new_nbmem
* sizeof(*new_tmp_events
));
933 if (!new_tmp_events
) {
934 PERROR("realloc agent events");
939 /* Zero the new memory */
940 memset(new_tmp_events
+ nbmem
, 0,
941 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
943 tmp_events
= new_tmp_events
;
945 memcpy(tmp_events
+ count
, agent_events
,
946 nb_ev
* sizeof(*tmp_events
));
953 *events
= tmp_events
;
964 * Create a agent app object using the given PID.
966 * Return newly allocated object or else NULL on error.
968 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
969 struct lttcomm_sock
*sock
)
971 struct agent_app
*app
;
975 app
= zmalloc(sizeof(*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
;
1005 lttng_ht_lookup(the_agent_apps_ht_by_sock
,
1006 (void *) ((unsigned long) sock
), &iter
);
1007 node
= lttng_ht_iter_get_node_ulong(&iter
);
1011 app
= caa_container_of(node
, struct 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
;
1044 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1046 iter
.iter
.node
= &app
->node
.node
;
1047 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1052 * Destroy an agent application object by detaching it from its corresponding
1053 * UST app if one is connected by closing the socket. Finally, perform a
1054 * delayed memory reclaim.
1056 void agent_destroy_app(struct agent_app
*app
)
1061 app
->sock
->ops
->close(app
->sock
);
1062 lttcomm_destroy_sock(app
->sock
);
1065 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1069 * Initialize an already allocated agent object.
1071 * Return 0 on success or else a negative errno value.
1073 int agent_init(struct agent
*agt
)
1079 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1084 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1086 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1094 * Add agent object to the given hash table.
1096 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1101 DBG3("Agent adding from domain %d", agt
->domain
);
1103 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1107 * Create an agent object for the given domain.
1109 * Return the allocated agent or NULL on error.
1111 struct agent
*agent_create(enum lttng_domain_type domain
)
1116 agt
= zmalloc(sizeof(struct agent
));
1120 agt
->domain
= domain
;
1122 ret
= agent_init(agt
);
1134 * Create a newly allocated agent event data structure.
1135 * Ownership of filter_expression is taken.
1137 * Return a new object else NULL on error.
1139 struct agent_event
*agent_create_event(const char *name
,
1140 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1141 struct lttng_bytecode
*filter
, char *filter_expression
)
1143 struct agent_event
*event
= NULL
;
1145 DBG3("Agent create new event with name %s, loglevel type %d, \
1146 loglevel value %d and filter %s",
1147 name
, loglevel_type
, loglevel_value
,
1148 filter_expression
? filter_expression
: "NULL");
1151 ERR("Failed to create agent event; no name provided.");
1155 event
= zmalloc(sizeof(*event
));
1160 strncpy(event
->name
, name
, sizeof(event
->name
));
1161 event
->name
[sizeof(event
->name
) - 1] = '\0';
1162 lttng_ht_node_init_str(&event
->node
, event
->name
);
1164 event
->loglevel_value
= loglevel_value
;
1165 event
->loglevel_type
= loglevel_type
;
1166 event
->filter
= filter
;
1167 event
->filter_expression
= filter_expression
;
1173 * Unique add of a agent event to an agent object.
1175 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1179 assert(agt
->events
);
1181 DBG3("Agent adding event %s", event
->name
);
1182 add_unique_agent_event(agt
->events
, event
);
1183 agt
->being_used
= 1;
1187 * Unique add of a agent context to an agent object.
1189 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1192 struct agent_app_ctx
*agent_ctx
= NULL
;
1196 assert(agt
->events
);
1197 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1199 agent_ctx
= create_app_ctx(ctx
);
1201 ret
= LTTNG_ERR_NOMEM
;
1205 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1206 ctx
->u
.app_ctx
.ctx_name
);
1207 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1213 * Find multiple agent events sharing the given name.
1215 * RCU read side lock MUST be acquired. It must be held for the
1216 * duration of the iteration.
1218 * Sets the given iterator.
1220 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1221 struct lttng_ht_iter
* iter
)
1223 struct lttng_ht
*ht
;
1224 struct agent_ht_key key
;
1228 assert(agt
->events
);
1234 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1235 ht_match_event_by_name
, &key
, &iter
->iter
);
1239 * Find the agent event matching a trigger.
1241 * RCU read side lock MUST be acquired. It must be held for as long as
1242 * the returned agent_event is used.
1244 * Return object if found else NULL.
1246 struct agent_event
*agent_find_event_by_trigger(
1247 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1249 enum lttng_condition_status c_status
;
1250 enum lttng_event_rule_status er_status
;
1251 enum lttng_domain_type domain
;
1252 const struct lttng_condition
*condition
;
1253 const struct lttng_event_rule
*rule
;
1255 const char *filter_expression
;
1256 const struct lttng_log_level_rule
*log_level_rule
;
1257 /* Unused when loglevel_type is 'ALL'. */
1258 int loglevel_value
= 0;
1259 enum lttng_loglevel_type loglevel_type
;
1260 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1261 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1264 assert(agt
->events
);
1266 condition
= lttng_trigger_get_const_condition(trigger
);
1268 assert(lttng_condition_get_type(condition
) ==
1269 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1271 c_status
= lttng_condition_event_rule_matches_get_rule(
1273 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
1275 switch (lttng_event_rule_get_type(rule
)) {
1276 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1277 logging_get_name_pattern
=
1278 lttng_event_rule_jul_logging_get_name_pattern
;
1279 logging_get_log_level_rule
=
1280 lttng_event_rule_jul_logging_get_log_level_rule
;
1282 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1283 logging_get_name_pattern
=
1284 lttng_event_rule_log4j_logging_get_name_pattern
;
1285 logging_get_log_level_rule
=
1286 lttng_event_rule_log4j_logging_get_log_level_rule
;
1288 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1289 logging_get_name_pattern
=
1290 lttng_event_rule_python_logging_get_name_pattern
;
1291 logging_get_log_level_rule
=
1292 lttng_event_rule_python_logging_get_log_level_rule
;
1299 domain
= lttng_event_rule_get_domain_type(rule
);
1300 assert(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1301 domain
== LTTNG_DOMAIN_PYTHON
);
1303 /* Get the event's pattern name ('name' in the legacy terminology). */
1304 er_status
= logging_get_name_pattern(rule
, &name
);
1305 assert(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1307 /* Get the internal filter expression. */
1308 filter_expression
= lttng_event_rule_get_filter(rule
);
1310 /* Map log_level_rule to loglevel value. */
1311 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1312 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1313 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1315 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1316 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1321 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1322 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
,
1333 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1335 struct agent_ht_key key
;
1339 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
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 assert(agt
->events
);
1368 key
.loglevel_value
= loglevel_value
;
1369 key
.loglevel_type
= loglevel_type
;
1370 key
.filter_expression
= filter_expression
;
1372 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1373 ht_match_event
, &key
, &iter
.iter
);
1374 node
= lttng_ht_iter_get_node_str(&iter
);
1379 DBG3("Agent event found %s.", name
);
1380 return caa_container_of(node
, struct agent_event
, node
);
1383 DBG3("Agent event NOT found %s.", name
);
1388 * Free given agent event. This event must not be globally visible at this
1389 * point (only expected to be used on failure just after event creation). After
1390 * this call, the pointer is not usable anymore.
1392 void agent_destroy_event(struct agent_event
*event
)
1396 free(event
->filter
);
1397 free(event
->filter_expression
);
1398 free(event
->exclusion
);
1403 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1405 struct agent_app_ctx
*ctx
=
1406 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1408 destroy_app_ctx(ctx
);
1412 * Destroy an agent completely.
1414 void agent_destroy(struct agent
*agt
)
1416 struct lttng_ht_node_str
*node
;
1417 struct lttng_ht_iter iter
;
1418 struct agent_app_ctx
*ctx
;
1422 DBG3("Agent destroy");
1425 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1427 struct agent_event
*event
;
1430 * When destroying an event, we have to try to disable it on the
1431 * agent side so the event stops generating data. The return
1432 * value is not important since we have to continue anyway
1433 * destroying the object.
1435 event
= caa_container_of(node
, struct agent_event
, node
);
1436 (void) agent_disable_event(event
, agt
->domain
);
1438 ret
= lttng_ht_del(agt
->events
, &iter
);
1440 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1443 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1444 (void) disable_context(ctx
, agt
->domain
);
1445 cds_list_del(&ctx
->list_node
);
1446 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1449 ht_cleanup_push(agt
->events
);
1454 * Allocate agent_apps_ht_by_sock.
1456 int agent_app_ht_alloc(void)
1458 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1459 return the_agent_apps_ht_by_sock
? 0 : -1;
1463 * Destroy a agent application by socket.
1465 void agent_destroy_app_by_sock(int sock
)
1467 struct agent_app
*app
;
1472 * Not finding an application is a very important error that should NEVER
1473 * happen. The hash table deletion is ONLY done through this call when the
1474 * main sessiond thread is torn down.
1477 app
= agent_find_app_by_sock(sock
);
1480 /* RCU read side lock is assumed to be held by this function. */
1481 agent_delete_app(app
);
1483 /* The application is freed in a RCU call but the socket is closed here. */
1484 agent_destroy_app(app
);
1489 * Clean-up the agent app hash table and destroy it.
1491 void agent_app_ht_clean(void)
1493 struct lttng_ht_node_ulong
*node
;
1494 struct lttng_ht_iter iter
;
1496 if (!the_agent_apps_ht_by_sock
) {
1500 cds_lfht_for_each_entry(
1501 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1502 struct agent_app
*app
;
1504 app
= caa_container_of(node
, struct agent_app
, node
);
1505 agent_destroy_app_by_sock(app
->sock
->fd
);
1509 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1513 * Update a agent application (given socket) using the given agent.
1515 * Note that this function is most likely to be used with a tracing session
1516 * thus the caller should make sure to hold the appropriate lock(s).
1518 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1521 struct agent_event
*event
;
1522 struct lttng_ht_iter iter
;
1523 struct agent_app_ctx
*ctx
;
1528 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1532 * We are in the registration path thus if the application is gone,
1533 * there is a serious code flow error.
1536 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1537 /* Skip event if disabled. */
1538 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1542 ret
= enable_event(app
, event
);
1543 if (ret
!= LTTNG_OK
) {
1544 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1545 event
->name
, app
->pid
, app
->sock
->fd
);
1546 /* Let's try the others here and don't assume the app is dead. */
1551 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1552 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1553 if (ret
!= LTTNG_OK
) {
1554 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1555 ctx
->provider_name
, ctx
->ctx_name
,
1556 app
->pid
, app
->sock
->fd
);
1565 * Allocate the per-event notifier domain agent hash table. It is lazily
1566 * populated as domains are used.
1568 int agent_by_event_notifier_domain_ht_create(void)
1570 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1571 return the_trigger_agents_ht_by_domain
? 0 : -1;
1575 * Clean-up the per-event notifier domain agent hash table and destroy it.
1577 void agent_by_event_notifier_domain_ht_destroy(void)
1579 struct lttng_ht_node_u64
*node
;
1580 struct lttng_ht_iter iter
;
1582 if (!the_trigger_agents_ht_by_domain
) {
1587 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
1588 &iter
.iter
, node
, node
) {
1589 struct agent
*agent
=
1590 caa_container_of(node
, struct agent
, node
);
1591 const int ret
= lttng_ht_del(
1592 the_trigger_agents_ht_by_domain
, &iter
);
1595 agent_destroy(agent
);
1599 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1602 struct agent
*agent_find_by_event_notifier_domain(
1603 enum lttng_domain_type domain_type
)
1605 struct agent
*agt
= NULL
;
1606 struct lttng_ht_node_u64
*node
;
1607 struct lttng_ht_iter iter
;
1608 const uint64_t key
= (uint64_t) domain_type
;
1610 assert(the_trigger_agents_ht_by_domain
);
1612 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1613 lttng_domain_type_str(domain_type
));
1615 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1616 node
= lttng_ht_iter_get_node_u64(&iter
);
1621 agt
= caa_container_of(node
, struct agent
, node
);