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
10 #include <urcu/uatomic.h>
11 #include <urcu/rculist.h>
13 #include <lttng/event-rule/event-rule.h>
14 #include <lttng/event-rule/event-rule-internal.h>
15 #include <lttng/event-rule/jul-logging.h>
16 #include <lttng/event-rule/log4j-logging.h>
17 #include <lttng/event-rule/python-logging.h>
18 #include <lttng/condition/condition.h>
19 #include <lttng/condition/event-rule-matches.h>
20 #include <lttng/domain-internal.h>
21 #include <lttng/log-level-rule-internal.h>
23 #include <common/common.h>
24 #include <common/sessiond-comm/agent.h>
26 #include <common/compat/endian.h>
31 #include "common/error.h"
33 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
35 typedef enum lttng_event_rule_status (*event_rule_logging_get_name_pattern
)(
36 const struct lttng_event_rule
*rule
, const char **pattern
);
37 typedef enum lttng_event_rule_status (*event_rule_logging_get_log_level_rule
)(
38 const struct lttng_event_rule
*rule
,
39 const struct lttng_log_level_rule
**log_level_rule
);
42 * Agent application context representation.
44 struct agent_app_ctx
{
48 /* agent_app_ctx are part of the agent app_ctx_list. */
49 struct cds_list_head list_node
;
51 /* For call_rcu teardown. */
52 struct rcu_head rcu_node
;
56 * Human readable agent return code.
58 static const char *error_string_array
[] = {
59 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
60 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
61 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
64 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
68 void log_reply_code(uint32_t in_reply_ret_code
)
70 int level
= PRINT_DBG3
;
72 * reply_ret_code and in_reply_ret_code are kept separate to have a
73 * sanitized value (used to retrieve the human readable string) and the
74 * original value which is logged as-is.
76 uint32_t reply_ret_code
= in_reply_ret_code
;
78 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
79 reply_ret_code
>= AGENT_RET_CODE_NR
) {
80 reply_ret_code
= AGENT_RET_CODE_NR
;
84 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
85 error_string_array
[AGENT_RET_CODE_INDEX(
91 * Match function for the events hash table lookup by name.
93 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
96 struct agent_event
*event
;
97 const struct agent_ht_key
*key
;
102 event
= caa_container_of(node
, struct agent_event
, node
.node
);
105 /* Match 1 elements of the key: name. */
108 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
119 * Match function for the events hash table lookup by name, log level and
122 static int ht_match_event(struct cds_lfht_node
*node
,
125 struct agent_event
*event
;
126 const struct agent_ht_key
*key
;
132 event
= caa_container_of(node
, struct agent_event
, node
.node
);
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
, key
->loglevel_type
,
145 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
151 /* Filter expression */
152 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
153 /* One has a filter expression, the other does not */
157 if (event
->filter_expression
) {
158 if (strncmp(event
->filter_expression
, key
->filter_expression
,
159 strlen(event
->filter_expression
)) != 0) {
171 * Add unique agent event based on the event name and loglevel.
173 static void add_unique_agent_event(struct lttng_ht
*ht
,
174 struct agent_event
*event
)
176 struct cds_lfht_node
*node_ptr
;
177 struct agent_ht_key key
;
180 LTTNG_ASSERT(ht
->ht
);
183 key
.name
= event
->name
;
184 key
.loglevel_value
= event
->loglevel_value
;
185 key
.loglevel_type
= event
->loglevel_type
;
186 key
.filter_expression
= event
->filter_expression
;
188 node_ptr
= cds_lfht_add_unique(ht
->ht
,
189 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
190 ht_match_event
, &key
, &event
->node
.node
);
191 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
195 * URCU delayed agent event reclaim.
197 static void destroy_event_agent_rcu(struct rcu_head
*head
)
199 struct lttng_ht_node_str
*node
=
200 caa_container_of(head
, struct lttng_ht_node_str
, head
);
201 struct agent_event
*event
=
202 caa_container_of(node
, struct agent_event
, node
);
204 agent_destroy_event(event
);
208 * URCU delayed agent app reclaim.
210 static void destroy_app_agent_rcu(struct rcu_head
*head
)
212 struct lttng_ht_node_ulong
*node
=
213 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
214 struct agent_app
*app
=
215 caa_container_of(node
, struct agent_app
, node
);
221 * Communication with the agent. Send the message header to the given socket in
224 * Return 0 on success or else a negative errno message of sendmsg() op.
226 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
227 uint32_t cmd
, uint32_t cmd_version
)
231 struct lttcomm_agent_hdr msg
;
235 memset(&msg
, 0, sizeof(msg
));
236 msg
.data_size
= htobe64(data_size
);
237 msg
.cmd
= htobe32(cmd
);
238 msg
.cmd_version
= htobe32(cmd_version
);
240 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
241 if (size
< sizeof(msg
)) {
252 * Communication call with the agent. Send the payload to the given socket. The
253 * header MUST be sent prior to this call.
255 * Return 0 on success or else a negative errno value of sendmsg() op.
257 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
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
= NULL
;
316 struct lttcomm_agent_list_reply
*reply
= NULL
;
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
,
326 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
331 /* Get list header so we know how much we'll receive. */
332 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
337 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
338 log_reply_code(reply_ret_code
);
339 switch (reply_ret_code
) {
340 case AGENT_RET_CODE_SUCCESS
:
341 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
348 reply
= zmalloc(data_size
);
350 ret
= LTTNG_ERR_NOMEM
;
354 /* Get the list with the appropriate data size. */
355 ret
= recv_reply(app
->sock
, reply
, data_size
);
360 nb_event
= be32toh(reply
->nb_event
);
361 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
363 ret
= LTTNG_ERR_NOMEM
;
367 for (i
= 0; i
< nb_event
; i
++) {
369 if (lttng_strncpy(tmp_events
[i
].name
, 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
;
394 * Internal enable agent event on a agent application. This function
395 * communicates with the agent to enable a given event.
397 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
399 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
404 size_t filter_expression_length
;
405 uint32_t reply_ret_code
;
406 struct lttcomm_agent_enable_event msg
;
407 struct lttcomm_agent_generic_reply reply
;
410 LTTNG_ASSERT(app
->sock
);
413 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
414 app
->pid
, app
->sock
->fd
);
417 * Calculate the payload's size, which is the fixed-size struct followed
418 * by the variable-length filter expression (+1 for the ending \0).
420 if (!event
->filter_expression
) {
421 filter_expression_length
= 0;
423 filter_expression_length
= strlen(event
->filter_expression
) + 1;
425 data_size
= sizeof(msg
) + filter_expression_length
;
427 memset(&msg
, 0, sizeof(msg
));
428 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
429 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
430 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
431 ret
= LTTNG_ERR_INVALID
;
434 msg
.filter_expression_length
= htobe32(filter_expression_length
);
436 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
441 bytes_to_send
= zmalloc(data_size
);
442 if (!bytes_to_send
) {
443 ret
= LTTNG_ERR_NOMEM
;
447 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
448 if (filter_expression_length
> 0) {
449 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
450 filter_expression_length
);
453 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
459 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
464 reply_ret_code
= be32toh(reply
.ret_code
);
465 log_reply_code(reply_ret_code
);
466 switch (reply_ret_code
) {
467 case AGENT_RET_CODE_SUCCESS
:
469 case AGENT_RET_CODE_UNKNOWN_NAME
:
470 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
480 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
486 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
489 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
494 len_be
= htobe32(len
);
495 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
500 ret
= send_payload(sock
, str
, len
);
509 * Internal enable application context on an agent application. This function
510 * communicates with the agent to enable a given application context.
512 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
514 static int app_context_op(const struct agent_app
*app
,
515 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
518 uint32_t reply_ret_code
;
519 struct lttcomm_agent_generic_reply reply
;
520 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
523 LTTNG_ASSERT(app
->sock
);
525 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
526 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
528 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
529 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
530 ctx
->provider_name
, ctx
->ctx_name
,
531 app
->pid
, app
->sock
->fd
);
534 * Calculate the payload's size, which consists of the size (u32, BE)
535 * of the provider name, the NULL-terminated provider name string, the
536 * size (u32, BE) of the context name, followed by the NULL-terminated
537 * context name string.
539 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
540 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
541 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
542 sizeof(uint32_t) + app_ctx_name_len
;
544 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
549 if (app_ctx_provider_name_len
> UINT32_MAX
||
550 app_ctx_name_len
> UINT32_MAX
) {
551 ERR("Application context name > MAX_UINT32");
552 ret
= LTTNG_ERR_INVALID
;
556 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
557 (uint32_t) app_ctx_provider_name_len
);
562 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
563 (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", event
->name
,
610 app
->pid
, app
->sock
->fd
);
612 data_size
= sizeof(msg
);
613 memset(&msg
, 0, sizeof(msg
));
614 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
615 ret
= LTTNG_ERR_INVALID
;
619 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
624 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
629 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
634 reply_ret_code
= be32toh(reply
.ret_code
);
635 log_reply_code(reply_ret_code
);
636 switch (reply_ret_code
) {
637 case AGENT_RET_CODE_SUCCESS
:
639 case AGENT_RET_CODE_UNKNOWN_NAME
:
640 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
650 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
656 * Send back the registration DONE command to a given agent application.
658 * Return 0 on success or else a negative value.
660 int agent_send_registration_done(struct agent_app
*app
)
663 LTTNG_ASSERT(app
->sock
);
665 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
667 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
671 * Enable agent event on every agent applications registered with the session
674 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
676 int agent_enable_event(struct agent_event
*event
,
677 enum lttng_domain_type domain
)
680 struct agent_app
*app
;
681 struct lttng_ht_iter iter
;
687 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
689 if (app
->domain
!= domain
) {
693 /* Enable event on agent application through TCP socket. */
694 ret
= enable_event(app
, event
);
695 if (ret
!= LTTNG_OK
) {
700 event
->enabled_count
++;
709 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
711 free(ctx
->provider_name
);
717 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
719 struct agent_app_ctx
*agent_ctx
= NULL
;
725 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
726 agent_ctx
= zmalloc(sizeof(*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
,
748 enum lttng_domain_type domain
)
751 struct agent_app
*app
;
752 struct lttng_ht_iter iter
;
755 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
756 ret
= LTTNG_ERR_INVALID
;
762 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
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
,
799 enum lttng_domain_type domain
)
802 struct agent_app
*app
;
803 struct lttng_ht_iter iter
;
806 if (!AGENT_EVENT_IS_ENABLED(event
)) {
810 if (--event
->enabled_count
!= 0) {
812 * Agent event still enabled. Disable the agent event only when
813 * all "users" have disabled it (event notifiers, event rules,
822 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
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
) {
835 /* event->enabled_count is now 0. */
836 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
,
851 enum lttng_domain_type domain
)
854 struct agent_app
*app
;
855 struct lttng_ht_iter iter
;
860 DBG2("Disabling agent application context %s:%s",
861 ctx
->provider_name
, ctx
->ctx_name
);
862 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
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
,
885 enum lttng_domain_type domain
)
888 size_t nbmem
, count
= 0;
889 struct agent_app
*app
;
890 struct lttng_event
*tmp_events
= NULL
;
891 struct lttng_ht_iter iter
;
893 LTTNG_ASSERT(events
);
895 DBG2("Agent listing events for domain %d", domain
);
897 nbmem
= UST_APP_EVENT_LIST_SIZE
;
898 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
900 PERROR("zmalloc agent list events");
906 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
909 struct lttng_event
*agent_events
;
911 /* Skip domain not asked by the list. */
912 if (app
->domain
!= domain
) {
916 nb_ev
= list_events(app
, &agent_events
);
922 if (count
+ nb_ev
> nbmem
) {
923 /* In case the realloc fails, we free the memory */
924 struct lttng_event
*new_tmp_events
;
927 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
928 DBG2("Reallocating agent event list from %zu to %zu entries",
930 new_tmp_events
= realloc(tmp_events
,
931 new_nbmem
* sizeof(*new_tmp_events
));
932 if (!new_tmp_events
) {
933 PERROR("realloc agent events");
938 /* Zero the new memory */
939 memset(new_tmp_events
+ nbmem
, 0,
940 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
942 tmp_events
= new_tmp_events
;
944 memcpy(tmp_events
+ count
, agent_events
,
945 nb_ev
* sizeof(*tmp_events
));
952 *events
= tmp_events
;
963 * Create a agent app object using the given PID.
965 * Return newly allocated object or else NULL on error.
967 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
968 struct lttcomm_sock
*sock
)
970 struct agent_app
*app
;
974 app
= zmalloc(sizeof(*app
));
976 PERROR("Failed to allocate agent application instance");
981 app
->domain
= domain
;
983 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
990 * Lookup agent app by socket in the global hash table.
992 * RCU read side lock MUST be acquired.
994 * Return object if found else NULL.
996 struct agent_app
*agent_find_app_by_sock(int sock
)
998 struct lttng_ht_node_ulong
*node
;
999 struct lttng_ht_iter iter
;
1000 struct agent_app
*app
;
1002 LTTNG_ASSERT(sock
>= 0);
1004 lttng_ht_lookup(the_agent_apps_ht_by_sock
,
1005 (void *) ((unsigned long) sock
), &iter
);
1006 node
= lttng_ht_iter_get_node_ulong(&iter
);
1010 app
= caa_container_of(node
, struct agent_app
, node
);
1012 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1016 DBG3("Agent app NOT found by sock %d.", sock
);
1021 * Add agent application object to the global hash table.
1023 void agent_add_app(struct agent_app
*app
)
1027 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1028 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1032 * Delete agent application from the global hash table.
1034 * rcu_read_lock() must be held by the caller.
1036 void agent_delete_app(struct agent_app
*app
)
1039 struct lttng_ht_iter iter
;
1043 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1045 iter
.iter
.node
= &app
->node
.node
;
1046 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1051 * Destroy an agent application object by detaching it from its corresponding
1052 * UST app if one is connected by closing the socket. Finally, perform a
1053 * delayed memory reclaim.
1055 void agent_destroy_app(struct agent_app
*app
)
1060 app
->sock
->ops
->close(app
->sock
);
1061 lttcomm_destroy_sock(app
->sock
);
1064 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1068 * Initialize an already allocated agent object.
1070 * Return 0 on success or else a negative errno value.
1072 int agent_init(struct agent
*agt
)
1078 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1083 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1085 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1093 * Add agent object to the given hash table.
1095 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1100 DBG3("Agent adding from domain %d", agt
->domain
);
1102 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1106 * Create an agent object for the given domain.
1108 * Return the allocated agent or NULL on error.
1110 struct agent
*agent_create(enum lttng_domain_type domain
)
1115 agt
= zmalloc(sizeof(struct agent
));
1119 agt
->domain
= domain
;
1121 ret
= agent_init(agt
);
1133 * Create a newly allocated agent event data structure.
1134 * Ownership of filter_expression is taken.
1136 * Return a new object else NULL on error.
1138 struct agent_event
*agent_create_event(const char *name
,
1139 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1140 struct lttng_bytecode
*filter
, char *filter_expression
)
1142 struct agent_event
*event
= NULL
;
1144 DBG3("Agent create new event with name %s, loglevel type %d, \
1145 loglevel value %d and filter %s",
1146 name
, loglevel_type
, loglevel_value
,
1147 filter_expression
? filter_expression
: "NULL");
1150 ERR("Failed to create agent event; no name provided.");
1154 event
= zmalloc(sizeof(*event
));
1159 strncpy(event
->name
, name
, sizeof(event
->name
));
1160 event
->name
[sizeof(event
->name
) - 1] = '\0';
1161 lttng_ht_node_init_str(&event
->node
, event
->name
);
1163 event
->loglevel_value
= loglevel_value
;
1164 event
->loglevel_type
= loglevel_type
;
1165 event
->filter
= filter
;
1166 event
->filter_expression
= filter_expression
;
1172 * Unique add of a agent event to an agent object.
1174 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1176 LTTNG_ASSERT(event
);
1178 LTTNG_ASSERT(agt
->events
);
1180 DBG3("Agent adding event %s", event
->name
);
1181 add_unique_agent_event(agt
->events
, event
);
1182 agt
->being_used
= 1;
1186 * Unique add of a agent context to an agent object.
1188 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1191 struct agent_app_ctx
*agent_ctx
= NULL
;
1195 LTTNG_ASSERT(agt
->events
);
1196 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1198 agent_ctx
= create_app_ctx(ctx
);
1200 ret
= LTTNG_ERR_NOMEM
;
1204 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1205 ctx
->u
.app_ctx
.ctx_name
);
1206 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1212 * Find multiple agent events sharing the given name.
1214 * RCU read side lock MUST be acquired. It must be held for the
1215 * duration of the iteration.
1217 * Sets the given iterator.
1219 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1220 struct lttng_ht_iter
* iter
)
1222 struct lttng_ht
*ht
;
1223 struct agent_ht_key key
;
1227 LTTNG_ASSERT(agt
->events
);
1233 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1234 ht_match_event_by_name
, &key
, &iter
->iter
);
1238 * Find the agent event matching a trigger.
1240 * RCU read side lock MUST be acquired. It must be held for as long as
1241 * the returned agent_event is used.
1243 * Return object if found else NULL.
1245 struct agent_event
*agent_find_event_by_trigger(
1246 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1248 enum lttng_condition_status c_status
;
1249 enum lttng_event_rule_status er_status
;
1250 enum lttng_domain_type domain
;
1251 const struct lttng_condition
*condition
;
1252 const struct lttng_event_rule
*rule
;
1254 const char *filter_expression
;
1255 const struct lttng_log_level_rule
*log_level_rule
;
1256 /* Unused when loglevel_type is 'ALL'. */
1257 int loglevel_value
= 0;
1258 enum lttng_loglevel_type loglevel_type
;
1259 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1260 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1263 LTTNG_ASSERT(agt
->events
);
1265 condition
= lttng_trigger_get_const_condition(trigger
);
1267 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1268 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1270 c_status
= lttng_condition_event_rule_matches_get_rule(
1272 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1274 switch (lttng_event_rule_get_type(rule
)) {
1275 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1276 logging_get_name_pattern
=
1277 lttng_event_rule_jul_logging_get_name_pattern
;
1278 logging_get_log_level_rule
=
1279 lttng_event_rule_jul_logging_get_log_level_rule
;
1281 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1282 logging_get_name_pattern
=
1283 lttng_event_rule_log4j_logging_get_name_pattern
;
1284 logging_get_log_level_rule
=
1285 lttng_event_rule_log4j_logging_get_log_level_rule
;
1287 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1288 logging_get_name_pattern
=
1289 lttng_event_rule_python_logging_get_name_pattern
;
1290 logging_get_log_level_rule
=
1291 lttng_event_rule_python_logging_get_log_level_rule
;
1298 domain
= lttng_event_rule_get_domain_type(rule
);
1299 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1300 domain
== LTTNG_DOMAIN_PYTHON
);
1302 /* Get the event's pattern name ('name' in the legacy terminology). */
1303 er_status
= logging_get_name_pattern(rule
, &name
);
1304 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1306 /* Get the internal filter expression. */
1307 filter_expression
= lttng_event_rule_get_filter(rule
);
1309 /* Map log_level_rule to loglevel value. */
1310 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1311 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1312 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1314 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1315 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1320 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1321 filter_expression
, agt
);
1325 * Get the next agent event duplicate by name. This should be called
1326 * after a call to agent_find_events_by_name() to iterate on events.
1328 * The RCU read lock must be held during the iteration and for as long
1329 * as the object the iterator points to remains in use.
1331 void agent_event_next_duplicate(const char *name
,
1332 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1334 struct agent_ht_key key
;
1338 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1343 * Find a agent event in the given agent using name, loglevel and filter.
1345 * RCU read side lock MUST be acquired. It must be kept for as long as
1346 * the returned agent_event is used.
1348 * Return object if found else NULL.
1350 struct agent_event
*agent_find_event(const char *name
,
1351 enum lttng_loglevel_type loglevel_type
,
1353 const char *filter_expression
,
1356 struct lttng_ht_node_str
*node
;
1357 struct lttng_ht_iter iter
;
1358 struct lttng_ht
*ht
;
1359 struct agent_ht_key key
;
1363 LTTNG_ASSERT(agt
->events
);
1367 key
.loglevel_value
= loglevel_value
;
1368 key
.loglevel_type
= loglevel_type
;
1369 key
.filter_expression
= filter_expression
;
1371 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1372 ht_match_event
, &key
, &iter
.iter
);
1373 node
= lttng_ht_iter_get_node_str(&iter
);
1378 DBG3("Agent event found %s.", name
);
1379 return caa_container_of(node
, struct agent_event
, node
);
1382 DBG3("Agent event NOT found %s.", name
);
1387 * Free given agent event. This event must not be globally visible at this
1388 * point (only expected to be used on failure just after event creation). After
1389 * this call, the pointer is not usable anymore.
1391 void agent_destroy_event(struct agent_event
*event
)
1393 LTTNG_ASSERT(event
);
1395 free(event
->filter
);
1396 free(event
->filter_expression
);
1397 free(event
->exclusion
);
1402 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1404 struct agent_app_ctx
*ctx
=
1405 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1407 destroy_app_ctx(ctx
);
1411 * Destroy an agent completely.
1413 void agent_destroy(struct agent
*agt
)
1415 struct lttng_ht_node_str
*node
;
1416 struct lttng_ht_iter iter
;
1417 struct agent_app_ctx
*ctx
;
1421 DBG3("Agent destroy");
1424 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1426 struct agent_event
*event
;
1429 * When destroying an event, we have to try to disable it on the
1430 * agent side so the event stops generating data. The return
1431 * value is not important since we have to continue anyway
1432 * destroying the object.
1434 event
= caa_container_of(node
, struct agent_event
, node
);
1435 (void) agent_disable_event(event
, agt
->domain
);
1437 ret
= lttng_ht_del(agt
->events
, &iter
);
1439 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1442 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1443 (void) disable_context(ctx
, agt
->domain
);
1444 cds_list_del(&ctx
->list_node
);
1445 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1448 ht_cleanup_push(agt
->events
);
1453 * Allocate agent_apps_ht_by_sock.
1455 int agent_app_ht_alloc(void)
1457 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1458 return the_agent_apps_ht_by_sock
? 0 : -1;
1462 * Destroy a agent application by socket.
1464 void agent_destroy_app_by_sock(int sock
)
1466 struct agent_app
*app
;
1468 LTTNG_ASSERT(sock
>= 0);
1471 * Not finding an application is a very important error that should NEVER
1472 * happen. The hash table deletion is ONLY done through this call when the
1473 * main sessiond thread is torn down.
1476 app
= agent_find_app_by_sock(sock
);
1479 /* RCU read side lock is assumed to be held by this function. */
1480 agent_delete_app(app
);
1482 /* The application is freed in a RCU call but the socket is closed here. */
1483 agent_destroy_app(app
);
1488 * Clean-up the agent app hash table and destroy it.
1490 void agent_app_ht_clean(void)
1492 struct lttng_ht_node_ulong
*node
;
1493 struct lttng_ht_iter iter
;
1495 if (!the_agent_apps_ht_by_sock
) {
1499 cds_lfht_for_each_entry(
1500 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1501 struct agent_app
*app
;
1503 app
= caa_container_of(node
, struct agent_app
, node
);
1504 agent_destroy_app_by_sock(app
->sock
->fd
);
1508 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1512 * Update a agent application (given socket) using the given agent.
1514 * Note that this function is most likely to be used with a tracing session
1515 * thus the caller should make sure to hold the appropriate lock(s).
1517 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1520 struct agent_event
*event
;
1521 struct lttng_ht_iter iter
;
1522 struct agent_app_ctx
*ctx
;
1527 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1531 * We are in the registration path thus if the application is gone,
1532 * there is a serious code flow error.
1535 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1536 /* Skip event if disabled. */
1537 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1541 ret
= enable_event(app
, event
);
1542 if (ret
!= LTTNG_OK
) {
1543 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1544 event
->name
, app
->pid
, app
->sock
->fd
);
1545 /* Let's try the others here and don't assume the app is dead. */
1550 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1551 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1552 if (ret
!= LTTNG_OK
) {
1553 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1554 ctx
->provider_name
, ctx
->ctx_name
,
1555 app
->pid
, app
->sock
->fd
);
1564 * Allocate the per-event notifier domain agent hash table. It is lazily
1565 * populated as domains are used.
1567 int agent_by_event_notifier_domain_ht_create(void)
1569 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1570 return the_trigger_agents_ht_by_domain
? 0 : -1;
1574 * Clean-up the per-event notifier domain agent hash table and destroy it.
1576 void agent_by_event_notifier_domain_ht_destroy(void)
1578 struct lttng_ht_node_u64
*node
;
1579 struct lttng_ht_iter iter
;
1581 if (!the_trigger_agents_ht_by_domain
) {
1586 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
1587 &iter
.iter
, node
, node
) {
1588 struct agent
*agent
=
1589 caa_container_of(node
, struct agent
, node
);
1590 const int ret
= lttng_ht_del(
1591 the_trigger_agents_ht_by_domain
, &iter
);
1593 LTTNG_ASSERT(ret
== 0);
1594 agent_destroy(agent
);
1598 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1601 struct agent
*agent_find_by_event_notifier_domain(
1602 enum lttng_domain_type domain_type
)
1604 struct agent
*agt
= NULL
;
1605 struct lttng_ht_node_u64
*node
;
1606 struct lttng_ht_iter iter
;
1607 const uint64_t key
= (uint64_t) domain_type
;
1609 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1611 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1612 lttng_domain_type_str(domain_type
));
1614 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1615 node
= lttng_ht_iter_get_node_u64(&iter
);
1620 agt
= caa_container_of(node
, struct agent
, node
);