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.hpp>
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.hpp>
21 #include <lttng/log-level-rule-internal.hpp>
23 #include <common/common.hpp>
24 #include <common/sessiond-comm/agent.hpp>
26 #include <common/compat/endian.hpp>
29 #include "ust-app.hpp"
31 #include "common/error.hpp"
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.
43 struct agent_app_ctx
{
47 /* agent_app_ctx are part of the agent app_ctx_list. */
48 struct cds_list_head list_node
;
50 /* For call_rcu teardown. */
51 struct rcu_head rcu_node
;
56 * Human readable agent return code.
59 const char *lttcomm_agent_ret_code_str(lttcomm_agent_ret_code code
)
62 case AGENT_RET_CODE_SUCCESS
:
64 case AGENT_RET_CODE_INVALID
:
65 return "Invalid command";
66 case AGENT_RET_CODE_UNKNOWN_NAME
:
67 return "Unknown logger name";
69 return "Unknown code";
74 void log_reply_code(uint32_t in_reply_ret_code
)
76 int level
= PRINT_DBG3
;
78 * reply_ret_code and in_reply_ret_code are kept separate to have a
79 * sanitized value (used to retrieve the human readable string) and the
80 * original value which is logged as-is.
82 uint32_t reply_ret_code
= in_reply_ret_code
;
84 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
85 reply_ret_code
>= AGENT_RET_CODE_NR
) {
86 reply_ret_code
= AGENT_RET_CODE_NR
;
90 LOG(level
, "Agent replied with retcode: %s (%" PRIu32
")",
91 lttcomm_agent_ret_code_str((lttcomm_agent_ret_code
) reply_ret_code
),
96 * Match function for the events hash table lookup by name.
98 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
101 struct agent_event
*event
;
102 const struct agent_ht_key
*key
;
107 event
= caa_container_of(node
, struct agent_event
, node
.node
);
108 key
= (agent_ht_key
*) _key
;
110 /* Match 1 elements of the key: name. */
113 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
124 * Match function for the events hash table lookup by name, log level and
127 static int ht_match_event(struct cds_lfht_node
*node
,
130 struct agent_event
*event
;
131 const struct agent_ht_key
*key
;
137 event
= caa_container_of(node
, struct agent_event
, node
.node
);
138 key
= (agent_ht_key
*) _key
;
140 /* Match 2 elements of the key: name and loglevel. */
143 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
147 /* Event loglevel value and type. */
148 ll_match
= loglevels_match(event
->loglevel_type
,
149 event
->loglevel_value
, key
->loglevel_type
,
150 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
156 /* Filter expression */
157 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
158 /* One has a filter expression, the other does not */
162 if (event
->filter_expression
) {
163 if (strncmp(event
->filter_expression
, key
->filter_expression
,
164 strlen(event
->filter_expression
)) != 0) {
176 * Add unique agent event based on the event name and loglevel.
178 static void add_unique_agent_event(struct lttng_ht
*ht
,
179 struct agent_event
*event
)
181 struct cds_lfht_node
*node_ptr
;
182 struct agent_ht_key key
;
185 LTTNG_ASSERT(ht
->ht
);
188 key
.name
= event
->name
;
189 key
.loglevel_value
= event
->loglevel_value
;
190 key
.loglevel_type
= event
->loglevel_type
;
191 key
.filter_expression
= event
->filter_expression
;
193 node_ptr
= cds_lfht_add_unique(ht
->ht
,
194 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
195 ht_match_event
, &key
, &event
->node
.node
);
196 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
200 * URCU delayed agent event reclaim.
202 static void destroy_event_agent_rcu(struct rcu_head
*head
)
204 struct lttng_ht_node_str
*node
=
205 lttng::utils::container_of(head
, <tng_ht_node_str::head
);
206 struct agent_event
*event
=
207 lttng::utils::container_of(node
, &agent_event::node
);
209 agent_destroy_event(event
);
213 * URCU delayed agent app reclaim.
215 static void destroy_app_agent_rcu(struct rcu_head
*head
)
217 struct lttng_ht_node_ulong
*node
=
218 lttng::utils::container_of(head
, <tng_ht_node_ulong::head
);
219 struct agent_app
*app
=
220 lttng::utils::container_of(node
, &agent_app::node
);
226 * Communication with the agent. Send the message header to the given socket in
229 * Return 0 on success or else a negative errno message of sendmsg() op.
231 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
232 uint32_t cmd
, uint32_t cmd_version
)
236 struct lttcomm_agent_hdr msg
;
240 memset(&msg
, 0, sizeof(msg
));
241 msg
.data_size
= htobe64(data_size
);
242 msg
.cmd
= htobe32(cmd
);
243 msg
.cmd_version
= htobe32(cmd_version
);
245 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
246 if (size
< sizeof(msg
)) {
257 * Communication call with the agent. Send the payload to the given socket. The
258 * header MUST be sent prior to this call.
260 * Return 0 on success or else a negative errno value of sendmsg() op.
262 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
271 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
283 * Communication call with the agent. Receive reply from the agent using the
286 * Return 0 on success or else a negative errno value from recvmsg() op.
288 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
296 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
308 * Internal event listing for a given app. Populate events.
310 * Return number of element in the list or else a negative LTTNG_ERR* code.
311 * On success, the caller is responsible for freeing the memory
312 * allocated for "events".
314 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
316 int ret
, i
, len
= 0, offset
= 0;
319 uint32_t reply_ret_code
;
320 struct lttng_event
*tmp_events
= NULL
;
321 struct lttcomm_agent_list_reply
*reply
= NULL
;
322 struct lttcomm_agent_list_reply_hdr reply_hdr
;
325 LTTNG_ASSERT(app
->sock
);
326 LTTNG_ASSERT(events
);
328 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
331 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
336 /* Get list header so we know how much we'll receive. */
337 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
342 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
343 log_reply_code(reply_ret_code
);
344 switch (reply_ret_code
) {
345 case AGENT_RET_CODE_SUCCESS
:
346 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
353 reply
= zmalloc
<lttcomm_agent_list_reply
>(data_size
);
355 ret
= LTTNG_ERR_NOMEM
;
359 /* Get the list with the appropriate data size. */
360 ret
= recv_reply(app
->sock
, reply
, data_size
);
365 nb_event
= be32toh(reply
->nb_event
);
366 tmp_events
= calloc
<lttng_event
>(nb_event
);
368 ret
= LTTNG_ERR_NOMEM
;
372 for (i
= 0; i
< nb_event
; i
++) {
374 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
375 sizeof(tmp_events
[i
].name
))) {
376 ret
= LTTNG_ERR_INVALID
;
379 tmp_events
[i
].pid
= app
->pid
;
380 tmp_events
[i
].enabled
= -1;
381 len
= strlen(reply
->payload
+ offset
) + 1;
384 *events
= tmp_events
;
390 ret
= LTTNG_ERR_UST_LIST_FAIL
;
399 * Internal enable agent event on a agent application. This function
400 * communicates with the agent to enable a given event.
402 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
404 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
409 size_t filter_expression_length
;
410 uint32_t reply_ret_code
;
411 struct lttcomm_agent_enable_event msg
;
412 struct lttcomm_agent_generic_reply reply
;
415 LTTNG_ASSERT(app
->sock
);
418 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
419 app
->pid
, app
->sock
->fd
);
422 * Calculate the payload's size, which is the fixed-size struct followed
423 * by the variable-length filter expression (+1 for the ending \0).
425 if (!event
->filter_expression
) {
426 filter_expression_length
= 0;
428 filter_expression_length
= strlen(event
->filter_expression
) + 1;
430 data_size
= sizeof(msg
) + filter_expression_length
;
432 memset(&msg
, 0, sizeof(msg
));
433 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
434 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
435 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
436 ret
= LTTNG_ERR_INVALID
;
439 msg
.filter_expression_length
= htobe32(filter_expression_length
);
441 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
446 bytes_to_send
= calloc
<char>(data_size
);
447 if (!bytes_to_send
) {
448 ret
= LTTNG_ERR_NOMEM
;
452 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
453 if (filter_expression_length
> 0) {
454 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
455 filter_expression_length
);
458 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
464 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
469 reply_ret_code
= be32toh(reply
.ret_code
);
470 log_reply_code(reply_ret_code
);
471 switch (reply_ret_code
) {
472 case AGENT_RET_CODE_SUCCESS
:
474 case AGENT_RET_CODE_UNKNOWN_NAME
:
475 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
485 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
491 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
494 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
499 len_be
= htobe32(len
);
500 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
505 ret
= send_payload(sock
, str
, len
);
514 * Internal enable application context on an agent application. This function
515 * communicates with the agent to enable a given application context.
517 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
519 static int app_context_op(const struct agent_app
*app
,
520 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
523 uint32_t reply_ret_code
;
524 struct lttcomm_agent_generic_reply reply
;
525 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
528 LTTNG_ASSERT(app
->sock
);
530 LTTNG_ASSERT(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
531 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
533 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
534 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
535 ctx
->provider_name
, ctx
->ctx_name
,
536 app
->pid
, app
->sock
->fd
);
539 * Calculate the payload's size, which consists of the size (u32, BE)
540 * of the provider name, the NULL-terminated provider name string, the
541 * size (u32, BE) of the context name, followed by the NULL-terminated
542 * context name string.
544 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
545 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
546 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
547 sizeof(uint32_t) + app_ctx_name_len
;
549 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
554 if (app_ctx_provider_name_len
> UINT32_MAX
||
555 app_ctx_name_len
> UINT32_MAX
) {
556 ERR("Application context name > MAX_UINT32");
557 ret
= LTTNG_ERR_INVALID
;
561 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
562 (uint32_t) app_ctx_provider_name_len
);
567 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
568 (uint32_t) app_ctx_name_len
);
573 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
578 reply_ret_code
= be32toh(reply
.ret_code
);
579 log_reply_code(reply_ret_code
);
580 switch (reply_ret_code
) {
581 case AGENT_RET_CODE_SUCCESS
:
591 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
597 * Internal disable agent event call on a agent application. This function
598 * communicates with the agent to disable a given event.
600 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
602 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
606 uint32_t reply_ret_code
;
607 struct lttcomm_agent_disable_event msg
;
608 struct lttcomm_agent_generic_reply reply
;
611 LTTNG_ASSERT(app
->sock
);
614 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
615 app
->pid
, app
->sock
->fd
);
617 data_size
= sizeof(msg
);
618 memset(&msg
, 0, sizeof(msg
));
619 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
620 ret
= LTTNG_ERR_INVALID
;
624 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
629 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
634 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
639 reply_ret_code
= be32toh(reply
.ret_code
);
640 log_reply_code(reply_ret_code
);
641 switch (reply_ret_code
) {
642 case AGENT_RET_CODE_SUCCESS
:
644 case AGENT_RET_CODE_UNKNOWN_NAME
:
645 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
655 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
661 * Send back the registration DONE command to a given agent application.
663 * Return 0 on success or else a negative value.
665 int agent_send_registration_done(struct agent_app
*app
)
668 LTTNG_ASSERT(app
->sock
);
670 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
672 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
676 * Enable agent event on every agent applications registered with the session
679 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
681 int agent_enable_event(struct agent_event
*event
,
682 enum lttng_domain_type domain
)
685 struct agent_app
*app
;
686 struct lttng_ht_iter iter
;
692 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
694 if (app
->domain
!= domain
) {
698 /* Enable event on agent application through TCP socket. */
699 ret
= enable_event(app
, event
);
700 if (ret
!= LTTNG_OK
) {
705 event
->enabled_count
++;
714 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
716 free(ctx
->provider_name
);
722 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
724 struct agent_app_ctx
*agent_ctx
= NULL
;
730 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
731 agent_ctx
= zmalloc
<agent_app_ctx
>();
736 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
737 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
738 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
739 destroy_app_ctx(agent_ctx
);
747 * Enable agent context on every agent applications registered with the session
750 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
752 int agent_enable_context(const struct lttng_event_context
*ctx
,
753 enum lttng_domain_type domain
)
756 struct agent_app
*app
;
757 struct lttng_ht_iter iter
;
760 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
761 ret
= LTTNG_ERR_INVALID
;
767 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
769 struct agent_app_ctx
*agent_ctx
;
771 if (app
->domain
!= domain
) {
775 agent_ctx
= create_app_ctx(ctx
);
777 ret
= LTTNG_ERR_NOMEM
;
781 /* Enable event on agent application through TCP socket. */
782 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
783 destroy_app_ctx(agent_ctx
);
784 if (ret
!= LTTNG_OK
) {
798 * Disable agent event on every agent application registered with the session
801 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
803 int agent_disable_event(struct agent_event
*event
,
804 enum lttng_domain_type domain
)
807 struct agent_app
*app
;
808 struct lttng_ht_iter iter
;
811 if (!AGENT_EVENT_IS_ENABLED(event
)) {
815 if (--event
->enabled_count
!= 0) {
817 * Agent event still enabled. Disable the agent event only when
818 * all "users" have disabled it (event notifiers, event rules,
827 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
829 if (app
->domain
!= domain
) {
833 /* Enable event on agent application through TCP socket. */
834 ret
= disable_event(app
, event
);
835 if (ret
!= LTTNG_OK
) {
840 /* event->enabled_count is now 0. */
841 LTTNG_ASSERT(!AGENT_EVENT_IS_ENABLED(event
));
850 * Disable agent context on every agent application registered with the session
853 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
855 static int disable_context(struct agent_app_ctx
*ctx
,
856 enum lttng_domain_type domain
)
859 struct agent_app
*app
;
860 struct lttng_ht_iter iter
;
865 DBG2("Disabling agent application context %s:%s",
866 ctx
->provider_name
, ctx
->ctx_name
);
867 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
869 if (app
->domain
!= domain
) {
873 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
874 if (ret
!= LTTNG_OK
) {
884 * Ask every agent for the list of possible event. Events is allocated with the
885 * events of every agent application.
887 * Return the number of events or else a negative value.
889 int agent_list_events(struct lttng_event
**events
,
890 enum lttng_domain_type domain
)
893 size_t nbmem
, count
= 0;
894 struct agent_app
*app
;
895 struct lttng_event
*tmp_events
= NULL
;
896 struct lttng_ht_iter iter
;
898 LTTNG_ASSERT(events
);
900 DBG2("Agent listing events for domain %d", domain
);
902 nbmem
= UST_APP_EVENT_LIST_SIZE
;
903 tmp_events
= calloc
<lttng_event
>(nbmem
);
905 PERROR("zmalloc agent list events");
911 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
914 struct lttng_event
*agent_events
;
916 /* Skip domain not asked by the list. */
917 if (app
->domain
!= domain
) {
921 nb_ev
= list_events(app
, &agent_events
);
927 if (count
+ nb_ev
> nbmem
) {
928 /* In case the realloc fails, we free the memory */
929 struct lttng_event
*new_tmp_events
;
932 new_nbmem
= std::max(count
+ nb_ev
, nbmem
<< 1);
933 DBG2("Reallocating agent event list from %zu to %zu entries",
935 new_tmp_events
= (lttng_event
*) realloc(tmp_events
,
936 new_nbmem
* sizeof(*new_tmp_events
));
937 if (!new_tmp_events
) {
938 PERROR("realloc agent events");
943 /* Zero the new memory */
944 memset(new_tmp_events
+ nbmem
, 0,
945 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
947 tmp_events
= new_tmp_events
;
949 memcpy(tmp_events
+ count
, agent_events
,
950 nb_ev
* sizeof(*tmp_events
));
957 *events
= tmp_events
;
968 * Create a agent app object using the given PID.
970 * Return newly allocated object or else NULL on error.
972 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
973 struct lttcomm_sock
*sock
)
975 struct agent_app
*app
;
979 app
= zmalloc
<agent_app
>();
981 PERROR("Failed to allocate agent application instance");
986 app
->domain
= domain
;
988 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
995 * Lookup agent app by socket in the global hash table.
997 * RCU read side lock MUST be acquired.
999 * Return object if found else NULL.
1001 struct agent_app
*agent_find_app_by_sock(int sock
)
1003 struct lttng_ht_node_ulong
*node
;
1004 struct lttng_ht_iter iter
;
1005 struct agent_app
*app
;
1007 LTTNG_ASSERT(sock
>= 0);
1008 ASSERT_RCU_READ_LOCKED();
1010 lttng_ht_lookup(the_agent_apps_ht_by_sock
,
1011 (void *) ((unsigned long) sock
), &iter
);
1012 node
= lttng_ht_iter_get_node_ulong(&iter
);
1016 app
= lttng::utils::container_of(node
, &agent_app::node
);
1018 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1022 DBG3("Agent app NOT found by sock %d.", sock
);
1027 * Add agent application object to the global hash table.
1029 void agent_add_app(struct agent_app
*app
)
1033 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1034 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1038 * Delete agent application from the global hash table.
1040 * rcu_read_lock() must be held by the caller.
1042 void agent_delete_app(struct agent_app
*app
)
1045 struct lttng_ht_iter iter
;
1048 ASSERT_RCU_READ_LOCKED();
1050 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1052 iter
.iter
.node
= &app
->node
.node
;
1053 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1058 * Destroy an agent application object by detaching it from its corresponding
1059 * UST app if one is connected by closing the socket. Finally, perform a
1060 * delayed memory reclaim.
1062 void agent_destroy_app(struct agent_app
*app
)
1067 app
->sock
->ops
->close(app
->sock
);
1068 lttcomm_destroy_sock(app
->sock
);
1071 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1075 * Initialize an already allocated agent object.
1077 * Return 0 on success or else a negative errno value.
1079 int agent_init(struct agent
*agt
)
1085 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1090 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1092 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1100 * Add agent object to the given hash table.
1102 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1107 DBG3("Agent adding from domain %d", agt
->domain
);
1109 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1113 * Create an agent object for the given domain.
1115 * Return the allocated agent or NULL on error.
1117 struct agent
*agent_create(enum lttng_domain_type domain
)
1122 agt
= zmalloc
<agent
>();
1126 agt
->domain
= domain
;
1128 ret
= agent_init(agt
);
1140 * Create a newly allocated agent event data structure.
1141 * Ownership of filter_expression is taken.
1143 * Return a new object else NULL on error.
1145 struct agent_event
*agent_create_event(const char *name
,
1146 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1147 struct lttng_bytecode
*filter
, char *filter_expression
)
1149 struct agent_event
*event
= NULL
;
1151 DBG3("Agent create new event with name %s, loglevel type %d, \
1152 loglevel value %d and filter %s",
1153 name
, loglevel_type
, loglevel_value
,
1154 filter_expression
? filter_expression
: "NULL");
1157 ERR("Failed to create agent event; no name provided.");
1161 event
= zmalloc
<agent_event
>();
1166 strncpy(event
->name
, name
, sizeof(event
->name
));
1167 event
->name
[sizeof(event
->name
) - 1] = '\0';
1168 lttng_ht_node_init_str(&event
->node
, event
->name
);
1170 event
->loglevel_value
= loglevel_value
;
1171 event
->loglevel_type
= loglevel_type
;
1172 event
->filter
= filter
;
1173 event
->filter_expression
= filter_expression
;
1179 * Unique add of a agent event to an agent object.
1181 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1183 LTTNG_ASSERT(event
);
1185 LTTNG_ASSERT(agt
->events
);
1187 DBG3("Agent adding event %s", event
->name
);
1188 add_unique_agent_event(agt
->events
, event
);
1189 agt
->being_used
= 1;
1193 * Unique add of a agent context to an agent object.
1195 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1198 struct agent_app_ctx
*agent_ctx
= NULL
;
1202 LTTNG_ASSERT(agt
->events
);
1203 LTTNG_ASSERT(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1205 agent_ctx
= create_app_ctx(ctx
);
1207 ret
= LTTNG_ERR_NOMEM
;
1211 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1212 ctx
->u
.app_ctx
.ctx_name
);
1213 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1219 * Find multiple agent events sharing the given name.
1221 * RCU read side lock MUST be acquired. It must be held for the
1222 * duration of the iteration.
1224 * Sets the given iterator.
1226 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1227 struct lttng_ht_iter
* iter
)
1229 struct lttng_ht
*ht
;
1230 struct agent_ht_key key
;
1234 LTTNG_ASSERT(agt
->events
);
1236 ASSERT_RCU_READ_LOCKED();
1241 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1242 ht_match_event_by_name
, &key
, &iter
->iter
);
1246 * Find the agent event matching a trigger.
1248 * RCU read side lock MUST be acquired. It must be held for as long as
1249 * the returned agent_event is used.
1251 * Return object if found else NULL.
1253 struct agent_event
*agent_find_event_by_trigger(
1254 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1256 enum lttng_condition_status c_status
;
1257 enum lttng_event_rule_status er_status
;
1258 enum lttng_domain_type domain
;
1259 const struct lttng_condition
*condition
;
1260 const struct lttng_event_rule
*rule
;
1262 const char *filter_expression
;
1263 const struct lttng_log_level_rule
*log_level_rule
;
1264 /* Unused when loglevel_type is 'ALL'. */
1265 int loglevel_value
= 0;
1266 enum lttng_loglevel_type loglevel_type
;
1267 event_rule_logging_get_name_pattern logging_get_name_pattern
;
1268 event_rule_logging_get_log_level_rule logging_get_log_level_rule
;
1271 LTTNG_ASSERT(agt
->events
);
1272 ASSERT_RCU_READ_LOCKED();
1274 condition
= lttng_trigger_get_const_condition(trigger
);
1276 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1277 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1279 c_status
= lttng_condition_event_rule_matches_get_rule(
1281 LTTNG_ASSERT(c_status
== LTTNG_CONDITION_STATUS_OK
);
1283 switch (lttng_event_rule_get_type(rule
)) {
1284 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
1285 logging_get_name_pattern
=
1286 lttng_event_rule_jul_logging_get_name_pattern
;
1287 logging_get_log_level_rule
=
1288 lttng_event_rule_jul_logging_get_log_level_rule
;
1290 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
1291 logging_get_name_pattern
=
1292 lttng_event_rule_log4j_logging_get_name_pattern
;
1293 logging_get_log_level_rule
=
1294 lttng_event_rule_log4j_logging_get_log_level_rule
;
1296 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
1297 logging_get_name_pattern
=
1298 lttng_event_rule_python_logging_get_name_pattern
;
1299 logging_get_log_level_rule
=
1300 lttng_event_rule_python_logging_get_log_level_rule
;
1307 domain
= lttng_event_rule_get_domain_type(rule
);
1308 LTTNG_ASSERT(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1309 domain
== LTTNG_DOMAIN_PYTHON
);
1311 /* Get the event's pattern name ('name' in the legacy terminology). */
1312 er_status
= logging_get_name_pattern(rule
, &name
);
1313 LTTNG_ASSERT(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1315 /* Get the internal filter expression. */
1316 filter_expression
= lttng_event_rule_get_filter(rule
);
1318 /* Map log_level_rule to loglevel value. */
1319 er_status
= logging_get_log_level_rule(rule
, &log_level_rule
);
1320 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1321 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1323 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1324 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1329 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1330 filter_expression
, agt
);
1334 * Get the next agent event duplicate by name. This should be called
1335 * after a call to agent_find_events_by_name() to iterate on events.
1337 * The RCU read lock must be held during the iteration and for as long
1338 * as the object the iterator points to remains in use.
1340 void agent_event_next_duplicate(const char *name
,
1341 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1343 struct agent_ht_key key
;
1345 ASSERT_RCU_READ_LOCKED();
1349 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1354 * Find a agent event in the given agent using name, loglevel and filter.
1356 * RCU read side lock MUST be acquired. It must be kept for as long as
1357 * the returned agent_event is used.
1359 * Return object if found else NULL.
1361 struct agent_event
*agent_find_event(const char *name
,
1362 enum lttng_loglevel_type loglevel_type
,
1364 const char *filter_expression
,
1367 struct lttng_ht_node_str
*node
;
1368 struct lttng_ht_iter iter
;
1369 struct lttng_ht
*ht
;
1370 struct agent_ht_key key
;
1374 LTTNG_ASSERT(agt
->events
);
1375 ASSERT_RCU_READ_LOCKED();
1379 key
.loglevel_value
= loglevel_value
;
1380 key
.loglevel_type
= loglevel_type
;
1381 key
.filter_expression
= filter_expression
;
1383 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1384 ht_match_event
, &key
, &iter
.iter
);
1385 node
= lttng_ht_iter_get_node_str(&iter
);
1390 DBG3("Agent event found %s.", name
);
1391 return lttng::utils::container_of(node
, &agent_event::node
);
1394 DBG3("Agent event NOT found %s.", name
);
1399 * Free given agent event. This event must not be globally visible at this
1400 * point (only expected to be used on failure just after event creation). After
1401 * this call, the pointer is not usable anymore.
1403 void agent_destroy_event(struct agent_event
*event
)
1405 LTTNG_ASSERT(event
);
1407 free(event
->filter
);
1408 free(event
->filter_expression
);
1409 free(event
->exclusion
);
1414 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1416 struct agent_app_ctx
*ctx
=
1417 lttng::utils::container_of(head
, &agent_app_ctx::rcu_node
);
1419 destroy_app_ctx(ctx
);
1423 * Destroy an agent completely.
1425 void agent_destroy(struct agent
*agt
)
1427 struct lttng_ht_node_str
*node
;
1428 struct lttng_ht_iter iter
;
1429 struct agent_app_ctx
*ctx
;
1433 DBG3("Agent destroy");
1436 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1438 struct agent_event
*event
;
1441 * When destroying an event, we have to try to disable it on the
1442 * agent side so the event stops generating data. The return
1443 * value is not important since we have to continue anyway
1444 * destroying the object.
1446 event
= lttng::utils::container_of(node
, &agent_event::node
);
1447 (void) agent_disable_event(event
, agt
->domain
);
1449 ret
= lttng_ht_del(agt
->events
, &iter
);
1451 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1454 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1455 (void) disable_context(ctx
, agt
->domain
);
1456 cds_list_del(&ctx
->list_node
);
1457 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1460 lttng_ht_destroy(agt
->events
);
1465 * Allocate agent_apps_ht_by_sock.
1467 int agent_app_ht_alloc(void)
1469 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1470 return the_agent_apps_ht_by_sock
? 0 : -1;
1474 * Destroy a agent application by socket.
1476 void agent_destroy_app_by_sock(int sock
)
1478 struct agent_app
*app
;
1480 LTTNG_ASSERT(sock
>= 0);
1483 * Not finding an application is a very important error that should NEVER
1484 * happen. The hash table deletion is ONLY done through this call when the
1485 * main sessiond thread is torn down.
1488 app
= agent_find_app_by_sock(sock
);
1491 /* RCU read side lock is assumed to be held by this function. */
1492 agent_delete_app(app
);
1494 /* The application is freed in a RCU call but the socket is closed here. */
1495 agent_destroy_app(app
);
1500 * Clean-up the agent app hash table and destroy it.
1502 void agent_app_ht_clean(void)
1504 struct lttng_ht_node_ulong
*node
;
1505 struct lttng_ht_iter iter
;
1507 if (!the_agent_apps_ht_by_sock
) {
1511 cds_lfht_for_each_entry(
1512 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1513 struct agent_app
*app
;
1515 app
= lttng::utils::container_of(node
, &agent_app::node
);
1516 agent_destroy_app_by_sock(app
->sock
->fd
);
1520 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1524 * Update a agent application (given socket) using the given agent.
1526 * Note that this function is most likely to be used with a tracing session
1527 * thus the caller should make sure to hold the appropriate lock(s).
1529 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1532 struct agent_event
*event
;
1533 struct lttng_ht_iter iter
;
1534 struct agent_app_ctx
*ctx
;
1539 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1543 * We are in the registration path thus if the application is gone,
1544 * there is a serious code flow error.
1547 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1548 /* Skip event if disabled. */
1549 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1553 ret
= enable_event(app
, event
);
1554 if (ret
!= LTTNG_OK
) {
1555 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1556 event
->name
, app
->pid
, app
->sock
->fd
);
1557 /* Let's try the others here and don't assume the app is dead. */
1562 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1563 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1564 if (ret
!= LTTNG_OK
) {
1565 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1566 ctx
->provider_name
, ctx
->ctx_name
,
1567 app
->pid
, app
->sock
->fd
);
1576 * Allocate the per-event notifier domain agent hash table. It is lazily
1577 * populated as domains are used.
1579 int agent_by_event_notifier_domain_ht_create(void)
1581 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1582 return the_trigger_agents_ht_by_domain
? 0 : -1;
1586 * Clean-up the per-event notifier domain agent hash table and destroy it.
1588 void agent_by_event_notifier_domain_ht_destroy(void)
1590 struct lttng_ht_node_u64
*node
;
1591 struct lttng_ht_iter iter
;
1593 if (!the_trigger_agents_ht_by_domain
) {
1598 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
1599 &iter
.iter
, node
, node
) {
1600 struct agent
*agent
=
1601 lttng::utils::container_of(node
, &agent::node
);
1602 const int ret
= lttng_ht_del(
1603 the_trigger_agents_ht_by_domain
, &iter
);
1605 LTTNG_ASSERT(ret
== 0);
1606 agent_destroy(agent
);
1610 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1613 struct agent
*agent_find_by_event_notifier_domain(
1614 enum lttng_domain_type domain_type
)
1616 struct agent
*agt
= NULL
;
1617 struct lttng_ht_node_u64
*node
;
1618 struct lttng_ht_iter iter
;
1619 const uint64_t key
= (uint64_t) domain_type
;
1621 LTTNG_ASSERT(the_trigger_agents_ht_by_domain
);
1623 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1624 lttng_domain_type_str(domain_type
));
1626 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1627 node
= lttng_ht_iter_get_node_u64(&iter
);
1632 agt
= lttng::utils::container_of(node
, &agent::node
);