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
;
72 event
= caa_container_of(node
, struct agent_event
, node
.node
);
75 /* Match 2 elements of the key: name and loglevel. */
78 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
82 if (event
->loglevel_value
!= key
->loglevel_value
) {
83 if (event
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_ALL
&&
84 key
->loglevel_value
== 0 &&
85 event
->loglevel_value
== -1) {
98 * Add unique agent event based on the event name and loglevel.
100 static void add_unique_agent_event(struct lttng_ht
*ht
,
101 struct agent_event
*event
)
103 struct cds_lfht_node
*node_ptr
;
104 struct agent_ht_key key
;
110 key
.name
= event
->name
;
111 key
.loglevel_value
= event
->loglevel_value
;
113 node_ptr
= cds_lfht_add_unique(ht
->ht
,
114 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
115 ht_match_event
, &key
, &event
->node
.node
);
116 assert(node_ptr
== &event
->node
.node
);
120 * URCU delayed agent event reclaim.
122 static void destroy_event_agent_rcu(struct rcu_head
*head
)
124 struct lttng_ht_node_str
*node
=
125 caa_container_of(head
, struct lttng_ht_node_str
, head
);
126 struct agent_event
*event
=
127 caa_container_of(node
, struct agent_event
, node
);
129 agent_destroy_event(event
);
133 * URCU delayed agent app reclaim.
135 static void destroy_app_agent_rcu(struct rcu_head
*head
)
137 struct lttng_ht_node_ulong
*node
=
138 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
139 struct agent_app
*app
=
140 caa_container_of(node
, struct agent_app
, node
);
146 * Communication with the agent. Send the message header to the given socket in
149 * Return 0 on success or else a negative errno message of sendmsg() op.
151 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
152 uint32_t cmd
, uint32_t cmd_version
)
156 struct lttcomm_agent_hdr msg
;
160 memset(&msg
, 0, sizeof(msg
));
161 msg
.data_size
= htobe64(data_size
);
162 msg
.cmd
= htobe32(cmd
);
163 msg
.cmd_version
= htobe32(cmd_version
);
165 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
166 if (size
< sizeof(msg
)) {
177 * Communication call with the agent. Send the payload to the given socket. The
178 * header MUST be sent prior to this call.
180 * Return 0 on success or else a negative errno value of sendmsg() op.
182 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
191 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
203 * Communication call with the agent. Receive reply from the agent using the
206 * Return 0 on success or else a negative errno value from recvmsg() op.
208 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
216 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
228 * Internal event listing for a given app. Populate events.
230 * Return number of element in the list or else a negative LTTNG_ERR* code.
231 * On success, the caller is responsible for freeing the memory
232 * allocated for "events".
234 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
236 int ret
, i
, len
= 0, offset
= 0;
239 struct lttng_event
*tmp_events
= NULL
;
240 struct lttcomm_agent_list_reply
*reply
= NULL
;
241 struct lttcomm_agent_list_reply_hdr reply_hdr
;
247 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
250 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
255 /* Get list header so we know how much we'll receive. */
256 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
261 switch (be32toh(reply_hdr
.ret_code
)) {
262 case AGENT_RET_CODE_SUCCESS
:
263 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
266 ERR("Agent returned an unknown code: %" PRIu32
,
267 be32toh(reply_hdr
.ret_code
));
268 ret
= LTTNG_ERR_FATAL
;
272 reply
= zmalloc(data_size
);
274 ret
= LTTNG_ERR_NOMEM
;
278 /* Get the list with the appropriate data size. */
279 ret
= recv_reply(app
->sock
, reply
, data_size
);
284 nb_event
= be32toh(reply
->nb_event
);
285 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
287 ret
= LTTNG_ERR_NOMEM
;
291 for (i
= 0; i
< nb_event
; i
++) {
293 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
294 sizeof(tmp_events
[i
].name
));
295 tmp_events
[i
].pid
= app
->pid
;
296 tmp_events
[i
].enabled
= -1;
297 len
= strlen(reply
->payload
+ offset
) + 1;
300 *events
= tmp_events
;
306 ret
= LTTNG_ERR_UST_LIST_FAIL
;
315 * Internal enable agent event on a agent application. This function
316 * communicates with the agent to enable a given event.
318 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
320 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
324 struct lttcomm_agent_enable msg
;
325 struct lttcomm_agent_generic_reply reply
;
331 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
332 app
->pid
, app
->sock
->fd
);
334 data_size
= sizeof(msg
);
336 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
341 memset(&msg
, 0, sizeof(msg
));
342 msg
.loglevel_value
= event
->loglevel_value
;
343 msg
.loglevel_type
= event
->loglevel_type
;
344 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
345 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
350 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
355 switch (be32toh(reply
.ret_code
)) {
356 case AGENT_RET_CODE_SUCCESS
:
358 case AGENT_RET_CODE_UNKNOWN_NAME
:
359 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
362 ERR("Agent returned an unknown code: %" PRIu32
,
363 be32toh(reply
.ret_code
));
364 ret
= LTTNG_ERR_FATAL
;
371 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
377 * Internal disable agent event call on a agent application. This function
378 * communicates with the agent to disable a given event.
380 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
382 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
386 struct lttcomm_agent_disable msg
;
387 struct lttcomm_agent_generic_reply reply
;
393 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
394 app
->pid
, app
->sock
->fd
);
396 data_size
= sizeof(msg
);
398 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
403 memset(&msg
, 0, sizeof(msg
));
404 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
405 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
410 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
415 switch (be32toh(reply
.ret_code
)) {
416 case AGENT_RET_CODE_SUCCESS
:
418 case AGENT_RET_CODE_UNKNOWN_NAME
:
419 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
422 ERR("Agent returned an unknown code: %" PRIu32
,
423 be32toh(reply
.ret_code
));
424 ret
= LTTNG_ERR_FATAL
;
431 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
437 * Send back the registration DONE command to a given agent application.
439 * Return 0 on success or else a negative value.
441 int agent_send_registration_done(struct agent_app
*app
)
446 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
448 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
452 * Enable agent event on every agent applications registered with the session
455 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
457 int agent_enable_event(struct agent_event
*event
,
458 enum lttng_domain_type domain
)
461 struct agent_app
*app
;
462 struct lttng_ht_iter iter
;
468 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
470 if (app
->domain
!= domain
) {
474 /* Enable event on agent application through TCP socket. */
475 ret
= enable_event(app
, event
);
476 if (ret
!= LTTNG_OK
) {
490 * Disable agent event on every agent applications registered with the session
493 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
495 int agent_disable_event(struct agent_event
*event
,
496 enum lttng_domain_type domain
)
499 struct agent_app
*app
;
500 struct lttng_ht_iter iter
;
503 if (!event
->enabled
) {
509 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
511 if (app
->domain
!= domain
) {
515 /* Enable event on agent application through TCP socket. */
516 ret
= disable_event(app
, event
);
517 if (ret
!= LTTNG_OK
) {
531 * Ask every agent for the list of possible event. Events is allocated with the
532 * events of every agent application.
534 * Return the number of events or else a negative value.
536 int agent_list_events(struct lttng_event
**events
,
537 enum lttng_domain_type domain
)
540 size_t nbmem
, count
= 0;
541 struct agent_app
*app
;
542 struct lttng_event
*tmp_events
= NULL
;
543 struct lttng_ht_iter iter
;
547 DBG2("Agent listing events for domain %d", domain
);
549 nbmem
= UST_APP_EVENT_LIST_SIZE
;
550 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
552 PERROR("zmalloc agent list events");
558 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
561 struct lttng_event
*agent_events
;
563 /* Skip domain not asked by the list. */
564 if (app
->domain
!= domain
) {
568 nb_ev
= list_events(app
, &agent_events
);
574 if (count
+ nb_ev
> nbmem
) {
575 /* In case the realloc fails, we free the memory */
576 struct lttng_event
*new_tmp_events
;
579 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
580 DBG2("Reallocating agent event list from %zu to %zu entries",
582 new_tmp_events
= realloc(tmp_events
,
583 new_nbmem
* sizeof(*new_tmp_events
));
584 if (!new_tmp_events
) {
585 PERROR("realloc agent events");
590 /* Zero the new memory */
591 memset(new_tmp_events
+ nbmem
, 0,
592 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
594 tmp_events
= new_tmp_events
;
596 memcpy(tmp_events
+ count
, agent_events
,
597 nb_ev
* sizeof(*tmp_events
));
604 *events
= tmp_events
;
615 * Create a agent app object using the given PID.
617 * Return newly allocated object or else NULL on error.
619 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
620 struct lttcomm_sock
*sock
)
622 struct agent_app
*app
;
626 app
= zmalloc(sizeof(*app
));
628 PERROR("zmalloc agent create");
633 app
->domain
= domain
;
635 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
642 * Lookup agent app by socket in the global hash table.
644 * RCU read side lock MUST be acquired.
646 * Return object if found else NULL.
648 struct agent_app
*agent_find_app_by_sock(int sock
)
650 struct lttng_ht_node_ulong
*node
;
651 struct lttng_ht_iter iter
;
652 struct agent_app
*app
;
656 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
657 node
= lttng_ht_iter_get_node_ulong(&iter
);
661 app
= caa_container_of(node
, struct agent_app
, node
);
663 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
667 DBG3("Agent app NOT found by sock %d.", sock
);
672 * Add agent application object to the global hash table.
674 void agent_add_app(struct agent_app
*app
)
678 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
681 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
686 * Delete agent application from the global hash table.
688 * rcu_read_lock() must be held by the caller.
690 void agent_delete_app(struct agent_app
*app
)
693 struct lttng_ht_iter iter
;
697 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
699 iter
.iter
.node
= &app
->node
.node
;
700 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
705 * Destroy a agent application object by detaching it from its corresponding
706 * UST app if one is connected by closing the socket. Finally, perform a
707 * delayed memory reclaim.
709 void agent_destroy_app(struct agent_app
*app
)
714 app
->sock
->ops
->close(app
->sock
);
715 lttcomm_destroy_sock(app
->sock
);
718 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
722 * Initialize an already allocated agent object.
724 * Return 0 on success or else a negative errno value.
726 int agent_init(struct agent
*agt
)
732 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
737 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
746 * Add agent object to the given hash table.
748 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
753 DBG3("Agent adding from domain %d", agt
->domain
);
756 lttng_ht_add_unique_u64(ht
, &agt
->node
);
761 * Create an agent object for the given domain.
763 * Return the allocated agent or NULL on error.
765 struct agent
*agent_create(enum lttng_domain_type domain
)
770 agt
= zmalloc(sizeof(*agt
));
774 agt
->domain
= domain
;
776 ret
= agent_init(agt
);
788 * Create a newly allocated agent event data structure.
789 * Ownership of filter_expression is taken.
791 * Return a new object else NULL on error.
793 struct agent_event
*agent_create_event(const char *name
,
794 int loglevel_value
, enum lttng_loglevel_type loglevel_type
,
795 struct lttng_filter_bytecode
*filter
, char *filter_expression
)
797 struct agent_event
*event
= NULL
;
799 DBG3("Agent create new event with name %s", name
);
802 ERR("Failed to create agent event; no name provided.");
806 event
= zmalloc(sizeof(*event
));
811 strncpy(event
->name
, name
, sizeof(event
->name
));
812 event
->name
[sizeof(event
->name
) - 1] = '\0';
813 lttng_ht_node_init_str(&event
->node
, event
->name
);
815 event
->loglevel_value
= loglevel_value
;
816 event
->loglevel_type
= loglevel_type
;
817 event
->filter
= filter
;
818 event
->filter_expression
= filter_expression
;
824 * Unique add of a agent event to an agent object.
826 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
832 DBG3("Agent adding event %s", event
->name
);
835 add_unique_agent_event(agt
->events
, event
);
841 * Find a agent event in the given agent using name.
843 * RCU read side lock MUST be acquired.
845 * Return object if found else NULL.
847 struct agent_event
*agent_find_event_by_name(const char *name
,
850 struct lttng_ht_node_str
*node
;
851 struct lttng_ht_iter iter
;
853 struct agent_ht_key key
;
862 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
863 ht_match_event_by_name
, &key
, &iter
.iter
);
864 node
= lttng_ht_iter_get_node_str(&iter
);
869 DBG3("Agent event found %s by name.", name
);
870 return caa_container_of(node
, struct agent_event
, node
);
873 DBG3("Agent NOT found by name %s.", name
);
878 * Find a agent event in the given agent using name and loglevel.
880 * RCU read side lock MUST be acquired.
882 * Return object if found else NULL.
884 struct agent_event
*agent_find_event(const char *name
, int loglevel_value
,
887 struct lttng_ht_node_str
*node
;
888 struct lttng_ht_iter iter
;
890 struct agent_ht_key key
;
898 key
.loglevel_value
= loglevel_value
;
900 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
901 ht_match_event
, &key
, &iter
.iter
);
902 node
= lttng_ht_iter_get_node_str(&iter
);
907 DBG3("Agent event found %s.", name
);
908 return caa_container_of(node
, struct agent_event
, node
);
911 DBG3("Agent event NOT found %s.", name
);
916 * Free given agent event. This event must not be globally visible at this
917 * point (only expected to be used on failure just after event creation). After
918 * this call, the pointer is not usable anymore.
920 void agent_destroy_event(struct agent_event
*event
)
925 free(event
->filter_expression
);
926 free(event
->exclusion
);
931 * Destroy an agent completely.
933 void agent_destroy(struct agent
*agt
)
935 struct lttng_ht_node_str
*node
;
936 struct lttng_ht_iter iter
;
940 DBG3("Agent destroy");
943 * Just ignore if no events hash table exists. This is possible if for
944 * instance an agent object was allocated but not initialized.
951 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
953 struct agent_event
*event
;
956 * When destroying an event, we have to try to disable it on the agent
957 * side so the event stops generating data. The return value is not
958 * important since we have to continue anyway destroying the object.
960 event
= caa_container_of(node
, struct agent_event
, node
);
961 (void) agent_disable_event(event
, agt
->domain
);
963 ret
= lttng_ht_del(agt
->events
, &iter
);
965 call_rcu(&node
->head
, destroy_event_agent_rcu
);
969 ht_cleanup_push(agt
->events
);
974 * Allocate agent_apps_ht_by_sock.
976 int agent_app_ht_alloc(void)
980 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
981 if (!agent_apps_ht_by_sock
) {
989 * Destroy a agent application by socket.
991 void agent_destroy_app_by_sock(int sock
)
993 struct agent_app
*app
;
998 * Not finding an application is a very important error that should NEVER
999 * happen. The hash table deletion is ONLY done through this call when the
1000 * main sessiond thread is torn down.
1003 app
= agent_find_app_by_sock(sock
);
1006 /* RCU read side lock is assumed to be held by this function. */
1007 agent_delete_app(app
);
1009 /* The application is freed in a RCU call but the socket is closed here. */
1010 agent_destroy_app(app
);
1015 * Clean-up the agent app hash table and destroy it.
1017 void agent_app_ht_clean(void)
1019 struct lttng_ht_node_ulong
*node
;
1020 struct lttng_ht_iter iter
;
1022 if (!agent_apps_ht_by_sock
) {
1026 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1027 struct agent_app
*app
;
1029 app
= caa_container_of(node
, struct agent_app
, node
);
1030 agent_destroy_app_by_sock(app
->sock
->fd
);
1034 lttng_ht_destroy(agent_apps_ht_by_sock
);
1038 * Update a agent application (given socket) using the given agent.
1040 * Note that this function is most likely to be used with a tracing session
1041 * thus the caller should make sure to hold the appropriate lock(s).
1043 void agent_update(struct agent
*agt
, int sock
)
1046 struct agent_app
*app
;
1047 struct agent_event
*event
;
1048 struct lttng_ht_iter iter
;
1053 DBG("Agent updating app socket %d", sock
);
1056 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1057 /* Skip event if disabled. */
1058 if (!event
->enabled
) {
1062 app
= agent_find_app_by_sock(sock
);
1064 * We are in the registration path thus if the application is gone,
1065 * there is a serious code flow error.
1069 ret
= enable_event(app
, event
);
1070 if (ret
!= LTTNG_OK
) {
1071 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1072 event
->name
, app
->pid
, app
->sock
->fd
);
1073 /* Let's try the others here and don't assume the app is dead. */