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 if (event
->loglevel
!= key
->loglevel
) {
121 if (event
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_ALL
&&
122 key
->loglevel
== 0 && event
->loglevel
== -1) {
135 * Add unique agent event based on the event name and loglevel.
137 static void add_unique_agent_event(struct lttng_ht
*ht
,
138 struct agent_event
*event
)
140 struct cds_lfht_node
*node_ptr
;
141 struct agent_ht_key key
;
147 key
.name
= event
->name
;
148 key
.loglevel
= event
->loglevel
;
150 node_ptr
= cds_lfht_add_unique(ht
->ht
,
151 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
152 ht_match_event
, &key
, &event
->node
.node
);
153 assert(node_ptr
== &event
->node
.node
);
157 * URCU delayed agent event reclaim.
159 static void destroy_event_agent_rcu(struct rcu_head
*head
)
161 struct lttng_ht_node_str
*node
=
162 caa_container_of(head
, struct lttng_ht_node_str
, head
);
163 struct agent_event
*event
=
164 caa_container_of(node
, struct agent_event
, node
);
166 agent_destroy_event(event
);
170 * URCU delayed agent app reclaim.
172 static void destroy_app_agent_rcu(struct rcu_head
*head
)
174 struct lttng_ht_node_ulong
*node
=
175 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
176 struct agent_app
*app
=
177 caa_container_of(node
, struct agent_app
, node
);
183 * Communication with the agent. Send the message header to the given socket in
186 * Return 0 on success or else a negative errno message of sendmsg() op.
188 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
189 uint32_t cmd
, uint32_t cmd_version
)
193 struct lttcomm_agent_hdr msg
;
197 memset(&msg
, 0, sizeof(msg
));
198 msg
.data_size
= htobe64(data_size
);
199 msg
.cmd
= htobe32(cmd
);
200 msg
.cmd_version
= htobe32(cmd_version
);
202 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
203 if (size
< sizeof(msg
)) {
214 * Communication call with the agent. Send the payload to the given socket. The
215 * header MUST be sent prior to this call.
217 * Return 0 on success or else a negative errno value of sendmsg() op.
219 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
228 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
240 * Communication call with the agent. Receive reply from the agent using the
243 * Return 0 on success or else a negative errno value from recvmsg() op.
245 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
253 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
265 * Internal event listing for a given app. Populate events.
267 * Return number of element in the list or else a negative LTTNG_ERR* code.
268 * On success, the caller is responsible for freeing the memory
269 * allocated for "events".
271 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
273 int ret
, i
, len
= 0, offset
= 0;
276 uint32_t reply_ret_code
;
277 struct lttng_event
*tmp_events
= NULL
;
278 struct lttcomm_agent_list_reply
*reply
= NULL
;
279 struct lttcomm_agent_list_reply_hdr reply_hdr
;
285 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
288 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
293 /* Get list header so we know how much we'll receive. */
294 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
299 reply_ret_code
= be32toh(reply_hdr
.ret_code
);
300 log_reply_code(reply_ret_code
);
301 switch (reply_ret_code
) {
302 case AGENT_RET_CODE_SUCCESS
:
303 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
310 reply
= zmalloc(data_size
);
312 ret
= LTTNG_ERR_NOMEM
;
316 /* Get the list with the appropriate data size. */
317 ret
= recv_reply(app
->sock
, reply
, data_size
);
322 nb_event
= be32toh(reply
->nb_event
);
323 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
325 ret
= LTTNG_ERR_NOMEM
;
329 for (i
= 0; i
< nb_event
; i
++) {
331 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
332 sizeof(tmp_events
[i
].name
));
333 tmp_events
[i
].pid
= app
->pid
;
334 tmp_events
[i
].enabled
= -1;
335 len
= strlen(reply
->payload
+ offset
) + 1;
338 *events
= tmp_events
;
344 ret
= LTTNG_ERR_UST_LIST_FAIL
;
353 * Internal enable agent event on a agent application. This function
354 * communicates with the agent to enable a given event.
356 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
358 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
362 uint32_t reply_ret_code
;
363 struct lttcomm_agent_enable msg
;
364 struct lttcomm_agent_generic_reply reply
;
370 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
371 app
->pid
, app
->sock
->fd
);
373 data_size
= sizeof(msg
);
375 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
380 memset(&msg
, 0, sizeof(msg
));
381 msg
.loglevel
= event
->loglevel
;
382 msg
.loglevel_type
= event
->loglevel_type
;
383 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
384 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
389 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
394 reply_ret_code
= be32toh(reply
.ret_code
);
395 log_reply_code(reply_ret_code
);
396 switch (reply_ret_code
) {
397 case AGENT_RET_CODE_SUCCESS
:
399 case AGENT_RET_CODE_UNKNOWN_NAME
:
400 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
410 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
416 * Internal disable agent event call on a agent application. This function
417 * communicates with the agent to disable a given event.
419 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
421 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
425 uint32_t reply_ret_code
;
426 struct lttcomm_agent_disable msg
;
427 struct lttcomm_agent_generic_reply reply
;
433 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
434 app
->pid
, app
->sock
->fd
);
436 data_size
= sizeof(msg
);
438 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
443 memset(&msg
, 0, sizeof(msg
));
444 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
445 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
450 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
455 reply_ret_code
= be32toh(reply
.ret_code
);
456 log_reply_code(reply_ret_code
);
457 switch (reply_ret_code
) {
458 case AGENT_RET_CODE_SUCCESS
:
460 case AGENT_RET_CODE_UNKNOWN_NAME
:
461 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
471 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
477 * Send back the registration DONE command to a given agent application.
479 * Return 0 on success or else a negative value.
481 int agent_send_registration_done(struct agent_app
*app
)
486 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
488 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
492 * Enable 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_enable_event(struct agent_event
*event
,
498 enum lttng_domain_type domain
)
501 struct agent_app
*app
;
502 struct lttng_ht_iter iter
;
508 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
510 if (app
->domain
!= domain
) {
514 /* Enable event on agent application through TCP socket. */
515 ret
= enable_event(app
, event
);
516 if (ret
!= LTTNG_OK
) {
530 * Disable agent event on every agent applications registered with the session
533 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
535 int agent_disable_event(struct agent_event
*event
,
536 enum lttng_domain_type domain
)
539 struct agent_app
*app
;
540 struct lttng_ht_iter iter
;
543 if (!event
->enabled
) {
549 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
551 if (app
->domain
!= domain
) {
555 /* Enable event on agent application through TCP socket. */
556 ret
= disable_event(app
, event
);
557 if (ret
!= LTTNG_OK
) {
571 * Ask every agent for the list of possible event. Events is allocated with the
572 * events of every agent application.
574 * Return the number of events or else a negative value.
576 int agent_list_events(struct lttng_event
**events
,
577 enum lttng_domain_type domain
)
580 size_t nbmem
, count
= 0;
581 struct agent_app
*app
;
582 struct lttng_event
*tmp_events
= NULL
;
583 struct lttng_ht_iter iter
;
587 DBG2("Agent listing events for domain %d", domain
);
589 nbmem
= UST_APP_EVENT_LIST_SIZE
;
590 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
592 PERROR("zmalloc agent list events");
598 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
601 struct lttng_event
*agent_events
;
603 /* Skip domain not asked by the list. */
604 if (app
->domain
!= domain
) {
608 nb_ev
= list_events(app
, &agent_events
);
614 if (count
+ nb_ev
> nbmem
) {
615 /* In case the realloc fails, we free the memory */
616 struct lttng_event
*new_tmp_events
;
619 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
620 DBG2("Reallocating agent event list from %zu to %zu entries",
622 new_tmp_events
= realloc(tmp_events
,
623 new_nbmem
* sizeof(*new_tmp_events
));
624 if (!new_tmp_events
) {
625 PERROR("realloc agent events");
630 /* Zero the new memory */
631 memset(new_tmp_events
+ nbmem
, 0,
632 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
634 tmp_events
= new_tmp_events
;
636 memcpy(tmp_events
+ count
, agent_events
,
637 nb_ev
* sizeof(*tmp_events
));
644 *events
= tmp_events
;
655 * Create a agent app object using the given PID.
657 * Return newly allocated object or else NULL on error.
659 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
660 struct lttcomm_sock
*sock
)
662 struct agent_app
*app
;
666 app
= zmalloc(sizeof(*app
));
668 PERROR("zmalloc agent create");
673 app
->domain
= domain
;
675 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
682 * Lookup agent app by socket in the global hash table.
684 * RCU read side lock MUST be acquired.
686 * Return object if found else NULL.
688 struct agent_app
*agent_find_app_by_sock(int sock
)
690 struct lttng_ht_node_ulong
*node
;
691 struct lttng_ht_iter iter
;
692 struct agent_app
*app
;
696 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
697 node
= lttng_ht_iter_get_node_ulong(&iter
);
701 app
= caa_container_of(node
, struct agent_app
, node
);
703 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
707 DBG3("Agent app NOT found by sock %d.", sock
);
712 * Add agent application object to the global hash table.
714 void agent_add_app(struct agent_app
*app
)
718 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
719 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
723 * Delete agent application from the global hash table.
725 * rcu_read_lock() must be held by the caller.
727 void agent_delete_app(struct agent_app
*app
)
730 struct lttng_ht_iter iter
;
734 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
736 iter
.iter
.node
= &app
->node
.node
;
737 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
742 * Destroy an agent application object by detaching it from its corresponding
743 * UST app if one is connected by closing the socket. Finally, perform a
744 * delayed memory reclaim.
746 void agent_destroy_app(struct agent_app
*app
)
751 app
->sock
->ops
->close(app
->sock
);
752 lttcomm_destroy_sock(app
->sock
);
755 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
759 * Initialize an already allocated agent object.
761 * Return 0 on success or else a negative errno value.
763 int agent_init(struct agent
*agt
)
769 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
774 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
783 * Add agent object to the given hash table.
785 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
790 DBG3("Agent adding from domain %d", agt
->domain
);
792 lttng_ht_add_unique_u64(ht
, &agt
->node
);
796 * Create an agent object for the given domain.
798 * Return the allocated agent or NULL on error.
800 struct agent
*agent_create(enum lttng_domain_type domain
)
805 agt
= zmalloc(sizeof(struct agent
));
809 agt
->domain
= domain
;
811 ret
= agent_init(agt
);
823 * Create a newly allocated agent event data structure.
824 * Ownership of filter_expression is taken.
826 * Return a new object else NULL on error.
828 struct agent_event
*agent_create_event(const char *name
,
829 int loglevel
, enum lttng_loglevel_type loglevel_type
,
830 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
832 struct agent_event
*event
= NULL
;
834 DBG3("Agent create new event with name %s", name
);
837 ERR("Failed to create agent event; no name provided.");
841 event
= zmalloc(sizeof(*event
));
846 strncpy(event
->name
, name
, sizeof(event
->name
));
847 event
->name
[sizeof(event
->name
) - 1] = '\0';
848 lttng_ht_node_init_str(&event
->node
, event
->name
);
850 event
->loglevel
= loglevel
;
851 event
->loglevel_type
= loglevel_type
;
852 event
->filter
= filter
;
853 event
->filter_expression
= filter_expression
;
859 * Unique add of a agent event to an agent object.
861 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
867 DBG3("Agent adding event %s", event
->name
);
868 add_unique_agent_event(agt
->events
, event
);
873 * Find a agent event in the given agent using name.
875 * RCU read side lock MUST be acquired.
877 * Return object if found else NULL.
879 struct agent_event
*agent_find_event_by_name(const char *name
,
882 struct lttng_ht_node_str
*node
;
883 struct lttng_ht_iter iter
;
885 struct agent_ht_key key
;
894 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
895 ht_match_event_by_name
, &key
, &iter
.iter
);
896 node
= lttng_ht_iter_get_node_str(&iter
);
901 DBG3("Agent event found %s by name.", name
);
902 return caa_container_of(node
, struct agent_event
, node
);
905 DBG3("Agent NOT found by name %s.", name
);
910 * Find a agent event in the given agent using name and loglevel.
912 * RCU read side lock MUST be acquired.
914 * Return object if found else NULL.
916 struct agent_event
*agent_find_event(const char *name
, int loglevel
,
919 struct lttng_ht_node_str
*node
;
920 struct lttng_ht_iter iter
;
922 struct agent_ht_key key
;
930 key
.loglevel
= loglevel
;
932 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
933 ht_match_event
, &key
, &iter
.iter
);
934 node
= lttng_ht_iter_get_node_str(&iter
);
939 DBG3("Agent event found %s.", name
);
940 return caa_container_of(node
, struct agent_event
, node
);
943 DBG3("Agent event NOT found %s.", name
);
948 * Free given agent event. This event must not be globally visible at this
949 * point (only expected to be used on failure just after event creation). After
950 * this call, the pointer is not usable anymore.
952 void agent_destroy_event(struct agent_event
*event
)
957 free(event
->filter_expression
);
958 free(event
->exclusion
);
963 * Destroy an agent completely.
965 void agent_destroy(struct agent
*agt
)
967 struct lttng_ht_node_str
*node
;
968 struct lttng_ht_iter iter
;
972 DBG3("Agent destroy");
975 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
977 struct agent_event
*event
;
980 * When destroying an event, we have to try to disable it on the agent
981 * side so the event stops generating data. The return value is not
982 * important since we have to continue anyway destroying the object.
984 event
= caa_container_of(node
, struct agent_event
, node
);
985 (void) agent_disable_event(event
, agt
->domain
);
987 ret
= lttng_ht_del(agt
->events
, &iter
);
989 call_rcu(&node
->head
, destroy_event_agent_rcu
);
993 ht_cleanup_push(agt
->events
);
998 * Allocate agent_apps_ht_by_sock.
1000 int agent_app_ht_alloc(void)
1004 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1005 if (!agent_apps_ht_by_sock
) {
1013 * Destroy a agent application by socket.
1015 void agent_destroy_app_by_sock(int sock
)
1017 struct agent_app
*app
;
1022 * Not finding an application is a very important error that should NEVER
1023 * happen. The hash table deletion is ONLY done through this call when the
1024 * main sessiond thread is torn down.
1027 app
= agent_find_app_by_sock(sock
);
1030 /* RCU read side lock is assumed to be held by this function. */
1031 agent_delete_app(app
);
1033 /* The application is freed in a RCU call but the socket is closed here. */
1034 agent_destroy_app(app
);
1039 * Clean-up the agent app hash table and destroy it.
1041 void agent_app_ht_clean(void)
1043 struct lttng_ht_node_ulong
*node
;
1044 struct lttng_ht_iter iter
;
1046 if (!agent_apps_ht_by_sock
) {
1050 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1051 struct agent_app
*app
;
1053 app
= caa_container_of(node
, struct agent_app
, node
);
1054 agent_destroy_app_by_sock(app
->sock
->fd
);
1058 lttng_ht_destroy(agent_apps_ht_by_sock
);
1062 * Update a agent application (given socket) using the given agent.
1064 * Note that this function is most likely to be used with a tracing session
1065 * thus the caller should make sure to hold the appropriate lock(s).
1067 void agent_update(struct agent
*agt
, int sock
)
1070 struct agent_app
*app
;
1071 struct agent_event
*event
;
1072 struct lttng_ht_iter iter
;
1077 DBG("Agent updating app socket %d", sock
);
1080 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1081 /* Skip event if disabled. */
1082 if (!event
->enabled
) {
1086 app
= agent_find_app_by_sock(sock
);
1088 * We are in the registration path thus if the application is gone,
1089 * there is a serious code flow error.
1093 ret
= enable_event(app
, event
);
1094 if (ret
!= LTTNG_OK
) {
1095 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1096 event
->name
, app
->pid
, app
->sock
->fd
);
1097 /* Let's try the others here and don't assume the app is dead. */