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 msg
.data_size
= htobe64(data_size
);
71 msg
.cmd
= htobe32(cmd
);
72 msg
.cmd_version
= htobe32(cmd_version
);
74 size
= sock
->ops
->sendmsg(sock
, &msg
, sizeof(msg
), 0);
75 if (size
< sizeof(msg
)) {
86 * Communication call with the Java agent. Send the payload to the given
87 * socket. The header MUST be sent prior to this call.
89 * Return 0 on success or else a negative errno value of sendmsg() op.
91 static int send_payload(struct lttcomm_sock
*sock
, void *data
,
100 len
= sock
->ops
->sendmsg(sock
, data
, size
, 0);
112 * Communication call with the Java agent. Receive reply from the agent using
115 * Return 0 on success or else a negative errno value from recvmsg() op.
117 static int recv_reply(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
125 len
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
138 * Internal event listing for a given app. Populate events.
140 * Return number of element in the list or else a negative LTTNG_ERR* code.
141 * On success, the caller is responsible for freeing the memory
142 * allocated for "events".
144 static ssize_t
list_events(struct jul_app
*app
, struct lttng_event
**events
)
146 int ret
, i
, len
= 0, offset
= 0;
149 struct lttng_event
*tmp_events
= NULL
;
150 struct lttcomm_jul_list_reply
*reply
= NULL
;
151 struct lttcomm_jul_list_reply_hdr reply_hdr
;
157 DBG2("JUL listing events for app pid: %d and socket %d", app
->pid
,
160 ret
= send_header(app
->sock
, 0, JUL_CMD_LIST
, 0);
165 /* Get list header so we know how much we'll receive. */
166 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
171 switch (be32toh(reply_hdr
.ret_code
)) {
172 case JUL_RET_CODE_SUCCESS
:
173 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
176 ERR("Java agent returned an unknown code: %" PRIu32
,
177 be32toh(reply_hdr
.ret_code
));
178 ret
= LTTNG_ERR_FATAL
;
182 reply
= zmalloc(data_size
);
184 ret
= LTTNG_ERR_NOMEM
;
188 /* Get the list with the appropriate data size. */
189 ret
= recv_reply(app
->sock
, reply
, data_size
);
194 nb_event
= be32toh(reply
->nb_event
);
195 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
197 ret
= LTTNG_ERR_NOMEM
;
201 for (i
= 0; i
< nb_event
; i
++) {
203 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
204 sizeof(tmp_events
[i
].name
));
205 tmp_events
[i
].pid
= app
->pid
;
206 tmp_events
[i
].enabled
= -1;
207 len
= strlen(reply
->payload
+ offset
) + 1;
210 *events
= tmp_events
;
216 ret
= LTTNG_ERR_UST_LIST_FAIL
;
225 * Internal enable JUL event on a JUL application. This function
226 * communicates with the Java agent to enable a given event (Logger name).
228 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
230 static int enable_event(struct jul_app
*app
, struct jul_event
*event
)
234 struct lttcomm_jul_enable msg
;
235 struct lttcomm_jul_generic_reply reply
;
241 DBG2("JUL enabling event %s for app pid: %d and socket %d", event
->name
,
242 app
->pid
, app
->sock
->fd
);
244 data_size
= sizeof(msg
);
246 ret
= send_header(app
->sock
, data_size
, JUL_CMD_ENABLE
, 0);
251 msg
.loglevel
= event
->loglevel
;
252 msg
.loglevel_type
= event
->loglevel_type
;
253 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
254 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
259 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
264 switch (be32toh(reply
.ret_code
)) {
265 case JUL_RET_CODE_SUCCESS
:
267 case JUL_RET_CODE_UNKNOWN_NAME
:
268 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
271 ERR("Java agent returned an unknown code: %" PRIu32
,
272 be32toh(reply
.ret_code
));
273 ret
= LTTNG_ERR_FATAL
;
280 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
286 * Internal disable JUL event call on a JUL application. This function
287 * communicates with the Java agent to disable a given event (Logger name).
289 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
291 static int disable_event(struct jul_app
*app
, struct jul_event
*event
)
295 struct lttcomm_jul_disable msg
;
296 struct lttcomm_jul_generic_reply reply
;
302 DBG2("JUL disabling event %s for app pid: %d and socket %d", event
->name
,
303 app
->pid
, app
->sock
->fd
);
305 data_size
= sizeof(msg
);
307 ret
= send_header(app
->sock
, data_size
, JUL_CMD_DISABLE
, 0);
312 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
313 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
318 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
323 switch (be32toh(reply
.ret_code
)) {
324 case JUL_RET_CODE_SUCCESS
:
326 case JUL_RET_CODE_UNKNOWN_NAME
:
327 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
330 ERR("Java agent returned an unknown code: %" PRIu32
,
331 be32toh(reply
.ret_code
));
332 ret
= LTTNG_ERR_FATAL
;
339 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
345 * Enable JUL event on every JUL applications registered with the session
348 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
350 int jul_enable_event(struct jul_event
*event
)
354 struct lttng_ht_iter iter
;
360 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
362 /* Enable event on JUL application through TCP socket. */
363 ret
= enable_event(app
, event
);
364 if (ret
!= LTTNG_OK
) {
378 * Disable JUL event on every JUL applications registered with the session
381 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
383 int jul_disable_event(struct jul_event
*event
)
387 struct lttng_ht_iter iter
;
393 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
395 /* Enable event on JUL application through TCP socket. */
396 ret
= disable_event(app
, event
);
397 if (ret
!= LTTNG_OK
) {
411 * Ask every java agent for the list of possible event (logger name). Events is
412 * allocated with the events of every JUL application.
414 * Return the number of events or else a negative value.
416 int jul_list_events(struct lttng_event
**events
)
419 size_t nbmem
, count
= 0;
421 struct lttng_event
*tmp_events
= NULL
;
422 struct lttng_ht_iter iter
;
426 nbmem
= UST_APP_EVENT_LIST_SIZE
;
427 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
429 PERROR("zmalloc jul list events");
435 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
438 struct lttng_event
*jul_events
;
440 nb_ev
= list_events(app
, &jul_events
);
446 if (count
>= nbmem
) {
447 /* In case the realloc fails, we free the memory */
450 DBG2("Reallocating JUL event list from %zu to %zu entries", nbmem
,
453 ptr
= realloc(tmp_events
, nbmem
* sizeof(*tmp_events
));
455 PERROR("realloc JUL events");
462 memcpy(tmp_events
+ (count
* sizeof(*tmp_events
)), jul_events
,
463 nb_ev
* sizeof(*tmp_events
));
470 *events
= tmp_events
;
481 * Create a JUL app object using the given PID.
483 * Return newly allocated object or else NULL on error.
485 struct jul_app
*jul_create_app(pid_t pid
, struct lttcomm_sock
*sock
)
491 app
= zmalloc(sizeof(*app
));
493 PERROR("zmalloc JUL create");
499 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
506 * Lookup JUL app by socket in the global hash table.
508 * RCU read side lock MUST be acquired.
510 * Return object if found else NULL.
512 struct jul_app
*jul_find_app_by_sock(int sock
)
514 struct lttng_ht_node_ulong
*node
;
515 struct lttng_ht_iter iter
;
520 lttng_ht_lookup(jul_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
521 node
= lttng_ht_iter_get_node_ulong(&iter
);
525 app
= caa_container_of(node
, struct jul_app
, node
);
527 DBG3("JUL app pid %d found by sock %d.", app
->pid
, sock
);
531 DBG3("JUL app NOT found by sock %d.", sock
);
536 * Add JUL application object to a given hash table.
538 void jul_add_app(struct jul_app
*app
)
542 DBG3("JUL adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
545 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock
, &app
->node
);
550 * Delete JUL application from the global hash table.
552 void jul_delete_app(struct jul_app
*app
)
555 struct lttng_ht_iter iter
;
559 DBG3("JUL deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
561 iter
.iter
.node
= &app
->node
.node
;
563 ret
= lttng_ht_del(jul_apps_ht_by_sock
, &iter
);
569 * Destroy a JUL application object by detaching it from its corresponding UST
570 * app if one is connected by closing the socket. Finally, perform a
571 * delayed memory reclaim.
573 void jul_destroy_app(struct jul_app
*app
)
578 app
->sock
->ops
->close(app
->sock
);
579 lttcomm_destroy_sock(app
->sock
);
582 call_rcu(&app
->node
.head
, destroy_app_jul_rcu
);
586 * Initialize an already allocated JUL domain object.
588 * Return 0 on success or else a negative errno value.
590 int jul_init_domain(struct jul_domain
*dom
)
596 dom
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
609 * Create a newly allocated JUL event data structure. If name is valid, it's
610 * copied into the created event.
612 * Return a new object else NULL on error.
614 struct jul_event
*jul_create_event(const char *name
)
616 struct jul_event
*event
;
618 DBG3("JUL create new event with name %s", name
);
620 event
= zmalloc(sizeof(*event
));
626 strncpy(event
->name
, name
, sizeof(event
->name
));
627 event
->name
[sizeof(event
->name
) - 1] = '\0';
628 lttng_ht_node_init_str(&event
->node
, event
->name
);
636 * Unique add of a JUL event to a given domain.
638 void jul_add_event(struct jul_event
*event
, struct jul_domain
*dom
)
644 DBG3("JUL adding event %s to domain", event
->name
);
647 lttng_ht_add_unique_str(dom
->events
, &event
->node
);
653 * Find a JUL event in the given domain using name.
655 * RCU read side lock MUST be acquired.
657 * Return object if found else NULL.
659 struct jul_event
*jul_find_by_name(const char *name
, struct jul_domain
*dom
)
661 struct lttng_ht_node_str
*node
;
662 struct lttng_ht_iter iter
;
668 lttng_ht_lookup(dom
->events
, (void *)name
, &iter
);
669 node
= lttng_ht_iter_get_node_str(&iter
);
674 DBG3("JUL found by name %s in domain.", name
);
675 return caa_container_of(node
, struct jul_event
, node
);
678 DBG3("JUL NOT found by name %s in domain.", name
);
683 * Free given JUL event. This event must not be globally visible at this
684 * point (only expected to be used on failure just after event
685 * creation). After this call, the pointer is not usable anymore.
687 void jul_destroy_event(struct jul_event
*event
)
695 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
696 * thus a reference to static or stack data can be passed to this function.
698 void jul_destroy_domain(struct jul_domain
*dom
)
700 struct lttng_ht_node_str
*node
;
701 struct lttng_ht_iter iter
;
705 DBG3("JUL destroy domain");
708 * Just ignore if no events hash table exists. This is possible if for
709 * instance a JUL domain object was allocated but not initialized.
716 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, node
, node
) {
719 ret
= lttng_ht_del(dom
->events
, &iter
);
721 call_rcu(&node
->head
, destroy_event_jul_rcu
);
725 lttng_ht_destroy(dom
->events
);
729 * Initialize JUL subsystem.
733 jul_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
734 if (!jul_apps_ht_by_sock
) {
742 * Update a JUL application (given socket) using the given domain.
744 * Note that this function is most likely to be used with a tracing session
745 * thus the caller should make sure to hold the appropriate lock(s).
747 void jul_update(struct jul_domain
*domain
, int sock
)
751 struct jul_event
*event
;
752 struct lttng_ht_iter iter
;
757 DBG("JUL updating app socket %d", sock
);
760 cds_lfht_for_each_entry(domain
->events
->ht
, &iter
.iter
, event
, node
.node
) {
761 /* Skip event if disabled. */
762 if (!event
->enabled
) {
766 app
= jul_find_app_by_sock(sock
);
768 * We are in the registration path thus if the application is gone,
769 * there is a serious code flow error.
773 ret
= enable_event(app
, event
);
774 if (ret
!= LTTNG_OK
) {
775 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
776 event
->name
, app
->pid
, app
->sock
->fd
);
777 /* Let's try the others here and don't assume the app is dead. */