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
);
92 /* Filter expression */
93 if (strncmp(event
->filter_expression
, key
->filter_expression
,
94 strlen(event
->filter_expression
)) != 0) {
105 * Add unique agent event based on the event name and loglevel.
107 static void add_unique_agent_event(struct lttng_ht
*ht
,
108 struct agent_event
*event
)
110 struct cds_lfht_node
*node_ptr
;
111 struct agent_ht_key key
;
117 key
.name
= event
->name
;
118 key
.loglevel_value
= event
->loglevel_value
;
119 key
.loglevel_type
= event
->loglevel_type
;
120 key
.filter_expression
= event
->filter_expression
;
122 node_ptr
= cds_lfht_add_unique(ht
->ht
,
123 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
124 ht_match_event
, &key
, &event
->node
.node
);
125 assert(node_ptr
== &event
->node
.node
);
129 * URCU delayed agent event reclaim.
131 static void destroy_event_agent_rcu(struct rcu_head
*head
)
133 struct lttng_ht_node_str
*node
=
134 caa_container_of(head
, struct lttng_ht_node_str
, head
);
135 struct agent_event
*event
=
136 caa_container_of(node
, struct agent_event
, node
);
138 agent_destroy_event(event
);
142 * URCU delayed agent app reclaim.
144 static void destroy_app_agent_rcu(struct rcu_head
*head
)
146 struct lttng_ht_node_ulong
*node
=
147 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
148 struct agent_app
*app
=
149 caa_container_of(node
, struct agent_app
, node
);
155 * Communication with the agent. Send the message header to the given socket in
158 * Return 0 on success or else a negative errno message of sendmsg() op.
160 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
161 uint32_t cmd
, uint32_t cmd_version
)
165 struct lttcomm_agent_hdr msg
;
169 memset(&msg
, 0, sizeof(msg
));
170 msg
.data_size
= htobe64(data_size
);
171 msg
.cmd
= htobe32(cmd
);
172 msg
.cmd_version
= htobe32(cmd_version
);
174 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
175 if (size
< sizeof(msg
)) {
186 * Communication call with the agent. Send the payload to the given socket. The
187 * header MUST be sent prior to this call.
189 * Return 0 on success or else a negative errno value of sendmsg() op.
191 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
200 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
212 * Communication call with the agent. Receive reply from the agent using the
215 * Return 0 on success or else a negative errno value from recvmsg() op.
217 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
225 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
237 * Internal event listing for a given app. Populate events.
239 * Return number of element in the list or else a negative LTTNG_ERR* code.
240 * On success, the caller is responsible for freeing the memory
241 * allocated for "events".
243 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
245 int ret
, i
, len
= 0, offset
= 0;
248 struct lttng_event
*tmp_events
= NULL
;
249 struct lttcomm_agent_list_reply
*reply
= NULL
;
250 struct lttcomm_agent_list_reply_hdr reply_hdr
;
256 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
259 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
264 /* Get list header so we know how much we'll receive. */
265 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
270 switch (be32toh(reply_hdr
.ret_code
)) {
271 case AGENT_RET_CODE_SUCCESS
:
272 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
275 ERR("Agent returned an unknown code: %" PRIu32
,
276 be32toh(reply_hdr
.ret_code
));
277 ret
= LTTNG_ERR_FATAL
;
281 reply
= zmalloc(data_size
);
283 ret
= LTTNG_ERR_NOMEM
;
287 /* Get the list with the appropriate data size. */
288 ret
= recv_reply(app
->sock
, reply
, data_size
);
293 nb_event
= be32toh(reply
->nb_event
);
294 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
296 ret
= LTTNG_ERR_NOMEM
;
300 for (i
= 0; i
< nb_event
; i
++) {
302 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
303 sizeof(tmp_events
[i
].name
));
304 tmp_events
[i
].pid
= app
->pid
;
305 tmp_events
[i
].enabled
= -1;
306 len
= strlen(reply
->payload
+ offset
) + 1;
309 *events
= tmp_events
;
315 ret
= LTTNG_ERR_UST_LIST_FAIL
;
324 * Internal enable agent event on a agent application. This function
325 * communicates with the agent to enable a given event.
327 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
329 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
333 struct lttcomm_agent_enable msg
;
334 struct lttcomm_agent_generic_reply reply
;
340 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
341 app
->pid
, app
->sock
->fd
);
343 data_size
= sizeof(msg
);
345 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
350 memset(&msg
, 0, sizeof(msg
));
351 msg
.loglevel_value
= event
->loglevel_value
;
352 msg
.loglevel_type
= event
->loglevel_type
;
353 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
354 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
359 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
364 switch (be32toh(reply
.ret_code
)) {
365 case AGENT_RET_CODE_SUCCESS
:
367 case AGENT_RET_CODE_UNKNOWN_NAME
:
368 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
371 ERR("Agent returned an unknown code: %" PRIu32
,
372 be32toh(reply
.ret_code
));
373 ret
= LTTNG_ERR_FATAL
;
380 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
386 * Internal disable agent event call on a agent application. This function
387 * communicates with the agent to disable a given event.
389 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
391 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
395 struct lttcomm_agent_disable msg
;
396 struct lttcomm_agent_generic_reply reply
;
402 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
403 app
->pid
, app
->sock
->fd
);
405 data_size
= sizeof(msg
);
407 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
412 memset(&msg
, 0, sizeof(msg
));
413 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
414 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
419 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
424 switch (be32toh(reply
.ret_code
)) {
425 case AGENT_RET_CODE_SUCCESS
:
427 case AGENT_RET_CODE_UNKNOWN_NAME
:
428 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
431 ERR("Agent returned an unknown code: %" PRIu32
,
432 be32toh(reply
.ret_code
));
433 ret
= LTTNG_ERR_FATAL
;
440 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
446 * Send back the registration DONE command to a given agent application.
448 * Return 0 on success or else a negative value.
450 int agent_send_registration_done(struct agent_app
*app
)
455 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
457 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
461 * Enable agent event on every agent applications registered with the session
464 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
466 int agent_enable_event(struct agent_event
*event
,
467 enum lttng_domain_type domain
)
470 struct agent_app
*app
;
471 struct lttng_ht_iter iter
;
477 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
479 if (app
->domain
!= domain
) {
483 /* Enable event on agent application through TCP socket. */
484 ret
= enable_event(app
, event
);
485 if (ret
!= LTTNG_OK
) {
499 * Disable agent event on every agent applications registered with the session
502 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
504 int agent_disable_event(struct agent_event
*event
,
505 enum lttng_domain_type domain
)
508 struct agent_app
*app
;
509 struct lttng_ht_iter iter
;
512 if (!event
->enabled
) {
518 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
520 if (app
->domain
!= domain
) {
524 /* Enable event on agent application through TCP socket. */
525 ret
= disable_event(app
, event
);
526 if (ret
!= LTTNG_OK
) {
540 * Ask every agent for the list of possible event. Events is allocated with the
541 * events of every agent application.
543 * Return the number of events or else a negative value.
545 int agent_list_events(struct lttng_event
**events
,
546 enum lttng_domain_type domain
)
549 size_t nbmem
, count
= 0;
550 struct agent_app
*app
;
551 struct lttng_event
*tmp_events
= NULL
;
552 struct lttng_ht_iter iter
;
556 DBG2("Agent listing events for domain %d", domain
);
558 nbmem
= UST_APP_EVENT_LIST_SIZE
;
559 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
561 PERROR("zmalloc agent list events");
567 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
570 struct lttng_event
*agent_events
;
572 /* Skip domain not asked by the list. */
573 if (app
->domain
!= domain
) {
577 nb_ev
= list_events(app
, &agent_events
);
583 if (count
+ nb_ev
> nbmem
) {
584 /* In case the realloc fails, we free the memory */
585 struct lttng_event
*new_tmp_events
;
588 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
589 DBG2("Reallocating agent event list from %zu to %zu entries",
591 new_tmp_events
= realloc(tmp_events
,
592 new_nbmem
* sizeof(*new_tmp_events
));
593 if (!new_tmp_events
) {
594 PERROR("realloc agent events");
599 /* Zero the new memory */
600 memset(new_tmp_events
+ nbmem
, 0,
601 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
603 tmp_events
= new_tmp_events
;
605 memcpy(tmp_events
+ count
, agent_events
,
606 nb_ev
* sizeof(*tmp_events
));
613 *events
= tmp_events
;
624 * Create a agent app object using the given PID.
626 * Return newly allocated object or else NULL on error.
628 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
629 struct lttcomm_sock
*sock
)
631 struct agent_app
*app
;
635 app
= zmalloc(sizeof(*app
));
637 PERROR("zmalloc agent create");
642 app
->domain
= domain
;
644 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
651 * Lookup agent app by socket in the global hash table.
653 * RCU read side lock MUST be acquired.
655 * Return object if found else NULL.
657 struct agent_app
*agent_find_app_by_sock(int sock
)
659 struct lttng_ht_node_ulong
*node
;
660 struct lttng_ht_iter iter
;
661 struct agent_app
*app
;
665 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
666 node
= lttng_ht_iter_get_node_ulong(&iter
);
670 app
= caa_container_of(node
, struct agent_app
, node
);
672 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
676 DBG3("Agent app NOT found by sock %d.", sock
);
681 * Add agent application object to the global hash table.
683 void agent_add_app(struct agent_app
*app
)
687 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
690 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
695 * Delete agent application from the global hash table.
697 * rcu_read_lock() must be held by the caller.
699 void agent_delete_app(struct agent_app
*app
)
702 struct lttng_ht_iter iter
;
706 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
708 iter
.iter
.node
= &app
->node
.node
;
709 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
714 * Destroy a agent application object by detaching it from its corresponding
715 * UST app if one is connected by closing the socket. Finally, perform a
716 * delayed memory reclaim.
718 void agent_destroy_app(struct agent_app
*app
)
723 app
->sock
->ops
->close(app
->sock
);
724 lttcomm_destroy_sock(app
->sock
);
727 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
731 * Initialize an already allocated agent object.
733 * Return 0 on success or else a negative errno value.
735 int agent_init(struct agent
*agt
)
741 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
746 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
755 * Add agent object to the given hash table.
757 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
762 DBG3("Agent adding from domain %d", agt
->domain
);
765 lttng_ht_add_unique_u64(ht
, &agt
->node
);
770 * Create an agent object for the given domain.
772 * Return the allocated agent or NULL on error.
774 struct agent
*agent_create(enum lttng_domain_type domain
)
779 agt
= zmalloc(sizeof(*agt
));
783 agt
->domain
= domain
;
785 ret
= agent_init(agt
);
797 * Create a newly allocated agent event data structure.
798 * Ownership of filter_expression is taken.
800 * Return a new object else NULL on error.
802 struct agent_event
*agent_create_event(const char *name
,
803 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
804 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
806 struct agent_event
*event
= NULL
;
808 DBG3("Agent create new event with name %s, loglevel type %d, \
809 loglevel value %d and filter %s",
810 name
, loglevel_type
, loglevel_value
,
811 filter_expression
? filter_expression
: "NULL");
814 ERR("Failed to create agent event; no name provided.");
818 event
= zmalloc(sizeof(*event
));
823 strncpy(event
->name
, name
, sizeof(event
->name
));
824 event
->name
[sizeof(event
->name
) - 1] = '\0';
825 lttng_ht_node_init_str(&event
->node
, event
->name
);
827 event
->loglevel_value
= loglevel_value
;
828 event
->loglevel_type
= loglevel_type
;
829 event
->filter
= filter
;
830 event
->filter_expression
= filter_expression
;
836 * Unique add of a agent event to an agent object.
838 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
844 DBG3("Agent adding event %s", event
->name
);
847 add_unique_agent_event(agt
->events
, event
);
853 * Find multiple agent events sharing the given name.
855 * RCU read side lock MUST be acquired. It must be held for the
856 * duration of the iteration.
858 * Sets the given iterator.
860 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
861 struct lttng_ht_iter
* iter
)
864 struct agent_ht_key key
;
874 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
875 ht_match_event_by_name
, &key
, &iter
->iter
);
879 * Get the next agent event duplicate by name. This should be called
880 * after a call to agent_find_events_by_name() to iterate on events.
882 * The RCU read lock must be held during the iteration and for as long
883 * as the object the iterator points to remains in use.
885 void agent_event_next_duplicate(const char *name
,
886 struct agent
*agt
, struct lttng_ht_iter
* iter
)
888 struct agent_ht_key key
;
892 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
897 * Find a agent event in the given agent using name, loglevel and filter.
899 * RCU read side lock MUST be acquired.
901 * Return object if found else NULL.
903 struct agent_event
*agent_find_event(const char *name
,
904 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
905 char *filter_expression
, struct agent
*agt
)
907 struct lttng_ht_node_str
*node
;
908 struct lttng_ht_iter iter
;
910 struct agent_ht_key key
;
918 key
.loglevel_value
= loglevel_value
;
919 key
.loglevel_type
= loglevel_type
;
920 key
.filter_expression
= filter_expression
;
922 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
923 ht_match_event
, &key
, &iter
.iter
);
924 node
= lttng_ht_iter_get_node_str(&iter
);
929 DBG3("Agent event found %s.", name
);
930 return caa_container_of(node
, struct agent_event
, node
);
933 DBG3("Agent event NOT found %s.", name
);
938 * Free given agent event. This event must not be globally visible at this
939 * point (only expected to be used on failure just after event creation). After
940 * this call, the pointer is not usable anymore.
942 void agent_destroy_event(struct agent_event
*event
)
947 free(event
->filter_expression
);
948 free(event
->exclusion
);
953 * Destroy an agent completely.
955 void agent_destroy(struct agent
*agt
)
957 struct lttng_ht_node_str
*node
;
958 struct lttng_ht_iter iter
;
962 DBG3("Agent destroy");
965 * Just ignore if no events hash table exists. This is possible if for
966 * instance an agent object was allocated but not initialized.
973 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
975 struct agent_event
*event
;
978 * When destroying an event, we have to try to disable it on the agent
979 * side so the event stops generating data. The return value is not
980 * important since we have to continue anyway destroying the object.
982 event
= caa_container_of(node
, struct agent_event
, node
);
983 (void) agent_disable_event(event
, agt
->domain
);
985 ret
= lttng_ht_del(agt
->events
, &iter
);
987 call_rcu(&node
->head
, destroy_event_agent_rcu
);
991 ht_cleanup_push(agt
->events
);
996 * Allocate agent_apps_ht_by_sock.
998 int agent_app_ht_alloc(void)
1002 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1003 if (!agent_apps_ht_by_sock
) {
1011 * Destroy a agent application by socket.
1013 void agent_destroy_app_by_sock(int sock
)
1015 struct agent_app
*app
;
1020 * Not finding an application is a very important error that should NEVER
1021 * happen. The hash table deletion is ONLY done through this call when the
1022 * main sessiond thread is torn down.
1025 app
= agent_find_app_by_sock(sock
);
1028 /* RCU read side lock is assumed to be held by this function. */
1029 agent_delete_app(app
);
1031 /* The application is freed in a RCU call but the socket is closed here. */
1032 agent_destroy_app(app
);
1037 * Clean-up the agent app hash table and destroy it.
1039 void agent_app_ht_clean(void)
1041 struct lttng_ht_node_ulong
*node
;
1042 struct lttng_ht_iter iter
;
1044 if (!agent_apps_ht_by_sock
) {
1048 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1049 struct agent_app
*app
;
1051 app
= caa_container_of(node
, struct agent_app
, node
);
1052 agent_destroy_app_by_sock(app
->sock
->fd
);
1056 lttng_ht_destroy(agent_apps_ht_by_sock
);
1060 * Update a agent application (given socket) using the given agent.
1062 * Note that this function is most likely to be used with a tracing session
1063 * thus the caller should make sure to hold the appropriate lock(s).
1065 void agent_update(struct agent
*agt
, int sock
)
1068 struct agent_app
*app
;
1069 struct agent_event
*event
;
1070 struct lttng_ht_iter iter
;
1075 DBG("Agent updating app socket %d", sock
);
1078 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1079 /* Skip event if disabled. */
1080 if (!event
->enabled
) {
1084 app
= agent_find_app_by_sock(sock
);
1086 * We are in the registration path thus if the application is gone,
1087 * there is a serious code flow error.
1091 ret
= enable_event(app
, event
);
1092 if (ret
!= LTTNG_OK
) {
1093 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1094 event
->name
, app
->pid
, app
->sock
->fd
);
1095 /* Let's try the others here and don't assume the app is dead. */