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
;
110 event
= caa_container_of(node
, struct agent_event
, node
.node
);
113 /* Match 2 elements of the key: name and loglevel. */
116 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
120 /* Event loglevel value and type. */
121 if (event
->loglevel_type
== key
->loglevel_type
) {
122 /* Same loglevel type. */
123 if (key
->loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
125 * Loglevel value must also match since the loglevel
128 if (event
->loglevel_value
!= key
->loglevel_value
) {
133 /* Loglevel type is different: no match. */
144 * Add unique agent event based on the event name and loglevel.
146 static void add_unique_agent_event(struct lttng_ht
*ht
,
147 struct agent_event
*event
)
149 struct cds_lfht_node
*node_ptr
;
150 struct agent_ht_key key
;
156 key
.name
= event
->name
;
157 key
.loglevel_value
= event
->loglevel_value
;
158 key
.loglevel_type
= event
->loglevel_type
;
160 node_ptr
= cds_lfht_add_unique(ht
->ht
,
161 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
162 ht_match_event
, &key
, &event
->node
.node
);
163 assert(node_ptr
== &event
->node
.node
);
167 * URCU delayed agent event reclaim.
169 static void destroy_event_agent_rcu(struct rcu_head
*head
)
171 struct lttng_ht_node_str
*node
=
172 caa_container_of(head
, struct lttng_ht_node_str
, head
);
173 struct agent_event
*event
=
174 caa_container_of(node
, struct agent_event
, node
);
176 agent_destroy_event(event
);
180 * URCU delayed agent app reclaim.
182 static void destroy_app_agent_rcu(struct rcu_head
*head
)
184 struct lttng_ht_node_ulong
*node
=
185 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
186 struct agent_app
*app
=
187 caa_container_of(node
, struct agent_app
, node
);
193 * Communication with the agent. Send the message header to the given socket in
196 * Return 0 on success or else a negative errno message of sendmsg() op.
198 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
199 uint32_t cmd
, uint32_t cmd_version
)
203 struct lttcomm_agent_hdr msg
;
207 memset(&msg
, 0, sizeof(msg
));
208 msg
.data_size
= htobe64(data_size
);
209 msg
.cmd
= htobe32(cmd
);
210 msg
.cmd_version
= htobe32(cmd_version
);
212 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
213 if (size
< sizeof(msg
)) {
224 * Communication call with the agent. Send the payload to the given socket. The
225 * header MUST be sent prior to this call.
227 * Return 0 on success or else a negative errno value of sendmsg() op.
229 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
238 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
250 * Communication call with the agent. Receive reply from the agent using the
253 * Return 0 on success or else a negative errno value from recvmsg() op.
255 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
263 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
275 * Internal event listing for a given app. Populate events.
277 * Return number of element in the list or else a negative LTTNG_ERR* code.
278 * On success, the caller is responsible for freeing the memory
279 * allocated for "events".
281 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
283 int ret
, i
, len
= 0, offset
= 0;
286 uint32_t reply_ret_code
;
287 struct lttng_event
*tmp_events
= NULL
;
288 struct lttcomm_agent_list_reply
*reply
= NULL
;
289 struct lttcomm_agent_list_reply_hdr reply_hdr
;
295 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
298 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
303 /* Get list header so we know how much we'll receive. */
304 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
309 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
310 log_reply_code(reply_ret_code
);
311 switch (reply_ret_code
) {
312 case AGENT_RET_CODE_SUCCESS
:
313 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
320 reply
= zmalloc(data_size
);
322 ret
= LTTNG_ERR_NOMEM
;
326 /* Get the list with the appropriate data size. */
327 ret
= recv_reply(app
->sock
, reply
, data_size
);
332 nb_event
= be32toh(reply
->nb_event
);
333 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
335 ret
= LTTNG_ERR_NOMEM
;
339 for (i
= 0; i
< nb_event
; i
++) {
341 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
342 sizeof(tmp_events
[i
].name
));
343 tmp_events
[i
].pid
= app
->pid
;
344 tmp_events
[i
].enabled
= -1;
345 len
= strlen(reply
->payload
+ offset
) + 1;
348 *events
= tmp_events
;
354 ret
= LTTNG_ERR_UST_LIST_FAIL
;
363 * Internal enable agent event on a agent application. This function
364 * communicates with the agent to enable a given event.
366 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
368 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
372 uint32_t reply_ret_code
;
373 struct lttcomm_agent_enable msg
;
374 struct lttcomm_agent_generic_reply reply
;
380 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
381 app
->pid
, app
->sock
->fd
);
383 data_size
= sizeof(msg
);
385 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
390 memset(&msg
, 0, sizeof(msg
));
391 msg
.loglevel_value
= event
->loglevel_value
;
392 msg
.loglevel_type
= event
->loglevel_type
;
393 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
394 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
399 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
404 reply_ret_code
= be32toh(reply
.ret_code
);
405 log_reply_code(reply_ret_code
);
406 switch (reply_ret_code
) {
407 case AGENT_RET_CODE_SUCCESS
:
409 case AGENT_RET_CODE_UNKNOWN_NAME
:
410 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
420 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
426 * Internal disable agent event call on a agent application. This function
427 * communicates with the agent to disable a given event.
429 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
431 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
435 uint32_t reply_ret_code
;
436 struct lttcomm_agent_disable msg
;
437 struct lttcomm_agent_generic_reply reply
;
443 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
444 app
->pid
, app
->sock
->fd
);
446 data_size
= sizeof(msg
);
448 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
453 memset(&msg
, 0, sizeof(msg
));
454 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
455 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
460 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
465 reply_ret_code
= be32toh(reply
.ret_code
);
466 log_reply_code(reply_ret_code
);
467 switch (reply_ret_code
) {
468 case AGENT_RET_CODE_SUCCESS
:
470 case AGENT_RET_CODE_UNKNOWN_NAME
:
471 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
481 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
487 * Send back the registration DONE command to a given agent application.
489 * Return 0 on success or else a negative value.
491 int agent_send_registration_done(struct agent_app
*app
)
496 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
498 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
502 * Enable agent event on every agent applications registered with the session
505 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
507 int agent_enable_event(struct agent_event
*event
,
508 enum lttng_domain_type domain
)
511 struct agent_app
*app
;
512 struct lttng_ht_iter iter
;
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
= enable_event(app
, event
);
526 if (ret
!= LTTNG_OK
) {
540 * Disable agent event on every agent applications registered with the session
543 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
545 int agent_disable_event(struct agent_event
*event
,
546 enum lttng_domain_type domain
)
549 struct agent_app
*app
;
550 struct lttng_ht_iter iter
;
553 if (!event
->enabled
) {
559 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
561 if (app
->domain
!= domain
) {
565 /* Enable event on agent application through TCP socket. */
566 ret
= disable_event(app
, event
);
567 if (ret
!= LTTNG_OK
) {
581 * Ask every agent for the list of possible event. Events is allocated with the
582 * events of every agent application.
584 * Return the number of events or else a negative value.
586 int agent_list_events(struct lttng_event
**events
,
587 enum lttng_domain_type domain
)
590 size_t nbmem
, count
= 0;
591 struct agent_app
*app
;
592 struct lttng_event
*tmp_events
= NULL
;
593 struct lttng_ht_iter iter
;
597 DBG2("Agent listing events for domain %d", domain
);
599 nbmem
= UST_APP_EVENT_LIST_SIZE
;
600 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
602 PERROR("zmalloc agent list events");
608 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
611 struct lttng_event
*agent_events
;
613 /* Skip domain not asked by the list. */
614 if (app
->domain
!= domain
) {
618 nb_ev
= list_events(app
, &agent_events
);
624 if (count
+ nb_ev
> nbmem
) {
625 /* In case the realloc fails, we free the memory */
626 struct lttng_event
*new_tmp_events
;
629 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
630 DBG2("Reallocating agent event list from %zu to %zu entries",
632 new_tmp_events
= realloc(tmp_events
,
633 new_nbmem
* sizeof(*new_tmp_events
));
634 if (!new_tmp_events
) {
635 PERROR("realloc agent events");
640 /* Zero the new memory */
641 memset(new_tmp_events
+ nbmem
, 0,
642 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
644 tmp_events
= new_tmp_events
;
646 memcpy(tmp_events
+ count
, agent_events
,
647 nb_ev
* sizeof(*tmp_events
));
654 *events
= tmp_events
;
665 * Create a agent app object using the given PID.
667 * Return newly allocated object or else NULL on error.
669 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
670 struct lttcomm_sock
*sock
)
672 struct agent_app
*app
;
676 app
= zmalloc(sizeof(*app
));
678 PERROR("zmalloc agent create");
683 app
->domain
= domain
;
685 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
692 * Lookup agent app by socket in the global hash table.
694 * RCU read side lock MUST be acquired.
696 * Return object if found else NULL.
698 struct agent_app
*agent_find_app_by_sock(int sock
)
700 struct lttng_ht_node_ulong
*node
;
701 struct lttng_ht_iter iter
;
702 struct agent_app
*app
;
706 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
707 node
= lttng_ht_iter_get_node_ulong(&iter
);
711 app
= caa_container_of(node
, struct agent_app
, node
);
713 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
717 DBG3("Agent app NOT found by sock %d.", sock
);
722 * Add agent application object to the global hash table.
724 void agent_add_app(struct agent_app
*app
)
728 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
729 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
733 * Delete agent application from the global hash table.
735 * rcu_read_lock() must be held by the caller.
737 void agent_delete_app(struct agent_app
*app
)
740 struct lttng_ht_iter iter
;
744 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
746 iter
.iter
.node
= &app
->node
.node
;
747 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
752 * Destroy an agent application object by detaching it from its corresponding
753 * UST app if one is connected by closing the socket. Finally, perform a
754 * delayed memory reclaim.
756 void agent_destroy_app(struct agent_app
*app
)
761 app
->sock
->ops
->close(app
->sock
);
762 lttcomm_destroy_sock(app
->sock
);
765 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
769 * Initialize an already allocated agent object.
771 * Return 0 on success or else a negative errno value.
773 int agent_init(struct agent
*agt
)
779 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
784 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
793 * Add agent object to the given hash table.
795 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
800 DBG3("Agent adding from domain %d", agt
->domain
);
802 lttng_ht_add_unique_u64(ht
, &agt
->node
);
806 * Create an agent object for the given domain.
808 * Return the allocated agent or NULL on error.
810 struct agent
*agent_create(enum lttng_domain_type domain
)
815 agt
= zmalloc(sizeof(struct agent
));
819 agt
->domain
= domain
;
821 ret
= agent_init(agt
);
833 * Create a newly allocated agent event data structure.
834 * Ownership of filter_expression is taken.
836 * Return a new object else NULL on error.
838 struct agent_event
*agent_create_event(const char *name
,
839 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
840 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
842 struct agent_event
*event
= NULL
;
844 DBG3("Agent create new event with name %s, loglevel type %d and loglevel value %d",
845 name
, loglevel_type
, loglevel_value
);
848 ERR("Failed to create agent event; no name provided.");
852 event
= zmalloc(sizeof(*event
));
857 strncpy(event
->name
, name
, sizeof(event
->name
));
858 event
->name
[sizeof(event
->name
) - 1] = '\0';
859 lttng_ht_node_init_str(&event
->node
, event
->name
);
861 event
->loglevel_value
= loglevel_value
;
862 event
->loglevel_type
= loglevel_type
;
863 event
->filter
= filter
;
864 event
->filter_expression
= filter_expression
;
870 * Unique add of a agent event to an agent object.
872 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
878 DBG3("Agent adding event %s", event
->name
);
879 add_unique_agent_event(agt
->events
, event
);
884 * Find a agent event in the given agent using name.
886 * RCU read side lock MUST be acquired.
888 * Return object if found else NULL.
890 struct agent_event
*agent_find_event_by_name(const char *name
,
893 struct lttng_ht_node_str
*node
;
894 struct lttng_ht_iter iter
;
896 struct agent_ht_key key
;
905 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
906 ht_match_event_by_name
, &key
, &iter
.iter
);
907 node
= lttng_ht_iter_get_node_str(&iter
);
912 DBG3("Agent event found %s by name.", name
);
913 return caa_container_of(node
, struct agent_event
, node
);
916 DBG3("Agent NOT found by name %s.", name
);
921 * Find a agent event in the given agent using name and loglevel.
923 * RCU read side lock MUST be acquired.
925 * Return object if found else NULL.
927 struct agent_event
*agent_find_event(const char *name
,
928 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
931 struct lttng_ht_node_str
*node
;
932 struct lttng_ht_iter iter
;
934 struct agent_ht_key key
;
942 key
.loglevel_value
= loglevel_value
;
943 key
.loglevel_type
= loglevel_type
;
945 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
946 ht_match_event
, &key
, &iter
.iter
);
947 node
= lttng_ht_iter_get_node_str(&iter
);
952 DBG3("Agent event found %s.", name
);
953 return caa_container_of(node
, struct agent_event
, node
);
956 DBG3("Agent event NOT found %s.", name
);
961 * Free given agent event. This event must not be globally visible at this
962 * point (only expected to be used on failure just after event creation). After
963 * this call, the pointer is not usable anymore.
965 void agent_destroy_event(struct agent_event
*event
)
970 free(event
->filter_expression
);
971 free(event
->exclusion
);
976 * Destroy an agent completely.
978 void agent_destroy(struct agent
*agt
)
980 struct lttng_ht_node_str
*node
;
981 struct lttng_ht_iter iter
;
985 DBG3("Agent destroy");
988 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
990 struct agent_event
*event
;
993 * When destroying an event, we have to try to disable it on the agent
994 * side so the event stops generating data. The return value is not
995 * important since we have to continue anyway destroying the object.
997 event
= caa_container_of(node
, struct agent_event
, node
);
998 (void) agent_disable_event(event
, agt
->domain
);
1000 ret
= lttng_ht_del(agt
->events
, &iter
);
1002 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1006 ht_cleanup_push(agt
->events
);
1011 * Allocate agent_apps_ht_by_sock.
1013 int agent_app_ht_alloc(void)
1017 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1018 if (!agent_apps_ht_by_sock
) {
1026 * Destroy a agent application by socket.
1028 void agent_destroy_app_by_sock(int sock
)
1030 struct agent_app
*app
;
1035 * Not finding an application is a very important error that should NEVER
1036 * happen. The hash table deletion is ONLY done through this call when the
1037 * main sessiond thread is torn down.
1040 app
= agent_find_app_by_sock(sock
);
1043 /* RCU read side lock is assumed to be held by this function. */
1044 agent_delete_app(app
);
1046 /* The application is freed in a RCU call but the socket is closed here. */
1047 agent_destroy_app(app
);
1052 * Clean-up the agent app hash table and destroy it.
1054 void agent_app_ht_clean(void)
1056 struct lttng_ht_node_ulong
*node
;
1057 struct lttng_ht_iter iter
;
1059 if (!agent_apps_ht_by_sock
) {
1063 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1064 struct agent_app
*app
;
1066 app
= caa_container_of(node
, struct agent_app
, node
);
1067 agent_destroy_app_by_sock(app
->sock
->fd
);
1071 lttng_ht_destroy(agent_apps_ht_by_sock
);
1075 * Update a agent application (given socket) using the given agent.
1077 * Note that this function is most likely to be used with a tracing session
1078 * thus the caller should make sure to hold the appropriate lock(s).
1080 void agent_update(struct agent
*agt
, int sock
)
1083 struct agent_app
*app
;
1084 struct agent_event
*event
;
1085 struct lttng_ht_iter iter
;
1090 DBG("Agent updating app socket %d", sock
);
1093 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1094 /* Skip event if disabled. */
1095 if (!event
->enabled
) {
1099 app
= agent_find_app_by_sock(sock
);
1101 * We are in the registration path thus if the application is gone,
1102 * there is a serious code flow error.
1106 ret
= enable_event(app
, event
);
1107 if (ret
!= LTTNG_OK
) {
1108 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1109 event
->name
, app
->pid
, app
->sock
->fd
);
1110 /* Let's try the others here and don't assume the app is dead. */