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 (!!event
->filter_expression
^ !!key
->filter_expression
) {
94 /* One has a filter expression, the other does not */
98 if (event
->filter_expression
) {
99 if (strncmp(event
->filter_expression
, key
->filter_expression
,
100 strlen(event
->filter_expression
)) != 0) {
112 * Add unique agent event based on the event name and loglevel.
114 static void add_unique_agent_event(struct lttng_ht
*ht
,
115 struct agent_event
*event
)
117 struct cds_lfht_node
*node_ptr
;
118 struct agent_ht_key key
;
124 key
.name
= event
->name
;
125 key
.loglevel_value
= event
->loglevel_value
;
126 key
.loglevel_type
= event
->loglevel_type
;
127 key
.filter_expression
= event
->filter_expression
;
129 node_ptr
= cds_lfht_add_unique(ht
->ht
,
130 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
131 ht_match_event
, &key
, &event
->node
.node
);
132 assert(node_ptr
== &event
->node
.node
);
136 * URCU delayed agent event reclaim.
138 static void destroy_event_agent_rcu(struct rcu_head
*head
)
140 struct lttng_ht_node_str
*node
=
141 caa_container_of(head
, struct lttng_ht_node_str
, head
);
142 struct agent_event
*event
=
143 caa_container_of(node
, struct agent_event
, node
);
145 agent_destroy_event(event
);
149 * URCU delayed agent app reclaim.
151 static void destroy_app_agent_rcu(struct rcu_head
*head
)
153 struct lttng_ht_node_ulong
*node
=
154 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
155 struct agent_app
*app
=
156 caa_container_of(node
, struct agent_app
, node
);
162 * Communication with the agent. Send the message header to the given socket in
165 * Return 0 on success or else a negative errno message of sendmsg() op.
167 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
168 uint32_t cmd
, uint32_t cmd_version
)
172 struct lttcomm_agent_hdr msg
;
176 memset(&msg
, 0, sizeof(msg
));
177 msg
.data_size
= htobe64(data_size
);
178 msg
.cmd
= htobe32(cmd
);
179 msg
.cmd_version
= htobe32(cmd_version
);
181 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
182 if (size
< sizeof(msg
)) {
193 * Communication call with the agent. Send the payload to the given socket. The
194 * header MUST be sent prior to this call.
196 * Return 0 on success or else a negative errno value of sendmsg() op.
198 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
207 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
219 * Communication call with the agent. Receive reply from the agent using the
222 * Return 0 on success or else a negative errno value from recvmsg() op.
224 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
232 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
244 * Internal event listing for a given app. Populate events.
246 * Return number of element in the list or else a negative LTTNG_ERR* code.
247 * On success, the caller is responsible for freeing the memory
248 * allocated for "events".
250 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
252 int ret
, i
, len
= 0, offset
= 0;
255 struct lttng_event
*tmp_events
= NULL
;
256 struct lttcomm_agent_list_reply
*reply
= NULL
;
257 struct lttcomm_agent_list_reply_hdr reply_hdr
;
263 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
266 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
271 /* Get list header so we know how much we'll receive. */
272 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
277 switch (be32toh(reply_hdr
.ret_code
)) {
278 case AGENT_RET_CODE_SUCCESS
:
279 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
282 ERR("Agent returned an unknown code: %" PRIu32
,
283 be32toh(reply_hdr
.ret_code
));
284 ret
= LTTNG_ERR_FATAL
;
288 reply
= zmalloc(data_size
);
290 ret
= LTTNG_ERR_NOMEM
;
294 /* Get the list with the appropriate data size. */
295 ret
= recv_reply(app
->sock
, reply
, data_size
);
300 nb_event
= be32toh(reply
->nb_event
);
301 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
303 ret
= LTTNG_ERR_NOMEM
;
307 for (i
= 0; i
< nb_event
; i
++) {
309 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
310 sizeof(tmp_events
[i
].name
))) {
311 ret
= LTTNG_ERR_INVALID
;
314 tmp_events
[i
].pid
= app
->pid
;
315 tmp_events
[i
].enabled
= -1;
316 len
= strlen(reply
->payload
+ offset
) + 1;
319 *events
= tmp_events
;
325 ret
= LTTNG_ERR_UST_LIST_FAIL
;
334 * Internal enable agent event on a agent application. This function
335 * communicates with the agent to enable a given event.
337 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
339 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
343 struct lttcomm_agent_enable msg
;
344 struct lttcomm_agent_generic_reply reply
;
350 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
351 app
->pid
, app
->sock
->fd
);
353 data_size
= sizeof(msg
);
355 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
360 memset(&msg
, 0, sizeof(msg
));
361 msg
.loglevel_value
= event
->loglevel_value
;
362 msg
.loglevel_type
= event
->loglevel_type
;
363 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
364 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
369 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
374 switch (be32toh(reply
.ret_code
)) {
375 case AGENT_RET_CODE_SUCCESS
:
377 case AGENT_RET_CODE_UNKNOWN_NAME
:
378 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
381 ERR("Agent returned an unknown code: %" PRIu32
,
382 be32toh(reply
.ret_code
));
383 ret
= LTTNG_ERR_FATAL
;
390 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
396 * Internal disable agent event call on a agent application. This function
397 * communicates with the agent to disable a given event.
399 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
401 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
405 struct lttcomm_agent_disable msg
;
406 struct lttcomm_agent_generic_reply reply
;
412 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
413 app
->pid
, app
->sock
->fd
);
415 data_size
= sizeof(msg
);
416 memset(&msg
, 0, sizeof(msg
));
417 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
418 ret
= LTTNG_ERR_INVALID
;
422 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
427 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
432 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
437 switch (be32toh(reply
.ret_code
)) {
438 case AGENT_RET_CODE_SUCCESS
:
440 case AGENT_RET_CODE_UNKNOWN_NAME
:
441 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
444 ERR("Agent returned an unknown code: %" PRIu32
,
445 be32toh(reply
.ret_code
));
446 ret
= LTTNG_ERR_FATAL
;
453 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
459 * Send back the registration DONE command to a given agent application.
461 * Return 0 on success or else a negative value.
463 int agent_send_registration_done(struct agent_app
*app
)
468 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
470 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
474 * Enable agent event on every agent applications registered with the session
477 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
479 int agent_enable_event(struct agent_event
*event
,
480 enum lttng_domain_type domain
)
483 struct agent_app
*app
;
484 struct lttng_ht_iter iter
;
490 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
492 if (app
->domain
!= domain
) {
496 /* Enable event on agent application through TCP socket. */
497 ret
= enable_event(app
, event
);
498 if (ret
!= LTTNG_OK
) {
512 * Disable agent event on every agent applications registered with the session
515 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
517 int agent_disable_event(struct agent_event
*event
,
518 enum lttng_domain_type domain
)
521 struct agent_app
*app
;
522 struct lttng_ht_iter iter
;
525 if (!event
->enabled
) {
531 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
533 if (app
->domain
!= domain
) {
537 /* Enable event on agent application through TCP socket. */
538 ret
= disable_event(app
, event
);
539 if (ret
!= LTTNG_OK
) {
553 * Ask every agent for the list of possible event. Events is allocated with the
554 * events of every agent application.
556 * Return the number of events or else a negative value.
558 int agent_list_events(struct lttng_event
**events
,
559 enum lttng_domain_type domain
)
562 size_t nbmem
, count
= 0;
563 struct agent_app
*app
;
564 struct lttng_event
*tmp_events
= NULL
;
565 struct lttng_ht_iter iter
;
569 DBG2("Agent listing events for domain %d", domain
);
571 nbmem
= UST_APP_EVENT_LIST_SIZE
;
572 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
574 PERROR("zmalloc agent list events");
580 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
583 struct lttng_event
*agent_events
;
585 /* Skip domain not asked by the list. */
586 if (app
->domain
!= domain
) {
590 nb_ev
= list_events(app
, &agent_events
);
596 if (count
+ nb_ev
> nbmem
) {
597 /* In case the realloc fails, we free the memory */
598 struct lttng_event
*new_tmp_events
;
601 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
602 DBG2("Reallocating agent event list from %zu to %zu entries",
604 new_tmp_events
= realloc(tmp_events
,
605 new_nbmem
* sizeof(*new_tmp_events
));
606 if (!new_tmp_events
) {
607 PERROR("realloc agent events");
612 /* Zero the new memory */
613 memset(new_tmp_events
+ nbmem
, 0,
614 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
616 tmp_events
= new_tmp_events
;
618 memcpy(tmp_events
+ count
, agent_events
,
619 nb_ev
* sizeof(*tmp_events
));
626 *events
= tmp_events
;
637 * Create a agent app object using the given PID.
639 * Return newly allocated object or else NULL on error.
641 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
642 struct lttcomm_sock
*sock
)
644 struct agent_app
*app
;
648 app
= zmalloc(sizeof(*app
));
650 PERROR("zmalloc agent create");
655 app
->domain
= domain
;
657 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
664 * Lookup agent app by socket in the global hash table.
666 * RCU read side lock MUST be acquired.
668 * Return object if found else NULL.
670 struct agent_app
*agent_find_app_by_sock(int sock
)
672 struct lttng_ht_node_ulong
*node
;
673 struct lttng_ht_iter iter
;
674 struct agent_app
*app
;
678 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
679 node
= lttng_ht_iter_get_node_ulong(&iter
);
683 app
= caa_container_of(node
, struct agent_app
, node
);
685 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
689 DBG3("Agent app NOT found by sock %d.", sock
);
694 * Add agent application object to the global hash table.
696 void agent_add_app(struct agent_app
*app
)
700 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
703 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
708 * Delete agent application from the global hash table.
710 * rcu_read_lock() must be held by the caller.
712 void agent_delete_app(struct agent_app
*app
)
715 struct lttng_ht_iter iter
;
719 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
721 iter
.iter
.node
= &app
->node
.node
;
722 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
727 * Destroy a agent application object by detaching it from its corresponding
728 * UST app if one is connected by closing the socket. Finally, perform a
729 * delayed memory reclaim.
731 void agent_destroy_app(struct agent_app
*app
)
736 app
->sock
->ops
->close(app
->sock
);
737 lttcomm_destroy_sock(app
->sock
);
740 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
744 * Initialize an already allocated agent object.
746 * Return 0 on success or else a negative errno value.
748 int agent_init(struct agent
*agt
)
754 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
759 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
768 * Add agent object to the given hash table.
770 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
775 DBG3("Agent adding from domain %d", agt
->domain
);
778 lttng_ht_add_unique_u64(ht
, &agt
->node
);
783 * Create an agent object for the given domain.
785 * Return the allocated agent or NULL on error.
787 struct agent
*agent_create(enum lttng_domain_type domain
)
792 agt
= zmalloc(sizeof(*agt
));
796 agt
->domain
= domain
;
798 ret
= agent_init(agt
);
810 * Create a newly allocated agent event data structure.
811 * Ownership of filter_expression is taken.
813 * Return a new object else NULL on error.
815 struct agent_event
*agent_create_event(const char *name
,
816 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
817 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
819 struct agent_event
*event
= NULL
;
821 DBG3("Agent create new event with name %s, loglevel type %d, \
822 loglevel value %d and filter %s",
823 name
, loglevel_type
, loglevel_value
,
824 filter_expression
? filter_expression
: "NULL");
827 ERR("Failed to create agent event; no name provided.");
831 event
= zmalloc(sizeof(*event
));
836 strncpy(event
->name
, name
, sizeof(event
->name
));
837 event
->name
[sizeof(event
->name
) - 1] = '\0';
838 lttng_ht_node_init_str(&event
->node
, event
->name
);
840 event
->loglevel_value
= loglevel_value
;
841 event
->loglevel_type
= loglevel_type
;
842 event
->filter
= filter
;
843 event
->filter_expression
= filter_expression
;
849 * Unique add of a agent event to an agent object.
851 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
857 DBG3("Agent adding event %s", event
->name
);
860 add_unique_agent_event(agt
->events
, event
);
866 * Find multiple agent events sharing the given name.
868 * RCU read side lock MUST be acquired. It must be held for the
869 * duration of the iteration.
871 * Sets the given iterator.
873 void agent_find_events_by_name(const char *name
, struct agent
*agt
,
874 struct lttng_ht_iter
* iter
)
877 struct agent_ht_key key
;
887 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
888 ht_match_event_by_name
, &key
, &iter
->iter
);
892 * Get the next agent event duplicate by name. This should be called
893 * after a call to agent_find_events_by_name() to iterate on events.
895 * The RCU read lock must be held during the iteration and for as long
896 * as the object the iterator points to remains in use.
898 void agent_event_next_duplicate(const char *name
,
899 struct agent
*agt
, struct lttng_ht_iter
* iter
)
901 struct agent_ht_key key
;
905 cds_lfht_next_duplicate(agt
->events
->ht
, ht_match_event_by_name
,
910 * Find a agent event in the given agent using name, loglevel and filter.
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
,
917 enum lttng_loglevel_type loglevel_type
, int loglevel_value
,
918 char *filter_expression
, struct agent
*agt
)
920 struct lttng_ht_node_str
*node
;
921 struct lttng_ht_iter iter
;
923 struct agent_ht_key key
;
931 key
.loglevel_value
= loglevel_value
;
932 key
.loglevel_type
= loglevel_type
;
933 key
.filter_expression
= filter_expression
;
935 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
936 ht_match_event
, &key
, &iter
.iter
);
937 node
= lttng_ht_iter_get_node_str(&iter
);
942 DBG3("Agent event found %s.", name
);
943 return caa_container_of(node
, struct agent_event
, node
);
946 DBG3("Agent event NOT found %s.", name
);
951 * Free given agent event. This event must not be globally visible at this
952 * point (only expected to be used on failure just after event creation). After
953 * this call, the pointer is not usable anymore.
955 void agent_destroy_event(struct agent_event
*event
)
960 free(event
->filter_expression
);
961 free(event
->exclusion
);
966 * Destroy an agent completely.
968 void agent_destroy(struct agent
*agt
)
970 struct lttng_ht_node_str
*node
;
971 struct lttng_ht_iter iter
;
975 DBG3("Agent destroy");
978 * Just ignore if no events hash table exists. This is possible if for
979 * instance an agent object was allocated but not initialized.
986 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
988 struct agent_event
*event
;
991 * When destroying an event, we have to try to disable it on the agent
992 * side so the event stops generating data. The return value is not
993 * important since we have to continue anyway destroying the object.
995 event
= caa_container_of(node
, struct agent_event
, node
);
996 (void) agent_disable_event(event
, agt
->domain
);
998 ret
= lttng_ht_del(agt
->events
, &iter
);
1000 call_rcu(&node
->head
, destroy_event_agent_rcu
);
1004 ht_cleanup_push(agt
->events
);
1009 * Allocate agent_apps_ht_by_sock.
1011 int agent_app_ht_alloc(void)
1015 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1016 if (!agent_apps_ht_by_sock
) {
1024 * Destroy a agent application by socket.
1026 void agent_destroy_app_by_sock(int sock
)
1028 struct agent_app
*app
;
1033 * Not finding an application is a very important error that should NEVER
1034 * happen. The hash table deletion is ONLY done through this call when the
1035 * main sessiond thread is torn down.
1038 app
= agent_find_app_by_sock(sock
);
1041 /* RCU read side lock is assumed to be held by this function. */
1042 agent_delete_app(app
);
1044 /* The application is freed in a RCU call but the socket is closed here. */
1045 agent_destroy_app(app
);
1050 * Clean-up the agent app hash table and destroy it.
1052 void agent_app_ht_clean(void)
1054 struct lttng_ht_node_ulong
*node
;
1055 struct lttng_ht_iter iter
;
1057 if (!agent_apps_ht_by_sock
) {
1061 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1062 struct agent_app
*app
;
1064 app
= caa_container_of(node
, struct agent_app
, node
);
1065 agent_destroy_app_by_sock(app
->sock
->fd
);
1069 lttng_ht_destroy(agent_apps_ht_by_sock
);
1073 * Update a agent application (given socket) using the given agent.
1075 * Note that this function is most likely to be used with a tracing session
1076 * thus the caller should make sure to hold the appropriate lock(s).
1078 void agent_update(struct agent
*agt
, int sock
)
1081 struct agent_app
*app
;
1082 struct agent_event
*event
;
1083 struct lttng_ht_iter iter
;
1088 DBG("Agent updating app socket %d", sock
);
1091 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1092 /* Skip event if disabled. */
1093 if (!event
->enabled
) {
1097 app
= agent_find_app_by_sock(sock
);
1099 * We are in the registration path thus if the application is gone,
1100 * there is a serious code flow error.
1104 ret
= enable_event(app
, event
);
1105 if (ret
!= LTTNG_OK
) {
1106 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1107 event
->name
, app
->pid
, app
->sock
->fd
);
1108 /* Let's try the others here and don't assume the app is dead. */