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
!= key
->loglevel
) {
83 if (event
->loglevel_type
== LTTNG_EVENT_LOGLEVEL_ALL
&&
84 key
->loglevel
== 0 && event
->loglevel
== -1) {
97 * Add unique agent event based on the event name and loglevel.
99 static void add_unique_agent_event(struct lttng_ht
*ht
,
100 struct agent_event
*event
)
102 struct cds_lfht_node
*node_ptr
;
103 struct agent_ht_key key
;
109 key
.name
= event
->name
;
110 key
.loglevel
= event
->loglevel
;
112 node_ptr
= cds_lfht_add_unique(ht
->ht
,
113 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
114 ht_match_event
, &key
, &event
->node
.node
);
115 assert(node_ptr
== &event
->node
.node
);
119 * URCU delayed agent event reclaim.
121 static void destroy_event_agent_rcu(struct rcu_head
*head
)
123 struct lttng_ht_node_str
*node
=
124 caa_container_of(head
, struct lttng_ht_node_str
, head
);
125 struct agent_event
*event
=
126 caa_container_of(node
, struct agent_event
, node
);
132 * URCU delayed agent app reclaim.
134 static void destroy_app_agent_rcu(struct rcu_head
*head
)
136 struct lttng_ht_node_ulong
*node
=
137 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
138 struct agent_app
*app
=
139 caa_container_of(node
, struct agent_app
, node
);
145 * Communication with the agent. Send the message header to the given socket in
148 * Return 0 on success or else a negative errno message of sendmsg() op.
150 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
151 uint32_t cmd
, uint32_t cmd_version
)
155 struct lttcomm_agent_hdr msg
;
159 memset(&msg
, 0, sizeof(msg
));
160 msg
.data_size
= htobe64(data_size
);
161 msg
.cmd
= htobe32(cmd
);
162 msg
.cmd_version
= htobe32(cmd_version
);
164 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
165 if (size
< sizeof(msg
)) {
176 * Communication call with the agent. Send the payload to the given socket. The
177 * header MUST be sent prior to this call.
179 * Return 0 on success or else a negative errno value of sendmsg() op.
181 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
190 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
202 * Communication call with the agent. Receive reply from the agent using the
205 * Return 0 on success or else a negative errno value from recvmsg() op.
207 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
215 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
227 * Internal event listing for a given app. Populate events.
229 * Return number of element in the list or else a negative LTTNG_ERR* code.
230 * On success, the caller is responsible for freeing the memory
231 * allocated for "events".
233 static ssize_t
list_events(struct agent_app
*app
, struct lttng_event
**events
)
235 int ret
, i
, len
= 0, offset
= 0;
238 struct lttng_event
*tmp_events
= NULL
;
239 struct lttcomm_agent_list_reply
*reply
= NULL
;
240 struct lttcomm_agent_list_reply_hdr reply_hdr
;
246 DBG2("Agent listing events for app pid: %d and socket %d", app
->pid
,
249 ret
= send_header(app
->sock
, 0, AGENT_CMD_LIST
, 0);
254 /* Get list header so we know how much we'll receive. */
255 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
260 switch (be32toh(reply_hdr
.ret_code
)) {
261 case AGENT_RET_CODE_SUCCESS
:
262 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
265 ERR("Agent returned an unknown code: %" PRIu32
,
266 be32toh(reply_hdr
.ret_code
));
267 ret
= LTTNG_ERR_FATAL
;
271 reply
= zmalloc(data_size
);
273 ret
= LTTNG_ERR_NOMEM
;
277 /* Get the list with the appropriate data size. */
278 ret
= recv_reply(app
->sock
, reply
, data_size
);
283 nb_event
= be32toh(reply
->nb_event
);
284 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
286 ret
= LTTNG_ERR_NOMEM
;
290 for (i
= 0; i
< nb_event
; i
++) {
292 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
293 sizeof(tmp_events
[i
].name
));
294 tmp_events
[i
].pid
= app
->pid
;
295 tmp_events
[i
].enabled
= -1;
296 len
= strlen(reply
->payload
+ offset
) + 1;
299 *events
= tmp_events
;
305 ret
= LTTNG_ERR_UST_LIST_FAIL
;
314 * Internal enable agent event on a agent application. This function
315 * communicates with the agent to enable a given event.
317 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
319 static int enable_event(struct agent_app
*app
, struct agent_event
*event
)
323 struct lttcomm_agent_enable msg
;
324 struct lttcomm_agent_generic_reply reply
;
330 DBG2("Agent enabling event %s for app pid: %d and socket %d", event
->name
,
331 app
->pid
, app
->sock
->fd
);
333 data_size
= sizeof(msg
);
335 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_ENABLE
, 0);
340 memset(&msg
, 0, sizeof(msg
));
341 msg
.loglevel
= event
->loglevel
;
342 msg
.loglevel_type
= event
->loglevel_type
;
343 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
344 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
349 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
354 switch (be32toh(reply
.ret_code
)) {
355 case AGENT_RET_CODE_SUCCESS
:
357 case AGENT_RET_CODE_UNKNOWN_NAME
:
358 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
361 ERR("Agent returned an unknown code: %" PRIu32
,
362 be32toh(reply
.ret_code
));
363 ret
= LTTNG_ERR_FATAL
;
370 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
376 * Internal disable agent event call on a agent application. This function
377 * communicates with the agent to disable a given event.
379 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
381 static int disable_event(struct agent_app
*app
, struct agent_event
*event
)
385 struct lttcomm_agent_disable msg
;
386 struct lttcomm_agent_generic_reply reply
;
392 DBG2("Agent disabling event %s for app pid: %d and socket %d", event
->name
,
393 app
->pid
, app
->sock
->fd
);
395 data_size
= sizeof(msg
);
397 ret
= send_header(app
->sock
, data_size
, AGENT_CMD_DISABLE
, 0);
402 memset(&msg
, 0, sizeof(msg
));
403 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
404 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
409 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
414 switch (be32toh(reply
.ret_code
)) {
415 case AGENT_RET_CODE_SUCCESS
:
417 case AGENT_RET_CODE_UNKNOWN_NAME
:
418 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
421 ERR("Agent returned an unknown code: %" PRIu32
,
422 be32toh(reply
.ret_code
));
423 ret
= LTTNG_ERR_FATAL
;
430 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
436 * Send back the registration DONE command to a given agent application.
438 * Return 0 on success or else a negative value.
440 int agent_send_registration_done(struct agent_app
*app
)
445 DBG("Agent sending registration done to app socket %d", app
->sock
->fd
);
447 return send_header(app
->sock
, 0, AGENT_CMD_REG_DONE
, 0);
451 * Enable agent event on every agent applications registered with the session
454 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
456 int agent_enable_event(struct agent_event
*event
,
457 enum lttng_domain_type domain
)
460 struct agent_app
*app
;
461 struct lttng_ht_iter iter
;
467 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
469 if (app
->domain
!= domain
) {
473 /* Enable event on agent application through TCP socket. */
474 ret
= enable_event(app
, event
);
475 if (ret
!= LTTNG_OK
) {
489 * Disable agent event on every agent applications registered with the session
492 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
494 int agent_disable_event(struct agent_event
*event
,
495 enum lttng_domain_type domain
)
498 struct agent_app
*app
;
499 struct lttng_ht_iter iter
;
502 if (!event
->enabled
) {
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
= disable_event(app
, event
);
516 if (ret
!= LTTNG_OK
) {
530 * Ask every agent for the list of possible event. Events is allocated with the
531 * events of every agent application.
533 * Return the number of events or else a negative value.
535 int agent_list_events(struct lttng_event
**events
,
536 enum lttng_domain_type domain
)
539 size_t nbmem
, count
= 0;
540 struct agent_app
*app
;
541 struct lttng_event
*tmp_events
= NULL
;
542 struct lttng_ht_iter iter
;
546 DBG2("Agent listing events for domain %d", domain
);
548 nbmem
= UST_APP_EVENT_LIST_SIZE
;
549 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
551 PERROR("zmalloc agent list events");
557 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
560 struct lttng_event
*agent_events
;
562 /* Skip domain not asked by the list. */
563 if (app
->domain
!= domain
) {
567 nb_ev
= list_events(app
, &agent_events
);
573 if (count
+ nb_ev
> nbmem
) {
574 /* In case the realloc fails, we free the memory */
575 struct lttng_event
*new_tmp_events
;
578 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
579 DBG2("Reallocating agent event list from %zu to %zu entries",
581 new_tmp_events
= realloc(tmp_events
,
582 new_nbmem
* sizeof(*new_tmp_events
));
583 if (!new_tmp_events
) {
584 PERROR("realloc agent events");
589 /* Zero the new memory */
590 memset(new_tmp_events
+ nbmem
, 0,
591 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
593 tmp_events
= new_tmp_events
;
595 memcpy(tmp_events
+ count
, agent_events
,
596 nb_ev
* sizeof(*tmp_events
));
603 *events
= tmp_events
;
614 * Create a agent app object using the given PID.
616 * Return newly allocated object or else NULL on error.
618 struct agent_app
*agent_create_app(pid_t pid
, enum lttng_domain_type domain
,
619 struct lttcomm_sock
*sock
)
621 struct agent_app
*app
;
625 app
= zmalloc(sizeof(*app
));
627 PERROR("zmalloc agent create");
632 app
->domain
= domain
;
634 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
641 * Lookup agent app by socket in the global hash table.
643 * RCU read side lock MUST be acquired.
645 * Return object if found else NULL.
647 struct agent_app
*agent_find_app_by_sock(int sock
)
649 struct lttng_ht_node_ulong
*node
;
650 struct lttng_ht_iter iter
;
651 struct agent_app
*app
;
655 lttng_ht_lookup(agent_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
656 node
= lttng_ht_iter_get_node_ulong(&iter
);
660 app
= caa_container_of(node
, struct agent_app
, node
);
662 DBG3("Agent app pid %d found by sock %d.", app
->pid
, sock
);
666 DBG3("Agent app NOT found by sock %d.", sock
);
671 * Add agent application object to the global hash table.
673 void agent_add_app(struct agent_app
*app
)
677 DBG3("Agent adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
680 lttng_ht_add_unique_ulong(agent_apps_ht_by_sock
, &app
->node
);
685 * Delete agent application from the global hash table.
687 * rcu_read_lock() must be held by the caller.
689 void agent_delete_app(struct agent_app
*app
)
692 struct lttng_ht_iter iter
;
696 DBG3("Agent deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
698 iter
.iter
.node
= &app
->node
.node
;
699 ret
= lttng_ht_del(agent_apps_ht_by_sock
, &iter
);
704 * Destroy a agent application object by detaching it from its corresponding
705 * UST app if one is connected by closing the socket. Finally, perform a
706 * delayed memory reclaim.
708 void agent_destroy_app(struct agent_app
*app
)
713 app
->sock
->ops
->close(app
->sock
);
714 lttcomm_destroy_sock(app
->sock
);
717 call_rcu(&app
->node
.head
, destroy_app_agent_rcu
);
721 * Initialize an already allocated agent object.
723 * Return 0 on success or else a negative errno value.
725 int agent_init(struct agent
*agt
)
731 agt
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
736 lttng_ht_node_init_u64(&agt
->node
, agt
->domain
);
745 * Add agent object to the given hash table.
747 void agent_add(struct agent
*agt
, struct lttng_ht
*ht
)
752 DBG3("Agent adding from domain %d", agt
->domain
);
755 lttng_ht_add_unique_u64(ht
, &agt
->node
);
760 * Create an agent object for the given domain.
762 * Return the allocated agent or NULL on error.
764 struct agent
*agent_create(enum lttng_domain_type domain
)
769 agt
= zmalloc(sizeof(*agt
));
773 agt
->domain
= domain
;
775 ret
= agent_init(agt
);
787 * Create a newly allocated agent event data structure. If name is valid, it's
788 * copied into the created event.
790 * Return a new object else NULL on error.
792 struct agent_event
*agent_create_event(const char *name
,
793 struct lttng_filter_bytecode
*filter
)
795 struct agent_event
*event
;
797 DBG3("Agent create new event with name %s", name
);
799 event
= zmalloc(sizeof(*event
));
805 strncpy(event
->name
, name
, sizeof(event
->name
));
806 event
->name
[sizeof(event
->name
) - 1] = '\0';
807 lttng_ht_node_init_str(&event
->node
, event
->name
);
811 event
->filter
= filter
;
819 * Unique add of a agent event to an agent object.
821 void agent_add_event(struct agent_event
*event
, struct agent
*agt
)
827 DBG3("Agent adding event %s", event
->name
);
830 add_unique_agent_event(agt
->events
, event
);
836 * Find a agent event in the given agent using name.
838 * RCU read side lock MUST be acquired.
840 * Return object if found else NULL.
842 struct agent_event
*agent_find_event_by_name(const char *name
,
845 struct lttng_ht_node_str
*node
;
846 struct lttng_ht_iter iter
;
848 struct agent_ht_key key
;
857 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
858 ht_match_event_by_name
, &key
, &iter
.iter
);
859 node
= lttng_ht_iter_get_node_str(&iter
);
864 DBG3("Agent event found %s by name.", name
);
865 return caa_container_of(node
, struct agent_event
, node
);
868 DBG3("Agent NOT found by name %s.", name
);
873 * Find a agent event in the given agent using name and loglevel.
875 * RCU read side lock MUST be acquired.
877 * Return object if found else NULL.
879 struct agent_event
*agent_find_event(const char *name
, int loglevel
,
882 struct lttng_ht_node_str
*node
;
883 struct lttng_ht_iter iter
;
885 struct agent_ht_key key
;
893 key
.loglevel
= loglevel
;
895 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
896 ht_match_event
, &key
, &iter
.iter
);
897 node
= lttng_ht_iter_get_node_str(&iter
);
902 DBG3("Agent event found %s.", name
);
903 return caa_container_of(node
, struct agent_event
, node
);
906 DBG3("Agent event NOT found %s.", name
);
911 * Free given agent event. This event must not be globally visible at this
912 * point (only expected to be used on failure just after event creation). After
913 * this call, the pointer is not usable anymore.
915 void agent_destroy_event(struct agent_event
*event
)
920 free(event
->filter_expression
);
925 * Destroy an agent completely. Note that the given pointer is NOT freed
926 * thus a reference to static or stack data can be passed to this function.
928 void agent_destroy(struct agent
*agt
)
930 struct lttng_ht_node_str
*node
;
931 struct lttng_ht_iter iter
;
935 DBG3("Agent destroy");
938 * Just ignore if no events hash table exists. This is possible if for
939 * instance an agent object was allocated but not initialized.
946 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, node
, node
) {
948 struct agent_event
*event
;
951 * When destroying an event, we have to try to disable it on the agent
952 * side so the event stops generating data. The return value is not
953 * important since we have to continue anyway destroying the object.
955 event
= caa_container_of(node
, struct agent_event
, node
);
956 (void) agent_disable_event(event
, agt
->domain
);
958 ret
= lttng_ht_del(agt
->events
, &iter
);
960 call_rcu(&node
->head
, destroy_event_agent_rcu
);
964 ht_cleanup_push(agt
->events
);
968 * Allocate agent_apps_ht_by_sock.
970 int agent_app_ht_alloc(void)
974 agent_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
975 if (!agent_apps_ht_by_sock
) {
983 * Destroy a agent application by socket.
985 void agent_destroy_app_by_sock(int sock
)
987 struct agent_app
*app
;
992 * Not finding an application is a very important error that should NEVER
993 * happen. The hash table deletion is ONLY done through this call when the
994 * main sessiond thread is torn down.
997 app
= agent_find_app_by_sock(sock
);
1000 /* RCU read side lock is assumed to be held by this function. */
1001 agent_delete_app(app
);
1003 /* The application is freed in a RCU call but the socket is closed here. */
1004 agent_destroy_app(app
);
1009 * Clean-up the agent app hash table and destroy it.
1011 void agent_app_ht_clean(void)
1013 struct lttng_ht_node_ulong
*node
;
1014 struct lttng_ht_iter iter
;
1016 if (!agent_apps_ht_by_sock
) {
1020 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1021 struct agent_app
*app
;
1023 app
= caa_container_of(node
, struct agent_app
, node
);
1024 agent_destroy_app_by_sock(app
->sock
->fd
);
1028 lttng_ht_destroy(agent_apps_ht_by_sock
);
1032 * Update a agent application (given socket) using the given agent.
1034 * Note that this function is most likely to be used with a tracing session
1035 * thus the caller should make sure to hold the appropriate lock(s).
1037 void agent_update(struct agent
*agt
, int sock
)
1040 struct agent_app
*app
;
1041 struct agent_event
*event
;
1042 struct lttng_ht_iter iter
;
1047 DBG("Agent updating app socket %d", sock
);
1050 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
1051 /* Skip event if disabled. */
1052 if (!event
->enabled
) {
1056 app
= agent_find_app_by_sock(sock
);
1058 * We are in the registration path thus if the application is gone,
1059 * there is a serious code flow error.
1063 ret
= enable_event(app
, event
);
1064 if (ret
!= LTTNG_OK
) {
1065 DBG2("Agent update unable to enable event %s on app pid: %d sock %d",
1066 event
->name
, app
->pid
, app
->sock
->fd
);
1067 /* Let's try the others here and don't assume the app is dead. */