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 typedef enum lttng_event_rule_status (*event_rule_logging_get_name_pattern
)(
34 const struct lttng_event_rule
*rule
, const char **pattern
);
35 typedef enum lttng_event_rule_status (*event_rule_logging_get_log_level_rule
)(
36 const struct lttng_event_rule
*rule
,
37 const struct lttng_log_level_rule
**log_level_rule
);
40 * 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
;
54 * Human readable agent return code.
57 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";
72 void log_reply_code(uint32_t in_reply_ret_code
)
74 int level
= PRINT_DBG3
;
76 * reply_ret_code and in_reply_ret_code are kept separate to have a
77 * sanitized value (used to retrieve the human readable string) and the
78 * original value which is logged as-is.
80 uint32_t reply_ret_code
= in_reply_ret_code
;
82 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
83 reply_ret_code
>= AGENT_RET_CODE_NR
) {
84 reply_ret_code
= AGENT_RET_CODE_NR
;
88 LOG(level
, "Agent replied with retcode: %s (%" PRIu32
")",
89 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code
) reply_ret_code
),
94 * Match function for the events hash table lookup by name.
96 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
99 struct agent_event
*event
;
100 const struct agent_ht_key
*key
;
105 event
= caa_container_of(node
, struct agent_event
, node
.node
);
106 key
= (agent_ht_key
*) _key
;
108 /* Match 1 elements of the key: name. */
111 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
122 * Match function for the events hash table lookup by name, log level and
125 static int ht_match_event(struct cds_lfht_node
*node
,
128 struct agent_event
*event
;
129 const struct agent_ht_key
*key
;
135 event
= caa_container_of(node
, struct agent_event
, node
.node
);
136 key
= (agent_ht_key
*) _key
;
138 /* Match 2 elements of the key: name and loglevel. */
141 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
145 /* Event loglevel value and type. */
146 ll_match
= loglevels_match(event
->loglevel_type
,
147 event
->loglevel_value
, key
->loglevel_type
,
148 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
154 /* Filter expression */
155 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
156 /* One has a filter expression, the other does not */
160 if (event
->filter_expression
) {
161 if (strncmp(event
->filter_expression
, 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
,
177 struct agent_event
*event
)
179 struct cds_lfht_node
*node_ptr
;
180 struct agent_ht_key key
;
183 LTTNG_ASSERT(ht
->ht
);
186 key
.name
= event
->name
;
187 key
.loglevel_value
= event
->loglevel_value
;
188 key
.loglevel_type
= event
->loglevel_type
;
189 key
.filter_expression
= event
->filter_expression
;
191 node_ptr
= cds_lfht_add_unique(ht
->ht
,
192 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
193 ht_match_event
, &key
, &event
->node
.node
);
194 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
198 * URCU delayed agent event reclaim.
200 static void destroy_event_agent_rcu(struct rcu_head
*head
)
202 struct lttng_ht_node_str
*node
=
203 caa_container_of(head
, struct lttng_ht_node_str
, head
);
204 struct agent_event
*event
=
205 caa_container_of(node
, struct agent_event
, node
);
207 agent_destroy_event(event
);
211 * URCU delayed agent app reclaim.
213 static void destroy_app_agent_rcu(struct rcu_head
*head
)
215 struct lttng_ht_node_ulong
*node
=
216 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
217 struct agent_app
*app
=
218 caa_container_of(node
, struct agent_app
, node
);
224 * Communication with the agent. Send the message header to the given socket in
227 * Return 0 on success or else a negative errno message of sendmsg() op.
229 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
230 uint32_t cmd
, uint32_t cmd_version
)
234 struct lttcomm_agent_hdr msg
;
238 memset(&msg
, 0, sizeof(msg
));
239 msg
.data_size
= htobe64(data_size
);
240 msg
.cmd
= htobe32(cmd
);
241 msg
.cmd_version
= htobe32(cmd_version
);
243 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
244 if (size
< sizeof(msg
)) {
255 * Communication call with the agent. Send the payload to the given socket. The
256 * header MUST be sent prior to this call.
258 * Return 0 on success or else a negative errno value of sendmsg() op.
260 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
269 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
281 * Communication call with the agent. Receive reply from the agent using the
284 * Return 0 on success or else a negative errno value from recvmsg() op.
286 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
294 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
306 * Internal event listing for a given app. Populate events.
308 * Return number of element in the list or else a negative LTTNG_ERR* code.
309 * On success, the caller is responsible for freeing the memory
310 * allocated for "events".
312 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
314 int ret
, i
, len
= 0, offset
= 0;
317 uint32_t reply_ret_code
;
318 struct lttng_event
*tmp_events
= NULL
;
319 struct lttcomm_agent_list_reply
*reply
= NULL
;
320 struct lttcomm_agent_list_reply_hdr reply_hdr
;
323 LTTNG_ASSERT(app
->sock
);
324 LTTNG_ASSERT(events
);
326 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
329 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
334 /* Get list header so we know how much we'll receive. */
335 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
340 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
341 log_reply_code(reply_ret_code
);
342 switch (reply_ret_code
) {
343 case AGENT_RET_CODE_SUCCESS
:
344 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
351 reply
= (lttcomm_agent_list_reply
*) zmalloc(data_size
);
353 ret
= LTTNG_ERR_NOMEM
;
357 /* Get the list with the appropriate data size. */
358 ret
= recv_reply(app
->sock
, reply
, data_size
);
363 nb_event
= be32toh(reply
->nb_event
);
364 tmp_events
= (lttng_event
*) zmalloc(sizeof(*tmp_events
) * nb_event
);
366 ret
= LTTNG_ERR_NOMEM
;
370 for (i
= 0; i
< nb_event
; i
++) {
372 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
373 sizeof(tmp_events
[i
].name
))) {
374 ret
= LTTNG_ERR_INVALID
;
377 tmp_events
[i
].pid
= app
->pid
;
378 tmp_events
[i
].enabled
= -1;
379 len
= strlen(reply
->payload
+ offset
) + 1;
382 *events
= tmp_events
;
388 ret
= LTTNG_ERR_UST_LIST_FAIL
;
397 * Internal enable agent event on a agent application. This function
398 * communicates with the agent to enable a given event.
400 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
402 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
407 size_t filter_expression_length
;
408 uint32_t reply_ret_code
;
409 struct lttcomm_agent_enable_event msg
;
410 struct lttcomm_agent_generic_reply reply
;
413 LTTNG_ASSERT(app
->sock
);
416 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
417 app
->pid
, app
->sock
->fd
);
420 * Calculate the payload's size, which is the fixed-size struct followed
421 * by the variable-length filter expression (+1 for the ending \0).
423 if (!event
->filter_expression
) {
424 filter_expression_length
= 0;
426 filter_expression_length
= strlen(event
->filter_expression
) + 1;
428 data_size
= sizeof(msg
) + filter_expression_length
;
430 memset(&msg
, 0, sizeof(msg
));
431 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
432 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
433 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
434 ret
= LTTNG_ERR_INVALID
;
437 msg
.filter_expression_length
= htobe32(filter_expression_length
);
439 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
444 bytes_to_send
= (char *) zmalloc(data_size
);
445 if (!bytes_to_send
) {
446 ret
= LTTNG_ERR_NOMEM
;
450 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
451 if (filter_expression_length
> 0) {
452 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
453 filter_expression_length
);
456 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
462 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
467 reply_ret_code
= be32toh(reply
.ret_code
);
468 log_reply_code(reply_ret_code
);
469 switch (reply_ret_code
) {
470 case AGENT_RET_CODE_SUCCESS
:
472 case AGENT_RET_CODE_UNKNOWN_NAME
:
473 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
483 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
489 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
492 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
497 len_be
= htobe32(len
);
498 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
503 ret
= send_payload(sock
, str
, len
);
512 * Internal enable application context on an agent application. This function
513 * communicates with the agent to enable a given application context.
515 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
517 static int app_context_op(const struct agent_app
*app
,
518 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
521 uint32_t reply_ret_code
;
522 struct lttcomm_agent_generic_reply reply
;
523 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
526 LTTNG_ASSERT(app
->sock
);
528 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
529 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
531 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
532 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
533 ctx
->provider_name
, ctx
->ctx_name
,
534 app
->pid
, app
->sock
->fd
);
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;
544 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
545 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
||
553 app_ctx_name_len
> UINT32_MAX
) {
554 ERR("Application context name > MAX_UINT32");
555 ret
= LTTNG_ERR_INVALID
;
559 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
560 (uint32_t) app_ctx_provider_name_len
);
565 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
566 (uint32_t) app_ctx_name_len
);
571 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
576 reply_ret_code
= be32toh(reply
.ret_code
);
577 log_reply_code(reply_ret_code
);
578 switch (reply_ret_code
) {
579 case AGENT_RET_CODE_SUCCESS
:
589 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
595 * Internal disable agent event call on a agent application. This function
596 * communicates with the agent to disable a given event.
598 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
600 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
604 uint32_t reply_ret_code
;
605 struct lttcomm_agent_disable_event msg
;
606 struct lttcomm_agent_generic_reply reply
;
609 LTTNG_ASSERT(app
->sock
);
612 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
613 app
->pid
, app
->sock
->fd
);
615 data_size
= sizeof(msg
);
616 memset(&msg
, 0, sizeof(msg
));
617 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
618 ret
= LTTNG_ERR_INVALID
;
622 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
627 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
632 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
637 reply_ret_code
= be32toh(reply
.ret_code
);
638 log_reply_code(reply_ret_code
);
639 switch (reply_ret_code
) {
640 case AGENT_RET_CODE_SUCCESS
:
642 case AGENT_RET_CODE_UNKNOWN_NAME
:
643 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
653 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
659 * Send back the registration DONE command to a given agent application.
661 * Return 0 on success or else a negative value.
663 int agent_send_registration_done(struct agent_app
*app
)
666 LTTNG_ASSERT(app
->sock
);
668 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
670 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
674 * Enable agent event on every agent applications registered with the session
677 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
679 int agent_enable_event(struct agent_event
*event
,
680 enum lttng_domain_type domain
)
683 struct agent_app
*app
;
684 struct lttng_ht_iter iter
;
690 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
692 if (app
->domain
!= domain
) {
696 /* Enable event on agent application through TCP socket. */
697 ret
= enable_event(app
, event
);
698 if (ret
!= LTTNG_OK
) {
703 event
->enabled_count
++;
712 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
714 free(ctx
->provider_name
);
720 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
722 struct agent_app_ctx
*agent_ctx
= NULL
;
728 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
729 agent_ctx
= (agent_app_ctx
*) zmalloc(sizeof(*ctx
));
734 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
735 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
736 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
737 destroy_app_ctx(agent_ctx
);
745 * Enable agent context on every agent applications registered with the session
748 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
750 int agent_enable_context(const struct lttng_event_context
*ctx
,
751 enum lttng_domain_type domain
)
754 struct agent_app
*app
;
755 struct lttng_ht_iter iter
;
758 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
759 ret
= LTTNG_ERR_INVALID
;
765 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
767 struct agent_app_ctx
*agent_ctx
;
769 if (app
->domain
!= domain
) {
773 agent_ctx
= create_app_ctx(ctx
);
775 ret
= LTTNG_ERR_NOMEM
;
779 /* Enable event on agent application through TCP socket. */
780 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
781 destroy_app_ctx(agent_ctx
);
782 if (ret
!= LTTNG_OK
) {
796 * Disable agent event on every agent application registered with the session
799 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
801 int agent_disable_event(struct agent_event
*event
,
802 enum lttng_domain_type domain
)
805 struct agent_app
*app
;
806 struct lttng_ht_iter iter
;
809 if (!AGENT_EVENT_IS_ENABLED(event
)) {
813 if (--event
->enabled_count
!= 0) {
815 * Agent event still enabled. Disable the agent event only when
816 * all "users" have disabled it (event notifiers, event rules,
825 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
827 if (app
->domain
!= domain
) {
831 /* Enable event on agent application through TCP socket. */
832 ret
= disable_event(app
, event
);
833 if (ret
!= LTTNG_OK
) {
838 /* event->enabled_count is now 0. */
839 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event
));
848 * Disable agent context on every agent application registered with the session
851 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
853 static int disable_context(struct agent_app_ctx
*ctx
,
854 enum lttng_domain_type domain
)
857 struct agent_app
*app
;
858 struct lttng_ht_iter iter
;
863 DBG2("Disabling agent application context %s:%s",
864 ctx
->provider_name
, ctx
->ctx_name
);
865 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
867 if (app
->domain
!= domain
) {
871 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
872 if (ret
!= LTTNG_OK
) {
882 * Ask every agent for the list of possible event. Events is allocated with the
883 * events of every agent application.
885 * Return the number of events or else a negative value.
887 int agent_list_events(struct lttng_event
**events
,
888 enum lttng_domain_type domain
)
891 size_t nbmem
, count
= 0;
892 struct agent_app
*app
;
893 struct lttng_event
*tmp_events
= NULL
;
894 struct lttng_ht_iter iter
;
896 LTTNG_ASSERT(events
);
898 DBG2("Agent listing events for domain %d", domain
);
900 nbmem
= UST_APP_EVENT_LIST_SIZE
;
901 tmp_events
= (lttng_event
*) zmalloc(nbmem
* sizeof(*tmp_events
));
903 PERROR("zmalloc agent list events");
909 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
912 struct lttng_event
*agent_events
;
914 /* Skip domain not asked by the list. */
915 if (app
->domain
!= domain
) {
919 nb_ev
= list_events(app
, &agent_events
);
925 if (count
+ nb_ev
> nbmem
) {
926 /* In case the realloc fails, we free the memory */
927 struct lttng_event
*new_tmp_events
;
930 new_nbmem
= std::max(count
+ nb_ev
, nbmem
<< 1);
931 DBG2("Reallocating agent event list from %zu to %zu entries",
933 new_tmp_events
= (lttng_event
*) realloc(tmp_events
,
934 new_nbmem
* sizeof(*new_tmp_events
));
935 if (!new_tmp_events
) {
936 PERROR("realloc agent events");
941 /* Zero the new memory */
942 memset(new_tmp_events
+ nbmem
, 0,
943 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
945 tmp_events
= new_tmp_events
;
947 memcpy(tmp_events
+ count
, agent_events
,
948 nb_ev
* sizeof(*tmp_events
));
955 *events
= tmp_events
;
966 * Create a agent app object using the given PID.
968 * Return newly allocated object or else NULL on error.
970 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
971 struct lttcomm_sock
*sock
)
973 struct agent_app
*app
;
977 app
= (agent_app
*) zmalloc(sizeof(*app
));
979 PERROR("Failed to allocate agent application instance");
984 app
->domain
= domain
;
986 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
993 * Lookup agent app by socket in the global hash table.
995 * RCU read side lock MUST be acquired.
997 * Return object if found else NULL.
999 struct agent_app
*agent_find_app_by_sock(int sock
)
1001 struct lttng_ht_node_ulong
*node
;
1002 struct lttng_ht_iter iter
;
1003 struct agent_app
*app
;
1005 LTTNG_ASSERT(sock
>= 0);
1007 lttng_ht_lookup(the_agent_apps_ht_by_sock
,
1008 (void *) ((unsigned long) sock
), &iter
);
1009 node
= lttng_ht_iter_get_node_ulong(&iter
);
1013 app
= caa_container_of(node
, struct agent_app
, node
);
1015 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1019 DBG3("Agent app NOT found by sock %d.", sock
);
1024 * Add agent application object to the global hash table.
1026 void agent_add_app(struct agent_app
*app
)
1030 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1031 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1035 * Delete agent application from the global hash table.
1037 * rcu_read_lock() must be held by the caller.
1039 void agent_delete_app(struct agent_app
*app
)
1042 struct lttng_ht_iter iter
;
1046 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1048 iter
.iter
.node
= &app
->node
.node
;
1049 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1054 * Destroy an agent application object by detaching it from its corresponding
1055 * UST app if one is connected by closing the socket. Finally, perform a
1056 * delayed memory reclaim.
1058 void agent_destroy_app(struct agent_app
*app
)
1063 app
->sock
->ops
->close(app
->sock
);
1064 lttcomm_destroy_sock(app
->sock
);
1067 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1071 * Initialize an already allocated agent object.
1073 * Return 0 on success or else a negative errno value.
1075 int agent_init(struct agent
*agt
)
1081 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1086 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1088 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1096 * Add agent object to the given hash table.
1098 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1103 DBG3("Agent adding from domain %d", agt
->domain
);
1105 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1109 * Create an agent object for the given domain.
1111 * Return the allocated agent or NULL on error.
1113 struct agent
*agent_create(enum lttng_domain_type domain
)
1118 agt
= (agent
*) zmalloc(sizeof(struct agent
));
1122 agt
->domain
= domain
;
1124 ret
= agent_init(agt
);
1136 * Create a newly allocated agent event data structure.
1137 * Ownership of filter_expression is taken.
1139 * Return a new object else NULL on error.
1141 struct agent_event
*agent_create_event(const char *name
,
1142 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1143 struct lttng_bytecode
*filter
, char *filter_expression
)
1145 struct agent_event
*event
= NULL
;
1147 DBG3("Agent create new event with name %s, loglevel type %d, \
1148 loglevel value %d and filter %s",
1149 name
, loglevel_type
, loglevel_value
,
1150 filter_expression
? filter_expression
: "NULL");
1153 ERR("Failed to create agent event; no name provided.");
1157 event
= (agent_event
*) zmalloc(sizeof(*event
));
1162 strncpy(event
->name
, name
, sizeof(event
->name
));
1163 event
->name
[sizeof(event
->name
) - 1] = '\0';
1164 lttng_ht_node_init_str(&event
->node
, event
->name
);
1166 event
->loglevel_value
= loglevel_value
;
1167 event
->loglevel_type
= loglevel_type
;
1168 event
->filter
= filter
;
1169 event
->filter_expression
= filter_expression
;
1175 * Unique add of a agent event to an agent object.
1177 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1179 LTTNG_ASSERT(event
);
1181 LTTNG_ASSERT(agt
->events
);
1183 DBG3("Agent adding event %s", event
->name
);
1184 add_unique_agent_event(agt
->events
, event
);
1185 agt
->being_used
= 1;
1189 * Unique add of a agent context to an agent object.
1191 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1194 struct agent_app_ctx
*agent_ctx
= NULL
;
1198 LTTNG_ASSERT(agt
->events
);
1199 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1201 agent_ctx
= create_app_ctx(ctx
);
1203 ret
= LTTNG_ERR_NOMEM
;
1207 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1208 ctx
->u
.app_ctx
.ctx_name
);
1209 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1215 * Find multiple agent events sharing the given name.
1217 * RCU read side lock MUST be acquired. It must be held for the
1218 * duration of the iteration.
1220 * Sets the given iterator.
1222 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1223 struct lttng_ht_iter
* iter
)
1225 struct lttng_ht
*ht
;
1226 struct agent_ht_key key
;
1230 LTTNG_ASSERT(agt
->events
);
1236 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1237 ht_match_event_by_name
, &key
, &iter
->iter
);
1241 * Find the agent event matching a trigger.
1243 * RCU read side lock MUST be acquired. It must be held for as long as
1244 * the returned agent_event is used.
1246 * Return object if found else NULL.
1248 struct agent_event
*agent_find_event_by_trigger(
1249 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1251 enum lttng_condition_status c_status
;
1252 enum lttng_event_rule_status er_status
;
1253 enum lttng_domain_type domain
;
1254 const struct lttng_condition
*condition
;
1255 const struct lttng_event_rule
*rule
;
1257 const char *filter_expression
;
1258 const struct lttng_log_level_rule
*log_level_rule
;
1259 /* Unused when loglevel_type is 'ALL'. */
1260 int loglevel_value
= 0;
1261 enum lttng_loglevel_type loglevel_type
;
1262 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1263 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1266 LTTNG_ASSERT(agt
->events
);
1268 condition
= lttng_trigger_get_const_condition(trigger
);
1270 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1271 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1273 c_status
= lttng_condition_event_rule_matches_get_rule(
1275 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1277 switch (lttng_event_rule_get_type(rule
)) {
1278 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1279 logging_get_name_pattern
=
1280 lttng_event_rule_jul_logging_get_name_pattern
;
1281 logging_get_log_level_rule
=
1282 lttng_event_rule_jul_logging_get_log_level_rule
;
1284 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1285 logging_get_name_pattern
=
1286 lttng_event_rule_log4j_logging_get_name_pattern
;
1287 logging_get_log_level_rule
=
1288 lttng_event_rule_log4j_logging_get_log_level_rule
;
1290 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1291 logging_get_name_pattern
=
1292 lttng_event_rule_python_logging_get_name_pattern
;
1293 logging_get_log_level_rule
=
1294 lttng_event_rule_python_logging_get_log_level_rule
;
1301 domain
= lttng_event_rule_get_domain_type(rule
);
1302 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1303 domain
== LTTNG_DOMAIN_PYTHON
);
1305 /* Get the event's pattern name ('name' in the legacy terminology). */
1306 er_status
= logging_get_name_pattern(rule
, &name
);
1307 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1309 /* Get the internal filter expression. */
1310 filter_expression
= lttng_event_rule_get_filter(rule
);
1312 /* Map log_level_rule to loglevel value. */
1313 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1314 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1315 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1317 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1318 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1323 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1324 filter_expression
, agt
);
1328 * Get the next agent event duplicate by name. This should be called
1329 * after a call to agent_find_events_by_name() to iterate on events.
1331 * The RCU read lock must be held during the iteration and for as long
1332 * as the object the iterator points to remains in use.
1334 void agent_event_next_duplicate(const char *name
,
1335 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1337 struct agent_ht_key key
;
1341 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1346 * Find a agent event in the given agent using name, loglevel and filter.
1348 * RCU read side lock MUST be acquired. It must be kept for as long as
1349 * the returned agent_event is used.
1351 * Return object if found else NULL.
1353 struct agent_event
*agent_find_event(const char *name
,
1354 enum lttng_loglevel_type loglevel_type
,
1356 const char *filter_expression
,
1359 struct lttng_ht_node_str
*node
;
1360 struct lttng_ht_iter iter
;
1361 struct lttng_ht
*ht
;
1362 struct agent_ht_key key
;
1366 LTTNG_ASSERT(agt
->events
);
1370 key
.loglevel_value
= loglevel_value
;
1371 key
.loglevel_type
= loglevel_type
;
1372 key
.filter_expression
= filter_expression
;
1374 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1375 ht_match_event
, &key
, &iter
.iter
);
1376 node
= lttng_ht_iter_get_node_str(&iter
);
1381 DBG3("Agent event found %s.", name
);
1382 return caa_container_of(node
, struct agent_event
, node
);
1385 DBG3("Agent event NOT found %s.", name
);
1390 * Free given agent event. This event must not be globally visible at this
1391 * point (only expected to be used on failure just after event creation). After
1392 * this call, the pointer is not usable anymore.
1394 void agent_destroy_event(struct agent_event
*event
)
1396 LTTNG_ASSERT(event
);
1398 free(event
->filter
);
1399 free(event
->filter_expression
);
1400 free(event
->exclusion
);
1405 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1407 struct agent_app_ctx
*ctx
=
1408 caa_container_of(head
, struct 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 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1429 struct agent_event
*event
;
1432 * When destroying an event, we have to try to disable it on the
1433 * agent side so the event stops generating data. The return
1434 * value is not important since we have to continue anyway
1435 * destroying the object.
1437 event
= caa_container_of(node
, struct agent_event
, node
);
1438 (void) agent_disable_event(event
, agt
->domain
);
1440 ret
= lttng_ht_del(agt
->events
, &iter
);
1442 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1445 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1446 (void) disable_context(ctx
, agt
->domain
);
1447 cds_list_del(&ctx
->list_node
);
1448 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1451 ht_cleanup_push(agt
->events
);
1456 * Allocate agent_apps_ht_by_sock.
1458 int agent_app_ht_alloc(void)
1460 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1461 return the_agent_apps_ht_by_sock
? 0 : -1;
1465 * Destroy a agent application by socket.
1467 void agent_destroy_app_by_sock(int sock
)
1469 struct agent_app
*app
;
1471 LTTNG_ASSERT(sock
>= 0);
1474 * Not finding an application is a very important error that should NEVER
1475 * happen. The hash table deletion is ONLY done through this call when the
1476 * main sessiond thread is torn down.
1479 app
= agent_find_app_by_sock(sock
);
1482 /* RCU read side lock is assumed to be held by this function. */
1483 agent_delete_app(app
);
1485 /* The application is freed in a RCU call but the socket is closed here. */
1486 agent_destroy_app(app
);
1491 * Clean-up the agent app hash table and destroy it.
1493 void agent_app_ht_clean(void)
1495 struct lttng_ht_node_ulong
*node
;
1496 struct lttng_ht_iter iter
;
1498 if (!the_agent_apps_ht_by_sock
) {
1502 cds_lfht_for_each_entry(
1503 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1504 struct agent_app
*app
;
1506 app
= caa_container_of(node
, struct agent_app
, node
);
1507 agent_destroy_app_by_sock(app
->sock
->fd
);
1511 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1515 * Update a agent application (given socket) using the given agent.
1517 * Note that this function is most likely to be used with a tracing session
1518 * thus the caller should make sure to hold the appropriate lock(s).
1520 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1523 struct agent_event
*event
;
1524 struct lttng_ht_iter iter
;
1525 struct agent_app_ctx
*ctx
;
1530 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1534 * We are in the registration path thus if the application is gone,
1535 * there is a serious code flow error.
1538 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1539 /* Skip event if disabled. */
1540 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1544 ret
= enable_event(app
, event
);
1545 if (ret
!= LTTNG_OK
) {
1546 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1547 event
->name
, app
->pid
, app
->sock
->fd
);
1548 /* Let's try the others here and don't assume the app is dead. */
1553 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1554 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1555 if (ret
!= LTTNG_OK
) {
1556 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1557 ctx
->provider_name
, ctx
->ctx_name
,
1558 app
->pid
, app
->sock
->fd
);
1567 * Allocate the per-event notifier domain agent hash table. It is lazily
1568 * populated as domains are used.
1570 int agent_by_event_notifier_domain_ht_create(void)
1572 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1573 return the_trigger_agents_ht_by_domain
? 0 : -1;
1577 * Clean-up the per-event notifier domain agent hash table and destroy it.
1579 void agent_by_event_notifier_domain_ht_destroy(void)
1581 struct lttng_ht_node_u64
*node
;
1582 struct lttng_ht_iter iter
;
1584 if (!the_trigger_agents_ht_by_domain
) {
1589 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
1590 &iter
.iter
, node
, node
) {
1591 struct agent
*agent
=
1592 caa_container_of(node
, struct agent
, node
);
1593 const int ret
= lttng_ht_del(
1594 the_trigger_agents_ht_by_domain
, &iter
);
1596 LTTNG_ASSERT(ret
== 0);
1597 agent_destroy(agent
);
1601 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1604 struct agent
*agent_find_by_event_notifier_domain(
1605 enum lttng_domain_type domain_type
)
1607 struct agent
*agt
= NULL
;
1608 struct lttng_ht_node_u64
*node
;
1609 struct lttng_ht_iter iter
;
1610 const uint64_t key
= (uint64_t) domain_type
;
1612 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1614 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1615 lttng_domain_type_str(domain_type
));
1617 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1618 node
= lttng_ht_iter_get_node_u64(&iter
);
1623 agt
= caa_container_of(node
, struct agent
, node
);