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/on-event.h>
19 #include <lttng/domain-internal.h>
20 #include <lttng/log-level-rule-internal.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/agent.h>
25 #include <common/compat/endian.h>
30 #include "common/error.h"
32 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
35 * Agent application context representation.
37 struct agent_app_ctx
{
41 /* agent_app_ctx are part of the agent app_ctx_list. */
42 struct cds_list_head list_node
;
44 /* For call_rcu teardown. */
45 struct rcu_head rcu_node
;
49 * Human readable agent return code.
51 static const char *error_string_array
[] = {
52 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
53 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
54 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
57 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
61 void log_reply_code(uint32_t in_reply_ret_code
)
63 int level
= PRINT_DBG3
;
65 * reply_ret_code and in_reply_ret_code are kept separate to have a
66 * sanitized value (used to retrieve the human readable string) and the
67 * original value which is logged as-is.
69 uint32_t reply_ret_code
= in_reply_ret_code
;
71 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
72 reply_ret_code
>= AGENT_RET_CODE_NR
) {
73 reply_ret_code
= AGENT_RET_CODE_NR
;
77 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
78 error_string_array
[AGENT_RET_CODE_INDEX(
84 * Match function for the events hash table lookup by name.
86 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
89 struct agent_event
*event
;
90 const struct agent_ht_key
*key
;
95 event
= caa_container_of(node
, struct agent_event
, node
.node
);
98 /* Match 1 elements of the key: name. */
101 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
112 * Match function for the events hash table lookup by name, log level and
115 static int ht_match_event(struct cds_lfht_node
*node
,
118 struct agent_event
*event
;
119 const struct agent_ht_key
*key
;
125 event
= caa_container_of(node
, struct agent_event
, node
.node
);
128 /* Match 2 elements of the key: name and loglevel. */
131 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
135 /* Event loglevel value and type. */
136 ll_match
= loglevels_match(event
->loglevel_type
,
137 event
->loglevel_value
, key
->loglevel_type
,
138 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
144 /* Filter expression */
145 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
146 /* One has a filter expression, the other does not */
150 if (event
->filter_expression
) {
151 if (strncmp(event
->filter_expression
, key
->filter_expression
,
152 strlen(event
->filter_expression
)) != 0) {
164 * Add unique agent event based on the event name and loglevel.
166 static void add_unique_agent_event(struct lttng_ht
*ht
,
167 struct agent_event
*event
)
169 struct cds_lfht_node
*node_ptr
;
170 struct agent_ht_key key
;
176 key
.name
= event
->name
;
177 key
.loglevel_value
= event
->loglevel_value
;
178 key
.loglevel_type
= event
->loglevel_type
;
179 key
.filter_expression
= event
->filter_expression
;
181 node_ptr
= cds_lfht_add_unique(ht
->ht
,
182 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
183 ht_match_event
, &key
, &event
->node
.node
);
184 assert(node_ptr
== &event
->node
.node
);
188 * URCU delayed agent event reclaim.
190 static void destroy_event_agent_rcu(struct rcu_head
*head
)
192 struct lttng_ht_node_str
*node
=
193 caa_container_of(head
, struct lttng_ht_node_str
, head
);
194 struct agent_event
*event
=
195 caa_container_of(node
, struct agent_event
, node
);
197 agent_destroy_event(event
);
201 * URCU delayed agent app reclaim.
203 static void destroy_app_agent_rcu(struct rcu_head
*head
)
205 struct lttng_ht_node_ulong
*node
=
206 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
207 struct agent_app
*app
=
208 caa_container_of(node
, struct agent_app
, node
);
214 * Communication with the agent. Send the message header to the given socket in
217 * Return 0 on success or else a negative errno message of sendmsg() op.
219 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
220 uint32_t cmd
, uint32_t cmd_version
)
224 struct lttcomm_agent_hdr msg
;
228 memset(&msg
, 0, sizeof(msg
));
229 msg
.data_size
= htobe64(data_size
);
230 msg
.cmd
= htobe32(cmd
);
231 msg
.cmd_version
= htobe32(cmd_version
);
233 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
234 if (size
< sizeof(msg
)) {
245 * Communication call with the agent. Send the payload to the given socket. The
246 * header MUST be sent prior to this call.
248 * Return 0 on success or else a negative errno value of sendmsg() op.
250 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
259 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
271 * Communication call with the agent. Receive reply from the agent using the
274 * Return 0 on success or else a negative errno value from recvmsg() op.
276 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
284 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
296 * Internal event listing for a given app. Populate events.
298 * Return number of element in the list or else a negative LTTNG_ERR* code.
299 * On success, the caller is responsible for freeing the memory
300 * allocated for "events".
302 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
304 int ret
, i
, len
= 0, offset
= 0;
307 uint32_t reply_ret_code
;
308 struct lttng_event
*tmp_events
= NULL
;
309 struct lttcomm_agent_list_reply
*reply
= NULL
;
310 struct lttcomm_agent_list_reply_hdr reply_hdr
;
316 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
319 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
324 /* Get list header so we know how much we'll receive. */
325 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
330 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
331 log_reply_code(reply_ret_code
);
332 switch (reply_ret_code
) {
333 case AGENT_RET_CODE_SUCCESS
:
334 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
341 reply
= zmalloc(data_size
);
343 ret
= LTTNG_ERR_NOMEM
;
347 /* Get the list with the appropriate data size. */
348 ret
= recv_reply(app
->sock
, reply
, data_size
);
353 nb_event
= be32toh(reply
->nb_event
);
354 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
356 ret
= LTTNG_ERR_NOMEM
;
360 for (i
= 0; i
< nb_event
; i
++) {
362 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
363 sizeof(tmp_events
[i
].name
))) {
364 ret
= LTTNG_ERR_INVALID
;
367 tmp_events
[i
].pid
= app
->pid
;
368 tmp_events
[i
].enabled
= -1;
369 len
= strlen(reply
->payload
+ offset
) + 1;
372 *events
= tmp_events
;
378 ret
= LTTNG_ERR_UST_LIST_FAIL
;
387 * Internal enable agent event on a agent application. This function
388 * communicates with the agent to enable a given event.
390 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
392 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
397 size_t filter_expression_length
;
398 uint32_t reply_ret_code
;
399 struct lttcomm_agent_enable_event msg
;
400 struct lttcomm_agent_generic_reply reply
;
406 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
407 app
->pid
, app
->sock
->fd
);
410 * Calculate the payload's size, which is the fixed-size struct followed
411 * by the variable-length filter expression (+1 for the ending \0).
413 if (!event
->filter_expression
) {
414 filter_expression_length
= 0;
416 filter_expression_length
= strlen(event
->filter_expression
) + 1;
418 data_size
= sizeof(msg
) + filter_expression_length
;
420 memset(&msg
, 0, sizeof(msg
));
421 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
422 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
423 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
424 ret
= LTTNG_ERR_INVALID
;
427 msg
.filter_expression_length
= htobe32(filter_expression_length
);
429 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
434 bytes_to_send
= zmalloc(data_size
);
435 if (!bytes_to_send
) {
436 ret
= LTTNG_ERR_NOMEM
;
440 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
441 if (filter_expression_length
> 0) {
442 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
443 filter_expression_length
);
446 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
452 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
457 reply_ret_code
= be32toh(reply
.ret_code
);
458 log_reply_code(reply_ret_code
);
459 switch (reply_ret_code
) {
460 case AGENT_RET_CODE_SUCCESS
:
462 case AGENT_RET_CODE_UNKNOWN_NAME
:
463 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
473 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
479 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
482 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
487 len_be
= htobe32(len
);
488 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
493 ret
= send_payload(sock
, str
, len
);
502 * Internal enable application context on an agent application. This function
503 * communicates with the agent to enable a given application context.
505 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
507 static int app_context_op(const struct agent_app
*app
,
508 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
511 uint32_t reply_ret_code
;
512 struct lttcomm_agent_generic_reply reply
;
513 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
518 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
519 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
521 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
522 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
523 ctx
->provider_name
, ctx
->ctx_name
,
524 app
->pid
, app
->sock
->fd
);
527 * Calculate the payload's size, which consists of the size (u32, BE)
528 * of the provider name, the NULL-terminated provider name string, the
529 * size (u32, BE) of the context name, followed by the NULL-terminated
530 * context name string.
532 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
533 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
534 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
535 sizeof(uint32_t) + app_ctx_name_len
;
537 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
542 if (app_ctx_provider_name_len
> UINT32_MAX
||
543 app_ctx_name_len
> UINT32_MAX
) {
544 ERR("Application context name > MAX_UINT32");
545 ret
= LTTNG_ERR_INVALID
;
549 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
550 (uint32_t) app_ctx_provider_name_len
);
555 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
556 (uint32_t) app_ctx_name_len
);
561 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
566 reply_ret_code
= be32toh(reply
.ret_code
);
567 log_reply_code(reply_ret_code
);
568 switch (reply_ret_code
) {
569 case AGENT_RET_CODE_SUCCESS
:
579 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
585 * Internal disable agent event call on a agent application. This function
586 * communicates with the agent to disable a given event.
588 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
590 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
594 uint32_t reply_ret_code
;
595 struct lttcomm_agent_disable_event msg
;
596 struct lttcomm_agent_generic_reply reply
;
602 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
603 app
->pid
, app
->sock
->fd
);
605 data_size
= sizeof(msg
);
606 memset(&msg
, 0, sizeof(msg
));
607 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
608 ret
= LTTNG_ERR_INVALID
;
612 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
617 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
622 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
627 reply_ret_code
= be32toh(reply
.ret_code
);
628 log_reply_code(reply_ret_code
);
629 switch (reply_ret_code
) {
630 case AGENT_RET_CODE_SUCCESS
:
632 case AGENT_RET_CODE_UNKNOWN_NAME
:
633 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
643 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
649 * Send back the registration DONE command to a given agent application.
651 * Return 0 on success or else a negative value.
653 int agent_send_registration_done(struct agent_app
*app
)
658 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
660 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
664 * Enable agent event on every agent applications registered with the session
667 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
669 int agent_enable_event(struct agent_event
*event
,
670 enum lttng_domain_type domain
)
673 struct agent_app
*app
;
674 struct lttng_ht_iter iter
;
680 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
682 if (app
->domain
!= domain
) {
686 /* Enable event on agent application through TCP socket. */
687 ret
= enable_event(app
, event
);
688 if (ret
!= LTTNG_OK
) {
693 event
->enabled_count
++;
702 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
704 free(ctx
->provider_name
);
710 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
712 struct agent_app_ctx
*agent_ctx
= NULL
;
718 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
719 agent_ctx
= zmalloc(sizeof(*ctx
));
724 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
725 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
726 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
727 destroy_app_ctx(agent_ctx
);
735 * Enable agent context on every agent applications registered with the session
738 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
740 int agent_enable_context(const struct lttng_event_context
*ctx
,
741 enum lttng_domain_type domain
)
744 struct agent_app
*app
;
745 struct lttng_ht_iter iter
;
748 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
749 ret
= LTTNG_ERR_INVALID
;
755 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
757 struct agent_app_ctx
*agent_ctx
;
759 if (app
->domain
!= domain
) {
763 agent_ctx
= create_app_ctx(ctx
);
765 ret
= LTTNG_ERR_NOMEM
;
769 /* Enable event on agent application through TCP socket. */
770 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
771 destroy_app_ctx(agent_ctx
);
772 if (ret
!= LTTNG_OK
) {
786 * Disable agent event on every agent application registered with the session
789 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
791 int agent_disable_event(struct agent_event
*event
,
792 enum lttng_domain_type domain
)
795 struct agent_app
*app
;
796 struct lttng_ht_iter iter
;
799 if (!AGENT_EVENT_IS_ENABLED(event
)) {
803 if (--event
->enabled_count
!= 0) {
805 * Agent event still enabled. Disable the agent event only when
806 * all "users" have disabled it (event notifiers, event rules,
815 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
817 if (app
->domain
!= domain
) {
821 /* Enable event on agent application through TCP socket. */
822 ret
= disable_event(app
, event
);
823 if (ret
!= LTTNG_OK
) {
828 /* event->enabled_count is now 0. */
829 assert(!AGENT_EVENT_IS_ENABLED(event
));
838 * Disable agent context on every agent application registered with the session
841 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
843 static int disable_context(struct agent_app_ctx
*ctx
,
844 enum lttng_domain_type domain
)
847 struct agent_app
*app
;
848 struct lttng_ht_iter iter
;
853 DBG2("Disabling agent application context %s:%s",
854 ctx
->provider_name
, ctx
->ctx_name
);
855 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
857 if (app
->domain
!= domain
) {
861 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
862 if (ret
!= LTTNG_OK
) {
872 * Ask every agent for the list of possible event. Events is allocated with the
873 * events of every agent application.
875 * Return the number of events or else a negative value.
877 int agent_list_events(struct lttng_event
**events
,
878 enum lttng_domain_type domain
)
881 size_t nbmem
, count
= 0;
882 struct agent_app
*app
;
883 struct lttng_event
*tmp_events
= NULL
;
884 struct lttng_ht_iter iter
;
888 DBG2("Agent listing events for domain %d", domain
);
890 nbmem
= UST_APP_EVENT_LIST_SIZE
;
891 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
893 PERROR("zmalloc agent list events");
899 cds_lfht_for_each_entry(the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
902 struct lttng_event
*agent_events
;
904 /* Skip domain not asked by the list. */
905 if (app
->domain
!= domain
) {
909 nb_ev
= list_events(app
, &agent_events
);
915 if (count
+ nb_ev
> nbmem
) {
916 /* In case the realloc fails, we free the memory */
917 struct lttng_event
*new_tmp_events
;
920 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
921 DBG2("Reallocating agent event list from %zu to %zu entries",
923 new_tmp_events
= realloc(tmp_events
,
924 new_nbmem
* sizeof(*new_tmp_events
));
925 if (!new_tmp_events
) {
926 PERROR("realloc agent events");
931 /* Zero the new memory */
932 memset(new_tmp_events
+ nbmem
, 0,
933 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
935 tmp_events
= new_tmp_events
;
937 memcpy(tmp_events
+ count
, agent_events
,
938 nb_ev
* sizeof(*tmp_events
));
945 *events
= tmp_events
;
956 * Create a agent app object using the given PID.
958 * Return newly allocated object or else NULL on error.
960 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
961 struct lttcomm_sock
*sock
)
963 struct agent_app
*app
;
967 app
= zmalloc(sizeof(*app
));
969 PERROR("Failed to allocate agent application instance");
974 app
->domain
= domain
;
976 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
983 * Lookup agent app by socket in the global hash table.
985 * RCU read side lock MUST be acquired.
987 * Return object if found else NULL.
989 struct agent_app
*agent_find_app_by_sock(int sock
)
991 struct lttng_ht_node_ulong
*node
;
992 struct lttng_ht_iter iter
;
993 struct agent_app
*app
;
997 lttng_ht_lookup(the_agent_apps_ht_by_sock
,
998 (void *) ((unsigned long) sock
), &iter
);
999 node
= lttng_ht_iter_get_node_ulong(&iter
);
1003 app
= caa_container_of(node
, struct agent_app
, node
);
1005 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
1009 DBG3("Agent app NOT found by sock %d.", sock
);
1014 * Add agent application object to the global hash table.
1016 void agent_add_app(struct agent_app
*app
)
1020 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1021 lttng_ht_add_unique_ulong(the_agent_apps_ht_by_sock
, &app
->node
);
1025 * Delete agent application from the global hash table.
1027 * rcu_read_lock() must be held by the caller.
1029 void agent_delete_app(struct agent_app
*app
)
1032 struct lttng_ht_iter iter
;
1036 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1038 iter
.iter
.node
= &app
->node
.node
;
1039 ret
= lttng_ht_del(the_agent_apps_ht_by_sock
, &iter
);
1044 * Destroy an agent application object by detaching it from its corresponding
1045 * UST app if one is connected by closing the socket. Finally, perform a
1046 * delayed memory reclaim.
1048 void agent_destroy_app(struct agent_app
*app
)
1053 app
->sock
->ops
->close(app
->sock
);
1054 lttcomm_destroy_sock(app
->sock
);
1057 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1061 * Initialize an already allocated agent object.
1063 * Return 0 on success or else a negative errno value.
1065 int agent_init(struct agent
*agt
)
1071 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1076 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1078 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1086 * Add agent object to the given hash table.
1088 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1093 DBG3("Agent adding from domain %d", agt
->domain
);
1095 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1099 * Create an agent object for the given domain.
1101 * Return the allocated agent or NULL on error.
1103 struct agent
*agent_create(enum lttng_domain_type domain
)
1108 agt
= zmalloc(sizeof(struct agent
));
1112 agt
->domain
= domain
;
1114 ret
= agent_init(agt
);
1126 * Create a newly allocated agent event data structure.
1127 * Ownership of filter_expression is taken.
1129 * Return a new object else NULL on error.
1131 struct agent_event
*agent_create_event(const char *name
,
1132 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1133 struct lttng_bytecode
*filter
, char *filter_expression
)
1135 struct agent_event
*event
= NULL
;
1137 DBG3("Agent create new event with name %s, loglevel type %d, \
1138 loglevel value %d and filter %s",
1139 name
, loglevel_type
, loglevel_value
,
1140 filter_expression
? filter_expression
: "NULL");
1143 ERR("Failed to create agent event; no name provided.");
1147 event
= zmalloc(sizeof(*event
));
1152 strncpy(event
->name
, name
, sizeof(event
->name
));
1153 event
->name
[sizeof(event
->name
) - 1] = '\0';
1154 lttng_ht_node_init_str(&event
->node
, event
->name
);
1156 event
->loglevel_value
= loglevel_value
;
1157 event
->loglevel_type
= loglevel_type
;
1158 event
->filter
= filter
;
1159 event
->filter_expression
= filter_expression
;
1165 * Unique add of a agent event to an agent object.
1167 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1171 assert(agt
->events
);
1173 DBG3("Agent adding event %s", event
->name
);
1174 add_unique_agent_event(agt
->events
, event
);
1175 agt
->being_used
= 1;
1179 * Unique add of a agent context to an agent object.
1181 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1184 struct agent_app_ctx
*agent_ctx
= NULL
;
1188 assert(agt
->events
);
1189 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1191 agent_ctx
= create_app_ctx(ctx
);
1193 ret
= LTTNG_ERR_NOMEM
;
1197 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1198 ctx
->u
.app_ctx
.ctx_name
);
1199 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1205 * Find multiple agent events sharing the given name.
1207 * RCU read side lock MUST be acquired. It must be held for the
1208 * duration of the iteration.
1210 * Sets the given iterator.
1212 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1213 struct lttng_ht_iter
* iter
)
1215 struct lttng_ht
*ht
;
1216 struct agent_ht_key key
;
1220 assert(agt
->events
);
1226 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1227 ht_match_event_by_name
, &key
, &iter
->iter
);
1231 * Find the agent event matching a trigger.
1233 * RCU read side lock MUST be acquired. It must be held for as long as
1234 * the returned agent_event is used.
1236 * Return object if found else NULL.
1238 struct agent_event
*agent_find_event_by_trigger(
1239 const struct lttng_trigger
*trigger
, struct agent
*agt
)
1241 enum lttng_condition_status c_status
;
1242 enum lttng_event_rule_status er_status
;
1243 enum lttng_domain_type domain
;
1244 const struct lttng_condition
*condition
;
1245 const struct lttng_event_rule
*rule
;
1247 const char *filter_expression
;
1248 const struct lttng_log_level_rule
*log_level_rule
;
1249 /* Unused when loglevel_type is 'ALL'. */
1250 int loglevel_value
= 0;
1251 enum lttng_loglevel_type loglevel_type
;
1254 assert(agt
->events
);
1256 condition
= lttng_trigger_get_const_condition(trigger
);
1258 assert(lttng_condition_get_type(condition
) ==
1259 LTTNG_CONDITION_TYPE_ON_EVENT
);
1261 c_status
= lttng_condition_on_event_get_rule(condition
, &rule
);
1262 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
1264 assert(lttng_event_rule_get_type(rule
) ==
1265 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1267 domain
= lttng_event_rule_get_domain_type(rule
);
1268 assert(domain
== LTTNG_DOMAIN_JUL
|| domain
== LTTNG_DOMAIN_LOG4J
||
1269 domain
== LTTNG_DOMAIN_PYTHON
);
1271 /* Get the event's pattern ('name' in the legacy terminology). */
1272 er_status
= lttng_event_rule_tracepoint_get_pattern(rule
, &name
);
1273 assert(er_status
== LTTNG_EVENT_RULE_STATUS_OK
);
1275 /* Get the internal filter expression. */
1276 filter_expression
= lttng_event_rule_get_filter(rule
);
1278 /* Map log_level_rule to loglevel value. */
1279 er_status
= lttng_event_rule_tracepoint_get_log_level_rule(
1280 rule
, &log_level_rule
);
1281 if (er_status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
1282 loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1284 } else if (er_status
== LTTNG_EVENT_RULE_STATUS_OK
) {
1285 lttng_log_level_rule_to_loglevel(log_level_rule
, &loglevel_type
, &loglevel_value
);
1290 return agent_find_event(name
, loglevel_type
, loglevel_value
,
1291 filter_expression
, agt
);
1295 * Get the next agent event duplicate by name. This should be called
1296 * after a call to agent_find_events_by_name() to iterate on events.
1298 * The RCU read lock must be held during the iteration and for as long
1299 * as the object the iterator points to remains in use.
1301 void agent_event_next_duplicate(const char *name
,
1302 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1304 struct agent_ht_key key
;
1308 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1313 * Find a agent event in the given agent using name, loglevel and filter.
1315 * RCU read side lock MUST be acquired. It must be kept for as long as
1316 * the returned agent_event is used.
1318 * Return object if found else NULL.
1320 struct agent_event
*agent_find_event(const char *name
,
1321 enum lttng_loglevel_type loglevel_type
,
1323 const char *filter_expression
,
1326 struct lttng_ht_node_str
*node
;
1327 struct lttng_ht_iter iter
;
1328 struct lttng_ht
*ht
;
1329 struct agent_ht_key key
;
1333 assert(agt
->events
);
1337 key
.loglevel_value
= loglevel_value
;
1338 key
.loglevel_type
= loglevel_type
;
1339 key
.filter_expression
= filter_expression
;
1341 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1342 ht_match_event
, &key
, &iter
.iter
);
1343 node
= lttng_ht_iter_get_node_str(&iter
);
1348 DBG3("Agent event found %s.", name
);
1349 return caa_container_of(node
, struct agent_event
, node
);
1352 DBG3("Agent event NOT found %s.", name
);
1357 * Free given agent event. This event must not be globally visible at this
1358 * point (only expected to be used on failure just after event creation). After
1359 * this call, the pointer is not usable anymore.
1361 void agent_destroy_event(struct agent_event
*event
)
1365 free(event
->filter
);
1366 free(event
->filter_expression
);
1367 free(event
->exclusion
);
1372 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1374 struct agent_app_ctx
*ctx
=
1375 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1377 destroy_app_ctx(ctx
);
1381 * Destroy an agent completely.
1383 void agent_destroy(struct agent
*agt
)
1385 struct lttng_ht_node_str
*node
;
1386 struct lttng_ht_iter iter
;
1387 struct agent_app_ctx
*ctx
;
1391 DBG3("Agent destroy");
1394 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1396 struct agent_event
*event
;
1399 * When destroying an event, we have to try to disable it on the
1400 * agent side so the event stops generating data. The return
1401 * value is not important since we have to continue anyway
1402 * destroying the object.
1404 event
= caa_container_of(node
, struct agent_event
, node
);
1405 (void) agent_disable_event(event
, agt
->domain
);
1407 ret
= lttng_ht_del(agt
->events
, &iter
);
1409 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1412 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1413 (void) disable_context(ctx
, agt
->domain
);
1414 cds_list_del(&ctx
->list_node
);
1415 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1418 ht_cleanup_push(agt
->events
);
1423 * Allocate agent_apps_ht_by_sock.
1425 int agent_app_ht_alloc(void)
1427 the_agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1428 return the_agent_apps_ht_by_sock
? 0 : -1;
1432 * Destroy a agent application by socket.
1434 void agent_destroy_app_by_sock(int sock
)
1436 struct agent_app
*app
;
1441 * Not finding an application is a very important error that should NEVER
1442 * happen. The hash table deletion is ONLY done through this call when the
1443 * main sessiond thread is torn down.
1446 app
= agent_find_app_by_sock(sock
);
1449 /* RCU read side lock is assumed to be held by this function. */
1450 agent_delete_app(app
);
1452 /* The application is freed in a RCU call but the socket is closed here. */
1453 agent_destroy_app(app
);
1458 * Clean-up the agent app hash table and destroy it.
1460 void agent_app_ht_clean(void)
1462 struct lttng_ht_node_ulong
*node
;
1463 struct lttng_ht_iter iter
;
1465 if (!the_agent_apps_ht_by_sock
) {
1469 cds_lfht_for_each_entry(
1470 the_agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1471 struct agent_app
*app
;
1473 app
= caa_container_of(node
, struct agent_app
, node
);
1474 agent_destroy_app_by_sock(app
->sock
->fd
);
1478 lttng_ht_destroy(the_agent_apps_ht_by_sock
);
1482 * Update a agent application (given socket) using the given agent.
1484 * Note that this function is most likely to be used with a tracing session
1485 * thus the caller should make sure to hold the appropriate lock(s).
1487 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1490 struct agent_event
*event
;
1491 struct lttng_ht_iter iter
;
1492 struct agent_app_ctx
*ctx
;
1497 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1501 * We are in the registration path thus if the application is gone,
1502 * there is a serious code flow error.
1505 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1506 /* Skip event if disabled. */
1507 if (!AGENT_EVENT_IS_ENABLED(event
)) {
1511 ret
= enable_event(app
, event
);
1512 if (ret
!= LTTNG_OK
) {
1513 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1514 event
->name
, app
->pid
, app
->sock
->fd
);
1515 /* Let's try the others here and don't assume the app is dead. */
1520 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1521 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1522 if (ret
!= LTTNG_OK
) {
1523 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1524 ctx
->provider_name
, ctx
->ctx_name
,
1525 app
->pid
, app
->sock
->fd
);
1534 * Allocate the per-event notifier domain agent hash table. It is lazily
1535 * populated as domains are used.
1537 int agent_by_event_notifier_domain_ht_create(void)
1539 the_trigger_agents_ht_by_domain
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
1540 return the_trigger_agents_ht_by_domain
? 0 : -1;
1544 * Clean-up the per-event notifier domain agent hash table and destroy it.
1546 void agent_by_event_notifier_domain_ht_destroy(void)
1548 struct lttng_ht_node_u64
*node
;
1549 struct lttng_ht_iter iter
;
1551 if (!the_trigger_agents_ht_by_domain
) {
1556 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
1557 &iter
.iter
, node
, node
) {
1558 struct agent
*agent
=
1559 caa_container_of(node
, struct agent
, node
);
1560 const int ret
= lttng_ht_del(
1561 the_trigger_agents_ht_by_domain
, &iter
);
1564 agent_destroy(agent
);
1568 lttng_ht_destroy(the_trigger_agents_ht_by_domain
);
1571 struct agent
*agent_find_by_event_notifier_domain(
1572 enum lttng_domain_type domain_type
)
1574 struct agent
*agt
= NULL
;
1575 struct lttng_ht_node_u64
*node
;
1576 struct lttng_ht_iter iter
;
1577 const uint64_t key
= (uint64_t) domain_type
;
1579 assert(the_trigger_agents_ht_by_domain
);
1581 DBG3("Per-event notifier domain agent lookup for domain '%s'",
1582 lttng_domain_type_str(domain_type
));
1584 lttng_ht_lookup(the_trigger_agents_ht_by_domain
, &key
, &iter
);
1585 node
= lttng_ht_iter_get_node_u64(&iter
);
1590 agt
= caa_container_of(node
, struct agent
, node
);