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>
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 (strncmp(event
->filter_expression
, key
->filter_expression
,
147 strlen(event
->filter_expression
)) != 0) {
158 * Add unique agent event based on the event name and loglevel.
160 static void add_unique_agent_event(struct lttng_ht
*ht
,
161 struct agent_event
*event
)
163 struct cds_lfht_node
*node_ptr
;
164 struct agent_ht_key key
;
170 key
.name
= event
->name
;
171 key
.loglevel_value
= event
->loglevel_value
;
172 key
.loglevel_type
= event
->loglevel_type
;
173 key
.filter_expression
= event
->filter_expression
;
175 node_ptr
= cds_lfht_add_unique(ht
->ht
,
176 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
177 ht_match_event
, &key
, &event
->node
.node
);
178 assert(node_ptr
== &event
->node
.node
);
182 * URCU delayed agent event reclaim.
184 static void destroy_event_agent_rcu(struct rcu_head
*head
)
186 struct lttng_ht_node_str
*node
=
187 caa_container_of(head
, struct lttng_ht_node_str
, head
);
188 struct agent_event
*event
=
189 caa_container_of(node
, struct agent_event
, node
);
191 agent_destroy_event(event
);
195 * URCU delayed agent app reclaim.
197 static void destroy_app_agent_rcu(struct rcu_head
*head
)
199 struct lttng_ht_node_ulong
*node
=
200 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
201 struct agent_app
*app
=
202 caa_container_of(node
, struct agent_app
, node
);
208 * Communication with the agent. Send the message header to the given socket in
211 * Return 0 on success or else a negative errno message of sendmsg() op.
213 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
214 uint32_t cmd
, uint32_t cmd_version
)
218 struct lttcomm_agent_hdr msg
;
222 memset(&msg
, 0, sizeof(msg
));
223 msg
.data_size
= htobe64(data_size
);
224 msg
.cmd
= htobe32(cmd
);
225 msg
.cmd_version
= htobe32(cmd_version
);
227 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
228 if (size
< sizeof(msg
)) {
239 * Communication call with the agent. Send the payload to the given socket. The
240 * header MUST be sent prior to this call.
242 * Return 0 on success or else a negative errno value of sendmsg() op.
244 static int send_payload(struct lttcomm_sock
*sock
, const void *data
,
253 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
265 * Communication call with the agent. Receive reply from the agent using the
268 * Return 0 on success or else a negative errno value from recvmsg() op.
270 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
278 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
290 * Internal event listing for a given app. Populate events.
292 * Return number of element in the list or else a negative LTTNG_ERR* code.
293 * On success, the caller is responsible for freeing the memory
294 * allocated for "events".
296 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
298 int ret
, i
, len
= 0, offset
= 0;
301 uint32_t reply_ret_code
;
302 struct lttng_event
*tmp_events
= NULL
;
303 struct lttcomm_agent_list_reply
*reply
= NULL
;
304 struct lttcomm_agent_list_reply_hdr reply_hdr
;
310 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
313 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
318 /* Get list header so we know how much we'll receive. */
319 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
324 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
325 log_reply_code(reply_ret_code
);
326 switch (reply_ret_code
) {
327 case AGENT_RET_CODE_SUCCESS
:
328 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
335 reply
= zmalloc(data_size
);
337 ret
= LTTNG_ERR_NOMEM
;
341 /* Get the list with the appropriate data size. */
342 ret
= recv_reply(app
->sock
, reply
, data_size
);
347 nb_event
= be32toh(reply
->nb_event
);
348 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
350 ret
= LTTNG_ERR_NOMEM
;
354 for (i
= 0; i
< nb_event
; i
++) {
356 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
357 sizeof(tmp_events
[i
].name
));
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(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 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
416 memset(&msg
, 0, sizeof(msg
));
417 msg
.loglevel_value
= htobe32(event
->loglevel_value
);
418 msg
.loglevel_type
= htobe32(event
->loglevel_type
);
419 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
420 msg
.filter_expression_length
= htobe32(filter_expression_length
);
422 bytes_to_send
= zmalloc(data_size
);
423 if (!bytes_to_send
) {
424 ret
= LTTNG_ERR_NOMEM
;
428 memcpy(bytes_to_send
, &msg
, sizeof(msg
));
429 if (filter_expression_length
> 0) {
430 memcpy(bytes_to_send
+ sizeof(msg
), event
->filter_expression
,
431 filter_expression_length
);
434 ret
= send_payload(app
->sock
, bytes_to_send
, data_size
);
440 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
445 reply_ret_code
= be32toh(reply
.ret_code
);
446 log_reply_code(reply_ret_code
);
447 switch (reply_ret_code
) {
448 case AGENT_RET_CODE_SUCCESS
:
450 case AGENT_RET_CODE_UNKNOWN_NAME
:
451 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
461 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
467 * Send Pascal-style string. Size is sent as a 32-bit big endian integer.
470 int send_pstring(struct lttcomm_sock
*sock
, const char *str
, uint32_t len
)
475 len_be
= htobe32(len
);
476 ret
= send_payload(sock
, &len_be
, sizeof(len_be
));
481 ret
= send_payload(sock
, str
, len
);
490 * Internal enable application context on an agent application. This function
491 * communicates with the agent to enable a given application context.
493 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
495 static int app_context_op(struct agent_app
*app
,
496 struct agent_app_ctx
*ctx
, enum lttcomm_agent_command cmd
)
499 uint32_t reply_ret_code
;
500 struct lttcomm_agent_generic_reply reply
;
501 size_t app_ctx_provider_name_len
, app_ctx_name_len
, data_size
;
506 assert(cmd
== AGENT_CMD_APP_CTX_ENABLE
||
507 cmd
== AGENT_CMD_APP_CTX_DISABLE
);
509 DBG2("Agent %s application %s:%s for app pid: %d and socket %d",
510 cmd
== AGENT_CMD_APP_CTX_ENABLE
? "enabling" : "disabling",
511 ctx
->provider_name
, ctx
->ctx_name
,
512 app
->pid
, app
->sock
->fd
);
515 * Calculate the payload's size, which consists of the size (u32, BE)
516 * of the provider name, the NULL-terminated provider name string, the
517 * size (u32, BE) of the context name, followed by the NULL-terminated
518 * context name string.
520 app_ctx_provider_name_len
= strlen(ctx
->provider_name
) + 1;
521 app_ctx_name_len
= strlen(ctx
->ctx_name
) + 1;
522 data_size
= sizeof(uint32_t) + app_ctx_provider_name_len
+
523 sizeof(uint32_t) + app_ctx_name_len
;
525 ret
= send_header(app
->sock
, data_size
, cmd
, 0);
530 if (app_ctx_provider_name_len
> UINT32_MAX
||
531 app_ctx_name_len
> UINT32_MAX
) {
532 ERR("Application context name > MAX_UINT32");
533 ret
= LTTNG_ERR_INVALID
;
537 ret
= send_pstring(app
->sock
, ctx
->provider_name
,
538 (uint32_t) app_ctx_provider_name_len
);
543 ret
= send_pstring(app
->sock
, ctx
->ctx_name
,
544 (uint32_t) app_ctx_name_len
);
549 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
554 reply_ret_code
= be32toh(reply
.ret_code
);
555 log_reply_code(reply_ret_code
);
556 switch (reply_ret_code
) {
557 case AGENT_RET_CODE_SUCCESS
:
567 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
573 * Internal disable agent event call on a agent application. This function
574 * communicates with the agent to disable a given event.
576 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
578 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
582 uint32_t reply_ret_code
;
583 struct lttcomm_agent_disable_event msg
;
584 struct lttcomm_agent_generic_reply reply
;
590 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
591 app
->pid
, app
->sock
->fd
);
593 data_size
= sizeof(msg
);
595 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
600 memset(&msg
, 0, sizeof(msg
));
601 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
602 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
607 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
612 reply_ret_code
= be32toh(reply
.ret_code
);
613 log_reply_code(reply_ret_code
);
614 switch (reply_ret_code
) {
615 case AGENT_RET_CODE_SUCCESS
:
617 case AGENT_RET_CODE_UNKNOWN_NAME
:
618 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
628 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
634 * Send back the registration DONE command to a given agent application.
636 * Return 0 on success or else a negative value.
638 int agent_send_registration_done(struct agent_app
*app
)
643 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
645 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
649 * Enable agent event on every agent applications registered with the session
652 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
654 int agent_enable_event(struct agent_event
*event
,
655 enum lttng_domain_type domain
)
658 struct agent_app
*app
;
659 struct lttng_ht_iter iter
;
665 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
667 if (app
->domain
!= domain
) {
671 /* Enable event on agent application through TCP socket. */
672 ret
= enable_event(app
, event
);
673 if (ret
!= LTTNG_OK
) {
687 void destroy_app_ctx(struct agent_app_ctx
*ctx
)
689 free(ctx
->provider_name
);
695 struct agent_app_ctx
*create_app_ctx(struct lttng_event_context
*ctx
)
697 struct agent_app_ctx
*agent_ctx
= NULL
;
703 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
704 agent_ctx
= zmalloc(sizeof(*ctx
));
709 agent_ctx
->provider_name
= strdup(ctx
->u
.app_ctx
.provider_name
);
710 agent_ctx
->ctx_name
= strdup(ctx
->u
.app_ctx
.ctx_name
);
711 if (!agent_ctx
->provider_name
|| !agent_ctx
->ctx_name
) {
712 destroy_app_ctx(agent_ctx
);
720 * Enable agent context on every agent applications registered with the session
723 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
725 int agent_enable_context(struct lttng_event_context
*ctx
,
726 enum lttng_domain_type domain
)
729 struct agent_app
*app
;
730 struct lttng_ht_iter iter
;
733 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
734 ret
= LTTNG_ERR_INVALID
;
740 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
742 struct agent_app_ctx
*agent_ctx
;
744 if (app
->domain
!= domain
) {
748 agent_ctx
= create_app_ctx(ctx
);
753 /* Enable event on agent application through TCP socket. */
754 ret
= app_context_op(app
, agent_ctx
, AGENT_CMD_APP_CTX_ENABLE
);
755 if (ret
!= LTTNG_OK
) {
756 destroy_app_ctx(agent_ctx
);
770 * Disable agent event on every agent application registered with the session
773 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
775 int agent_disable_event(struct agent_event
*event
,
776 enum lttng_domain_type domain
)
779 struct agent_app
*app
;
780 struct lttng_ht_iter iter
;
783 if (!event
->enabled
) {
789 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
791 if (app
->domain
!= domain
) {
795 /* Enable event on agent application through TCP socket. */
796 ret
= disable_event(app
, event
);
797 if (ret
!= LTTNG_OK
) {
811 * Disable agent context on every agent application registered with the session
814 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
816 int disable_context(struct agent_app_ctx
*ctx
, enum lttng_domain_type domain
)
819 struct agent_app
*app
;
820 struct lttng_ht_iter iter
;
825 DBG2("Disabling agent application context %s:%s",
826 ctx
->provider_name
, ctx
->ctx_name
);
827 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
829 if (app
->domain
!= domain
) {
833 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_DISABLE
);
834 if (ret
!= LTTNG_OK
) {
844 * Ask every agent for the list of possible event. Events is allocated with the
845 * events of every agent application.
847 * Return the number of events or else a negative value.
849 int agent_list_events(struct lttng_event
**events
,
850 enum lttng_domain_type domain
)
853 size_t nbmem
, count
= 0;
854 struct agent_app
*app
;
855 struct lttng_event
*tmp_events
= NULL
;
856 struct lttng_ht_iter iter
;
860 DBG2("Agent listing events for domain %d", domain
);
862 nbmem
= UST_APP_EVENT_LIST_SIZE
;
863 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
865 PERROR("zmalloc agent list events");
871 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
874 struct lttng_event
*agent_events
;
876 /* Skip domain not asked by the list. */
877 if (app
->domain
!= domain
) {
881 nb_ev
= list_events(app
, &agent_events
);
887 if (count
+ nb_ev
> nbmem
) {
888 /* In case the realloc fails, we free the memory */
889 struct lttng_event
*new_tmp_events
;
892 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
893 DBG2("Reallocating agent event list from %zu to %zu entries",
895 new_tmp_events
= realloc(tmp_events
,
896 new_nbmem
* sizeof(*new_tmp_events
));
897 if (!new_tmp_events
) {
898 PERROR("realloc agent events");
903 /* Zero the new memory */
904 memset(new_tmp_events
+ nbmem
, 0,
905 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
907 tmp_events
= new_tmp_events
;
909 memcpy(tmp_events
+ count
, agent_events
,
910 nb_ev
* sizeof(*tmp_events
));
917 *events
= tmp_events
;
928 * Create a agent app object using the given PID.
930 * Return newly allocated object or else NULL on error.
932 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
933 struct lttcomm_sock
*sock
)
935 struct agent_app
*app
;
939 app
= zmalloc(sizeof(*app
));
941 PERROR("zmalloc agent create");
946 app
->domain
= domain
;
948 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
955 * Lookup agent app by socket in the global hash table.
957 * RCU read side lock MUST be acquired.
959 * Return object if found else NULL.
961 struct agent_app
*agent_find_app_by_sock(int sock
)
963 struct lttng_ht_node_ulong
*node
;
964 struct lttng_ht_iter iter
;
965 struct agent_app
*app
;
969 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
970 node
= lttng_ht_iter_get_node_ulong(&iter
);
974 app
= caa_container_of(node
, struct agent_app
, node
);
976 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
980 DBG3("Agent app NOT found by sock %d.", sock
);
985 * Add agent application object to the global hash table.
987 void agent_add_app(struct agent_app
*app
)
991 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
992 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
996 * Delete agent application from the global hash table.
998 * rcu_read_lock() must be held by the caller.
1000 void agent_delete_app(struct agent_app
*app
)
1003 struct lttng_ht_iter iter
;
1007 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
1009 iter
.iter
.node
= &app
->node
.node
;
1010 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
1015 * Destroy an agent application object by detaching it from its corresponding
1016 * UST app if one is connected by closing the socket. Finally, perform a
1017 * delayed memory reclaim.
1019 void agent_destroy_app(struct agent_app
*app
)
1024 app
->sock
->ops
->close(app
->sock
);
1025 lttcomm_destroy_sock(app
->sock
);
1028 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
1032 * Initialize an already allocated agent object.
1034 * Return 0 on success or else a negative errno value.
1036 int agent_init(struct agent
*agt
)
1042 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1047 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
1049 CDS_INIT_LIST_HEAD(&agt
->app_ctx_list
);
1057 * Add agent object to the given hash table.
1059 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
1064 DBG3("Agent adding from domain %d", agt
->domain
);
1066 lttng_ht_add_unique_u64(ht
, &agt
->node
);
1070 * Create an agent object for the given domain.
1072 * Return the allocated agent or NULL on error.
1074 struct agent
*agent_create(enum lttng_domain_type domain
)
1079 agt
= zmalloc(sizeof(struct agent
));
1083 agt
->domain
= domain
;
1085 ret
= agent_init(agt
);
1097 * Create a newly allocated agent event data structure.
1098 * Ownership of filter_expression is taken.
1100 * Return a new object else NULL on error.
1102 struct agent_event
*agent_create_event(const char *name
,
1103 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1104 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
1106 struct agent_event
*event
= NULL
;
1108 DBG3("Agent create new event with name %s, loglevel type %d, \
1109 loglevel value %d and filter %s",
1110 name
, loglevel_type
, loglevel_value
,
1111 filter_expression
? filter_expression
: "NULL");
1114 ERR("Failed to create agent event; no name provided.");
1118 event
= zmalloc(sizeof(*event
));
1123 strncpy(event
->name
, name
, sizeof(event
->name
));
1124 event
->name
[sizeof(event
->name
) - 1] = '\0';
1125 lttng_ht_node_init_str(&event
->node
, event
->name
);
1127 event
->loglevel_value
= loglevel_value
;
1128 event
->loglevel_type
= loglevel_type
;
1129 event
->filter
= filter
;
1130 event
->filter_expression
= filter_expression
;
1136 * Unique add of a agent event to an agent object.
1138 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
1142 assert(agt
->events
);
1144 DBG3("Agent adding event %s", event
->name
);
1145 add_unique_agent_event(agt
->events
, event
);
1146 agt
->being_used
= 1;
1150 * Unique add of a agent context to an agent object.
1152 int agent_add_context(struct lttng_event_context
*ctx
, struct agent
*agt
)
1155 struct agent_app_ctx
*agent_ctx
= NULL
;
1159 assert(agt
->events
);
1160 assert(ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
);
1162 agent_ctx
= create_app_ctx(ctx
);
1164 ret
= LTTNG_ERR_NOMEM
;
1168 DBG3("Agent adding context %s:%s", ctx
->u
.app_ctx
.provider_name
,
1169 ctx
->u
.app_ctx
.ctx_name
);
1170 cds_list_add_tail_rcu(&agent_ctx
->list_node
, &agt
->app_ctx_list
);
1176 * Find multiple agent events sharing the given name.
1178 * RCU read side lock MUST be acquired. It must be held for the
1179 * duration of the iteration.
1181 * Sets the given iterator.
1183 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
1184 struct lttng_ht_iter
* iter
)
1186 struct lttng_ht
*ht
;
1187 struct agent_ht_key key
;
1191 assert(agt
->events
);
1197 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1198 ht_match_event_by_name
, &key
, &iter
->iter
);
1202 * Get the next agent event duplicate by name. This should be called
1203 * after a call to agent_find_events_by_name() to iterate on events.
1205 * The RCU read lock must be held during the iteration and for as long
1206 * as the object the iterator points to remains in use.
1208 void agent_event_next_duplicate(const char *name
,
1209 struct agent
*agt
, struct lttng_ht_iter
* iter
)
1211 struct agent_ht_key key
;
1215 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
1220 * Find a agent event in the given agent using name, loglevel and filter.
1222 * RCU read side lock MUST be acquired. It must be kept for as long as
1223 * the returned agent_event is used.
1225 * Return object if found else NULL.
1227 struct agent_event
*agent_find_event(const char *name
,
1228 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
1229 char *filter_expression
, struct agent
*agt
)
1231 struct lttng_ht_node_str
*node
;
1232 struct lttng_ht_iter iter
;
1233 struct lttng_ht
*ht
;
1234 struct agent_ht_key key
;
1238 assert(agt
->events
);
1242 key
.loglevel_value
= loglevel_value
;
1243 key
.loglevel_type
= loglevel_type
;
1244 key
.filter_expression
= filter_expression
;
1246 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1247 ht_match_event
, &key
, &iter
.iter
);
1248 node
= lttng_ht_iter_get_node_str(&iter
);
1253 DBG3("Agent event found %s.", name
);
1254 return caa_container_of(node
, struct agent_event
, node
);
1257 DBG3("Agent event NOT found %s.", name
);
1262 * Free given agent event. This event must not be globally visible at this
1263 * point (only expected to be used on failure just after event creation). After
1264 * this call, the pointer is not usable anymore.
1266 void agent_destroy_event(struct agent_event
*event
)
1270 free(event
->filter
);
1271 free(event
->filter_expression
);
1272 free(event
->exclusion
);
1277 void destroy_app_ctx_rcu(struct rcu_head
*head
)
1279 struct agent_app_ctx
*ctx
=
1280 caa_container_of(head
, struct agent_app_ctx
, rcu_node
);
1282 destroy_app_ctx(ctx
);
1286 * Destroy an agent completely.
1288 void agent_destroy(struct agent
*agt
)
1290 struct lttng_ht_node_str
*node
;
1291 struct lttng_ht_iter iter
;
1292 struct agent_app_ctx
*ctx
;
1296 DBG3("Agent destroy");
1299 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
1301 struct agent_event
*event
;
1304 * When destroying an event, we have to try to disable it on the
1305 * agent side so the event stops generating data. The return
1306 * value is not important since we have to continue anyway
1307 * destroying the object.
1309 event
= caa_container_of(node
, struct agent_event
, node
);
1310 (void) agent_disable_event(event
, agt
->domain
);
1312 ret
= lttng_ht_del(agt
->events
, &iter
);
1314 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1317 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1318 (void) disable_context(ctx
, agt
->domain
);
1319 cds_list_del(&ctx
->list_node
);
1320 call_rcu(&ctx
->rcu_node
, destroy_app_ctx_rcu
);
1323 ht_cleanup_push(agt
->events
);
1328 * Allocate agent_apps_ht_by_sock.
1330 int agent_app_ht_alloc(void)
1334 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1335 if (!agent_apps_ht_by_sock
) {
1343 * Destroy a agent application by socket.
1345 void agent_destroy_app_by_sock(int sock
)
1347 struct agent_app
*app
;
1352 * Not finding an application is a very important error that should NEVER
1353 * happen. The hash table deletion is ONLY done through this call when the
1354 * main sessiond thread is torn down.
1357 app
= agent_find_app_by_sock(sock
);
1360 /* RCU read side lock is assumed to be held by this function. */
1361 agent_delete_app(app
);
1363 /* The application is freed in a RCU call but the socket is closed here. */
1364 agent_destroy_app(app
);
1369 * Clean-up the agent app hash table and destroy it.
1371 void agent_app_ht_clean(void)
1373 struct lttng_ht_node_ulong
*node
;
1374 struct lttng_ht_iter iter
;
1376 if (!agent_apps_ht_by_sock
) {
1380 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1381 struct agent_app
*app
;
1383 app
= caa_container_of(node
, struct agent_app
, node
);
1384 agent_destroy_app_by_sock(app
->sock
->fd
);
1388 lttng_ht_destroy(agent_apps_ht_by_sock
);
1392 * Update a agent application (given socket) using the given agent.
1394 * Note that this function is most likely to be used with a tracing session
1395 * thus the caller should make sure to hold the appropriate lock(s).
1397 void agent_update(struct agent
*agt
, int sock
)
1400 struct agent_app
*app
;
1401 struct agent_event
*event
;
1402 struct lttng_ht_iter iter
;
1403 struct agent_app_ctx
*ctx
;
1408 DBG("Agent updating app socket %d", sock
);
1411 app
= agent_find_app_by_sock(sock
);
1413 * We are in the registration path thus if the application is gone,
1414 * there is a serious code flow error.
1417 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1418 /* Skip event if disabled. */
1419 if (!event
->enabled
) {
1423 ret
= enable_event(app
, event
);
1424 if (ret
!= LTTNG_OK
) {
1425 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1426 event
->name
, app
->pid
, app
->sock
->fd
);
1427 /* Let's try the others here and don't assume the app is dead. */
1432 cds_list_for_each_entry_rcu(ctx
, &agt
->app_ctx_list
, list_node
) {
1433 ret
= app_context_op(app
, ctx
, AGENT_CMD_APP_CTX_ENABLE
);
1434 if (ret
!= LTTNG_OK
) {
1435 DBG2("Agent update unable to add application context %s:%s on app pid: %d sock %d",
1436 ctx
->provider_name
, ctx
->ctx_name
,
1437 app
->pid
, app
->sock
->fd
);