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/jul.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 jul_event
*event
;
38 const struct jul_ht_key
*key
;
43 event
= caa_container_of(node
, struct jul_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 jul_event
*event
;
66 const struct jul_ht_key
*key
;
71 event
= caa_container_of(node
, struct jul_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 JUL event based on the event name and loglevel.
98 static void add_unique_jul_event(struct lttng_ht
*ht
, struct jul_event
*event
)
100 struct cds_lfht_node
*node_ptr
;
101 struct jul_ht_key key
;
107 key
.name
= event
->name
;
108 key
.loglevel
= event
->loglevel
;
110 node_ptr
= cds_lfht_add_unique(ht
->ht
,
111 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
112 ht_match_event
, &key
, &event
->node
.node
);
113 assert(node_ptr
== &event
->node
.node
);
117 * URCU delayed JUL event reclaim.
119 static void destroy_event_jul_rcu(struct rcu_head
*head
)
121 struct lttng_ht_node_str
*node
=
122 caa_container_of(head
, struct lttng_ht_node_str
, head
);
123 struct jul_event
*event
=
124 caa_container_of(node
, struct jul_event
, node
);
130 * URCU delayed JUL app reclaim.
132 static void destroy_app_jul_rcu(struct rcu_head
*head
)
134 struct lttng_ht_node_ulong
*node
=
135 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
136 struct jul_app
*app
=
137 caa_container_of(node
, struct jul_app
, node
);
143 * Communication with Java agent. Send the message header to the given
144 * socket in big endian.
146 * Return 0 on success or else a negative errno message of sendmsg() op.
148 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
149 uint32_t cmd
, uint32_t cmd_version
)
153 struct lttcomm_jul_hdr msg
;
157 memset(&msg
, 0, sizeof(msg
));
158 msg
.data_size
= htobe64(data_size
);
159 msg
.cmd
= htobe32(cmd
);
160 msg
.cmd_version
= htobe32(cmd_version
);
162 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
163 if (size
< sizeof(msg
)) {
174 * Communication call with the Java agent. Send the payload to the given
175 * socket. The header MUST be sent prior to this call.
177 * Return 0 on success or else a negative errno value of sendmsg() op.
179 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
188 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
200 * Communication call with the Java agent. Receive reply from the agent using
203 * Return 0 on success or else a negative errno value from recvmsg() op.
205 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
213 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 jul_app
*app
, struct lttng_event
**events
)
234 int ret
, i
, len
= 0, offset
= 0;
237 struct lttng_event
*tmp_events
= NULL
;
238 struct lttcomm_jul_list_reply
*reply
= NULL
;
239 struct lttcomm_jul_list_reply_hdr reply_hdr
;
245 DBG2("JUL listing events for app pid: %d and socket %d", app
->pid
,
248 ret
= send_header(app
->sock
, 0, JUL_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 JUL_RET_CODE_SUCCESS
:
261 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
264 ERR("Java 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 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
292 sizeof(tmp_events
[i
].name
));
293 tmp_events
[i
].pid
= app
->pid
;
294 tmp_events
[i
].enabled
= -1;
295 len
= strlen(reply
->payload
+ offset
) + 1;
298 *events
= tmp_events
;
304 ret
= LTTNG_ERR_UST_LIST_FAIL
;
313 * Internal enable JUL event on a JUL application. This function
314 * communicates with the Java agent to enable a given event (Logger name).
316 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
318 static int enable_event(struct jul_app
*app
, struct jul_event
*event
)
322 struct lttcomm_jul_enable msg
;
323 struct lttcomm_jul_generic_reply reply
;
329 DBG2("JUL enabling event %s for app pid: %d and socket %d", event
->name
,
330 app
->pid
, app
->sock
->fd
);
332 data_size
= sizeof(msg
);
334 ret
= send_header(app
->sock
, data_size
, JUL_CMD_ENABLE
, 0);
339 memset(&msg
, 0, sizeof(msg
));
340 msg
.loglevel
= event
->loglevel
;
341 msg
.loglevel_type
= event
->loglevel_type
;
342 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
343 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
348 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
353 switch (be32toh(reply
.ret_code
)) {
354 case JUL_RET_CODE_SUCCESS
:
356 case JUL_RET_CODE_UNKNOWN_NAME
:
357 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
360 ERR("Java agent returned an unknown code: %" PRIu32
,
361 be32toh(reply
.ret_code
));
362 ret
= LTTNG_ERR_FATAL
;
369 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
375 * Internal disable JUL event call on a JUL application. This function
376 * communicates with the Java agent to disable a given event (Logger name).
378 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
380 static int disable_event(struct jul_app
*app
, struct jul_event
*event
)
384 struct lttcomm_jul_disable msg
;
385 struct lttcomm_jul_generic_reply reply
;
391 DBG2("JUL disabling event %s for app pid: %d and socket %d", event
->name
,
392 app
->pid
, app
->sock
->fd
);
394 data_size
= sizeof(msg
);
396 ret
= send_header(app
->sock
, data_size
, JUL_CMD_DISABLE
, 0);
401 memset(&msg
, 0, sizeof(msg
));
402 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
403 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
408 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
413 switch (be32toh(reply
.ret_code
)) {
414 case JUL_RET_CODE_SUCCESS
:
416 case JUL_RET_CODE_UNKNOWN_NAME
:
417 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
420 ERR("Java agent returned an unknown code: %" PRIu32
,
421 be32toh(reply
.ret_code
));
422 ret
= LTTNG_ERR_FATAL
;
429 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
435 * Send back the registration DONE command to a given JUL application.
437 * Return 0 on success or else a negative value.
439 int jul_send_registration_done(struct jul_app
*app
)
444 DBG("JUL sending registration done to app socket %d", app
->sock
->fd
);
446 return send_header(app
->sock
, 0, JUL_CMD_REG_DONE
, 0);
450 * Enable JUL event on every JUL applications registered with the session
453 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
455 int jul_enable_event(struct jul_event
*event
)
459 struct lttng_ht_iter iter
;
465 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
467 /* Enable event on JUL application through TCP socket. */
468 ret
= enable_event(app
, event
);
469 if (ret
!= LTTNG_OK
) {
483 * Disable JUL event on every JUL applications registered with the session
486 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
488 int jul_disable_event(struct jul_event
*event
)
492 struct lttng_ht_iter iter
;
498 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
500 /* Enable event on JUL application through TCP socket. */
501 ret
= disable_event(app
, event
);
502 if (ret
!= LTTNG_OK
) {
516 * Ask every java agent for the list of possible event (logger name). Events is
517 * allocated with the events of every JUL application.
519 * Return the number of events or else a negative value.
521 int jul_list_events(struct lttng_event
**events
)
524 size_t nbmem
, count
= 0;
526 struct lttng_event
*tmp_events
= NULL
;
527 struct lttng_ht_iter iter
;
531 nbmem
= UST_APP_EVENT_LIST_SIZE
;
532 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
534 PERROR("zmalloc jul list events");
540 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
543 struct lttng_event
*jul_events
;
545 nb_ev
= list_events(app
, &jul_events
);
551 if (count
+ nb_ev
> nbmem
) {
552 /* In case the realloc fails, we free the memory */
553 struct lttng_event
*new_tmp_events
;
556 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
557 DBG2("Reallocating JUL event list from %zu to %zu entries",
559 new_tmp_events
= realloc(tmp_events
,
560 new_nbmem
* sizeof(*new_tmp_events
));
561 if (!new_tmp_events
) {
562 PERROR("realloc JUL events");
567 /* Zero the new memory */
568 memset(new_tmp_events
+ nbmem
, 0,
569 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
571 tmp_events
= new_tmp_events
;
573 memcpy(tmp_events
+ count
, jul_events
,
574 nb_ev
* sizeof(*tmp_events
));
581 *events
= tmp_events
;
592 * Create a JUL app object using the given PID.
594 * Return newly allocated object or else NULL on error.
596 struct jul_app
*jul_create_app(pid_t pid
, struct lttcomm_sock
*sock
)
602 app
= zmalloc(sizeof(*app
));
604 PERROR("zmalloc JUL create");
610 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
617 * Lookup JUL app by socket in the global hash table.
619 * RCU read side lock MUST be acquired.
621 * Return object if found else NULL.
623 struct jul_app
*jul_find_app_by_sock(int sock
)
625 struct lttng_ht_node_ulong
*node
;
626 struct lttng_ht_iter iter
;
631 lttng_ht_lookup(jul_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
632 node
= lttng_ht_iter_get_node_ulong(&iter
);
636 app
= caa_container_of(node
, struct jul_app
, node
);
638 DBG3("JUL app pid %d found by sock %d.", app
->pid
, sock
);
642 DBG3("JUL app NOT found by sock %d.", sock
);
647 * Add JUL application object to a given hash table.
649 void jul_add_app(struct jul_app
*app
)
653 DBG3("JUL adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
656 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock
, &app
->node
);
661 * Delete JUL application from the global hash table.
663 void jul_delete_app(struct jul_app
*app
)
666 struct lttng_ht_iter iter
;
670 DBG3("JUL deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
672 iter
.iter
.node
= &app
->node
.node
;
674 ret
= lttng_ht_del(jul_apps_ht_by_sock
, &iter
);
680 * Destroy a JUL application object by detaching it from its corresponding UST
681 * app if one is connected by closing the socket. Finally, perform a
682 * delayed memory reclaim.
684 void jul_destroy_app(struct jul_app
*app
)
689 app
->sock
->ops
->close(app
->sock
);
690 lttcomm_destroy_sock(app
->sock
);
693 call_rcu(&app
->node
.head
, destroy_app_jul_rcu
);
697 * Initialize an already allocated JUL domain object.
699 * Return 0 on success or else a negative errno value.
701 int jul_init_domain(struct jul_domain
*dom
)
707 dom
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
720 * Create a newly allocated JUL event data structure. If name is valid, it's
721 * copied into the created event.
723 * Return a new object else NULL on error.
725 struct jul_event
*jul_create_event(const char *name
)
727 struct jul_event
*event
;
729 DBG3("JUL create new event with name %s", name
);
731 event
= zmalloc(sizeof(*event
));
737 strncpy(event
->name
, name
, sizeof(event
->name
));
738 event
->name
[sizeof(event
->name
) - 1] = '\0';
739 lttng_ht_node_init_str(&event
->node
, event
->name
);
747 * Unique add of a JUL event to a given domain.
749 void jul_add_event(struct jul_event
*event
, struct jul_domain
*dom
)
755 DBG3("JUL adding event %s to domain", event
->name
);
758 add_unique_jul_event(dom
->events
, event
);
764 * Find a JUL event in the given domain using name and loglevel.
766 * RCU read side lock MUST be acquired.
768 * Return object if found else NULL.
770 struct jul_event
*jul_find_event_by_name(const char *name
,
771 struct jul_domain
*dom
)
773 struct lttng_ht_node_str
*node
;
774 struct lttng_ht_iter iter
;
776 struct jul_ht_key key
;
785 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
786 ht_match_event_by_name
, &key
, &iter
.iter
);
787 node
= lttng_ht_iter_get_node_str(&iter
);
792 DBG3("JUL event found %s by name.", name
);
793 return caa_container_of(node
, struct jul_event
, node
);
796 DBG3("JUL NOT found by name %s.", name
);
801 * Find a JUL event in the given domain using name and loglevel.
803 * RCU read side lock MUST be acquired.
805 * Return object if found else NULL.
807 struct jul_event
*jul_find_event(const char *name
,
808 enum lttng_loglevel_jul loglevel
, struct jul_domain
*dom
)
810 struct lttng_ht_node_str
*node
;
811 struct lttng_ht_iter iter
;
813 struct jul_ht_key key
;
821 key
.loglevel
= loglevel
;
823 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
824 ht_match_event
, &key
, &iter
.iter
);
825 node
= lttng_ht_iter_get_node_str(&iter
);
830 DBG3("JUL event found %s.", name
);
831 return caa_container_of(node
, struct jul_event
, node
);
834 DBG3("JUL NOT found %s.", name
);
839 * Free given JUL event. This event must not be globally visible at this
840 * point (only expected to be used on failure just after event
841 * creation). After this call, the pointer is not usable anymore.
843 void jul_destroy_event(struct jul_event
*event
)
851 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
852 * thus a reference to static or stack data can be passed to this function.
854 void jul_destroy_domain(struct jul_domain
*dom
)
856 struct lttng_ht_node_str
*node
;
857 struct lttng_ht_iter iter
;
861 DBG3("JUL destroy domain");
864 * Just ignore if no events hash table exists. This is possible if for
865 * instance a JUL domain object was allocated but not initialized.
872 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, node
, node
) {
875 ret
= lttng_ht_del(dom
->events
, &iter
);
877 call_rcu(&node
->head
, destroy_event_jul_rcu
);
881 lttng_ht_destroy(dom
->events
);
885 * Initialize JUL subsystem.
889 jul_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
890 if (!jul_apps_ht_by_sock
) {
898 * Update a JUL application (given socket) using the given domain.
900 * Note that this function is most likely to be used with a tracing session
901 * thus the caller should make sure to hold the appropriate lock(s).
903 void jul_update(struct jul_domain
*domain
, int sock
)
907 struct jul_event
*event
;
908 struct lttng_ht_iter iter
;
913 DBG("JUL updating app socket %d", sock
);
916 cds_lfht_for_each_entry(domain
->events
->ht
, &iter
.iter
, event
, node
.node
) {
917 /* Skip event if disabled. */
918 if (!event
->enabled
) {
922 app
= jul_find_app_by_sock(sock
);
924 * We are in the registration path thus if the application is gone,
925 * there is a serious code flow error.
929 ret
= enable_event(app
, event
);
930 if (ret
!= LTTNG_OK
) {
931 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
932 event
->name
, app
->pid
, app
->sock
->fd
);
933 /* Let's try the others here and don't assume the app is dead. */