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.
20 #include <urcu/uatomic.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/agent.h>
25 #include <common/compat/endian.h>
32 * Match function for the events hash table lookup by name.
34 static int ht_match_event_by_name(struct cds_lfht_node
*node
,
37 struct agent_event
*event
;
38 const struct agent_ht_key
*key
;
43 event
= caa_container_of(node
, struct agent_event
, node
.node
);
46 /* Match 1 elements of the key: name. */
49 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
60 * Match function for the events hash table lookup by name and loglevel.
62 static int ht_match_event(struct cds_lfht_node
*node
,
65 struct agent_event
*event
;
66 const struct agent_ht_key
*key
;
71 event
= caa_container_of(node
, struct agent_event
, node
.node
);
74 /* Match 2 elements of the key: name and loglevel. */
77 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
81 if (event
->loglevel
!= key
->loglevel
) {
82 if (event
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_ALL
&&
83 key
->loglevel
== 0 && event
->loglevel
== -1) {
96 * Add unique agent event based on the event name and loglevel.
98 static void add_unique_agent_event(struct lttng_ht
*ht
,
99 struct agent_event
*event
)
101 struct cds_lfht_node
*node_ptr
;
102 struct agent_ht_key key
;
108 key
.name
= event
->name
;
109 key
.loglevel
= event
->loglevel
;
111 node_ptr
= cds_lfht_add_unique(ht
->ht
,
112 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
113 ht_match_event
, &key
, &event
->node
.node
);
114 assert(node_ptr
== &event
->node
.node
);
118 * URCU delayed agent event reclaim.
120 static void destroy_event_agent_rcu(struct rcu_head
*head
)
122 struct lttng_ht_node_str
*node
=
123 caa_container_of(head
, struct lttng_ht_node_str
, head
);
124 struct agent_event
*event
=
125 caa_container_of(node
, struct agent_event
, node
);
127 agent_destroy_event(event
);
131 * URCU delayed agent app reclaim.
133 static void destroy_app_agent_rcu(struct rcu_head
*head
)
135 struct lttng_ht_node_ulong
*node
=
136 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
137 struct agent_app
*app
=
138 caa_container_of(node
, struct agent_app
, node
);
144 * Communication with the agent. Send the message header to the given socket in
147 * Return 0 on success or else a negative errno message of sendmsg() op.
149 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
150 uint32_t cmd
, uint32_t cmd_version
)
154 struct lttcomm_agent_hdr msg
;
158 memset(&msg
, 0, sizeof(msg
));
159 msg
.data_size
= htobe64(data_size
);
160 msg
.cmd
= htobe32(cmd
);
161 msg
.cmd_version
= htobe32(cmd_version
);
163 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
164 if (size
< sizeof(msg
)) {
175 * Communication call with the agent. Send the payload to the given socket. The
176 * header MUST be sent prior to this call.
178 * Return 0 on success or else a negative errno value of sendmsg() op.
180 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
189 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
201 * Communication call with the agent. Receive reply from the agent using the
204 * Return 0 on success or else a negative errno value from recvmsg() op.
206 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
214 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
226 * Internal event listing for a given app. Populate events.
228 * Return number of element in the list or else a negative LTTNG_ERR* code.
229 * On success, the caller is responsible for freeing the memory
230 * allocated for "events".
232 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
234 int ret
, i
, len
= 0, offset
= 0;
237 struct lttng_event
*tmp_events
= NULL
;
238 struct lttcomm_agent_list_reply
*reply
= NULL
;
239 struct lttcomm_agent_list_reply_hdr reply_hdr
;
245 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
248 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
253 /* Get list header so we know how much we'll receive. */
254 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
259 switch (be32toh(reply_hdr
.ret_code
)) {
260 case AGENT_RET_CODE_SUCCESS
:
261 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
264 ERR("Agent returned an unknown code: %" PRIu32
,
265 be32toh(reply_hdr
.ret_code
));
266 ret
= LTTNG_ERR_FATAL
;
270 reply
= zmalloc(data_size
);
272 ret
= LTTNG_ERR_NOMEM
;
276 /* Get the list with the appropriate data size. */
277 ret
= recv_reply(app
->sock
, reply
, data_size
);
282 nb_event
= be32toh(reply
->nb_event
);
283 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
285 ret
= LTTNG_ERR_NOMEM
;
289 for (i
= 0; i
< nb_event
; i
++) {
291 if (lttng_strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
292 sizeof(tmp_events
[i
].name
))) {
293 ret
= LTTNG_ERR_INVALID
;
296 tmp_events
[i
].pid
= app
->pid
;
297 tmp_events
[i
].enabled
= -1;
298 len
= strlen(reply
->payload
+ offset
) + 1;
301 *events
= tmp_events
;
307 ret
= LTTNG_ERR_UST_LIST_FAIL
;
316 * Internal enable agent event on a agent application. This function
317 * communicates with the agent to enable a given event.
319 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
321 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
325 struct lttcomm_agent_enable msg
;
326 struct lttcomm_agent_generic_reply reply
;
332 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
333 app
->pid
, app
->sock
->fd
);
335 data_size
= sizeof(msg
);
337 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
342 memset(&msg
, 0, sizeof(msg
));
343 msg
.loglevel
= event
->loglevel
;
344 msg
.loglevel_type
= event
->loglevel_type
;
345 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
346 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
351 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
356 switch (be32toh(reply
.ret_code
)) {
357 case AGENT_RET_CODE_SUCCESS
:
359 case AGENT_RET_CODE_UNKNOWN_NAME
:
360 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
363 ERR("Agent returned an unknown code: %" PRIu32
,
364 be32toh(reply
.ret_code
));
365 ret
= LTTNG_ERR_FATAL
;
372 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
378 * Internal disable agent event call on a agent application. This function
379 * communicates with the agent to disable a given event.
381 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
383 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
387 struct lttcomm_agent_disable msg
;
388 struct lttcomm_agent_generic_reply reply
;
394 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
395 app
->pid
, app
->sock
->fd
);
397 data_size
= sizeof(msg
);
398 memset(&msg
, 0, sizeof(msg
));
399 if (lttng_strncpy(msg
.name
, event
->name
, sizeof(msg
.name
))) {
400 ret
= LTTNG_ERR_INVALID
;
404 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
409 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
414 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
419 switch (be32toh(reply
.ret_code
)) {
420 case AGENT_RET_CODE_SUCCESS
:
422 case AGENT_RET_CODE_UNKNOWN_NAME
:
423 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
426 ERR("Agent returned an unknown code: %" PRIu32
,
427 be32toh(reply
.ret_code
));
428 ret
= LTTNG_ERR_FATAL
;
435 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
441 * Send back the registration DONE command to a given agent application.
443 * Return 0 on success or else a negative value.
445 int agent_send_registration_done(struct agent_app
*app
)
450 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
452 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 1);
456 * Enable agent event on every agent applications registered with the session
459 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
461 int agent_enable_event(struct agent_event
*event
,
462 enum lttng_domain_type domain
)
465 struct agent_app
*app
;
466 struct lttng_ht_iter iter
;
472 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
474 if (app
->domain
!= domain
) {
478 /* Enable event on agent application through TCP socket. */
479 ret
= enable_event(app
, event
);
480 if (ret
!= LTTNG_OK
) {
494 * Disable agent event on every agent applications registered with the session
497 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
499 int agent_disable_event(struct agent_event
*event
,
500 enum lttng_domain_type domain
)
503 struct agent_app
*app
;
504 struct lttng_ht_iter iter
;
507 if (!event
->enabled
) {
513 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
515 if (app
->domain
!= domain
) {
519 /* Enable event on agent application through TCP socket. */
520 ret
= disable_event(app
, event
);
521 if (ret
!= LTTNG_OK
) {
535 * Ask every agent for the list of possible event. Events is allocated with the
536 * events of every agent application.
538 * Return the number of events or else a negative value.
540 int agent_list_events(struct lttng_event
**events
,
541 enum lttng_domain_type domain
)
544 size_t nbmem
, count
= 0;
545 struct agent_app
*app
;
546 struct lttng_event
*tmp_events
= NULL
;
547 struct lttng_ht_iter iter
;
551 nbmem
= UST_APP_EVENT_LIST_SIZE
;
552 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
554 PERROR("zmalloc agent list events");
560 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
563 struct lttng_event
*agent_events
;
565 /* Skip domain not asked by the list. */
566 if (app
->domain
!= domain
) {
570 nb_ev
= list_events(app
, &agent_events
);
576 if (count
+ nb_ev
> nbmem
) {
577 /* In case the realloc fails, we free the memory */
578 struct lttng_event
*new_tmp_events
;
581 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
582 DBG2("Reallocating agent event list from %zu to %zu entries",
584 new_tmp_events
= realloc(tmp_events
,
585 new_nbmem
* sizeof(*new_tmp_events
));
586 if (!new_tmp_events
) {
587 PERROR("realloc agent events");
592 /* Zero the new memory */
593 memset(new_tmp_events
+ nbmem
, 0,
594 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
596 tmp_events
= new_tmp_events
;
598 memcpy(tmp_events
+ count
, agent_events
,
599 nb_ev
* sizeof(*tmp_events
));
606 *events
= tmp_events
;
617 * Create a agent app object using the given PID.
619 * Return newly allocated object or else NULL on error.
621 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
622 struct lttcomm_sock
*sock
)
624 struct agent_app
*app
;
628 app
= zmalloc(sizeof(*app
));
630 PERROR("zmalloc agent create");
635 app
->domain
= domain
;
637 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
644 * Lookup agent app by socket in the global hash table.
646 * RCU read side lock MUST be acquired.
648 * Return object if found else NULL.
650 struct agent_app
*agent_find_app_by_sock(int sock
)
652 struct lttng_ht_node_ulong
*node
;
653 struct lttng_ht_iter iter
;
654 struct agent_app
*app
;
658 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
659 node
= lttng_ht_iter_get_node_ulong(&iter
);
663 app
= caa_container_of(node
, struct agent_app
, node
);
665 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
669 DBG3("Agent app NOT found by sock %d.", sock
);
674 * Add agent application object to the global hash table.
676 void agent_add_app(struct agent_app
*app
)
680 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
683 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
688 * Delete agent application from the global hash table.
690 * rcu_read_lock() must be held by the caller.
692 void agent_delete_app(struct agent_app
*app
)
695 struct lttng_ht_iter iter
;
699 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
701 iter
.iter
.node
= &app
->node
.node
;
702 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
707 * Destroy a agent application object by detaching it from its corresponding
708 * UST app if one is connected by closing the socket. Finally, perform a
709 * delayed memory reclaim.
711 void agent_destroy_app(struct agent_app
*app
)
716 app
->sock
->ops
->close(app
->sock
);
717 lttcomm_destroy_sock(app
->sock
);
720 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
724 * Initialize an already allocated agent object.
726 * Return 0 on success or else a negative errno value.
728 int agent_init(struct agent
*agt
)
734 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
739 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
748 * Add agent object to the given hash table.
750 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
755 DBG3("Agent adding from domain %d", agt
->domain
);
758 lttng_ht_add_unique_u64(ht
, &agt
->node
);
763 * Create an agent object for the given domain.
765 * Return the allocated agent or NULL on error.
767 struct agent
*agent_create(enum lttng_domain_type domain
)
772 agt
= zmalloc(sizeof(*agt
));
776 agt
->domain
= domain
;
778 ret
= agent_init(agt
);
790 * Create a newly allocated agent event data structure. If name is valid, it's
791 * copied into the created event.
793 * Return a new object else NULL on error.
795 struct agent_event
*agent_create_event(const char *name
,
796 struct lttng_filter_bytecode
*filter
)
798 struct agent_event
*event
;
800 DBG3("Agent create new event with name %s", name
);
802 event
= zmalloc(sizeof(*event
));
808 strncpy(event
->name
, name
, sizeof(event
->name
));
809 event
->name
[sizeof(event
->name
) - 1] = '\0';
810 lttng_ht_node_init_str(&event
->node
, event
->name
);
814 event
->filter
= filter
;
822 * Unique add of a agent event to an agent object.
824 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
830 DBG3("Agent adding event %s", event
->name
);
833 add_unique_agent_event(agt
->events
, event
);
839 * Find a agent event in the given agent using name.
841 * RCU read side lock MUST be acquired.
843 * Return object if found else NULL.
845 struct agent_event
*agent_find_event_by_name(const char *name
,
848 struct lttng_ht_node_str
*node
;
849 struct lttng_ht_iter iter
;
851 struct agent_ht_key key
;
860 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
861 ht_match_event_by_name
, &key
, &iter
.iter
);
862 node
= lttng_ht_iter_get_node_str(&iter
);
867 DBG3("Agent event found %s by name.", name
);
868 return caa_container_of(node
, struct agent_event
, node
);
871 DBG3("Agent NOT found by name %s.", name
);
876 * Find a agent event in the given agent using name and loglevel.
878 * RCU read side lock MUST be acquired.
880 * Return object if found else NULL.
882 struct agent_event
*agent_find_event(const char *name
, int loglevel
,
885 struct lttng_ht_node_str
*node
;
886 struct lttng_ht_iter iter
;
888 struct agent_ht_key key
;
896 key
.loglevel
= loglevel
;
898 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
899 ht_match_event
, &key
, &iter
.iter
);
900 node
= lttng_ht_iter_get_node_str(&iter
);
905 DBG3("Agent event found %s.", name
);
906 return caa_container_of(node
, struct agent_event
, node
);
909 DBG3("Agent event NOT found %s.", name
);
914 * Free given agent event. This event must not be globally visible at this
915 * point (only expected to be used on failure just after event creation). After
916 * this call, the pointer is not usable anymore.
918 void agent_destroy_event(struct agent_event
*event
)
923 free(event
->filter_expression
);
928 * Destroy an agent completely.
930 void agent_destroy(struct agent
*agt
)
932 struct lttng_ht_node_str
*node
;
933 struct lttng_ht_iter iter
;
937 DBG3("Agent destroy");
940 * Just ignore if no events hash table exists. This is possible if for
941 * instance an agent object was allocated but not initialized.
948 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
950 struct agent_event
*event
;
953 * When destroying an event, we have to try to disable it on the agent
954 * side so the event stops generating data. The return value is not
955 * important since we have to continue anyway destroying the object.
957 event
= caa_container_of(node
, struct agent_event
, node
);
958 (void) agent_disable_event(event
, agt
->domain
);
960 ret
= lttng_ht_del(agt
->events
, &iter
);
962 call_rcu(&node
->head
, destroy_event_agent_rcu
);
966 ht_cleanup_push(agt
->events
);
971 * Allocate agent_apps_ht_by_sock.
973 int agent_app_ht_alloc(void)
977 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
978 if (!agent_apps_ht_by_sock
) {
986 * Destroy a agent application by socket.
988 void agent_destroy_app_by_sock(int sock
)
990 struct agent_app
*app
;
995 * Not finding an application is a very important error that should NEVER
996 * happen. The hash table deletion is ONLY done through this call when the
997 * main sessiond thread is torn down.
1000 app
= agent_find_app_by_sock(sock
);
1003 /* RCU read side lock is assumed to be held by this function. */
1004 agent_delete_app(app
);
1006 /* The application is freed in a RCU call but the socket is closed here. */
1007 agent_destroy_app(app
);
1012 * Clean-up the agent app hash table and destroy it.
1014 void agent_app_ht_clean(void)
1016 struct lttng_ht_node_ulong
*node
;
1017 struct lttng_ht_iter iter
;
1019 if (!agent_apps_ht_by_sock
) {
1023 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1024 struct agent_app
*app
;
1026 app
= caa_container_of(node
, struct agent_app
, node
);
1027 agent_destroy_app_by_sock(app
->sock
->fd
);
1031 lttng_ht_destroy(agent_apps_ht_by_sock
);
1035 * Update a agent application (given socket) using the given agent.
1037 * Note that this function is most likely to be used with a tracing session
1038 * thus the caller should make sure to hold the appropriate lock(s).
1040 void agent_update(struct agent
*agt
, int sock
)
1043 struct agent_app
*app
;
1044 struct agent_event
*event
;
1045 struct lttng_ht_iter iter
;
1050 DBG("Agent updating app socket %d", sock
);
1053 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1054 /* Skip event if disabled. */
1055 if (!event
->enabled
) {
1059 app
= agent_find_app_by_sock(sock
);
1061 * We are in the registration path thus if the application is gone,
1062 * there is a serious code flow error.
1066 ret
= enable_event(app
, event
);
1067 if (ret
!= LTTNG_OK
) {
1068 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1069 event
->name
, app
->pid
, app
->sock
->fd
);
1070 /* Let's try the others here and don't assume the app is dead. */