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>
30 * URCU delayed JUL event reclaim.
32 static void destroy_event_jul_rcu(struct rcu_head
*head
)
34 struct lttng_ht_node_str
*node
=
35 caa_container_of(head
, struct lttng_ht_node_str
, head
);
36 struct jul_event
*event
=
37 caa_container_of(node
, struct jul_event
, node
);
43 * URCU delayed JUL app reclaim.
45 static void destroy_app_jul_rcu(struct rcu_head
*head
)
47 struct lttng_ht_node_ulong
*node
=
48 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
50 caa_container_of(node
, struct jul_app
, node
);
56 * Communication with Java agent. Send the message header to the given
57 * socket in big endian.
59 * Return 0 on success or else a negative errno message of sendmsg() op.
61 static int send_header(struct lttcomm_sock
*sock
, uint64_t data_size
,
62 uint32_t cmd
, uint32_t cmd_version
)
66 struct lttcomm_jul_hdr msg
;
70 memset(&msg
, 0, sizeof(msg
));
71 msg
.data_size
= htobe64(data_size
);
72 msg
.cmd
= htobe32(cmd
);
73 msg
.cmd_version
= htobe32(cmd_version
);
75 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
76 if (size
< sizeof(msg
)) {
87 * Communication call with the Java agent. Send the payload to the given
88 * socket. The header MUST be sent prior to this call.
90 * Return 0 on success or else a negative errno value of sendmsg() op.
92 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
101 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
113 * Communication call with the Java agent. Receive reply from the agent using
116 * Return 0 on success or else a negative errno value from recvmsg() op.
118 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
126 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
139 * Internal event listing for a given app. Populate events.
141 * Return number of element in the list or else a negative LTTNG_ERR* code.
142 * On success, the caller is responsible for freeing the memory
143 * allocated for "events".
145 static ssize_t
list_events(struct jul_app
*app
, struct lttng_event
**events
)
147 int ret
, i
, len
= 0, offset
= 0;
150 struct lttng_event
*tmp_events
= NULL
;
151 struct lttcomm_jul_list_reply
*reply
= NULL
;
152 struct lttcomm_jul_list_reply_hdr reply_hdr
;
158 DBG2("JUL listing events for app pid: %d and socket %d", app
->pid
,
161 ret
= send_header(app
->sock
, 0, JUL_CMD_LIST
, 0);
166 /* Get list header so we know how much we'll receive. */
167 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
172 switch (be32toh(reply_hdr
.ret_code
)) {
173 case JUL_RET_CODE_SUCCESS
:
174 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
177 ERR("Java agent returned an unknown code: %" PRIu32
,
178 be32toh(reply_hdr
.ret_code
));
179 ret
= LTTNG_ERR_FATAL
;
183 reply
= zmalloc(data_size
);
185 ret
= LTTNG_ERR_NOMEM
;
189 /* Get the list with the appropriate data size. */
190 ret
= recv_reply(app
->sock
, reply
, data_size
);
195 nb_event
= be32toh(reply
->nb_event
);
196 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
198 ret
= LTTNG_ERR_NOMEM
;
202 for (i
= 0; i
< nb_event
; i
++) {
204 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
205 sizeof(tmp_events
[i
].name
));
206 tmp_events
[i
].pid
= app
->pid
;
207 tmp_events
[i
].enabled
= -1;
208 len
= strlen(reply
->payload
+ offset
) + 1;
211 *events
= tmp_events
;
217 ret
= LTTNG_ERR_UST_LIST_FAIL
;
226 * Internal enable JUL event on a JUL application. This function
227 * communicates with the Java agent to enable a given event (Logger name).
229 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
231 static int enable_event(struct jul_app
*app
, struct jul_event
*event
)
235 struct lttcomm_jul_enable msg
;
236 struct lttcomm_jul_generic_reply reply
;
242 DBG2("JUL enabling event %s for app pid: %d and socket %d", event
->name
,
243 app
->pid
, app
->sock
->fd
);
245 data_size
= sizeof(msg
);
247 ret
= send_header(app
->sock
, data_size
, JUL_CMD_ENABLE
, 0);
252 memset(&msg
, 0, sizeof(msg
));
253 msg
.loglevel
= event
->loglevel
;
254 msg
.loglevel_type
= event
->loglevel_type
;
255 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
256 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
261 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
266 switch (be32toh(reply
.ret_code
)) {
267 case JUL_RET_CODE_SUCCESS
:
269 case JUL_RET_CODE_UNKNOWN_NAME
:
270 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
273 ERR("Java agent returned an unknown code: %" PRIu32
,
274 be32toh(reply
.ret_code
));
275 ret
= LTTNG_ERR_FATAL
;
282 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
288 * Internal disable JUL event call on a JUL application. This function
289 * communicates with the Java agent to disable a given event (Logger name).
291 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
293 static int disable_event(struct jul_app
*app
, struct jul_event
*event
)
297 struct lttcomm_jul_disable msg
;
298 struct lttcomm_jul_generic_reply reply
;
304 DBG2("JUL disabling event %s for app pid: %d and socket %d", event
->name
,
305 app
->pid
, app
->sock
->fd
);
307 data_size
= sizeof(msg
);
309 ret
= send_header(app
->sock
, data_size
, JUL_CMD_DISABLE
, 0);
314 memset(&msg
, 0, sizeof(msg
));
315 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
316 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
321 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
326 switch (be32toh(reply
.ret_code
)) {
327 case JUL_RET_CODE_SUCCESS
:
329 case JUL_RET_CODE_UNKNOWN_NAME
:
330 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
333 ERR("Java agent returned an unknown code: %" PRIu32
,
334 be32toh(reply
.ret_code
));
335 ret
= LTTNG_ERR_FATAL
;
342 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
348 * Send back the registration DONE command to a given JUL application.
350 * Return 0 on success or else a negative value.
352 int jul_send_registration_done(struct jul_app
*app
)
357 DBG("JUL sending registration done to app socket %d", app
->sock
->fd
);
359 return send_header(app
->sock
, 0, JUL_CMD_REG_DONE
, 0);
363 * Enable JUL event on every JUL applications registered with the session
366 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
368 int jul_enable_event(struct jul_event
*event
)
372 struct lttng_ht_iter iter
;
378 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
380 /* Enable event on JUL application through TCP socket. */
381 ret
= enable_event(app
, event
);
382 if (ret
!= LTTNG_OK
) {
396 * Disable JUL event on every JUL applications registered with the session
399 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
401 int jul_disable_event(struct jul_event
*event
)
405 struct lttng_ht_iter iter
;
411 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
413 /* Enable event on JUL application through TCP socket. */
414 ret
= disable_event(app
, event
);
415 if (ret
!= LTTNG_OK
) {
429 * Ask every java agent for the list of possible event (logger name). Events is
430 * allocated with the events of every JUL application.
432 * Return the number of events or else a negative value.
434 int jul_list_events(struct lttng_event
**events
)
437 size_t nbmem
, count
= 0;
439 struct lttng_event
*tmp_events
= NULL
;
440 struct lttng_ht_iter iter
;
444 nbmem
= UST_APP_EVENT_LIST_SIZE
;
445 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
447 PERROR("zmalloc jul list events");
453 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
456 struct lttng_event
*jul_events
;
458 nb_ev
= list_events(app
, &jul_events
);
464 if (count
+ nb_ev
> nbmem
) {
465 /* In case the realloc fails, we free the memory */
466 struct lttng_event
*new_tmp_events
;
469 new_nbmem
= max_t(size_t, count
+ nb_ev
, nbmem
<< 1);
470 DBG2("Reallocating JUL event list from %zu to %zu entries",
472 new_tmp_events
= realloc(tmp_events
,
473 new_nbmem
* sizeof(*new_tmp_events
));
474 if (!new_tmp_events
) {
475 PERROR("realloc JUL events");
480 /* Zero the new memory */
481 memset(new_tmp_events
+ nbmem
, 0,
482 (new_nbmem
- nbmem
) * sizeof(*new_tmp_events
));
484 tmp_events
= new_tmp_events
;
486 memcpy(tmp_events
+ count
, jul_events
,
487 nb_ev
* sizeof(*tmp_events
));
494 *events
= tmp_events
;
505 * Create a JUL app object using the given PID.
507 * Return newly allocated object or else NULL on error.
509 struct jul_app
*jul_create_app(pid_t pid
, struct lttcomm_sock
*sock
)
515 app
= zmalloc(sizeof(*app
));
517 PERROR("zmalloc JUL create");
523 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
530 * Lookup JUL app by socket in the global hash table.
532 * RCU read side lock MUST be acquired.
534 * Return object if found else NULL.
536 struct jul_app
*jul_find_app_by_sock(int sock
)
538 struct lttng_ht_node_ulong
*node
;
539 struct lttng_ht_iter iter
;
544 lttng_ht_lookup(jul_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
545 node
= lttng_ht_iter_get_node_ulong(&iter
);
549 app
= caa_container_of(node
, struct jul_app
, node
);
551 DBG3("JUL app pid %d found by sock %d.", app
->pid
, sock
);
555 DBG3("JUL app NOT found by sock %d.", sock
);
560 * Add JUL application object to a given hash table.
562 void jul_add_app(struct jul_app
*app
)
566 DBG3("JUL adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
569 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock
, &app
->node
);
574 * Delete JUL application from the global hash table.
576 void jul_delete_app(struct jul_app
*app
)
579 struct lttng_ht_iter iter
;
583 DBG3("JUL deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
585 iter
.iter
.node
= &app
->node
.node
;
587 ret
= lttng_ht_del(jul_apps_ht_by_sock
, &iter
);
593 * Destroy a JUL application object by detaching it from its corresponding UST
594 * app if one is connected by closing the socket. Finally, perform a
595 * delayed memory reclaim.
597 void jul_destroy_app(struct jul_app
*app
)
602 app
->sock
->ops
->close(app
->sock
);
603 lttcomm_destroy_sock(app
->sock
);
606 call_rcu(&app
->node
.head
, destroy_app_jul_rcu
);
610 * Initialize an already allocated JUL domain object.
612 * Return 0 on success or else a negative errno value.
614 int jul_init_domain(struct jul_domain
*dom
)
620 dom
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
633 * Create a newly allocated JUL event data structure. If name is valid, it's
634 * copied into the created event.
636 * Return a new object else NULL on error.
638 struct jul_event
*jul_create_event(const char *name
)
640 struct jul_event
*event
;
642 DBG3("JUL create new event with name %s", name
);
644 event
= zmalloc(sizeof(*event
));
650 strncpy(event
->name
, name
, sizeof(event
->name
));
651 event
->name
[sizeof(event
->name
) - 1] = '\0';
652 lttng_ht_node_init_str(&event
->node
, event
->name
);
660 * Unique add of a JUL event to a given domain.
662 void jul_add_event(struct jul_event
*event
, struct jul_domain
*dom
)
668 DBG3("JUL adding event %s to domain", event
->name
);
671 lttng_ht_add_unique_str(dom
->events
, &event
->node
);
677 * Find a JUL event in the given domain using name.
679 * RCU read side lock MUST be acquired.
681 * Return object if found else NULL.
683 struct jul_event
*jul_find_by_name(const char *name
, struct jul_domain
*dom
)
685 struct lttng_ht_node_str
*node
;
686 struct lttng_ht_iter iter
;
692 lttng_ht_lookup(dom
->events
, (void *)name
, &iter
);
693 node
= lttng_ht_iter_get_node_str(&iter
);
698 DBG3("JUL found by name %s in domain.", name
);
699 return caa_container_of(node
, struct jul_event
, node
);
702 DBG3("JUL NOT found by name %s in domain.", name
);
707 * Free given JUL event. This event must not be globally visible at this
708 * point (only expected to be used on failure just after event
709 * creation). After this call, the pointer is not usable anymore.
711 void jul_destroy_event(struct jul_event
*event
)
719 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
720 * thus a reference to static or stack data can be passed to this function.
722 void jul_destroy_domain(struct jul_domain
*dom
)
724 struct lttng_ht_node_str
*node
;
725 struct lttng_ht_iter iter
;
729 DBG3("JUL destroy domain");
732 * Just ignore if no events hash table exists. This is possible if for
733 * instance a JUL domain object was allocated but not initialized.
740 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, node
, node
) {
743 ret
= lttng_ht_del(dom
->events
, &iter
);
745 call_rcu(&node
->head
, destroy_event_jul_rcu
);
749 lttng_ht_destroy(dom
->events
);
753 * Initialize JUL subsystem.
757 jul_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
758 if (!jul_apps_ht_by_sock
) {
766 * Update a JUL application (given socket) using the given domain.
768 * Note that this function is most likely to be used with a tracing session
769 * thus the caller should make sure to hold the appropriate lock(s).
771 void jul_update(struct jul_domain
*domain
, int sock
)
775 struct jul_event
*event
;
776 struct lttng_ht_iter iter
;
781 DBG("JUL updating app socket %d", sock
);
784 cds_lfht_for_each_entry(domain
->events
->ht
, &iter
.iter
, event
, node
.node
) {
785 /* Skip event if disabled. */
786 if (!event
->enabled
) {
790 app
= jul_find_app_by_sock(sock
);
792 * We are in the registration path thus if the application is gone,
793 * there is a serious code flow error.
797 ret
= enable_event(app
, event
);
798 if (ret
!= LTTNG_OK
) {
799 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
800 event
->name
, app
->pid
, app
->sock
->fd
);
801 /* Let's try the others here and don't assume the app is dead. */