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 <common/common.h>
15 #include <common/sessiond-comm/agent.h>
17 #include <common/compat/endian.h>
22 #include "common/error.h"
24 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
27 * Agent application context representation.
29 struct agent_app_ctx
{
33 /* agent_app_ctx are part of the agent app_ctx_list. */
34 struct cds_list_head list_node
;
36 /* For call_rcu teardown. */
37 struct rcu_head rcu_node
;
41 * Human readable agent return code.
43 static const char *error_string_array
[] = {
44 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
45 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
46 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
49 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
53 void log_reply_code(uint32_t in_reply_ret_code
)
55 int level
= PRINT_DBG3
;
57 * reply_ret_code and in_reply_ret_code are kept separate to have a
58 * sanitized value (used to retrieve the human readable string) and the
59 * original value which is logged as-is.
61 uint32_t reply_ret_code
= in_reply_ret_code
;
63 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
64 reply_ret_code
>= AGENT_RET_CODE_NR
) {
65 reply_ret_code
= AGENT_RET_CODE_NR
;
69 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
70 error_string_array
[AGENT_RET_CODE_INDEX(
76 * Match function for the events hash table lookup by name.
78 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
81 struct agent_event
*event
;
82 const struct agent_ht_key
*key
;
87 event
= caa_container_of(node
, struct agent_event
, node
.node
);
90 /* Match 1 elements of the key: name. */
93 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
104 * Match function for the events hash table lookup by name and loglevel.
106 static int ht_match_event(struct cds_lfht_node
*node
,
109 struct agent_event
*event
;
110 const struct agent_ht_key
*key
;
116 event
= caa_container_of(node
, struct agent_event
, node
.node
);
119 /* Match 2 elements of the key: name and loglevel. */
122 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
126 /* Event loglevel value and type. */
127 ll_match
= loglevels_match(event
->loglevel_type
,
128 event
->loglevel_value
, key
->loglevel_type
,
129 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
135 /* Filter expression */
136 if (!!event
->filter_expression
!= !!key
->filter_expression
) {
137 /* One has a filter expression, the other does not */
141 if (event
->filter_expression
) {
142 if (strncmp(event
->filter_expression
, key
->filter_expression
,
143 strlen(event
->filter_expression
)) != 0) {
155 * Add unique agent event based on the event name and loglevel.
157 static void add_unique_agent_event(struct lttng_ht
*ht
,
158 struct agent_event
*event
)
160 struct cds_lfht_node
*node_ptr
;
161 struct agent_ht_key key
;
167 key
.name
= event
->name
;
168 key
.loglevel_value
= event
->loglevel_value
;
169 key
.loglevel_type
= event
->loglevel_type
;
170 key
.filter_expression
= event
->filter_expression
;
172 node_ptr
= cds_lfht_add_unique(ht
->ht
,
173 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
174 ht_match_event
, &key
, &event
->node
.node
);
175 assert(node_ptr
== &event
->node
.node
);
179 * URCU delayed agent event reclaim.
181 static void destroy_event_agent_rcu(struct rcu_head
*head
)
183 struct lttng_ht_node_str
*node
=
184 caa_container_of(head
, struct lttng_ht_node_str
, head
);
185 struct agent_event
*event
=
186 caa_container_of(node
, struct agent_event
, node
);
188 agent_destroy_event(event
);
192 * URCU delayed agent app reclaim.
194 static void destroy_app_agent_rcu(struct rcu_head
*head
)
196 struct lttng_ht_node_ulong
*node
=
197 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
198 struct agent_app
*app
=
199 caa_container_of(node
, struct agent_app
, node
);
205 * Communication with the agent. Send the message header to the given socket in
208 * Return 0 on success or else a negative errno message of sendmsg() op.
210 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
211 uint32_t cmd
, uint32_t cmd_version
)
215 struct lttcomm_agent_hdr msg
;
219 memset(&msg
, 0, sizeof(msg
));
220 msg
.data_size
= htobe64(data_size
);
221 msg
.cmd
= htobe32(cmd
);
222 msg
.cmd_version
= htobe32(cmd_version
);
224 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
225 if (size
< sizeof(msg
)) {
236 * Communication call with the agent. Send the payload to the given socket. The
237 * header MUST be sent prior to this call.
239 * Return 0 on success or else a negative errno value of sendmsg() op.
241 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
250 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
262 * Communication call with the agent. Receive reply from the agent using the
265 * Return 0 on success or else a negative errno value from recvmsg() op.
267 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
275 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
287 * Internal event listing for a given app. Populate events.
289 * Return number of element in the list or else a negative LTTNG_ERR* code.
290 * On success, the caller is responsible for freeing the memory
291 * allocated for "events".
293 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
295 int ret
, i
, len
= 0, offset
= 0;
298 uint32_t reply_ret_code
;
299 struct lttng_event
*tmp_events
= NULL
;
300 struct lttcomm_agent_list_reply
*reply
= NULL
;
301 struct lttcomm_agent_list_reply_hdr reply_hdr
;
307 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
310 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
315 /* Get list header so we know how much we'll receive. */
316 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
321 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
322 log_reply_code(reply_ret_code
);
323 switch (reply_ret_code
) {
324 case AGENT_RET_CODE_SUCCESS
:
325 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
332 reply
= zmalloc(data_size
);
334 ret
= LTTNG_ERR_NOMEM
;
338 /* Get the list with the appropriate data size. */
339 ret
= recv_reply(app
->sock
, reply
, data_size
);
344 nb_event
= be32toh(reply
->nb_event
);
345 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
347 ret
= LTTNG_ERR_NOMEM
;
351 for (i
= 0; i
< nb_event
; i
++) {
353 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
354 sizeof(tmp_events
[i
].name
))) {
355 ret
= LTTNG_ERR_INVALID
;
358 tmp_events
[i
].pid
= app
->pid
;
359 tmp_events
[i
].enabled
= -1;
360 len
= strlen(reply
->payload
+ offset
) + 1;
363 *events
= tmp_events
;
369 ret
= LTTNG_ERR_UST_LIST_FAIL
;
378 * Internal enable agent event on a agent application. This function
379 * communicates with the agent to enable a given event.
381 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
383 static int enable_event(const struct agent_app
*app
, struct agent_event
*event
)
388 size_t filter_expression_length
;
389 uint32_t reply_ret_code
;
390 struct lttcomm_agent_enable_event msg
;
391 struct lttcomm_agent_generic_reply reply
;
397 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
398 app
->pid
, app
->sock
->fd
);
401 * Calculate the payload's size, which is the fixed-size struct followed
402 * by the variable-length filter expression (+1 for the ending \0).
404 if (!event
->filter_expression
) {
405 filter_expression_length
= 0;
407 filter_expression_length
= strlen(event
->filter_expression
) + 1;
409 data_size
= sizeof(msg
) + filter_expression_length
;
411 memset(&msg
, 0, sizeof(msg
));
412 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
413 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
414 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
415 ret
= LTTNG_ERR_INVALID
;
418 msg
.filter_expression_length
= htobe32(filter_expression_length
);
420 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
425 bytes_to_send
= zmalloc(data_size
);
426 if (!bytes_to_send
) {
427 ret
= LTTNG_ERR_NOMEM
;
431 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
432 if (filter_expression_length
> 0) {
433 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
434 filter_expression_length
);
437 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
443 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
448 reply_ret_code
= be32toh(reply
.ret_code
);
449 log_reply_code(reply_ret_code
);
450 switch (reply_ret_code
) {
451 case AGENT_RET_CODE_SUCCESS
:
453 case AGENT_RET_CODE_UNKNOWN_NAME
:
454 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
464 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
470 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
473 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
478 len_be
= htobe32(len
);
479 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
484 ret
= send_payload(sock
, str
, len
);
493 * Internal enable application context on an agent application. This function
494 * communicates with the agent to enable a given application context.
496 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
498 static int app_context_op(const struct agent_app
*app
,
499 const struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
502 uint32_t reply_ret_code
;
503 struct lttcomm_agent_generic_reply reply
;
504 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
509 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
510 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
512 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
513 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
514 ctx
->provider_name
, ctx
->ctx_name
,
515 app
->pid
, app
->sock
->fd
);
518 * Calculate the payload's size, which consists of the size (u32, BE)
519 * of the provider name, the NULL-terminated provider name string, the
520 * size (u32, BE) of the context name, followed by the NULL-terminated
521 * context name string.
523 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
524 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
525 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
526 sizeof(uint32_t) + app_ctx_name_len
;
528 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
533 if (app_ctx_provider_name_len
> UINT32_MAX
||
534 app_ctx_name_len
> UINT32_MAX
) {
535 ERR("Application context name > MAX_UINT32");
536 ret
= LTTNG_ERR_INVALID
;
540 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
541 (uint32_t) app_ctx_provider_name_len
);
546 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
547 (uint32_t) app_ctx_name_len
);
552 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
557 reply_ret_code
= be32toh(reply
.ret_code
);
558 log_reply_code(reply_ret_code
);
559 switch (reply_ret_code
) {
560 case AGENT_RET_CODE_SUCCESS
:
570 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
576 * Internal disable agent event call on a agent application. This function
577 * communicates with the agent to disable a given event.
579 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
581 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
585 uint32_t reply_ret_code
;
586 struct lttcomm_agent_disable_event msg
;
587 struct lttcomm_agent_generic_reply reply
;
593 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
594 app
->pid
, app
->sock
->fd
);
596 data_size
= sizeof(msg
);
597 memset(&msg
, 0, sizeof(msg
));
598 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
599 ret
= LTTNG_ERR_INVALID
;
603 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
608 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
613 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
618 reply_ret_code
= be32toh(reply
.ret_code
);
619 log_reply_code(reply_ret_code
);
620 switch (reply_ret_code
) {
621 case AGENT_RET_CODE_SUCCESS
:
623 case AGENT_RET_CODE_UNKNOWN_NAME
:
624 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
634 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
640 * Send back the registration DONE command to a given agent application.
642 * Return 0 on success or else a negative value.
644 int agent_send_registration_done(struct agent_app
*app
)
649 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
651 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
655 * Enable agent event on every agent applications registered with the session
658 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
660 int agent_enable_event(struct agent_event
*event
,
661 enum lttng_domain_type domain
)
664 struct agent_app
*app
;
665 struct lttng_ht_iter iter
;
671 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
673 if (app
->domain
!= domain
) {
677 /* Enable event on agent application through TCP socket. */
678 ret
= enable_event(app
, event
);
679 if (ret
!= LTTNG_OK
) {
693 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
695 free(ctx
->provider_name
);
701 struct agent_app_ctx
*create_app_ctx(const struct lttng_event_context
*ctx
)
703 struct agent_app_ctx
*agent_ctx
= NULL
;
709 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
710 agent_ctx
= zmalloc(sizeof(*ctx
));
715 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
716 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
717 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
718 destroy_app_ctx(agent_ctx
);
726 * Enable agent context on every agent applications registered with the session
729 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
731 int agent_enable_context(const struct lttng_event_context
*ctx
,
732 enum lttng_domain_type domain
)
735 struct agent_app
*app
;
736 struct lttng_ht_iter iter
;
739 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
740 ret
= LTTNG_ERR_INVALID
;
746 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
748 struct agent_app_ctx
*agent_ctx
;
750 if (app
->domain
!= domain
) {
754 agent_ctx
= create_app_ctx(ctx
);
756 ret
= LTTNG_ERR_NOMEM
;
760 /* Enable event on agent application through TCP socket. */
761 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
762 destroy_app_ctx(agent_ctx
);
763 if (ret
!= LTTNG_OK
) {
777 * Disable agent event on every agent application registered with the session
780 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
782 int agent_disable_event(struct agent_event
*event
,
783 enum lttng_domain_type domain
)
786 struct agent_app
*app
;
787 struct lttng_ht_iter iter
;
790 if (!event
->enabled
) {
796 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
798 if (app
->domain
!= domain
) {
802 /* Enable event on agent application through TCP socket. */
803 ret
= disable_event(app
, event
);
804 if (ret
!= LTTNG_OK
) {
818 * Disable agent context on every agent application registered with the session
821 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
823 static int disable_context(struct agent_app_ctx
*ctx
,
824 enum lttng_domain_type domain
)
827 struct agent_app
*app
;
828 struct lttng_ht_iter iter
;
833 DBG2("Disabling agent application context %s:%s",
834 ctx
->provider_name
, ctx
->ctx_name
);
835 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
837 if (app
->domain
!= domain
) {
841 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
842 if (ret
!= LTTNG_OK
) {
852 * Ask every agent for the list of possible event. Events is allocated with the
853 * events of every agent application.
855 * Return the number of events or else a negative value.
857 int agent_list_events(struct lttng_event
**events
,
858 enum lttng_domain_type domain
)
861 size_t nbmem
, count
= 0;
862 struct agent_app
*app
;
863 struct lttng_event
*tmp_events
= NULL
;
864 struct lttng_ht_iter iter
;
868 DBG2("Agent listing events for domain %d", domain
);
870 nbmem
= UST_APP_EVENT_LIST_SIZE
;
871 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
873 PERROR("zmalloc agent list events");
879 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
882 struct lttng_event
*agent_events
;
884 /* Skip domain not asked by the list. */
885 if (app
->domain
!= domain
) {
889 nb_ev
= list_events(app
, &agent_events
);
895 if (count
+ nb_ev
> nbmem
) {
896 /* In case the realloc fails, we free the memory */
897 struct lttng_event
*new_tmp_events
;
900 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
901 DBG2("Reallocating agent event list from %zu to %zu entries",
903 new_tmp_events
= realloc(tmp_events
,
904 new_nbmem
* sizeof(*new_tmp_events
));
905 if (!new_tmp_events
) {
906 PERROR("realloc agent events");
911 /* Zero the new memory */
912 memset(new_tmp_events
+ nbmem
, 0,
913 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
915 tmp_events
= new_tmp_events
;
917 memcpy(tmp_events
+ count
, agent_events
,
918 nb_ev
* sizeof(*tmp_events
));
925 *events
= tmp_events
;
936 * Create a agent app object using the given PID.
938 * Return newly allocated object or else NULL on error.
940 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
941 struct lttcomm_sock
*sock
)
943 struct agent_app
*app
;
947 app
= zmalloc(sizeof(*app
));
949 PERROR("Failed to allocate agent application instance");
954 app
->domain
= domain
;
956 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
963 * Lookup agent app by socket in the global hash table.
965 * RCU read side lock MUST be acquired.
967 * Return object if found else NULL.
969 struct agent_app
*agent_find_app_by_sock(int sock
)
971 struct lttng_ht_node_ulong
*node
;
972 struct lttng_ht_iter iter
;
973 struct agent_app
*app
;
977 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
978 node
= lttng_ht_iter_get_node_ulong(&iter
);
982 app
= caa_container_of(node
, struct agent_app
, node
);
984 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
988 DBG3("Agent app NOT found by sock %d.", sock
);
993 * Add agent application object to the global hash table.
995 void agent_add_app(struct agent_app
*app
)
999 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1000 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
1004 * Delete agent application from the global hash table.
1006 * rcu_read_lock() must be held by the caller.
1008 void agent_delete_app(struct agent_app
*app
)
1011 struct lttng_ht_iter iter
;
1015 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1017 iter
.iter
.node
= &app
->node
.node
;
1018 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
1023 * Destroy an agent application object by detaching it from its corresponding
1024 * UST app if one is connected by closing the socket. Finally, perform a
1025 * delayed memory reclaim.
1027 void agent_destroy_app(struct agent_app
*app
)
1032 app
->sock
->ops
->close(app
->sock
);
1033 lttcomm_destroy_sock(app
->sock
);
1036 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1040 * Initialize an already allocated agent object.
1042 * Return 0 on success or else a negative errno value.
1044 int agent_init(struct agent
*agt
)
1050 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1055 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1057 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1065 * Add agent object to the given hash table.
1067 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1072 DBG3("Agent adding from domain %d", agt
->domain
);
1074 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1078 * Create an agent object for the given domain.
1080 * Return the allocated agent or NULL on error.
1082 struct agent
*agent_create(enum lttng_domain_type domain
)
1087 agt
= zmalloc(sizeof(struct agent
));
1091 agt
->domain
= domain
;
1093 ret
= agent_init(agt
);
1105 * Create a newly allocated agent event data structure.
1106 * Ownership of filter_expression is taken.
1108 * Return a new object else NULL on error.
1110 struct agent_event
*agent_create_event(const char *name
,
1111 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1112 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
1114 struct agent_event
*event
= NULL
;
1116 DBG3("Agent create new event with name %s, loglevel type %d, \
1117 loglevel value %d and filter %s",
1118 name
, loglevel_type
, loglevel_value
,
1119 filter_expression
? filter_expression
: "NULL");
1122 ERR("Failed to create agent event; no name provided.");
1126 event
= zmalloc(sizeof(*event
));
1131 strncpy(event
->name
, name
, sizeof(event
->name
));
1132 event
->name
[sizeof(event
->name
) - 1] = '\0';
1133 lttng_ht_node_init_str(&event
->node
, event
->name
);
1135 event
->loglevel_value
= loglevel_value
;
1136 event
->loglevel_type
= loglevel_type
;
1137 event
->filter
= filter
;
1138 event
->filter_expression
= filter_expression
;
1144 * Unique add of a agent event to an agent object.
1146 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1150 assert(agt
->events
);
1152 DBG3("Agent adding event %s", event
->name
);
1153 add_unique_agent_event(agt
->events
, event
);
1154 agt
->being_used
= 1;
1158 * Unique add of a agent context to an agent object.
1160 int agent_add_context(const struct lttng_event_context
*ctx
, struct agent
*agt
)
1163 struct agent_app_ctx
*agent_ctx
= NULL
;
1167 assert(agt
->events
);
1168 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1170 agent_ctx
= create_app_ctx(ctx
);
1172 ret
= LTTNG_ERR_NOMEM
;
1176 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1177 ctx
->u
.app_ctx
.ctx_name
);
1178 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1184 * Find multiple agent events sharing the given name.
1186 * RCU read side lock MUST be acquired. It must be held for the
1187 * duration of the iteration.
1189 * Sets the given iterator.
1191 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1192 struct lttng_ht_iter
* iter
)
1194 struct lttng_ht
*ht
;
1195 struct agent_ht_key key
;
1199 assert(agt
->events
);
1205 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1206 ht_match_event_by_name
, &key
, &iter
->iter
);
1210 * Get the next agent event duplicate by name. This should be called
1211 * after a call to agent_find_events_by_name() to iterate on events.
1213 * The RCU read lock must be held during the iteration and for as long
1214 * as the object the iterator points to remains in use.
1216 void agent_event_next_duplicate(const char *name
,
1217 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1219 struct agent_ht_key key
;
1223 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1228 * Find a agent event in the given agent using name, loglevel and filter.
1230 * RCU read side lock MUST be acquired. It must be kept for as long as
1231 * the returned agent_event is used.
1233 * Return object if found else NULL.
1235 struct agent_event
*agent_find_event(const char *name
,
1236 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1237 char *filter_expression
, struct agent
*agt
)
1239 struct lttng_ht_node_str
*node
;
1240 struct lttng_ht_iter iter
;
1241 struct lttng_ht
*ht
;
1242 struct agent_ht_key key
;
1246 assert(agt
->events
);
1250 key
.loglevel_value
= loglevel_value
;
1251 key
.loglevel_type
= loglevel_type
;
1252 key
.filter_expression
= filter_expression
;
1254 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1255 ht_match_event
, &key
, &iter
.iter
);
1256 node
= lttng_ht_iter_get_node_str(&iter
);
1261 DBG3("Agent event found %s.", name
);
1262 return caa_container_of(node
, struct agent_event
, node
);
1265 DBG3("Agent event NOT found %s.", name
);
1270 * Free given agent event. This event must not be globally visible at this
1271 * point (only expected to be used on failure just after event creation). After
1272 * this call, the pointer is not usable anymore.
1274 void agent_destroy_event(struct agent_event
*event
)
1278 free(event
->filter
);
1279 free(event
->filter_expression
);
1280 free(event
->exclusion
);
1285 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1287 struct agent_app_ctx
*ctx
=
1288 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1290 destroy_app_ctx(ctx
);
1294 * Destroy an agent completely.
1296 void agent_destroy(struct agent
*agt
)
1298 struct lttng_ht_node_str
*node
;
1299 struct lttng_ht_iter iter
;
1300 struct agent_app_ctx
*ctx
;
1304 DBG3("Agent destroy");
1307 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1309 struct agent_event
*event
;
1312 * When destroying an event, we have to try to disable it on the
1313 * agent side so the event stops generating data. The return
1314 * value is not important since we have to continue anyway
1315 * destroying the object.
1317 event
= caa_container_of(node
, struct agent_event
, node
);
1318 (void) agent_disable_event(event
, agt
->domain
);
1320 ret
= lttng_ht_del(agt
->events
, &iter
);
1322 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1325 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1326 (void) disable_context(ctx
, agt
->domain
);
1327 cds_list_del(&ctx
->list_node
);
1328 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1331 ht_cleanup_push(agt
->events
);
1336 * Allocate agent_apps_ht_by_sock.
1338 int agent_app_ht_alloc(void)
1342 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1343 if (!agent_apps_ht_by_sock
) {
1351 * Destroy a agent application by socket.
1353 void agent_destroy_app_by_sock(int sock
)
1355 struct agent_app
*app
;
1360 * Not finding an application is a very important error that should NEVER
1361 * happen. The hash table deletion is ONLY done through this call when the
1362 * main sessiond thread is torn down.
1365 app
= agent_find_app_by_sock(sock
);
1368 /* RCU read side lock is assumed to be held by this function. */
1369 agent_delete_app(app
);
1371 /* The application is freed in a RCU call but the socket is closed here. */
1372 agent_destroy_app(app
);
1377 * Clean-up the agent app hash table and destroy it.
1379 void agent_app_ht_clean(void)
1381 struct lttng_ht_node_ulong
*node
;
1382 struct lttng_ht_iter iter
;
1384 if (!agent_apps_ht_by_sock
) {
1388 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1389 struct agent_app
*app
;
1391 app
= caa_container_of(node
, struct agent_app
, node
);
1392 agent_destroy_app_by_sock(app
->sock
->fd
);
1396 lttng_ht_destroy(agent_apps_ht_by_sock
);
1400 * Update a agent application (given socket) using the given agent.
1402 * Note that this function is most likely to be used with a tracing session
1403 * thus the caller should make sure to hold the appropriate lock(s).
1405 void agent_update(const struct agent
*agt
, const struct agent_app
*app
)
1408 struct agent_event
*event
;
1409 struct lttng_ht_iter iter
;
1410 struct agent_app_ctx
*ctx
;
1415 DBG("Agent updating app: pid = %ld", (long) app
->pid
);
1419 * We are in the registration path thus if the application is gone,
1420 * there is a serious code flow error.
1423 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1424 /* Skip event if disabled. */
1425 if (!event
->enabled
) {
1429 ret
= enable_event(app
, event
);
1430 if (ret
!= LTTNG_OK
) {
1431 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1432 event
->name
, app
->pid
, app
->sock
->fd
);
1433 /* Let's try the others here and don't assume the app is dead. */
1438 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1439 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1440 if (ret
!= LTTNG_OK
) {
1441 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1442 ctx
->provider_name
, ctx
->ctx_name
,
1443 app
->pid
, app
->sock
->fd
);