2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <urcu/uatomic.h>
22 #include <urcu/rculist.h>
24 #include <common/common.h>
25 #include <common/sessiond-comm/agent.h>
27 #include <common/compat/endian.h>
32 #include "common/error.h"
34 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
37 * Agent application context representation.
39 struct agent_app_ctx
{
43 /* agent_app_ctx are part of the agent app_ctx_list. */
44 struct cds_list_head list_node
;
46 /* For call_rcu teardown. */
47 struct rcu_head rcu_node
;
51 * Human readable agent return code.
53 static const char *error_string_array
[] = {
54 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
55 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
56 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
59 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
63 void log_reply_code(uint32_t in_reply_ret_code
)
65 int level
= PRINT_DBG3
;
67 * reply_ret_code and in_reply_ret_code are kept separate to have a
68 * sanitized value (used to retrieve the human readable string) and the
69 * original value which is logged as-is.
71 uint32_t reply_ret_code
= in_reply_ret_code
;
73 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
74 reply_ret_code
>= AGENT_RET_CODE_NR
) {
75 reply_ret_code
= AGENT_RET_CODE_NR
;
79 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
80 error_string_array
[AGENT_RET_CODE_INDEX(
86 * Match function for the events hash table lookup by name.
88 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
91 struct agent_event
*event
;
92 const struct agent_ht_key
*key
;
97 event
= caa_container_of(node
, struct agent_event
, node
.node
);
100 /* Match 1 elements of the key: name. */
103 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
114 * Match function for the events hash table lookup by name and loglevel.
116 static int ht_match_event(struct cds_lfht_node
*node
,
119 struct agent_event
*event
;
120 const struct agent_ht_key
*key
;
126 event
= caa_container_of(node
, struct agent_event
, node
.node
);
129 /* Match 2 elements of the key: name and loglevel. */
132 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
136 /* Event loglevel value and type. */
137 ll_match
= loglevels_match(event
->loglevel_type
,
138 event
->loglevel_value
, key
->loglevel_type
,
139 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
145 /* Filter expression */
146 if (!!event
->filter_expression
^ !!key
->filter_expression
) {
147 /* One has a filter expression, the other does not */
151 if (event
->filter_expression
) {
152 if (strncmp(event
->filter_expression
, key
->filter_expression
,
153 strlen(event
->filter_expression
)) != 0) {
165 * Add unique agent event based on the event name and loglevel.
167 static void add_unique_agent_event(struct lttng_ht
*ht
,
168 struct agent_event
*event
)
170 struct cds_lfht_node
*node_ptr
;
171 struct agent_ht_key key
;
177 key
.name
= event
->name
;
178 key
.loglevel_value
= event
->loglevel_value
;
179 key
.loglevel_type
= event
->loglevel_type
;
180 key
.filter_expression
= event
->filter_expression
;
182 node_ptr
= cds_lfht_add_unique(ht
->ht
,
183 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
184 ht_match_event
, &key
, &event
->node
.node
);
185 assert(node_ptr
== &event
->node
.node
);
189 * URCU delayed agent event reclaim.
191 static void destroy_event_agent_rcu(struct rcu_head
*head
)
193 struct lttng_ht_node_str
*node
=
194 caa_container_of(head
, struct lttng_ht_node_str
, head
);
195 struct agent_event
*event
=
196 caa_container_of(node
, struct agent_event
, node
);
198 agent_destroy_event(event
);
202 * URCU delayed agent app reclaim.
204 static void destroy_app_agent_rcu(struct rcu_head
*head
)
206 struct lttng_ht_node_ulong
*node
=
207 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
208 struct agent_app
*app
=
209 caa_container_of(node
, struct agent_app
, node
);
215 * Communication with the agent. Send the message header to the given socket in
218 * Return 0 on success or else a negative errno message of sendmsg() op.
220 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
221 uint32_t cmd
, uint32_t cmd_version
)
225 struct lttcomm_agent_hdr msg
;
229 memset(&msg
, 0, sizeof(msg
));
230 msg
.data_size
= htobe64(data_size
);
231 msg
.cmd
= htobe32(cmd
);
232 msg
.cmd_version
= htobe32(cmd_version
);
234 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
235 if (size
< sizeof(msg
)) {
246 * Communication call with the agent. Send the payload to the given socket. The
247 * header MUST be sent prior to this call.
249 * Return 0 on success or else a negative errno value of sendmsg() op.
251 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
260 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
272 * Communication call with the agent. Receive reply from the agent using the
275 * Return 0 on success or else a negative errno value from recvmsg() op.
277 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
285 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
297 * Internal event listing for a given app. Populate events.
299 * Return number of element in the list or else a negative LTTNG_ERR* code.
300 * On success, the caller is responsible for freeing the memory
301 * allocated for "events".
303 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
305 int ret
, i
, len
= 0, offset
= 0;
308 uint32_t reply_ret_code
;
309 struct lttng_event
*tmp_events
= NULL
;
310 struct lttcomm_agent_list_reply
*reply
= NULL
;
311 struct lttcomm_agent_list_reply_hdr reply_hdr
;
317 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
320 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
325 /* Get list header so we know how much we'll receive. */
326 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
331 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
332 log_reply_code(reply_ret_code
);
333 switch (reply_ret_code
) {
334 case AGENT_RET_CODE_SUCCESS
:
335 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
342 reply
= zmalloc(data_size
);
344 ret
= LTTNG_ERR_NOMEM
;
348 /* Get the list with the appropriate data size. */
349 ret
= recv_reply(app
->sock
, reply
, data_size
);
354 nb_event
= be32toh(reply
->nb_event
);
355 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
357 ret
= LTTNG_ERR_NOMEM
;
361 for (i
= 0; i
< nb_event
; i
++) {
363 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
364 sizeof(tmp_events
[i
].name
))) {
365 ret
= LTTNG_ERR_INVALID
;
368 tmp_events
[i
].pid
= app
->pid
;
369 tmp_events
[i
].enabled
= -1;
370 len
= strlen(reply
->payload
+ offset
) + 1;
373 *events
= tmp_events
;
379 ret
= LTTNG_ERR_UST_LIST_FAIL
;
388 * Internal enable agent event on a agent application. This function
389 * communicates with the agent to enable a given event.
391 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
393 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
398 size_t filter_expression_length
;
399 uint32_t reply_ret_code
;
400 struct lttcomm_agent_enable_event msg
;
401 struct lttcomm_agent_generic_reply reply
;
407 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
408 app
->pid
, app
->sock
->fd
);
411 * Calculate the payload's size, which is the fixed-size struct followed
412 * by the variable-length filter expression (+1 for the ending \0).
414 if (!event
->filter_expression
) {
415 filter_expression_length
= 0;
417 filter_expression_length
= strlen(event
->filter_expression
) + 1;
419 data_size
= sizeof(msg
) + filter_expression_length
;
421 memset(&msg
, 0, sizeof(msg
));
422 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
423 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
424 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
425 ret
= LTTNG_ERR_INVALID
;
428 msg
.filter_expression_length
= htobe32(filter_expression_length
);
430 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
435 bytes_to_send
= zmalloc(data_size
);
436 if (!bytes_to_send
) {
437 ret
= LTTNG_ERR_NOMEM
;
441 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
442 if (filter_expression_length
> 0) {
443 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
444 filter_expression_length
);
447 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
453 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
458 reply_ret_code
= be32toh(reply
.ret_code
);
459 log_reply_code(reply_ret_code
);
460 switch (reply_ret_code
) {
461 case AGENT_RET_CODE_SUCCESS
:
463 case AGENT_RET_CODE_UNKNOWN_NAME
:
464 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
474 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
480 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
483 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
488 len_be
= htobe32(len
);
489 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
494 ret
= send_payload(sock
, str
, len
);
503 * Internal enable application context on an agent application. This function
504 * communicates with the agent to enable a given application context.
506 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
508 static int app_context_op(struct agent_app
*app
,
509 struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
512 uint32_t reply_ret_code
;
513 struct lttcomm_agent_generic_reply reply
;
514 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
519 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
520 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
522 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
523 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
524 ctx
->provider_name
, ctx
->ctx_name
,
525 app
->pid
, app
->sock
->fd
);
528 * Calculate the payload's size, which consists of the size (u32, BE)
529 * of the provider name, the NULL-terminated provider name string, the
530 * size (u32, BE) of the context name, followed by the NULL-terminated
531 * context name string.
533 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
534 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
535 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
536 sizeof(uint32_t) + app_ctx_name_len
;
538 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
543 if (app_ctx_provider_name_len
> UINT32_MAX
||
544 app_ctx_name_len
> UINT32_MAX
) {
545 ERR("Application context name > MAX_UINT32");
546 ret
= LTTNG_ERR_INVALID
;
550 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
551 (uint32_t) app_ctx_provider_name_len
);
556 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
557 (uint32_t) app_ctx_name_len
);
562 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
567 reply_ret_code
= be32toh(reply
.ret_code
);
568 log_reply_code(reply_ret_code
);
569 switch (reply_ret_code
) {
570 case AGENT_RET_CODE_SUCCESS
:
580 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
586 * Internal disable agent event call on a agent application. This function
587 * communicates with the agent to disable a given event.
589 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
591 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
595 uint32_t reply_ret_code
;
596 struct lttcomm_agent_disable_event msg
;
597 struct lttcomm_agent_generic_reply reply
;
603 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
604 app
->pid
, app
->sock
->fd
);
606 data_size
= sizeof(msg
);
607 memset(&msg
, 0, sizeof(msg
));
608 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
609 ret
= LTTNG_ERR_INVALID
;
613 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
618 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
623 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
628 reply_ret_code
= be32toh(reply
.ret_code
);
629 log_reply_code(reply_ret_code
);
630 switch (reply_ret_code
) {
631 case AGENT_RET_CODE_SUCCESS
:
633 case AGENT_RET_CODE_UNKNOWN_NAME
:
634 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
644 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
650 * Send back the registration DONE command to a given agent application.
652 * Return 0 on success or else a negative value.
654 int agent_send_registration_done(struct agent_app
*app
)
659 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
661 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
665 * Enable agent event on every agent applications registered with the session
668 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
670 int agent_enable_event(struct agent_event
*event
,
671 enum lttng_domain_type domain
)
674 struct agent_app
*app
;
675 struct lttng_ht_iter iter
;
681 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
683 if (app
->domain
!= domain
) {
687 /* Enable event on agent application through TCP socket. */
688 ret
= enable_event(app
, event
);
689 if (ret
!= LTTNG_OK
) {
703 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
705 free(ctx
->provider_name
);
711 struct agent_app_ctx
*create_app_ctx(struct lttng_event_context
*ctx
)
713 struct agent_app_ctx
*agent_ctx
= NULL
;
719 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
720 agent_ctx
= zmalloc(sizeof(*ctx
));
725 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
726 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
727 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
728 destroy_app_ctx(agent_ctx
);
736 * Enable agent context on every agent applications registered with the session
739 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
741 int agent_enable_context(struct lttng_event_context
*ctx
,
742 enum lttng_domain_type domain
)
745 struct agent_app
*app
;
746 struct lttng_ht_iter iter
;
749 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
750 ret
= LTTNG_ERR_INVALID
;
756 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
758 struct agent_app_ctx
*agent_ctx
;
760 if (app
->domain
!= domain
) {
764 agent_ctx
= create_app_ctx(ctx
);
766 ret
= LTTNG_ERR_NOMEM
;
770 /* Enable event on agent application through TCP socket. */
771 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
772 destroy_app_ctx(agent_ctx
);
773 if (ret
!= LTTNG_OK
) {
787 * Disable agent event on every agent application registered with the session
790 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
792 int agent_disable_event(struct agent_event
*event
,
793 enum lttng_domain_type domain
)
796 struct agent_app
*app
;
797 struct lttng_ht_iter iter
;
800 if (!event
->enabled
) {
806 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
808 if (app
->domain
!= domain
) {
812 /* Enable event on agent application through TCP socket. */
813 ret
= disable_event(app
, event
);
814 if (ret
!= LTTNG_OK
) {
828 * Disable agent context on every agent application registered with the session
831 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
833 int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
836 struct agent_app
*app
;
837 struct lttng_ht_iter iter
;
842 DBG2("Disabling agent application context %s:%s",
843 ctx
->provider_name
, ctx
->ctx_name
);
844 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
846 if (app
->domain
!= domain
) {
850 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
851 if (ret
!= LTTNG_OK
) {
861 * Ask every agent for the list of possible event. Events is allocated with the
862 * events of every agent application.
864 * Return the number of events or else a negative value.
866 int agent_list_events(struct lttng_event
**events
,
867 enum lttng_domain_type domain
)
870 size_t nbmem
, count
= 0;
871 struct agent_app
*app
;
872 struct lttng_event
*tmp_events
= NULL
;
873 struct lttng_ht_iter iter
;
877 DBG2("Agent listing events for domain %d", domain
);
879 nbmem
= UST_APP_EVENT_LIST_SIZE
;
880 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
882 PERROR("zmalloc agent list events");
888 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
891 struct lttng_event
*agent_events
;
893 /* Skip domain not asked by the list. */
894 if (app
->domain
!= domain
) {
898 nb_ev
= list_events(app
, &agent_events
);
904 if (count
+ nb_ev
> nbmem
) {
905 /* In case the realloc fails, we free the memory */
906 struct lttng_event
*new_tmp_events
;
909 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
910 DBG2("Reallocating agent event list from %zu to %zu entries",
912 new_tmp_events
= realloc(tmp_events
,
913 new_nbmem
* sizeof(*new_tmp_events
));
914 if (!new_tmp_events
) {
915 PERROR("realloc agent events");
920 /* Zero the new memory */
921 memset(new_tmp_events
+ nbmem
, 0,
922 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
924 tmp_events
= new_tmp_events
;
926 memcpy(tmp_events
+ count
, agent_events
,
927 nb_ev
* sizeof(*tmp_events
));
934 *events
= tmp_events
;
945 * Create a agent app object using the given PID.
947 * Return newly allocated object or else NULL on error.
949 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
950 struct lttcomm_sock
*sock
)
952 struct agent_app
*app
;
956 app
= zmalloc(sizeof(*app
));
958 PERROR("zmalloc agent create");
963 app
->domain
= domain
;
965 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
972 * Lookup agent app by socket in the global hash table.
974 * RCU read side lock MUST be acquired.
976 * Return object if found else NULL.
978 struct agent_app
*agent_find_app_by_sock(int sock
)
980 struct lttng_ht_node_ulong
*node
;
981 struct lttng_ht_iter iter
;
982 struct agent_app
*app
;
986 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
987 node
= lttng_ht_iter_get_node_ulong(&iter
);
991 app
= caa_container_of(node
, struct agent_app
, node
);
993 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
997 DBG3("Agent app NOT found by sock %d.", sock
);
1002 * Add agent application object to the global hash table.
1004 void agent_add_app(struct agent_app
*app
)
1008 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
1009 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
1013 * Delete agent application from the global hash table.
1015 * rcu_read_lock() must be held by the caller.
1017 void agent_delete_app(struct agent_app
*app
)
1020 struct lttng_ht_iter iter
;
1024 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1026 iter
.iter
.node
= &app
->node
.node
;
1027 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
1032 * Destroy an agent application object by detaching it from its corresponding
1033 * UST app if one is connected by closing the socket. Finally, perform a
1034 * delayed memory reclaim.
1036 void agent_destroy_app(struct agent_app
*app
)
1041 app
->sock
->ops
->close(app
->sock
);
1042 lttcomm_destroy_sock(app
->sock
);
1045 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1049 * Initialize an already allocated agent object.
1051 * Return 0 on success or else a negative errno value.
1053 int agent_init(struct agent
*agt
)
1059 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1064 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1066 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1074 * Add agent object to the given hash table.
1076 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1081 DBG3("Agent adding from domain %d", agt
->domain
);
1083 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1087 * Create an agent object for the given domain.
1089 * Return the allocated agent or NULL on error.
1091 struct agent
*agent_create(enum lttng_domain_type domain
)
1096 agt
= zmalloc(sizeof(struct agent
));
1100 agt
->domain
= domain
;
1102 ret
= agent_init(agt
);
1114 * Create a newly allocated agent event data structure.
1115 * Ownership of filter_expression is taken.
1117 * Return a new object else NULL on error.
1119 struct agent_event
*agent_create_event(const char *name
,
1120 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1121 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
1123 struct agent_event
*event
= NULL
;
1125 DBG3("Agent create new event with name %s, loglevel type %d, \
1126 loglevel value %d and filter %s",
1127 name
, loglevel_type
, loglevel_value
,
1128 filter_expression
? filter_expression
: "NULL");
1131 ERR("Failed to create agent event; no name provided.");
1135 event
= zmalloc(sizeof(*event
));
1140 strncpy(event
->name
, name
, sizeof(event
->name
));
1141 event
->name
[sizeof(event
->name
) - 1] = '\0';
1142 lttng_ht_node_init_str(&event
->node
, event
->name
);
1144 event
->loglevel_value
= loglevel_value
;
1145 event
->loglevel_type
= loglevel_type
;
1146 event
->filter
= filter
;
1147 event
->filter_expression
= filter_expression
;
1153 * Unique add of a agent event to an agent object.
1155 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1159 assert(agt
->events
);
1161 DBG3("Agent adding event %s", event
->name
);
1162 add_unique_agent_event(agt
->events
, event
);
1163 agt
->being_used
= 1;
1167 * Unique add of a agent context to an agent object.
1169 int agent_add_context(struct lttng_event_context
*ctx
, struct agent
*agt
)
1172 struct agent_app_ctx
*agent_ctx
= NULL
;
1176 assert(agt
->events
);
1177 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1179 agent_ctx
= create_app_ctx(ctx
);
1181 ret
= LTTNG_ERR_NOMEM
;
1185 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1186 ctx
->u
.app_ctx
.ctx_name
);
1187 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1193 * Find multiple agent events sharing the given name.
1195 * RCU read side lock MUST be acquired. It must be held for the
1196 * duration of the iteration.
1198 * Sets the given iterator.
1200 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1201 struct lttng_ht_iter
* iter
)
1203 struct lttng_ht
*ht
;
1204 struct agent_ht_key key
;
1208 assert(agt
->events
);
1214 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1215 ht_match_event_by_name
, &key
, &iter
->iter
);
1219 * Get the next agent event duplicate by name. This should be called
1220 * after a call to agent_find_events_by_name() to iterate on events.
1222 * The RCU read lock must be held during the iteration and for as long
1223 * as the object the iterator points to remains in use.
1225 void agent_event_next_duplicate(const char *name
,
1226 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1228 struct agent_ht_key key
;
1232 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1237 * Find a agent event in the given agent using name, loglevel and filter.
1239 * RCU read side lock MUST be acquired. It must be kept for as long as
1240 * the returned agent_event is used.
1242 * Return object if found else NULL.
1244 struct agent_event
*agent_find_event(const char *name
,
1245 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1246 char *filter_expression
, struct agent
*agt
)
1248 struct lttng_ht_node_str
*node
;
1249 struct lttng_ht_iter iter
;
1250 struct lttng_ht
*ht
;
1251 struct agent_ht_key key
;
1255 assert(agt
->events
);
1259 key
.loglevel_value
= loglevel_value
;
1260 key
.loglevel_type
= loglevel_type
;
1261 key
.filter_expression
= filter_expression
;
1263 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1264 ht_match_event
, &key
, &iter
.iter
);
1265 node
= lttng_ht_iter_get_node_str(&iter
);
1270 DBG3("Agent event found %s.", name
);
1271 return caa_container_of(node
, struct agent_event
, node
);
1274 DBG3("Agent event NOT found %s.", name
);
1279 * Free given agent event. This event must not be globally visible at this
1280 * point (only expected to be used on failure just after event creation). After
1281 * this call, the pointer is not usable anymore.
1283 void agent_destroy_event(struct agent_event
*event
)
1287 free(event
->filter
);
1288 free(event
->filter_expression
);
1289 free(event
->exclusion
);
1294 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1296 struct agent_app_ctx
*ctx
=
1297 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1299 destroy_app_ctx(ctx
);
1303 * Destroy an agent completely.
1305 void agent_destroy(struct agent
*agt
)
1307 struct lttng_ht_node_str
*node
;
1308 struct lttng_ht_iter iter
;
1309 struct agent_app_ctx
*ctx
;
1313 DBG3("Agent destroy");
1316 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1318 struct agent_event
*event
;
1321 * When destroying an event, we have to try to disable it on the
1322 * agent side so the event stops generating data. The return
1323 * value is not important since we have to continue anyway
1324 * destroying the object.
1326 event
= caa_container_of(node
, struct agent_event
, node
);
1327 (void) agent_disable_event(event
, agt
->domain
);
1329 ret
= lttng_ht_del(agt
->events
, &iter
);
1331 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1334 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1335 (void) disable_context(ctx
, agt
->domain
);
1336 cds_list_del(&ctx
->list_node
);
1337 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1340 ht_cleanup_push(agt
->events
);
1345 * Allocate agent_apps_ht_by_sock.
1347 int agent_app_ht_alloc(void)
1351 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1352 if (!agent_apps_ht_by_sock
) {
1360 * Destroy a agent application by socket.
1362 void agent_destroy_app_by_sock(int sock
)
1364 struct agent_app
*app
;
1369 * Not finding an application is a very important error that should NEVER
1370 * happen. The hash table deletion is ONLY done through this call when the
1371 * main sessiond thread is torn down.
1374 app
= agent_find_app_by_sock(sock
);
1377 /* RCU read side lock is assumed to be held by this function. */
1378 agent_delete_app(app
);
1380 /* The application is freed in a RCU call but the socket is closed here. */
1381 agent_destroy_app(app
);
1386 * Clean-up the agent app hash table and destroy it.
1388 void agent_app_ht_clean(void)
1390 struct lttng_ht_node_ulong
*node
;
1391 struct lttng_ht_iter iter
;
1393 if (!agent_apps_ht_by_sock
) {
1397 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1398 struct agent_app
*app
;
1400 app
= caa_container_of(node
, struct agent_app
, node
);
1401 agent_destroy_app_by_sock(app
->sock
->fd
);
1405 lttng_ht_destroy(agent_apps_ht_by_sock
);
1409 * Update a agent application (given socket) using the given agent.
1411 * Note that this function is most likely to be used with a tracing session
1412 * thus the caller should make sure to hold the appropriate lock(s).
1414 void agent_update(struct agent
*agt
, int sock
)
1417 struct agent_app
*app
;
1418 struct agent_event
*event
;
1419 struct lttng_ht_iter iter
;
1420 struct agent_app_ctx
*ctx
;
1425 DBG("Agent updating app socket %d", sock
);
1428 app
= agent_find_app_by_sock(sock
);
1430 * We are in the registration path thus if the application is gone,
1431 * there is a serious code flow error.
1434 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1435 /* Skip event if disabled. */
1436 if (!event
->enabled
) {
1440 ret
= enable_event(app
, event
);
1441 if (ret
!= LTTNG_OK
) {
1442 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1443 event
->name
, app
->pid
, app
->sock
->fd
);
1444 /* Let's try the others here and don't assume the app is dead. */
1449 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1450 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1451 if (ret
!= LTTNG_OK
) {
1452 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1453 ctx
->provider_name
, ctx
->ctx_name
,
1454 app
->pid
, app
->sock
->fd
);