2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include <urcu/uatomic.h>
12 #include <urcu/rculist.h>
14 #include <lttng/event-rule/event-rule.h>
15 #include <lttng/event-rule/event-rule-internal.h>
16 #include <lttng/event-rule/tracepoint.h>
17 #include <lttng/condition/condition.h>
18 #include <lttng/condition/event-rule.h>
19 #include <lttng/domain-internal.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/agent.h>
24 #include <common/compat/endian.h>
29 #include "common/error.h"
31 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
34 * Agent application context representation.
36 struct agent_app_ctx
{
40 /* agent_app_ctx are part of the agent app_ctx_list. */
41 struct cds_list_head list_node
;
43 /* For call_rcu teardown. */
44 struct rcu_head rcu_node
;
48 * Human readable agent return code.
50 static const char *error_string_array
[] = {
51 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
52 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
53 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
56 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
60 void log_reply_code(uint32_t in_reply_ret_code
)
62 int level
= PRINT_DBG3
;
64 * reply_ret_code and in_reply_ret_code are kept separate to have a
65 * sanitized value (used to retrieve the human readable string) and the
66 * original value which is logged as-is.
68 uint32_t reply_ret_code
= in_reply_ret_code
;
70 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
71 reply_ret_code
>= AGENT_RET_CODE_NR
) {
72 reply_ret_code
= AGENT_RET_CODE_NR
;
76 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
77 error_string_array
[AGENT_RET_CODE_INDEX(
83 * Match function for the events hash table lookup by name.
85 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
88 struct agent_event
*event
;
89 const struct agent_ht_key
*key
;
94 event
= caa_container_of(node
, struct agent_event
, node
.node
);
97 /* Match 1 elements of the key: name. */
100 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
111 * Match function for the events hash table lookup by name, log level and
114 static int ht_match_event(struct cds_lfht_node
*node
,
117 struct agent_event
*event
;
118 const struct agent_ht_key
*key
;
124 event
= caa_container_of(node
, struct agent_event
, node
.node
);
127 /* Match 2 elements of the key: name and loglevel. */
130 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
134 /* Event loglevel value and type. */
135 ll_match
= loglevels_match(event
->loglevel_type
,
136 event
->loglevel_value
, key
->loglevel_type
,
137 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
143 /* Filter expression */
144 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
145 /* One has a filter expression, the other does not */
149 if (event
->filter_expression
) {
150 if (strncmp(event
->filter_expression
, key
->filter_expression
,
151 strlen(event
->filter_expression
)) != 0) {
163 * Add unique agent event based on the event name and loglevel.
165 static void add_unique_agent_event(struct lttng_ht
*ht
,
166 struct agent_event
*event
)
168 struct cds_lfht_node
*node_ptr
;
169 struct agent_ht_key key
;
175 key
.name
= event
->name
;
176 key
.loglevel_value
= event
->loglevel_value
;
177 key
.loglevel_type
= event
->loglevel_type
;
178 key
.filter_expression
= event
->filter_expression
;
180 node_ptr
= cds_lfht_add_unique(ht
->ht
,
181 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
182 ht_match_event
, &key
, &event
->node
.node
);
183 assert(node_ptr
== &event
->node
.node
);
187 * URCU delayed agent event reclaim.
189 static void destroy_event_agent_rcu(struct rcu_head
*head
)
191 struct lttng_ht_node_str
*node
=
192 caa_container_of(head
, struct lttng_ht_node_str
, head
);
193 struct agent_event
*event
=
194 caa_container_of(node
, struct agent_event
, node
);
196 agent_destroy_event(event
);
200 * URCU delayed agent app reclaim.
202 static void destroy_app_agent_rcu(struct rcu_head
*head
)
204 struct lttng_ht_node_ulong
*node
=
205 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
206 struct agent_app
*app
=
207 caa_container_of(node
, struct agent_app
, node
);
213 * Communication with the agent. Send the message header to the given socket in
216 * Return 0 on success or else a negative errno message of sendmsg() op.
218 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
219 uint32_t cmd
, uint32_t cmd_version
)
223 struct lttcomm_agent_hdr msg
;
227 memset(&msg
, 0, sizeof(msg
));
228 msg
.data_size
= htobe64(data_size
);
229 msg
.cmd
= htobe32(cmd
);
230 msg
.cmd_version
= htobe32(cmd_version
);
232 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
233 if (size
< sizeof(msg
)) {
244 * Communication call with the agent. Send the payload to the given socket. The
245 * header MUST be sent prior to this call.
247 * Return 0 on success or else a negative errno value of sendmsg() op.
249 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
258 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
270 * Communication call with the agent. Receive reply from the agent using the
273 * Return 0 on success or else a negative errno value from recvmsg() op.
275 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
283 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
295 * Internal event listing for a given app. Populate events.
297 * Return number of element in the list or else a negative LTTNG_ERR* code.
298 * On success, the caller is responsible for freeing the memory
299 * allocated for "events".
301 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
303 int ret
, i
, len
= 0, offset
= 0;
306 uint32_t reply_ret_code
;
307 struct lttng_event
*tmp_events
= NULL
;
308 struct lttcomm_agent_list_reply
*reply
= NULL
;
309 struct lttcomm_agent_list_reply_hdr reply_hdr
;
315 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
318 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
323 /* Get list header so we know how much we'll receive. */
324 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
329 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
330 log_reply_code(reply_ret_code
);
331 switch (reply_ret_code
) {
332 case AGENT_RET_CODE_SUCCESS
:
333 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
340 reply
= zmalloc(data_size
);
342 ret
= LTTNG_ERR_NOMEM
;
346 /* Get the list with the appropriate data size. */
347 ret
= recv_reply(app
->sock
, reply
, data_size
);
352 nb_event
= be32toh(reply
->nb_event
);
353 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
355 ret
= LTTNG_ERR_NOMEM
;
359 for (i
= 0; i
< nb_event
; i
++) {
361 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
362 sizeof(tmp_events
[i
].name
))) {
363 ret
= LTTNG_ERR_INVALID
;
366 tmp_events
[i
].pid
= app
->pid
;
367 tmp_events
[i
].enabled
= -1;
368 len
= strlen(reply
->payload
+ offset
) + 1;
371 *events
= tmp_events
;
377 ret
= LTTNG_ERR_UST_LIST_FAIL
;
386 * Internal enable agent event on a agent application. This function
387 * communicates with the agent to enable a given event.
389 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
391 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
396 size_t filter_expression_length
;
397 uint32_t reply_ret_code
;
398 struct lttcomm_agent_enable_event msg
;
399 struct lttcomm_agent_generic_reply reply
;
405 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
406 app
->pid
, app
->sock
->fd
);
409 * Calculate the payload's size, which is the fixed-size struct followed
410 * by the variable-length filter expression (+1 for the ending \0).
412 if (!event
->filter_expression
) {
413 filter_expression_length
= 0;
415 filter_expression_length
= strlen(event
->filter_expression
) + 1;
417 data_size
= sizeof(msg
) + filter_expression_length
;
419 memset(&msg
, 0, sizeof(msg
));
420 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
421 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
422 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
423 ret
= LTTNG_ERR_INVALID
;
426 msg
.filter_expression_length
= htobe32(filter_expression_length
);
428 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
433 bytes_to_send
= zmalloc(data_size
);
434 if (!bytes_to_send
) {
435 ret
= LTTNG_ERR_NOMEM
;
439 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
440 if (filter_expression_length
> 0) {
441 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
442 filter_expression_length
);
445 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
451 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
456 reply_ret_code
= be32toh(reply
.ret_code
);
457 log_reply_code(reply_ret_code
);
458 switch (reply_ret_code
) {
459 case AGENT_RET_CODE_SUCCESS
:
461 case AGENT_RET_CODE_UNKNOWN_NAME
:
462 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
472 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
478 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
481 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
486 len_be
= htobe32(len
);
487 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
492 ret
= send_payload(sock
, str
, len
);
501 * Internal enable application context on an agent application. This function
502 * communicates with the agent to enable a given application context.
504 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
506 static int app_context_op(const struct agent_app
*app
,
507 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
510 uint32_t reply_ret_code
;
511 struct lttcomm_agent_generic_reply reply
;
512 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
517 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
518 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
520 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
521 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
522 ctx
->provider_name
, ctx
->ctx_name
,
523 app
->pid
, app
->sock
->fd
);
526 * Calculate the payload's size, which consists of the size (u32, BE)
527 * of the provider name, the NULL-terminated provider name string, the
528 * size (u32, BE) of the context name, followed by the NULL-terminated
529 * context name string.
531 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
532 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
533 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
534 sizeof(uint32_t) + app_ctx_name_len
;
536 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
541 if (app_ctx_provider_name_len
> UINT32_MAX
||
542 app_ctx_name_len
> UINT32_MAX
) {
543 ERR("Application context name > MAX_UINT32");
544 ret
= LTTNG_ERR_INVALID
;
548 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
549 (uint32_t) app_ctx_provider_name_len
);
554 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
555 (uint32_t) app_ctx_name_len
);
560 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
565 reply_ret_code
= be32toh(reply
.ret_code
);
566 log_reply_code(reply_ret_code
);
567 switch (reply_ret_code
) {
568 case AGENT_RET_CODE_SUCCESS
:
578 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
584 * Internal disable agent event call on a agent application. This function
585 * communicates with the agent to disable a given event.
587 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
589 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
593 uint32_t reply_ret_code
;
594 struct lttcomm_agent_disable_event msg
;
595 struct lttcomm_agent_generic_reply reply
;
601 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
602 app
->pid
, app
->sock
->fd
);
604 data_size
= sizeof(msg
);
605 memset(&msg
, 0, sizeof(msg
));
606 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
607 ret
= LTTNG_ERR_INVALID
;
611 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
616 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
621 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
626 reply_ret_code
= be32toh(reply
.ret_code
);
627 log_reply_code(reply_ret_code
);
628 switch (reply_ret_code
) {
629 case AGENT_RET_CODE_SUCCESS
:
631 case AGENT_RET_CODE_UNKNOWN_NAME
:
632 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
642 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
648 * Send back the registration DONE command to a given agent application.
650 * Return 0 on success or else a negative value.
652 int agent_send_registration_done(struct agent_app
*app
)
657 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
659 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
663 * Enable agent event on every agent applications registered with the session
666 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
668 int agent_enable_event(struct agent_event
*event
,
669 enum lttng_domain_type domain
)
672 struct agent_app
*app
;
673 struct lttng_ht_iter iter
;
679 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
681 if (app
->domain
!= domain
) {
685 /* Enable event on agent application through TCP socket. */
686 ret
= enable_event(app
, event
);
687 if (ret
!= LTTNG_OK
) {
692 event
->enabled_count
++;
701 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
703 free(ctx
->provider_name
);
709 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
711 struct agent_app_ctx
*agent_ctx
= NULL
;
717 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
718 agent_ctx
= zmalloc(sizeof(*ctx
));
723 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
724 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
725 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
726 destroy_app_ctx(agent_ctx
);
734 * Enable agent context on every agent applications registered with the session
737 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
739 int agent_enable_context(const struct lttng_event_context
*ctx
,
740 enum lttng_domain_type domain
)
743 struct agent_app
*app
;
744 struct lttng_ht_iter iter
;
747 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
748 ret
= LTTNG_ERR_INVALID
;
754 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
756 struct agent_app_ctx
*agent_ctx
;
758 if (app
->domain
!= domain
) {
762 agent_ctx
= create_app_ctx(ctx
);
764 ret
= LTTNG_ERR_NOMEM
;
768 /* Enable event on agent application through TCP socket. */
769 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
770 destroy_app_ctx(agent_ctx
);
771 if (ret
!= LTTNG_OK
) {
785 * Disable agent event on every agent application registered with the session
788 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
790 int agent_disable_event(struct agent_event
*event
,
791 enum lttng_domain_type domain
)
794 struct agent_app
*app
;
795 struct lttng_ht_iter iter
;
798 if (!AGENT_EVENT_IS_ENABLED(event
)) {
802 if (--event
->enabled_count
!= 0) {
804 * Agent event still enabled. Disable the agent event only when
805 * all "users" have disabled it (event notifiers, event rules,
814 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
816 if (app
->domain
!= domain
) {
820 /* Enable event on agent application through TCP socket. */
821 ret
= disable_event(app
, event
);
822 if (ret
!= LTTNG_OK
) {
827 /* event->enabled_count is now 0. */
828 assert(!AGENT_EVENT_IS_ENABLED(event
));
837 * Disable agent context on every agent application registered with the session
840 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
842 static int disable_context(struct agent_app_ctx
*ctx
,
843 enum lttng_domain_type domain
)
846 struct agent_app
*app
;
847 struct lttng_ht_iter iter
;
852 DBG2("Disabling agent application context %s:%s",
853 ctx
->provider_name
, ctx
->ctx_name
);
854 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
856 if (app
->domain
!= domain
) {
860 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
861 if (ret
!= LTTNG_OK
) {
871 * Ask every agent for the list of possible event. Events is allocated with the
872 * events of every agent application.
874 * Return the number of events or else a negative value.
876 int agent_list_events(struct lttng_event
**events
,
877 enum lttng_domain_type domain
)
880 size_t nbmem
, count
= 0;
881 struct agent_app
*app
;
882 struct lttng_event
*tmp_events
= NULL
;
883 struct lttng_ht_iter iter
;
887 DBG2("Agent listing events for domain %d", domain
);
889 nbmem
= UST_APP_EVENT_LIST_SIZE
;
890 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
892 PERROR("zmalloc agent list events");
898 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
901 struct lttng_event
*agent_events
;
903 /* Skip domain not asked by the list. */
904 if (app
->domain
!= domain
) {
908 nb_ev
= list_events(app
, &agent_events
);
914 if (count
+ nb_ev
> nbmem
) {
915 /* In case the realloc fails, we free the memory */
916 struct lttng_event
*new_tmp_events
;
919 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
920 DBG2("Reallocating agent event list from %zu to %zu entries",
922 new_tmp_events
= realloc(tmp_events
,
923 new_nbmem
* sizeof(*new_tmp_events
));
924 if (!new_tmp_events
) {
925 PERROR("realloc agent events");
930 /* Zero the new memory */
931 memset(new_tmp_events
+ nbmem
, 0,
932 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
934 tmp_events
= new_tmp_events
;
936 memcpy(tmp_events
+ count
, agent_events
,
937 nb_ev
* sizeof(*tmp_events
));
944 *events
= tmp_events
;
955 * Create a agent app object using the given PID.
957 * Return newly allocated object or else NULL on error.
959 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
960 struct lttcomm_sock
*sock
)
962 struct agent_app
*app
;
966 app
= zmalloc(sizeof(*app
));
968 PERROR("Failed to allocate agent application instance");
973 app
->domain
= domain
;
975 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
982 * Lookup agent app by socket in the global hash table.
984 * RCU read side lock MUST be acquired.
986 * Return object if found else NULL.
988 struct agent_app
*agent_find_app_by_sock(int sock
)
990 struct lttng_ht_node_ulong
*node
;
991 struct lttng_ht_iter iter
;
992 struct agent_app
*app
;
996 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
997 node
= lttng_ht_iter_get_node_ulong(&iter
);
1001 app
= caa_container_of(node
, struct agent_app
, node
);
1003 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1007 DBG3("Agent app NOT found by sock %d.", sock
);
1012 * Add agent application object to the global hash table.
1014 void agent_add_app(struct agent_app
*app
)
1018 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1019 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
1023 * Delete agent application from the global hash table.
1025 * rcu_read_lock() must be held by the caller.
1027 void agent_delete_app(struct agent_app
*app
)
1030 struct lttng_ht_iter iter
;
1034 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1036 iter
.iter
.node
= &app
->node
.node
;
1037 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
1042 * Destroy an agent application object by detaching it from its corresponding
1043 * UST app if one is connected by closing the socket. Finally, perform a
1044 * delayed memory reclaim.
1046 void agent_destroy_app(struct agent_app
*app
)
1051 app
->sock
->ops
->close(app
->sock
);
1052 lttcomm_destroy_sock(app
->sock
);
1055 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1059 * Initialize an already allocated agent object.
1061 * Return 0 on success or else a negative errno value.
1063 int agent_init(struct agent
*agt
)
1069 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1074 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1076 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1084 * Add agent object to the given hash table.
1086 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1091 DBG3("Agent adding from domain %d", agt
->domain
);
1093 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1097 * Create an agent object for the given domain.
1099 * Return the allocated agent or NULL on error.
1101 struct agent
*agent_create(enum lttng_domain_type domain
)
1106 agt
= zmalloc(sizeof(struct agent
));
1110 agt
->domain
= domain
;
1112 ret
= agent_init(agt
);
1124 * Create a newly allocated agent event data structure.
1125 * Ownership of filter_expression is taken.
1127 * Return a new object else NULL on error.
1129 struct agent_event
*agent_create_event(const char *name
,
1130 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1131 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
1133 struct agent_event
*event
= NULL
;
1135 DBG3("Agent create new event with name %s, loglevel type %d, \
1136 loglevel value %d and filter %s",
1137 name
, loglevel_type
, loglevel_value
,
1138 filter_expression
? filter_expression
: "NULL");
1141 ERR("Failed to create agent event; no name provided.");
1145 event
= zmalloc(sizeof(*event
));
1150 strncpy(event
->name
, name
, sizeof(event
->name
));
1151 event
->name
[sizeof(event
->name
) - 1] = '\0';
1152 lttng_ht_node_init_str(&event
->node
, event
->name
);
1154 event
->loglevel_value
= loglevel_value
;
1155 event
->loglevel_type
= loglevel_type
;
1156 event
->filter
= filter
;
1157 event
->filter_expression
= filter_expression
;
1163 * Unique add of a agent event to an agent object.
1165 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1169 assert(agt
->events
);
1171 DBG3("Agent adding event %s", event
->name
);
1172 add_unique_agent_event(agt
->events
, event
);
1173 agt
->being_used
= 1;
1177 * Unique add of a agent context to an agent object.
1179 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1182 struct agent_app_ctx
*agent_ctx
= NULL
;
1186 assert(agt
->events
);
1187 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1189 agent_ctx
= create_app_ctx(ctx
);
1191 ret
= LTTNG_ERR_NOMEM
;
1195 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1196 ctx
->u
.app_ctx
.ctx_name
);
1197 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1203 * Find multiple agent events sharing the given name.
1205 * RCU read side lock MUST be acquired. It must be held for the
1206 * duration of the iteration.
1208 * Sets the given iterator.
1210 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1211 struct lttng_ht_iter
* iter
)
1213 struct lttng_ht
*ht
;
1214 struct agent_ht_key key
;
1218 assert(agt
->events
);
1224 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1225 ht_match_event_by_name
, &key
, &iter
->iter
);
1229 * Find the agent event matching a trigger.
1231 * RCU read side lock MUST be acquired. It must be held for as long as
1232 * the returned agent_event is used.
1234 * Return object if found else NULL.
1236 struct agent_event
*agent_find_event_by_trigger(
1237 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1239 enum lttng_condition_status c_status
;
1240 enum lttng_event_rule_status er_status
;
1241 enum lttng_domain_type domain
;
1242 const struct lttng_condition
*condition
;
1243 const struct lttng_event_rule
*rule
;
1245 const char *filter_expression
;
1246 /* Unused when loglevel_type is 'ALL'. */
1247 int loglevel_value
= 0;
1248 enum lttng_loglevel_type loglevel_type
;
1251 assert(agt
->events
);
1253 condition
= lttng_trigger_get_const_condition(trigger
);
1255 assert(lttng_condition_get_type(condition
) ==
1256 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
1258 c_status
= lttng_condition_event_rule_get_rule(condition
, &rule
);
1259 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
1261 assert(lttng_event_rule_get_type(rule
) ==
1262 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1264 domain
= lttng_event_rule_get_domain_type(rule
);
1265 assert(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1266 domain
== LTTNG_DOMAIN_PYTHON
);
1268 /* Get the event's pattern ('name' in the legacy terminology). */
1269 er_status
= lttng_event_rule_tracepoint_get_pattern(rule
, &name
);
1270 assert(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1272 /* Get the internal filter expression. */
1273 filter_expression
= lttng_event_rule_get_filter(rule
);
1275 er_status
= lttng_event_rule_tracepoint_get_log_level_type(
1276 rule
, &loglevel_type
);
1277 assert(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1279 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1280 er_status
= lttng_event_rule_tracepoint_get_log_level(
1281 rule
, &loglevel_value
);
1282 assert(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1285 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1286 filter_expression
, agt
);
1290 * Get the next agent event duplicate by name. This should be called
1291 * after a call to agent_find_events_by_name() to iterate on events.
1293 * The RCU read lock must be held during the iteration and for as long
1294 * as the object the iterator points to remains in use.
1296 void agent_event_next_duplicate(const char *name
,
1297 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1299 struct agent_ht_key key
;
1303 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1308 * Find a agent event in the given agent using name, loglevel and filter.
1310 * RCU read side lock MUST be acquired. It must be kept for as long as
1311 * the returned agent_event is used.
1313 * Return object if found else NULL.
1315 struct agent_event
*agent_find_event(const char *name
,
1316 enum lttng_loglevel_type loglevel_type
,
1318 const char *filter_expression
,
1321 struct lttng_ht_node_str
*node
;
1322 struct lttng_ht_iter iter
;
1323 struct lttng_ht
*ht
;
1324 struct agent_ht_key key
;
1328 assert(agt
->events
);
1332 key
.loglevel_value
= loglevel_value
;
1333 key
.loglevel_type
= loglevel_type
;
1334 key
.filter_expression
= filter_expression
;
1336 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1337 ht_match_event
, &key
, &iter
.iter
);
1338 node
= lttng_ht_iter_get_node_str(&iter
);
1343 DBG3("Agent event found %s.", name
);
1344 return caa_container_of(node
, struct agent_event
, node
);
1347 DBG3("Agent event NOT found %s.", name
);
1352 * Free given agent event. This event must not be globally visible at this
1353 * point (only expected to be used on failure just after event creation). After
1354 * this call, the pointer is not usable anymore.
1356 void agent_destroy_event(struct agent_event
*event
)
1360 free(event
->filter
);
1361 free(event
->filter_expression
);
1362 free(event
->exclusion
);
1367 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1369 struct agent_app_ctx
*ctx
=
1370 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1372 destroy_app_ctx(ctx
);
1376 * Destroy an agent completely.
1378 void agent_destroy(struct agent
*agt
)
1380 struct lttng_ht_node_str
*node
;
1381 struct lttng_ht_iter iter
;
1382 struct agent_app_ctx
*ctx
;
1386 DBG3("Agent destroy");
1389 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1391 struct agent_event
*event
;
1394 * When destroying an event, we have to try to disable it on the
1395 * agent side so the event stops generating data. The return
1396 * value is not important since we have to continue anyway
1397 * destroying the object.
1399 event
= caa_container_of(node
, struct agent_event
, node
);
1400 (void) agent_disable_event(event
, agt
->domain
);
1402 ret
= lttng_ht_del(agt
->events
, &iter
);
1404 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1407 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1408 (void) disable_context(ctx
, agt
->domain
);
1409 cds_list_del(&ctx
->list_node
);
1410 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1413 ht_cleanup_push(agt
->events
);
1418 * Allocate agent_apps_ht_by_sock.
1420 int agent_app_ht_alloc(void)
1422 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1423 return agent_apps_ht_by_sock
? 0 : -1;
1427 * Destroy a agent application by socket.
1429 void agent_destroy_app_by_sock(int sock
)
1431 struct agent_app
*app
;
1436 * Not finding an application is a very important error that should NEVER
1437 * happen. The hash table deletion is ONLY done through this call when the
1438 * main sessiond thread is torn down.
1441 app
= agent_find_app_by_sock(sock
);
1444 /* RCU read side lock is assumed to be held by this function. */
1445 agent_delete_app(app
);
1447 /* The application is freed in a RCU call but the socket is closed here. */
1448 agent_destroy_app(app
);
1453 * Clean-up the agent app hash table and destroy it.
1455 void agent_app_ht_clean(void)
1457 struct lttng_ht_node_ulong
*node
;
1458 struct lttng_ht_iter iter
;
1460 if (!agent_apps_ht_by_sock
) {
1464 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1465 struct agent_app
*app
;
1467 app
= caa_container_of(node
, struct agent_app
, node
);
1468 agent_destroy_app_by_sock(app
->sock
->fd
);
1472 lttng_ht_destroy(agent_apps_ht_by_sock
);
1476 * Update a agent application (given socket) using the given agent.
1478 * Note that this function is most likely to be used with a tracing session
1479 * thus the caller should make sure to hold the appropriate lock(s).
1481 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1484 struct agent_event
*event
;
1485 struct lttng_ht_iter iter
;
1486 struct agent_app_ctx
*ctx
;
1491 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1495 * We are in the registration path thus if the application is gone,
1496 * there is a serious code flow error.
1499 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1500 /* Skip event if disabled. */
1501 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1505 ret
= enable_event(app
, event
);
1506 if (ret
!= LTTNG_OK
) {
1507 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1508 event
->name
, app
->pid
, app
->sock
->fd
);
1509 /* Let's try the others here and don't assume the app is dead. */
1514 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1515 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1516 if (ret
!= LTTNG_OK
) {
1517 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1518 ctx
->provider_name
, ctx
->ctx_name
,
1519 app
->pid
, app
->sock
->fd
);
1528 * Allocate the per-event notifier domain agent hash table. It is lazily
1529 * populated as domains are used.
1531 int agent_by_event_notifier_domain_ht_create(void)
1533 trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1534 return trigger_agents_ht_by_domain
? 0 : -1;
1538 * Clean-up the per-event notifier domain agent hash table and destroy it.
1540 void agent_by_event_notifier_domain_ht_destroy(void)
1542 struct lttng_ht_node_u64
*node
;
1543 struct lttng_ht_iter iter
;
1545 if (!trigger_agents_ht_by_domain
) {
1550 cds_lfht_for_each_entry (trigger_agents_ht_by_domain
->ht
, &iter
.iter
,
1552 struct agent
*agent
=
1553 caa_container_of(node
, struct agent
, node
);
1554 const int ret
= lttng_ht_del(
1555 trigger_agents_ht_by_domain
, &iter
);
1558 agent_destroy(agent
);
1562 lttng_ht_destroy(trigger_agents_ht_by_domain
);
1565 struct agent
*agent_find_by_event_notifier_domain(
1566 enum lttng_domain_type domain_type
)
1568 struct agent
*agt
= NULL
;
1569 struct lttng_ht_node_u64
*node
;
1570 struct lttng_ht_iter iter
;
1571 const uint64_t key
= (uint64_t) domain_type
;
1573 assert(trigger_agents_ht_by_domain
);
1575 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1576 lttng_domain_type_str(domain_type
));
1578 lttng_ht_lookup(trigger_agents_ht_by_domain
, &key
, &iter
);
1579 node
= lttng_ht_iter_get_node_u64(&iter
);
1584 agt
= caa_container_of(node
, struct agent
, node
);