2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/uatomic.h>
21 #include <urcu/rculist.h>
23 #include <common/common.h>
24 #include <common/sessiond-comm/agent.h>
26 #include <common/compat/endian.h>
33 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
36 * Agent application context representation.
38 struct agent_app_ctx
{
42 /* agent_app_ctx are part of the agent app_ctx_list. */
43 struct cds_list_head list_node
;
45 /* For call_rcu teardown. */
46 struct rcu_head rcu_node
;
50 * Human readable agent return code.
52 static const char *error_string_array
[] = {
53 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
54 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
55 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
58 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
62 void log_reply_code(uint32_t in_reply_ret_code
)
64 int level
= PRINT_DBG3
;
66 * reply_ret_code and in_reply_ret_code are kept separate to have a
67 * sanitized value (used to retrieve the human readable string) and the
68 * original value which is logged as-is.
70 uint32_t reply_ret_code
= in_reply_ret_code
;
72 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
73 reply_ret_code
>= AGENT_RET_CODE_NR
) {
74 reply_ret_code
= AGENT_RET_CODE_NR
;
78 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
79 error_string_array
[AGENT_RET_CODE_INDEX(
85 * Match function for the events hash table lookup by name.
87 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
90 struct agent_event
*event
;
91 const struct agent_ht_key
*key
;
96 event
= caa_container_of(node
, struct agent_event
, node
.node
);
99 /* Match 1 elements of the key: name. */
102 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
113 * Match function for the events hash table lookup by name and loglevel.
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 (strncmp(event
->filter_expression
, key
->filter_expression
,
146 strlen(event
->filter_expression
)) != 0) {
157 * Add unique agent event based on the event name and loglevel.
159 static void add_unique_agent_event(struct lttng_ht
*ht
,
160 struct agent_event
*event
)
162 struct cds_lfht_node
*node_ptr
;
163 struct agent_ht_key key
;
169 key
.name
= event
->name
;
170 key
.loglevel_value
= event
->loglevel_value
;
171 key
.loglevel_type
= event
->loglevel_type
;
172 key
.filter_expression
= event
->filter_expression
;
174 node_ptr
= cds_lfht_add_unique(ht
->ht
,
175 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
176 ht_match_event
, &key
, &event
->node
.node
);
177 assert(node_ptr
== &event
->node
.node
);
181 * URCU delayed agent event reclaim.
183 static void destroy_event_agent_rcu(struct rcu_head
*head
)
185 struct lttng_ht_node_str
*node
=
186 caa_container_of(head
, struct lttng_ht_node_str
, head
);
187 struct agent_event
*event
=
188 caa_container_of(node
, struct agent_event
, node
);
190 agent_destroy_event(event
);
194 * URCU delayed agent app reclaim.
196 static void destroy_app_agent_rcu(struct rcu_head
*head
)
198 struct lttng_ht_node_ulong
*node
=
199 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
200 struct agent_app
*app
=
201 caa_container_of(node
, struct agent_app
, node
);
207 * Communication with the agent. Send the message header to the given socket in
210 * Return 0 on success or else a negative errno message of sendmsg() op.
212 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
213 uint32_t cmd
, uint32_t cmd_version
)
217 struct lttcomm_agent_hdr msg
;
221 memset(&msg
, 0, sizeof(msg
));
222 msg
.data_size
= htobe64(data_size
);
223 msg
.cmd
= htobe32(cmd
);
224 msg
.cmd_version
= htobe32(cmd_version
);
226 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
227 if (size
< sizeof(msg
)) {
238 * Communication call with the agent. Send the payload to the given socket. The
239 * header MUST be sent prior to this call.
241 * Return 0 on success or else a negative errno value of sendmsg() op.
243 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
252 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
264 * Communication call with the agent. Receive reply from the agent using the
267 * Return 0 on success or else a negative errno value from recvmsg() op.
269 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
277 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
289 * Internal event listing for a given app. Populate events.
291 * Return number of element in the list or else a negative LTTNG_ERR* code.
292 * On success, the caller is responsible for freeing the memory
293 * allocated for "events".
295 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
297 int ret
, i
, len
= 0, offset
= 0;
300 uint32_t reply_ret_code
;
301 struct lttng_event
*tmp_events
= NULL
;
302 struct lttcomm_agent_list_reply
*reply
= NULL
;
303 struct lttcomm_agent_list_reply_hdr reply_hdr
;
309 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
312 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
317 /* Get list header so we know how much we'll receive. */
318 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
323 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
324 log_reply_code(reply_ret_code
);
325 switch (reply_ret_code
) {
326 case AGENT_RET_CODE_SUCCESS
:
327 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
334 reply
= zmalloc(data_size
);
336 ret
= LTTNG_ERR_NOMEM
;
340 /* Get the list with the appropriate data size. */
341 ret
= recv_reply(app
->sock
, reply
, data_size
);
346 nb_event
= be32toh(reply
->nb_event
);
347 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
349 ret
= LTTNG_ERR_NOMEM
;
353 for (i
= 0; i
< nb_event
; i
++) {
355 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
356 sizeof(tmp_events
[i
].name
));
357 tmp_events
[i
].pid
= app
->pid
;
358 tmp_events
[i
].enabled
= -1;
359 len
= strlen(reply
->payload
+ offset
) + 1;
362 *events
= tmp_events
;
368 ret
= LTTNG_ERR_UST_LIST_FAIL
;
377 * Internal enable agent event on a agent application. This function
378 * communicates with the agent to enable a given event.
380 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
382 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
387 size_t filter_expression_length
;
388 uint32_t reply_ret_code
;
389 struct lttcomm_agent_enable_event msg
;
390 struct lttcomm_agent_generic_reply reply
;
396 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
397 app
->pid
, app
->sock
->fd
);
400 * Calculate the payload's size, which is the fixed-size struct followed
401 * by the variable-length filter expression (+1 for the ending \0).
403 if (!event
->filter_expression
) {
404 filter_expression_length
= 0;
406 filter_expression_length
= strlen(event
->filter_expression
) + 1;
408 data_size
= sizeof(msg
) + filter_expression_length
;
410 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
415 memset(&msg
, 0, sizeof(msg
));
416 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
417 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
418 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
419 msg
.filter_expression_length
= htobe32(filter_expression_length
);
421 bytes_to_send
= zmalloc(data_size
);
422 if (!bytes_to_send
) {
423 ret
= LTTNG_ERR_NOMEM
;
427 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
428 if (filter_expression_length
> 0) {
429 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
430 filter_expression_length
);
433 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
439 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
444 reply_ret_code
= be32toh(reply
.ret_code
);
445 log_reply_code(reply_ret_code
);
446 switch (reply_ret_code
) {
447 case AGENT_RET_CODE_SUCCESS
:
449 case AGENT_RET_CODE_UNKNOWN_NAME
:
450 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
460 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
466 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
469 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
474 len_be
= htobe32(len
);
475 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
480 ret
= send_payload(sock
, str
, len
);
489 * Internal enable application context on an agent application. This function
490 * communicates with the agent to enable a given application context.
492 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
494 static int app_context_op(struct agent_app
*app
,
495 struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
498 uint32_t reply_ret_code
;
499 struct lttcomm_agent_generic_reply reply
;
500 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
505 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
506 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
508 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
509 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
510 ctx
->provider_name
, ctx
->ctx_name
,
511 app
->pid
, app
->sock
->fd
);
514 * Calculate the payload's size, which consists of the size (u32, BE)
515 * of the provider name, the NULL-terminated provider name string, the
516 * size (u32, BE) of the context name, followed by the NULL-terminated
517 * context name string.
519 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
520 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
521 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
522 sizeof(uint32_t) + app_ctx_name_len
;
524 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
529 if (app_ctx_provider_name_len
> UINT32_MAX
||
530 app_ctx_name_len
> UINT32_MAX
) {
531 ERR("Application context name > MAX_UINT32");
532 ret
= LTTNG_ERR_INVALID
;
536 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
537 (uint32_t) app_ctx_provider_name_len
);
542 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
543 (uint32_t) app_ctx_name_len
);
548 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
553 reply_ret_code
= be32toh(reply
.ret_code
);
554 log_reply_code(reply_ret_code
);
555 switch (reply_ret_code
) {
556 case AGENT_RET_CODE_SUCCESS
:
566 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
572 * Internal disable agent event call on a agent application. This function
573 * communicates with the agent to disable a given event.
575 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
577 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
581 uint32_t reply_ret_code
;
582 struct lttcomm_agent_disable_event msg
;
583 struct lttcomm_agent_generic_reply reply
;
589 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
590 app
->pid
, app
->sock
->fd
);
592 data_size
= sizeof(msg
);
594 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
599 memset(&msg
, 0, sizeof(msg
));
600 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
601 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
606 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
611 reply_ret_code
= be32toh(reply
.ret_code
);
612 log_reply_code(reply_ret_code
);
613 switch (reply_ret_code
) {
614 case AGENT_RET_CODE_SUCCESS
:
616 case AGENT_RET_CODE_UNKNOWN_NAME
:
617 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
627 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
633 * Send back the registration DONE command to a given agent application.
635 * Return 0 on success or else a negative value.
637 int agent_send_registration_done(struct agent_app
*app
)
642 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
644 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
648 * Enable agent event on every agent applications registered with the session
651 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
653 int agent_enable_event(struct agent_event
*event
,
654 enum lttng_domain_type domain
)
657 struct agent_app
*app
;
658 struct lttng_ht_iter iter
;
664 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
666 if (app
->domain
!= domain
) {
670 /* Enable event on agent application through TCP socket. */
671 ret
= enable_event(app
, event
);
672 if (ret
!= LTTNG_OK
) {
686 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
688 free(ctx
->provider_name
);
694 struct agent_app_ctx
*create_app_ctx(struct lttng_event_context
*ctx
)
696 struct agent_app_ctx
*agent_ctx
= NULL
;
702 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
703 agent_ctx
= zmalloc(sizeof(*ctx
));
708 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
709 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
710 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
711 destroy_app_ctx(agent_ctx
);
719 * Enable agent context on every agent applications registered with the session
722 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
724 int agent_enable_context(struct lttng_event_context
*ctx
,
725 enum lttng_domain_type domain
)
728 struct agent_app
*app
;
729 struct lttng_ht_iter iter
;
732 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
733 ret
= LTTNG_ERR_INVALID
;
739 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
741 struct agent_app_ctx
*agent_ctx
;
743 if (app
->domain
!= domain
) {
747 agent_ctx
= create_app_ctx(ctx
);
752 /* Enable event on agent application through TCP socket. */
753 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
754 if (ret
!= LTTNG_OK
) {
755 destroy_app_ctx(agent_ctx
);
769 * Disable agent event on every agent application registered with the session
772 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
774 int agent_disable_event(struct agent_event
*event
,
775 enum lttng_domain_type domain
)
778 struct agent_app
*app
;
779 struct lttng_ht_iter iter
;
782 if (!event
->enabled
) {
788 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
790 if (app
->domain
!= domain
) {
794 /* Enable event on agent application through TCP socket. */
795 ret
= disable_event(app
, event
);
796 if (ret
!= LTTNG_OK
) {
810 * Disable agent context on every agent application registered with the session
813 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
815 int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
818 struct agent_app
*app
;
819 struct lttng_ht_iter iter
;
824 DBG2("Disabling agent application context %s:%s",
825 ctx
->provider_name
, ctx
->ctx_name
);
826 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
828 if (app
->domain
!= domain
) {
832 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
833 if (ret
!= LTTNG_OK
) {
843 * Ask every agent for the list of possible event. Events is allocated with the
844 * events of every agent application.
846 * Return the number of events or else a negative value.
848 int agent_list_events(struct lttng_event
**events
,
849 enum lttng_domain_type domain
)
852 size_t nbmem
, count
= 0;
853 struct agent_app
*app
;
854 struct lttng_event
*tmp_events
= NULL
;
855 struct lttng_ht_iter iter
;
859 DBG2("Agent listing events for domain %d", domain
);
861 nbmem
= UST_APP_EVENT_LIST_SIZE
;
862 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
864 PERROR("zmalloc agent list events");
870 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
873 struct lttng_event
*agent_events
;
875 /* Skip domain not asked by the list. */
876 if (app
->domain
!= domain
) {
880 nb_ev
= list_events(app
, &agent_events
);
886 if (count
+ nb_ev
> nbmem
) {
887 /* In case the realloc fails, we free the memory */
888 struct lttng_event
*new_tmp_events
;
891 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
892 DBG2("Reallocating agent event list from %zu to %zu entries",
894 new_tmp_events
= realloc(tmp_events
,
895 new_nbmem
* sizeof(*new_tmp_events
));
896 if (!new_tmp_events
) {
897 PERROR("realloc agent events");
902 /* Zero the new memory */
903 memset(new_tmp_events
+ nbmem
, 0,
904 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
906 tmp_events
= new_tmp_events
;
908 memcpy(tmp_events
+ count
, agent_events
,
909 nb_ev
* sizeof(*tmp_events
));
916 *events
= tmp_events
;
927 * Create a agent app object using the given PID.
929 * Return newly allocated object or else NULL on error.
931 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
932 struct lttcomm_sock
*sock
)
934 struct agent_app
*app
;
938 app
= zmalloc(sizeof(*app
));
940 PERROR("zmalloc agent create");
945 app
->domain
= domain
;
947 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
954 * Lookup agent app by socket in the global hash table.
956 * RCU read side lock MUST be acquired.
958 * Return object if found else NULL.
960 struct agent_app
*agent_find_app_by_sock(int sock
)
962 struct lttng_ht_node_ulong
*node
;
963 struct lttng_ht_iter iter
;
964 struct agent_app
*app
;
968 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
969 node
= lttng_ht_iter_get_node_ulong(&iter
);
973 app
= caa_container_of(node
, struct agent_app
, node
);
975 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
979 DBG3("Agent app NOT found by sock %d.", sock
);
984 * Add agent application object to the global hash table.
986 void agent_add_app(struct agent_app
*app
)
990 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
991 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
995 * Delete agent application from the global hash table.
997 * rcu_read_lock() must be held by the caller.
999 void agent_delete_app(struct agent_app
*app
)
1002 struct lttng_ht_iter iter
;
1006 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1008 iter
.iter
.node
= &app
->node
.node
;
1009 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
1014 * Destroy an agent application object by detaching it from its corresponding
1015 * UST app if one is connected by closing the socket. Finally, perform a
1016 * delayed memory reclaim.
1018 void agent_destroy_app(struct agent_app
*app
)
1023 app
->sock
->ops
->close(app
->sock
);
1024 lttcomm_destroy_sock(app
->sock
);
1027 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1031 * Initialize an already allocated agent object.
1033 * Return 0 on success or else a negative errno value.
1035 int agent_init(struct agent
*agt
)
1041 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1046 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1048 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1056 * Add agent object to the given hash table.
1058 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1063 DBG3("Agent adding from domain %d", agt
->domain
);
1065 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1069 * Create an agent object for the given domain.
1071 * Return the allocated agent or NULL on error.
1073 struct agent
*agent_create(enum lttng_domain_type domain
)
1078 agt
= zmalloc(sizeof(struct agent
));
1082 agt
->domain
= domain
;
1084 ret
= agent_init(agt
);
1096 * Create a newly allocated agent event data structure.
1097 * Ownership of filter_expression is taken.
1099 * Return a new object else NULL on error.
1101 struct agent_event
*agent_create_event(const char *name
,
1102 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1103 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
1105 struct agent_event
*event
= NULL
;
1107 DBG3("Agent create new event with name %s, loglevel type %d, \
1108 loglevel value %d and filter %s",
1109 name
, loglevel_type
, loglevel_value
,
1110 filter_expression
? filter_expression
: "NULL");
1113 ERR("Failed to create agent event; no name provided.");
1117 event
= zmalloc(sizeof(*event
));
1122 strncpy(event
->name
, name
, sizeof(event
->name
));
1123 event
->name
[sizeof(event
->name
) - 1] = '\0';
1124 lttng_ht_node_init_str(&event
->node
, event
->name
);
1126 event
->loglevel_value
= loglevel_value
;
1127 event
->loglevel_type
= loglevel_type
;
1128 event
->filter
= filter
;
1129 event
->filter_expression
= filter_expression
;
1135 * Unique add of a agent event to an agent object.
1137 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1141 assert(agt
->events
);
1143 DBG3("Agent adding event %s", event
->name
);
1144 add_unique_agent_event(agt
->events
, event
);
1145 agt
->being_used
= 1;
1149 * Unique add of a agent context to an agent object.
1151 int agent_add_context(struct lttng_event_context
*ctx
, struct agent
*agt
)
1154 struct agent_app_ctx
*agent_ctx
= NULL
;
1158 assert(agt
->events
);
1159 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1161 agent_ctx
= create_app_ctx(ctx
);
1163 ret
= LTTNG_ERR_NOMEM
;
1167 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1168 ctx
->u
.app_ctx
.ctx_name
);
1169 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1175 * Find multiple agent events sharing the given name.
1177 * RCU read side lock MUST be acquired. It must be held for the
1178 * duration of the iteration.
1180 * Sets the given iterator.
1182 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1183 struct lttng_ht_iter
* iter
)
1185 struct lttng_ht
*ht
;
1186 struct agent_ht_key key
;
1190 assert(agt
->events
);
1196 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1197 ht_match_event_by_name
, &key
, &iter
->iter
);
1201 * Get the next agent event duplicate by name. This should be called
1202 * after a call to agent_find_events_by_name() to iterate on events.
1204 * The RCU read lock must be held during the iteration and for as long
1205 * as the object the iterator points to remains in use.
1207 void agent_event_next_duplicate(const char *name
,
1208 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1210 struct agent_ht_key key
;
1214 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1219 * Find a agent event in the given agent using name, loglevel and filter.
1221 * RCU read side lock MUST be acquired. It must be kept for as long as
1222 * the returned agent_event is used.
1224 * Return object if found else NULL.
1226 struct agent_event
*agent_find_event(const char *name
,
1227 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1228 char *filter_expression
, struct agent
*agt
)
1230 struct lttng_ht_node_str
*node
;
1231 struct lttng_ht_iter iter
;
1232 struct lttng_ht
*ht
;
1233 struct agent_ht_key key
;
1237 assert(agt
->events
);
1241 key
.loglevel_value
= loglevel_value
;
1242 key
.loglevel_type
= loglevel_type
;
1243 key
.filter_expression
= filter_expression
;
1245 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1246 ht_match_event
, &key
, &iter
.iter
);
1247 node
= lttng_ht_iter_get_node_str(&iter
);
1252 DBG3("Agent event found %s.", name
);
1253 return caa_container_of(node
, struct agent_event
, node
);
1256 DBG3("Agent event NOT found %s.", name
);
1261 * Free given agent event. This event must not be globally visible at this
1262 * point (only expected to be used on failure just after event creation). After
1263 * this call, the pointer is not usable anymore.
1265 void agent_destroy_event(struct agent_event
*event
)
1269 free(event
->filter
);
1270 free(event
->filter_expression
);
1271 free(event
->exclusion
);
1276 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1278 struct agent_app_ctx
*ctx
=
1279 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1281 destroy_app_ctx(ctx
);
1285 * Destroy an agent completely.
1287 void agent_destroy(struct agent
*agt
)
1289 struct lttng_ht_node_str
*node
;
1290 struct lttng_ht_iter iter
;
1291 struct agent_app_ctx
*ctx
;
1295 DBG3("Agent destroy");
1298 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1300 struct agent_event
*event
;
1303 * When destroying an event, we have to try to disable it on the
1304 * agent side so the event stops generating data. The return
1305 * value is not important since we have to continue anyway
1306 * destroying the object.
1308 event
= caa_container_of(node
, struct agent_event
, node
);
1309 (void) agent_disable_event(event
, agt
->domain
);
1311 ret
= lttng_ht_del(agt
->events
, &iter
);
1313 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1316 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1317 (void) disable_context(ctx
, agt
->domain
);
1318 cds_list_del(&ctx
->list_node
);
1319 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1322 ht_cleanup_push(agt
->events
);
1327 * Allocate agent_apps_ht_by_sock.
1329 int agent_app_ht_alloc(void)
1333 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1334 if (!agent_apps_ht_by_sock
) {
1342 * Destroy a agent application by socket.
1344 void agent_destroy_app_by_sock(int sock
)
1346 struct agent_app
*app
;
1351 * Not finding an application is a very important error that should NEVER
1352 * happen. The hash table deletion is ONLY done through this call when the
1353 * main sessiond thread is torn down.
1356 app
= agent_find_app_by_sock(sock
);
1359 /* RCU read side lock is assumed to be held by this function. */
1360 agent_delete_app(app
);
1362 /* The application is freed in a RCU call but the socket is closed here. */
1363 agent_destroy_app(app
);
1368 * Clean-up the agent app hash table and destroy it.
1370 void agent_app_ht_clean(void)
1372 struct lttng_ht_node_ulong
*node
;
1373 struct lttng_ht_iter iter
;
1375 if (!agent_apps_ht_by_sock
) {
1379 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1380 struct agent_app
*app
;
1382 app
= caa_container_of(node
, struct agent_app
, node
);
1383 agent_destroy_app_by_sock(app
->sock
->fd
);
1387 lttng_ht_destroy(agent_apps_ht_by_sock
);
1391 * Update a agent application (given socket) using the given agent.
1393 * Note that this function is most likely to be used with a tracing session
1394 * thus the caller should make sure to hold the appropriate lock(s).
1396 void agent_update(struct agent
*agt
, int sock
)
1399 struct agent_app
*app
;
1400 struct agent_event
*event
;
1401 struct lttng_ht_iter iter
;
1402 struct agent_app_ctx
*ctx
;
1407 DBG("Agent updating app socket %d", sock
);
1410 app
= agent_find_app_by_sock(sock
);
1412 * We are in the registration path thus if the application is gone,
1413 * there is a serious code flow error.
1416 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1417 /* Skip event if disabled. */
1418 if (!event
->enabled
) {
1422 ret
= enable_event(app
, event
);
1423 if (ret
!= LTTNG_OK
) {
1424 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1425 event
->name
, app
->pid
, app
->sock
->fd
);
1426 /* Let's try the others here and don't assume the app is dead. */
1431 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1432 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1433 if (ret
!= LTTNG_OK
) {
1434 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1435 ctx
->provider_name
, ctx
->ctx_name
,
1436 app
->pid
, app
->sock
->fd
);