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.
21 #include <urcu/uatomic.h>
23 #include <common/common.h>
24 #include <common/sessiond-comm/agent.h>
26 #include <common/compat/endian.h>
33 * Match function for the events hash table lookup by name.
35 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
38 struct agent_event
*event
;
39 const struct agent_ht_key
*key
;
44 event
= caa_container_of(node
, struct agent_event
, node
.node
);
47 /* Match 1 elements of the key: name. */
50 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
61 * Match function for the events hash table lookup by name and loglevel.
63 static int ht_match_event(struct cds_lfht_node
*node
,
66 struct agent_event
*event
;
67 const struct agent_ht_key
*key
;
73 event
= caa_container_of(node
, struct agent_event
, node
.node
);
76 /* Match 2 elements of the key: name and loglevel. */
79 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
83 /* Event loglevel value and type. */
84 ll_match
= loglevels_match(event
->loglevel_type
,
85 event
->loglevel_value
, key
->loglevel_type
,
86 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
99 * Add unique agent event based on the event name and loglevel.
101 static void add_unique_agent_event(struct lttng_ht
*ht
,
102 struct agent_event
*event
)
104 struct cds_lfht_node
*node_ptr
;
105 struct agent_ht_key key
;
111 key
.name
= event
->name
;
112 key
.loglevel_value
= event
->loglevel_value
;
113 key
.loglevel_type
= event
->loglevel_type
;
115 node_ptr
= cds_lfht_add_unique(ht
->ht
,
116 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
117 ht_match_event
, &key
, &event
->node
.node
);
118 assert(node_ptr
== &event
->node
.node
);
122 * URCU delayed agent event reclaim.
124 static void destroy_event_agent_rcu(struct rcu_head
*head
)
126 struct lttng_ht_node_str
*node
=
127 caa_container_of(head
, struct lttng_ht_node_str
, head
);
128 struct agent_event
*event
=
129 caa_container_of(node
, struct agent_event
, node
);
131 agent_destroy_event(event
);
135 * URCU delayed agent app reclaim.
137 static void destroy_app_agent_rcu(struct rcu_head
*head
)
139 struct lttng_ht_node_ulong
*node
=
140 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
141 struct agent_app
*app
=
142 caa_container_of(node
, struct agent_app
, node
);
148 * Communication with the agent. Send the message header to the given socket in
151 * Return 0 on success or else a negative errno message of sendmsg() op.
153 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
154 uint32_t cmd
, uint32_t cmd_version
)
158 struct lttcomm_agent_hdr msg
;
162 memset(&msg
, 0, sizeof(msg
));
163 msg
.data_size
= htobe64(data_size
);
164 msg
.cmd
= htobe32(cmd
);
165 msg
.cmd_version
= htobe32(cmd_version
);
167 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
168 if (size
< sizeof(msg
)) {
179 * Communication call with the agent. Send the payload to the given socket. The
180 * header MUST be sent prior to this call.
182 * Return 0 on success or else a negative errno value of sendmsg() op.
184 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
193 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
205 * Communication call with the agent. Receive reply from the agent using the
208 * Return 0 on success or else a negative errno value from recvmsg() op.
210 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
218 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
230 * Internal event listing for a given app. Populate events.
232 * Return number of element in the list or else a negative LTTNG_ERR* code.
233 * On success, the caller is responsible for freeing the memory
234 * allocated for "events".
236 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
238 int ret
, i
, len
= 0, offset
= 0;
241 struct lttng_event
*tmp_events
= NULL
;
242 struct lttcomm_agent_list_reply
*reply
= NULL
;
243 struct lttcomm_agent_list_reply_hdr reply_hdr
;
249 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
252 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
257 /* Get list header so we know how much we'll receive. */
258 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
263 switch (be32toh(reply_hdr
.ret_code
)) {
264 case AGENT_RET_CODE_SUCCESS
:
265 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
268 ERR("Agent returned an unknown code: %" PRIu32
,
269 be32toh(reply_hdr
.ret_code
));
270 ret
= LTTNG_ERR_FATAL
;
274 reply
= zmalloc(data_size
);
276 ret
= LTTNG_ERR_NOMEM
;
280 /* Get the list with the appropriate data size. */
281 ret
= recv_reply(app
->sock
, reply
, data_size
);
286 nb_event
= be32toh(reply
->nb_event
);
287 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
289 ret
= LTTNG_ERR_NOMEM
;
293 for (i
= 0; i
< nb_event
; i
++) {
295 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
296 sizeof(tmp_events
[i
].name
));
297 tmp_events
[i
].pid
= app
->pid
;
298 tmp_events
[i
].enabled
= -1;
299 len
= strlen(reply
->payload
+ offset
) + 1;
302 *events
= tmp_events
;
308 ret
= LTTNG_ERR_UST_LIST_FAIL
;
317 * Internal enable agent event on a agent application. This function
318 * communicates with the agent to enable a given event.
320 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
322 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
326 struct lttcomm_agent_enable msg
;
327 struct lttcomm_agent_generic_reply reply
;
333 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
334 app
->pid
, app
->sock
->fd
);
336 data_size
= sizeof(msg
);
338 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
343 memset(&msg
, 0, sizeof(msg
));
344 msg
.loglevel_value
= event
->loglevel_value
;
345 msg
.loglevel_type
= event
->loglevel_type
;
346 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
347 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
352 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
357 switch (be32toh(reply
.ret_code
)) {
358 case AGENT_RET_CODE_SUCCESS
:
360 case AGENT_RET_CODE_UNKNOWN_NAME
:
361 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
364 ERR("Agent returned an unknown code: %" PRIu32
,
365 be32toh(reply
.ret_code
));
366 ret
= LTTNG_ERR_FATAL
;
373 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
379 * Internal disable agent event call on a agent application. This function
380 * communicates with the agent to disable a given event.
382 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
384 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
388 struct lttcomm_agent_disable msg
;
389 struct lttcomm_agent_generic_reply reply
;
395 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
396 app
->pid
, app
->sock
->fd
);
398 data_size
= sizeof(msg
);
400 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
405 memset(&msg
, 0, sizeof(msg
));
406 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
407 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
412 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
417 switch (be32toh(reply
.ret_code
)) {
418 case AGENT_RET_CODE_SUCCESS
:
420 case AGENT_RET_CODE_UNKNOWN_NAME
:
421 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
424 ERR("Agent returned an unknown code: %" PRIu32
,
425 be32toh(reply
.ret_code
));
426 ret
= LTTNG_ERR_FATAL
;
433 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
439 * Send back the registration DONE command to a given agent application.
441 * Return 0 on success or else a negative value.
443 int agent_send_registration_done(struct agent_app
*app
)
448 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
450 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
454 * Enable agent event on every agent applications registered with the session
457 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
459 int agent_enable_event(struct agent_event
*event
,
460 enum lttng_domain_type domain
)
463 struct agent_app
*app
;
464 struct lttng_ht_iter iter
;
470 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
472 if (app
->domain
!= domain
) {
476 /* Enable event on agent application through TCP socket. */
477 ret
= enable_event(app
, event
);
478 if (ret
!= LTTNG_OK
) {
492 * Disable agent event on every agent applications registered with the session
495 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
497 int agent_disable_event(struct agent_event
*event
,
498 enum lttng_domain_type domain
)
501 struct agent_app
*app
;
502 struct lttng_ht_iter iter
;
505 if (!event
->enabled
) {
511 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
513 if (app
->domain
!= domain
) {
517 /* Enable event on agent application through TCP socket. */
518 ret
= disable_event(app
, event
);
519 if (ret
!= LTTNG_OK
) {
533 * Ask every agent for the list of possible event. Events is allocated with the
534 * events of every agent application.
536 * Return the number of events or else a negative value.
538 int agent_list_events(struct lttng_event
**events
,
539 enum lttng_domain_type domain
)
542 size_t nbmem
, count
= 0;
543 struct agent_app
*app
;
544 struct lttng_event
*tmp_events
= NULL
;
545 struct lttng_ht_iter iter
;
549 DBG2("Agent listing events for domain %d", domain
);
551 nbmem
= UST_APP_EVENT_LIST_SIZE
;
552 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
554 PERROR("zmalloc agent list events");
560 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
563 struct lttng_event
*agent_events
;
565 /* Skip domain not asked by the list. */
566 if (app
->domain
!= domain
) {
570 nb_ev
= list_events(app
, &agent_events
);
576 if (count
+ nb_ev
> nbmem
) {
577 /* In case the realloc fails, we free the memory */
578 struct lttng_event
*new_tmp_events
;
581 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
582 DBG2("Reallocating agent event list from %zu to %zu entries",
584 new_tmp_events
= realloc(tmp_events
,
585 new_nbmem
* sizeof(*new_tmp_events
));
586 if (!new_tmp_events
) {
587 PERROR("realloc agent events");
592 /* Zero the new memory */
593 memset(new_tmp_events
+ nbmem
, 0,
594 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
596 tmp_events
= new_tmp_events
;
598 memcpy(tmp_events
+ count
, agent_events
,
599 nb_ev
* sizeof(*tmp_events
));
606 *events
= tmp_events
;
617 * Create a agent app object using the given PID.
619 * Return newly allocated object or else NULL on error.
621 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
622 struct lttcomm_sock
*sock
)
624 struct agent_app
*app
;
628 app
= zmalloc(sizeof(*app
));
630 PERROR("zmalloc agent create");
635 app
->domain
= domain
;
637 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
644 * Lookup agent app by socket in the global hash table.
646 * RCU read side lock MUST be acquired.
648 * Return object if found else NULL.
650 struct agent_app
*agent_find_app_by_sock(int sock
)
652 struct lttng_ht_node_ulong
*node
;
653 struct lttng_ht_iter iter
;
654 struct agent_app
*app
;
658 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
659 node
= lttng_ht_iter_get_node_ulong(&iter
);
663 app
= caa_container_of(node
, struct agent_app
, node
);
665 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
669 DBG3("Agent app NOT found by sock %d.", sock
);
674 * Add agent application object to the global hash table.
676 void agent_add_app(struct agent_app
*app
)
680 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
683 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
688 * Delete agent application from the global hash table.
690 * rcu_read_lock() must be held by the caller.
692 void agent_delete_app(struct agent_app
*app
)
695 struct lttng_ht_iter iter
;
699 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
701 iter
.iter
.node
= &app
->node
.node
;
702 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
707 * Destroy a agent application object by detaching it from its corresponding
708 * UST app if one is connected by closing the socket. Finally, perform a
709 * delayed memory reclaim.
711 void agent_destroy_app(struct agent_app
*app
)
716 app
->sock
->ops
->close(app
->sock
);
717 lttcomm_destroy_sock(app
->sock
);
720 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
724 * Initialize an already allocated agent object.
726 * Return 0 on success or else a negative errno value.
728 int agent_init(struct agent
*agt
)
734 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
739 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
748 * Add agent object to the given hash table.
750 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
755 DBG3("Agent adding from domain %d", agt
->domain
);
758 lttng_ht_add_unique_u64(ht
, &agt
->node
);
763 * Create an agent object for the given domain.
765 * Return the allocated agent or NULL on error.
767 struct agent
*agent_create(enum lttng_domain_type domain
)
772 agt
= zmalloc(sizeof(*agt
));
776 agt
->domain
= domain
;
778 ret
= agent_init(agt
);
790 * Create a newly allocated agent event data structure.
791 * Ownership of filter_expression is taken.
793 * Return a new object else NULL on error.
795 struct agent_event
*agent_create_event(const char *name
,
796 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
797 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
799 struct agent_event
*event
= NULL
;
801 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
802 name
, loglevel_type
, loglevel_value
);
805 ERR("Failed to create agent event; no name provided.");
809 event
= zmalloc(sizeof(*event
));
814 strncpy(event
->name
, name
, sizeof(event
->name
));
815 event
->name
[sizeof(event
->name
) - 1] = '\0';
816 lttng_ht_node_init_str(&event
->node
, event
->name
);
818 event
->loglevel_value
= loglevel_value
;
819 event
->loglevel_type
= loglevel_type
;
820 event
->filter
= filter
;
821 event
->filter_expression
= filter_expression
;
827 * Unique add of a agent event to an agent object.
829 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
835 DBG3("Agent adding event %s", event
->name
);
838 add_unique_agent_event(agt
->events
, event
);
844 * Find multiple agent events sharing the given name.
846 * RCU read side lock MUST be acquired. It must be held for the
847 * duration of the iteration.
849 * Sets the given iterator.
851 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
852 struct lttng_ht_iter
* iter
)
855 struct agent_ht_key key
;
865 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
866 ht_match_event_by_name
, &key
, &iter
->iter
);
870 * Get the next agent event duplicate by name. This should be called
871 * after a call to agent_find_events_by_name() to iterate on events.
873 * The RCU read lock must be held during the iteration and for as long
874 * as the object the iterator points to remains in use.
876 void agent_event_next_duplicate(const char *name
,
877 struct agent
*agt
, struct lttng_ht_iter
* iter
)
879 struct agent_ht_key key
;
883 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
888 * Find a agent event in the given agent using name and loglevel.
890 * RCU read side lock MUST be acquired.
892 * Return object if found else NULL.
894 struct agent_event
*agent_find_event(const char *name
,
895 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
898 struct lttng_ht_node_str
*node
;
899 struct lttng_ht_iter iter
;
901 struct agent_ht_key key
;
909 key
.loglevel_value
= loglevel_value
;
910 key
.loglevel_type
= loglevel_type
;
912 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
913 ht_match_event
, &key
, &iter
.iter
);
914 node
= lttng_ht_iter_get_node_str(&iter
);
919 DBG3("Agent event found %s.", name
);
920 return caa_container_of(node
, struct agent_event
, node
);
923 DBG3("Agent event NOT found %s.", name
);
928 * Free given agent event. This event must not be globally visible at this
929 * point (only expected to be used on failure just after event creation). After
930 * this call, the pointer is not usable anymore.
932 void agent_destroy_event(struct agent_event
*event
)
937 free(event
->filter_expression
);
938 free(event
->exclusion
);
943 * Destroy an agent completely.
945 void agent_destroy(struct agent
*agt
)
947 struct lttng_ht_node_str
*node
;
948 struct lttng_ht_iter iter
;
952 DBG3("Agent destroy");
955 * Just ignore if no events hash table exists. This is possible if for
956 * instance an agent object was allocated but not initialized.
963 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
965 struct agent_event
*event
;
968 * When destroying an event, we have to try to disable it on the agent
969 * side so the event stops generating data. The return value is not
970 * important since we have to continue anyway destroying the object.
972 event
= caa_container_of(node
, struct agent_event
, node
);
973 (void) agent_disable_event(event
, agt
->domain
);
975 ret
= lttng_ht_del(agt
->events
, &iter
);
977 call_rcu(&node
->head
, destroy_event_agent_rcu
);
981 ht_cleanup_push(agt
->events
);
986 * Allocate agent_apps_ht_by_sock.
988 int agent_app_ht_alloc(void)
992 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
993 if (!agent_apps_ht_by_sock
) {
1001 * Destroy a agent application by socket.
1003 void agent_destroy_app_by_sock(int sock
)
1005 struct agent_app
*app
;
1010 * Not finding an application is a very important error that should NEVER
1011 * happen. The hash table deletion is ONLY done through this call when the
1012 * main sessiond thread is torn down.
1015 app
= agent_find_app_by_sock(sock
);
1018 /* RCU read side lock is assumed to be held by this function. */
1019 agent_delete_app(app
);
1021 /* The application is freed in a RCU call but the socket is closed here. */
1022 agent_destroy_app(app
);
1027 * Clean-up the agent app hash table and destroy it.
1029 void agent_app_ht_clean(void)
1031 struct lttng_ht_node_ulong
*node
;
1032 struct lttng_ht_iter iter
;
1034 if (!agent_apps_ht_by_sock
) {
1038 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1039 struct agent_app
*app
;
1041 app
= caa_container_of(node
, struct agent_app
, node
);
1042 agent_destroy_app_by_sock(app
->sock
->fd
);
1046 lttng_ht_destroy(agent_apps_ht_by_sock
);
1050 * Update a agent application (given socket) using the given agent.
1052 * Note that this function is most likely to be used with a tracing session
1053 * thus the caller should make sure to hold the appropriate lock(s).
1055 void agent_update(struct agent
*agt
, int sock
)
1058 struct agent_app
*app
;
1059 struct agent_event
*event
;
1060 struct lttng_ht_iter iter
;
1065 DBG("Agent updating app socket %d", sock
);
1068 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1069 /* Skip event if disabled. */
1070 if (!event
->enabled
) {
1074 app
= agent_find_app_by_sock(sock
);
1076 * We are in the registration path thus if the application is gone,
1077 * there is a serious code flow error.
1081 ret
= enable_event(app
, event
);
1082 if (ret
!= LTTNG_OK
) {
1083 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1084 event
->name
, app
->pid
, app
->sock
->fd
);
1085 /* Let's try the others here and don't assume the app is dead. */