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
,
726 struct lttng_filter_bytecode
*filter
)
728 struct jul_event
*event
;
730 DBG3("JUL create new event with name %s", name
);
732 event
= zmalloc(sizeof(*event
));
738 strncpy(event
->name
, name
, sizeof(event
->name
));
739 event
->name
[sizeof(event
->name
) - 1] = '\0';
740 lttng_ht_node_init_str(&event
->node
, event
->name
);
744 event
->filter
= filter
;
752 * Unique add of a JUL event to a given domain.
754 void jul_add_event(struct jul_event
*event
, struct jul_domain
*dom
)
760 DBG3("JUL adding event %s to domain", event
->name
);
763 add_unique_jul_event(dom
->events
, event
);
769 * Find a JUL event in the given domain using name and loglevel.
771 * RCU read side lock MUST be acquired.
773 * Return object if found else NULL.
775 struct jul_event
*jul_find_event_by_name(const char *name
,
776 struct jul_domain
*dom
)
778 struct lttng_ht_node_str
*node
;
779 struct lttng_ht_iter iter
;
781 struct jul_ht_key key
;
790 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
791 ht_match_event_by_name
, &key
, &iter
.iter
);
792 node
= lttng_ht_iter_get_node_str(&iter
);
797 DBG3("JUL event found %s by name.", name
);
798 return caa_container_of(node
, struct jul_event
, node
);
801 DBG3("JUL NOT found by name %s.", name
);
806 * Find a JUL event in the given domain using name and loglevel.
808 * RCU read side lock MUST be acquired.
810 * Return object if found else NULL.
812 struct jul_event
*jul_find_event(const char *name
,
813 enum lttng_loglevel_jul loglevel
, struct jul_domain
*dom
)
815 struct lttng_ht_node_str
*node
;
816 struct lttng_ht_iter iter
;
818 struct jul_ht_key key
;
826 key
.loglevel
= loglevel
;
828 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
829 ht_match_event
, &key
, &iter
.iter
);
830 node
= lttng_ht_iter_get_node_str(&iter
);
835 DBG3("JUL event found %s.", name
);
836 return caa_container_of(node
, struct jul_event
, node
);
839 DBG3("JUL NOT found %s.", name
);
844 * Free given JUL event. This event must not be globally visible at this
845 * point (only expected to be used on failure just after event
846 * creation). After this call, the pointer is not usable anymore.
848 void jul_destroy_event(struct jul_event
*event
)
856 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
857 * thus a reference to static or stack data can be passed to this function.
859 void jul_destroy_domain(struct jul_domain
*dom
)
861 struct lttng_ht_node_str
*node
;
862 struct lttng_ht_iter iter
;
866 DBG3("JUL destroy domain");
869 * Just ignore if no events hash table exists. This is possible if for
870 * instance a JUL domain object was allocated but not initialized.
877 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, node
, node
) {
879 struct jul_event
*event
;
882 * When destroying an event, we have to try to disable it on the agent
883 * side so the event stops generating data. The return value is not
884 * important since we have to continue anyway destroying the object.
886 event
= caa_container_of(node
, struct jul_event
, node
);
887 (void) jul_disable_event(event
);
889 ret
= lttng_ht_del(dom
->events
, &iter
);
891 call_rcu(&node
->head
, destroy_event_jul_rcu
);
895 lttng_ht_destroy(dom
->events
);
899 * Initialize JUL subsystem.
903 jul_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
904 if (!jul_apps_ht_by_sock
) {
912 * Update a JUL application (given socket) using the given domain.
914 * Note that this function is most likely to be used with a tracing session
915 * thus the caller should make sure to hold the appropriate lock(s).
917 void jul_update(struct jul_domain
*domain
, int sock
)
921 struct jul_event
*event
;
922 struct lttng_ht_iter iter
;
927 DBG("JUL updating app socket %d", sock
);
930 cds_lfht_for_each_entry(domain
->events
->ht
, &iter
.iter
, event
, node
.node
) {
931 /* Skip event if disabled. */
932 if (!event
->enabled
) {
936 app
= jul_find_app_by_sock(sock
);
938 * We are in the registration path thus if the application is gone,
939 * there is a serious code flow error.
943 ret
= enable_event(app
, event
);
944 if (ret
!= LTTNG_OK
) {
945 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
946 event
->name
, app
->pid
, app
->sock
->fd
);
947 /* Let's try the others here and don't assume the app is dead. */