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 #define AGENT_RET_CODE_INDEX(code) (code - AGENT_RET_CODE_SUCCESS)
36 * Human readable agent return code.
38 static const char *error_string_array
[] = {
39 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_SUCCESS
) ] = "Success",
40 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_INVALID
) ] = "Invalid command",
41 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_UNKNOWN_NAME
) ] = "Unknown logger name",
44 [ AGENT_RET_CODE_INDEX(AGENT_RET_CODE_NR
) ] = "Unknown code",
48 void log_reply_code(uint32_t in_reply_ret_code
)
50 int level
= PRINT_DBG3
;
52 * reply_ret_code and in_reply_ret_code are kept separate to have a
53 * sanitized value (used to retrieve the human readable string) and the
54 * original value which is logged as-is.
56 uint32_t reply_ret_code
= in_reply_ret_code
;
58 if (reply_ret_code
< AGENT_RET_CODE_SUCCESS
||
59 reply_ret_code
>= AGENT_RET_CODE_NR
) {
60 reply_ret_code
= AGENT_RET_CODE_NR
;
64 LOG(level
, "Agent replied with retcode: %s (%"PRIu32
")",
65 error_string_array
[AGENT_RET_CODE_INDEX(
71 * Match function for the events hash table lookup by name.
73 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
76 struct agent_event
*event
;
77 const struct agent_ht_key
*key
;
82 event
= caa_container_of(node
, struct agent_event
, node
.node
);
85 /* Match 1 elements of the key: name. */
88 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
99 * Match function for the events hash table lookup by name and loglevel.
101 static int ht_match_event(struct cds_lfht_node
*node
,
104 struct agent_event
*event
;
105 const struct agent_ht_key
*key
;
111 event
= caa_container_of(node
, struct agent_event
, node
.node
);
114 /* Match 2 elements of the key: name and loglevel. */
117 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
121 /* Event loglevel value and type. */
122 ll_match
= loglevels_match(event
->loglevel_type
,
123 event
->loglevel_value
, key
->loglevel_type
,
124 key
->loglevel_value
, LTTNG_EVENT_LOGLEVEL_ALL
);
137 * Add unique agent event based on the event name and loglevel.
139 static void add_unique_agent_event(struct lttng_ht
*ht
,
140 struct agent_event
*event
)
142 struct cds_lfht_node
*node_ptr
;
143 struct agent_ht_key key
;
149 key
.name
= event
->name
;
150 key
.loglevel_value
= event
->loglevel_value
;
151 key
.loglevel_type
= event
->loglevel_type
;
153 node_ptr
= cds_lfht_add_unique(ht
->ht
,
154 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
155 ht_match_event
, &key
, &event
->node
.node
);
156 assert(node_ptr
== &event
->node
.node
);
160 * URCU delayed agent event reclaim.
162 static void destroy_event_agent_rcu(struct rcu_head
*head
)
164 struct lttng_ht_node_str
*node
=
165 caa_container_of(head
, struct lttng_ht_node_str
, head
);
166 struct agent_event
*event
=
167 caa_container_of(node
, struct agent_event
, node
);
169 agent_destroy_event(event
);
173 * URCU delayed agent app reclaim.
175 static void destroy_app_agent_rcu(struct rcu_head
*head
)
177 struct lttng_ht_node_ulong
*node
=
178 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
179 struct agent_app
*app
=
180 caa_container_of(node
, struct agent_app
, node
);
186 * Communication with the agent. Send the message header to the given socket in
189 * Return 0 on success or else a negative errno message of sendmsg() op.
191 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
192 uint32_t cmd
, uint32_t cmd_version
)
196 struct lttcomm_agent_hdr msg
;
200 memset(&msg
, 0, sizeof(msg
));
201 msg
.data_size
= htobe64(data_size
);
202 msg
.cmd
= htobe32(cmd
);
203 msg
.cmd_version
= htobe32(cmd_version
);
205 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
206 if (size
< sizeof(msg
)) {
217 * Communication call with the agent. Send the payload to the given socket. The
218 * header MUST be sent prior to this call.
220 * Return 0 on success or else a negative errno value of sendmsg() op.
222 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
231 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
243 * Communication call with the agent. Receive reply from the agent using the
246 * Return 0 on success or else a negative errno value from recvmsg() op.
248 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
256 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
268 * Internal event listing for a given app. Populate events.
270 * Return number of element in the list or else a negative LTTNG_ERR* code.
271 * On success, the caller is responsible for freeing the memory
272 * allocated for "events".
274 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
276 int ret
, i
, len
= 0, offset
= 0;
279 uint32_t reply_ret_code
;
280 struct lttng_event
*tmp_events
= NULL
;
281 struct lttcomm_agent_list_reply
*reply
= NULL
;
282 struct lttcomm_agent_list_reply_hdr reply_hdr
;
288 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
291 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
296 /* Get list header so we know how much we'll receive. */
297 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
302 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
303 log_reply_code(reply_ret_code
);
304 switch (reply_ret_code
) {
305 case AGENT_RET_CODE_SUCCESS
:
306 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
313 reply
= zmalloc(data_size
);
315 ret
= LTTNG_ERR_NOMEM
;
319 /* Get the list with the appropriate data size. */
320 ret
= recv_reply(app
->sock
, reply
, data_size
);
325 nb_event
= be32toh(reply
->nb_event
);
326 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
328 ret
= LTTNG_ERR_NOMEM
;
332 for (i
= 0; i
< nb_event
; i
++) {
334 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
335 sizeof(tmp_events
[i
].name
));
336 tmp_events
[i
].pid
= app
->pid
;
337 tmp_events
[i
].enabled
= -1;
338 len
= strlen(reply
->payload
+ offset
) + 1;
341 *events
= tmp_events
;
347 ret
= LTTNG_ERR_UST_LIST_FAIL
;
356 * Internal enable agent event on a agent application. This function
357 * communicates with the agent to enable a given event.
359 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
361 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
365 uint32_t reply_ret_code
;
366 struct lttcomm_agent_enable msg
;
367 struct lttcomm_agent_generic_reply reply
;
373 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
374 app
->pid
, app
->sock
->fd
);
376 data_size
= sizeof(msg
);
378 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
383 memset(&msg
, 0, sizeof(msg
));
384 msg
.loglevel_value
= event
->loglevel_value
;
385 msg
.loglevel_type
= event
->loglevel_type
;
386 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
387 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
392 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
397 reply_ret_code
= be32toh(reply
.ret_code
);
398 log_reply_code(reply_ret_code
);
399 switch (reply_ret_code
) {
400 case AGENT_RET_CODE_SUCCESS
:
402 case AGENT_RET_CODE_UNKNOWN_NAME
:
403 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
413 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
419 * Internal disable agent event call on a agent application. This function
420 * communicates with the agent to disable a given event.
422 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
424 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
428 uint32_t reply_ret_code
;
429 struct lttcomm_agent_disable msg
;
430 struct lttcomm_agent_generic_reply reply
;
436 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
437 app
->pid
, app
->sock
->fd
);
439 data_size
= sizeof(msg
);
441 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
446 memset(&msg
, 0, sizeof(msg
));
447 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
448 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
453 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
458 reply_ret_code
= be32toh(reply
.ret_code
);
459 log_reply_code(reply_ret_code
);
460 switch (reply_ret_code
) {
461 case AGENT_RET_CODE_SUCCESS
:
463 case AGENT_RET_CODE_UNKNOWN_NAME
:
464 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
474 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
480 * Send back the registration DONE command to a given agent application.
482 * Return 0 on success or else a negative value.
484 int agent_send_registration_done(struct agent_app
*app
)
489 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
491 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
495 * Enable agent event on every agent applications registered with the session
498 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
500 int agent_enable_event(struct agent_event
*event
,
501 enum lttng_domain_type domain
)
504 struct agent_app
*app
;
505 struct lttng_ht_iter iter
;
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
= enable_event(app
, event
);
519 if (ret
!= LTTNG_OK
) {
533 * Disable agent event on every agent applications registered with the session
536 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
538 int agent_disable_event(struct agent_event
*event
,
539 enum lttng_domain_type domain
)
542 struct agent_app
*app
;
543 struct lttng_ht_iter iter
;
546 if (!event
->enabled
) {
552 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
554 if (app
->domain
!= domain
) {
558 /* Enable event on agent application through TCP socket. */
559 ret
= disable_event(app
, event
);
560 if (ret
!= LTTNG_OK
) {
574 * Ask every agent for the list of possible event. Events is allocated with the
575 * events of every agent application.
577 * Return the number of events or else a negative value.
579 int agent_list_events(struct lttng_event
**events
,
580 enum lttng_domain_type domain
)
583 size_t nbmem
, count
= 0;
584 struct agent_app
*app
;
585 struct lttng_event
*tmp_events
= NULL
;
586 struct lttng_ht_iter iter
;
590 DBG2("Agent listing events for domain %d", domain
);
592 nbmem
= UST_APP_EVENT_LIST_SIZE
;
593 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
595 PERROR("zmalloc agent list events");
601 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
604 struct lttng_event
*agent_events
;
606 /* Skip domain not asked by the list. */
607 if (app
->domain
!= domain
) {
611 nb_ev
= list_events(app
, &agent_events
);
617 if (count
+ nb_ev
> nbmem
) {
618 /* In case the realloc fails, we free the memory */
619 struct lttng_event
*new_tmp_events
;
622 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
623 DBG2("Reallocating agent event list from %zu to %zu entries",
625 new_tmp_events
= realloc(tmp_events
,
626 new_nbmem
* sizeof(*new_tmp_events
));
627 if (!new_tmp_events
) {
628 PERROR("realloc agent events");
633 /* Zero the new memory */
634 memset(new_tmp_events
+ nbmem
, 0,
635 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
637 tmp_events
= new_tmp_events
;
639 memcpy(tmp_events
+ count
, agent_events
,
640 nb_ev
* sizeof(*tmp_events
));
647 *events
= tmp_events
;
658 * Create a agent app object using the given PID.
660 * Return newly allocated object or else NULL on error.
662 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
663 struct lttcomm_sock
*sock
)
665 struct agent_app
*app
;
669 app
= zmalloc(sizeof(*app
));
671 PERROR("zmalloc agent create");
676 app
->domain
= domain
;
678 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
685 * Lookup agent app by socket in the global hash table.
687 * RCU read side lock MUST be acquired.
689 * Return object if found else NULL.
691 struct agent_app
*agent_find_app_by_sock(int sock
)
693 struct lttng_ht_node_ulong
*node
;
694 struct lttng_ht_iter iter
;
695 struct agent_app
*app
;
699 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
700 node
= lttng_ht_iter_get_node_ulong(&iter
);
704 app
= caa_container_of(node
, struct agent_app
, node
);
706 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
710 DBG3("Agent app NOT found by sock %d.", sock
);
715 * Add agent application object to the global hash table.
717 void agent_add_app(struct agent_app
*app
)
721 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
722 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
726 * Delete agent application from the global hash table.
728 * rcu_read_lock() must be held by the caller.
730 void agent_delete_app(struct agent_app
*app
)
733 struct lttng_ht_iter iter
;
737 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
739 iter
.iter
.node
= &app
->node
.node
;
740 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
745 * Destroy an agent application object by detaching it from its corresponding
746 * UST app if one is connected by closing the socket. Finally, perform a
747 * delayed memory reclaim.
749 void agent_destroy_app(struct agent_app
*app
)
754 app
->sock
->ops
->close(app
->sock
);
755 lttcomm_destroy_sock(app
->sock
);
758 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
762 * Initialize an already allocated agent object.
764 * Return 0 on success or else a negative errno value.
766 int agent_init(struct agent
*agt
)
772 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
777 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
786 * Add agent object to the given hash table.
788 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
793 DBG3("Agent adding from domain %d", agt
->domain
);
795 lttng_ht_add_unique_u64(ht
, &agt
->node
);
799 * Create an agent object for the given domain.
801 * Return the allocated agent or NULL on error.
803 struct agent
*agent_create(enum lttng_domain_type domain
)
808 agt
= zmalloc(sizeof(struct agent
));
812 agt
->domain
= domain
;
814 ret
= agent_init(agt
);
826 * Create a newly allocated agent event data structure.
827 * Ownership of filter_expression is taken.
829 * Return a new object else NULL on error.
831 struct agent_event
*agent_create_event(const char *name
,
832 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
833 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
835 struct agent_event
*event
= NULL
;
837 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
838 name
, loglevel_type
, loglevel_value
);
841 ERR("Failed to create agent event; no name provided.");
845 event
= zmalloc(sizeof(*event
));
850 strncpy(event
->name
, name
, sizeof(event
->name
));
851 event
->name
[sizeof(event
->name
) - 1] = '\0';
852 lttng_ht_node_init_str(&event
->node
, event
->name
);
854 event
->loglevel_value
= loglevel_value
;
855 event
->loglevel_type
= loglevel_type
;
856 event
->filter
= filter
;
857 event
->filter_expression
= filter_expression
;
863 * Unique add of a agent event to an agent object.
865 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
871 DBG3("Agent adding event %s", event
->name
);
872 add_unique_agent_event(agt
->events
, event
);
877 * Find multiple agent events sharing the given name.
879 * RCU read side lock MUST be acquired. It must be held for the
880 * duration of the iteration.
882 * Sets the given iterator.
884 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
885 struct lttng_ht_iter
* iter
)
888 struct agent_ht_key key
;
898 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
899 ht_match_event_by_name
, &key
, &iter
->iter
);
903 * Get the next agent event duplicate by name. This should be called
904 * after a call to agent_find_events_by_name() to iterate on events.
906 * The RCU read lock must be held during the iteration and for as long
907 * as the object the iterator points to remains in use.
909 void agent_event_next_duplicate(const char *name
,
910 struct agent
*agt
, struct lttng_ht_iter
* iter
)
912 struct agent_ht_key key
;
916 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
921 * Find a agent event in the given agent using name and loglevel.
923 * RCU read side lock MUST be acquired. It must be kept for as long as
924 * the returned agent_event is used.
926 * Return object if found else NULL.
928 struct agent_event
*agent_find_event(const char *name
,
929 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
932 struct lttng_ht_node_str
*node
;
933 struct lttng_ht_iter iter
;
935 struct agent_ht_key key
;
943 key
.loglevel_value
= loglevel_value
;
944 key
.loglevel_type
= loglevel_type
;
946 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
947 ht_match_event
, &key
, &iter
.iter
);
948 node
= lttng_ht_iter_get_node_str(&iter
);
953 DBG3("Agent event found %s.", name
);
954 return caa_container_of(node
, struct agent_event
, node
);
957 DBG3("Agent event NOT found %s.", name
);
962 * Free given agent event. This event must not be globally visible at this
963 * point (only expected to be used on failure just after event creation). After
964 * this call, the pointer is not usable anymore.
966 void agent_destroy_event(struct agent_event
*event
)
971 free(event
->filter_expression
);
972 free(event
->exclusion
);
977 * Destroy an agent completely.
979 void agent_destroy(struct agent
*agt
)
981 struct lttng_ht_node_str
*node
;
982 struct lttng_ht_iter iter
;
986 DBG3("Agent destroy");
989 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
991 struct agent_event
*event
;
994 * When destroying an event, we have to try to disable it on the agent
995 * side so the event stops generating data. The return value is not
996 * important since we have to continue anyway destroying the object.
998 event
= caa_container_of(node
, struct agent_event
, node
);
999 (void) agent_disable_event(event
, agt
->domain
);
1001 ret
= lttng_ht_del(agt
->events
, &iter
);
1003 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1007 ht_cleanup_push(agt
->events
);
1012 * Allocate agent_apps_ht_by_sock.
1014 int agent_app_ht_alloc(void)
1018 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1019 if (!agent_apps_ht_by_sock
) {
1027 * Destroy a agent application by socket.
1029 void agent_destroy_app_by_sock(int sock
)
1031 struct agent_app
*app
;
1036 * Not finding an application is a very important error that should NEVER
1037 * happen. The hash table deletion is ONLY done through this call when the
1038 * main sessiond thread is torn down.
1041 app
= agent_find_app_by_sock(sock
);
1044 /* RCU read side lock is assumed to be held by this function. */
1045 agent_delete_app(app
);
1047 /* The application is freed in a RCU call but the socket is closed here. */
1048 agent_destroy_app(app
);
1053 * Clean-up the agent app hash table and destroy it.
1055 void agent_app_ht_clean(void)
1057 struct lttng_ht_node_ulong
*node
;
1058 struct lttng_ht_iter iter
;
1060 if (!agent_apps_ht_by_sock
) {
1064 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1065 struct agent_app
*app
;
1067 app
= caa_container_of(node
, struct agent_app
, node
);
1068 agent_destroy_app_by_sock(app
->sock
->fd
);
1072 lttng_ht_destroy(agent_apps_ht_by_sock
);
1076 * Update a agent application (given socket) using the given agent.
1078 * Note that this function is most likely to be used with a tracing session
1079 * thus the caller should make sure to hold the appropriate lock(s).
1081 void agent_update(struct agent
*agt
, int sock
)
1084 struct agent_app
*app
;
1085 struct agent_event
*event
;
1086 struct lttng_ht_iter iter
;
1091 DBG("Agent updating app socket %d", sock
);
1094 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1095 /* Skip event if disabled. */
1096 if (!event
->enabled
) {
1100 app
= agent_find_app_by_sock(sock
);
1102 * We are in the registration path thus if the application is gone,
1103 * there is a serious code flow error.
1107 ret
= enable_event(app
, event
);
1108 if (ret
!= LTTNG_OK
) {
1109 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1110 event
->name
, app
->pid
, app
->sock
->fd
);
1111 /* Let's try the others here and don't assume the app is dead. */