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 intermediate call to complete destroy a JUL event.
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 intermediate call to complete destroy a JUL event.
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 call. Send the message header to the given
57 * socket all 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 call to list events on a given app. Populate events.
140 * Return number of element in the list or else a negative LTTNG_ERR* code.
142 static ssize_t
list_events(struct jul_app
*app
, struct lttng_event
**events
)
144 int ret
, i
, len
= 0, offset
= 0;
147 struct lttng_event
*tmp_events
= NULL
;
148 struct lttcomm_jul_list_reply
*reply
= NULL
;
149 struct lttcomm_jul_list_reply_hdr reply_hdr
;
155 DBG2("JUL listing events for app pid: %d and socket %d", app
->pid
,
158 ret
= send_header(app
->sock
, 0, JUL_CMD_LIST
, 0);
163 /* Get list header so we know how much we'll receive. */
164 ret
= recv_reply(app
->sock
, &reply_hdr
, sizeof(reply_hdr
));
169 switch (be32toh(reply_hdr
.ret_code
)) {
170 case JUL_RET_CODE_SUCCESS
:
171 data_size
= be32toh(reply_hdr
.data_size
) + sizeof(*reply
);
174 ERR("Java agent returned an unknown code: %" PRIu32
,
175 be32toh(reply_hdr
.ret_code
));
176 ret
= LTTNG_ERR_FATAL
;
180 reply
= zmalloc(data_size
);
182 ret
= LTTNG_ERR_NOMEM
;
186 /* Get the list with the appropriate data size. */
187 ret
= recv_reply(app
->sock
, reply
, data_size
);
192 nb_event
= be32toh(reply
->nb_event
);
193 tmp_events
= zmalloc(sizeof(*tmp_events
) * nb_event
);
195 ret
= LTTNG_ERR_NOMEM
;
199 for (i
= 0; i
< nb_event
; i
++) {
201 strncpy(tmp_events
[i
].name
, reply
->payload
+ offset
,
202 sizeof(tmp_events
[i
].name
));
203 tmp_events
[i
].pid
= app
->pid
;
204 tmp_events
[i
].enabled
= -1;
205 len
= strlen(reply
->payload
+ offset
) + 1;
208 *events
= tmp_events
;
214 ret
= LTTNG_ERR_UST_LIST_FAIL
;
223 * Internal enable JUL event call on a JUL application. This function
224 * communicates with the Java agent to enable a given event (Logger name).
226 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
228 static int enable_event(struct jul_app
*app
, struct jul_event
*event
)
232 struct lttcomm_jul_enable msg
;
233 struct lttcomm_jul_generic_reply reply
;
239 DBG2("JUL enabling event %s for app pid: %d and socket %d", event
->name
,
240 app
->pid
, app
->sock
->fd
);
242 data_size
= sizeof(msg
);
244 ret
= send_header(app
->sock
, data_size
, JUL_CMD_ENABLE
, 0);
249 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
250 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
255 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
260 switch (be32toh(reply
.ret_code
)) {
261 case JUL_RET_CODE_SUCCESS
:
263 case JUL_RET_CODE_UNKNOWN_NAME
:
264 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
267 ERR("Java agent returned an unknown code: %" PRIu32
,
268 be32toh(reply
.ret_code
));
269 ret
= LTTNG_ERR_FATAL
;
276 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
282 * Internal disable JUL event call on a JUL application. This function
283 * communicates with the Java agent to disable a given event (Logger name).
285 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
287 static int disable_event(struct jul_app
*app
, struct jul_event
*event
)
291 struct lttcomm_jul_disable msg
;
292 struct lttcomm_jul_generic_reply reply
;
298 DBG2("JUL disabling event %s for app pid: %d and socket %d", event
->name
,
299 app
->pid
, app
->sock
->fd
);
301 data_size
= sizeof(msg
);
303 ret
= send_header(app
->sock
, data_size
, JUL_CMD_DISABLE
, 0);
308 strncpy(msg
.name
, event
->name
, sizeof(msg
.name
));
309 ret
= send_payload(app
->sock
, &msg
, sizeof(msg
));
314 ret
= recv_reply(app
->sock
, &reply
, sizeof(reply
));
319 switch (be32toh(reply
.ret_code
)) {
320 case JUL_RET_CODE_SUCCESS
:
322 case JUL_RET_CODE_UNKNOWN_NAME
:
323 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
326 ERR("Java agent returned an unknown code: %" PRIu32
,
327 be32toh(reply
.ret_code
));
328 ret
= LTTNG_ERR_FATAL
;
335 ret
= LTTNG_ERR_UST_DISABLE_FAIL
;
341 * Enable JUL event on every JUL applications registered with the session
344 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
346 int jul_enable_event(struct jul_event
*event
)
350 struct lttng_ht_iter iter
;
356 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
358 /* Enable event on JUL application through TCP socket. */
359 ret
= enable_event(app
, event
);
360 if (ret
!= LTTNG_OK
) {
374 * Disable JUL event on every JUL applications registered with the session
377 * Return LTTNG_OK on success or else a LTTNG_ERR* code.
379 int jul_disable_event(struct jul_event
*event
)
383 struct lttng_ht_iter iter
;
389 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
391 /* Enable event on JUL application through TCP socket. */
392 ret
= disable_event(app
, event
);
393 if (ret
!= LTTNG_OK
) {
407 * Ask every java agent for the list of possible event (logger name). Events is
408 * allocated with the events of every JUL application.
410 * Return the number of events or else a negative value.
412 int jul_list_events(struct lttng_event
**events
)
415 size_t nbmem
, count
= 0;
417 struct lttng_event
*tmp_events
= NULL
;
418 struct lttng_ht_iter iter
;
422 nbmem
= UST_APP_EVENT_LIST_SIZE
;
423 tmp_events
= zmalloc(nbmem
* sizeof(*tmp_events
));
425 PERROR("zmalloc jul list events");
431 cds_lfht_for_each_entry(jul_apps_ht_by_sock
->ht
, &iter
.iter
, app
,
434 struct lttng_event
*jul_events
;
436 nb_ev
= list_events(app
, &jul_events
);
443 if (count
>= nbmem
) {
444 /* In case the realloc fails, we free the memory */
447 DBG2("Reallocating JUL event list from %zu to %zu entries", nbmem
,
450 ptr
= realloc(tmp_events
, nbmem
* sizeof(*tmp_events
));
452 PERROR("realloc JUL events");
460 memcpy(tmp_events
+ (count
* sizeof(*tmp_events
)), jul_events
,
461 nb_ev
* sizeof(*tmp_events
));
468 *events
= tmp_events
;
477 * Create a JUL app object using the given PID.
479 * Return newly allocated object or else NULL on error.
481 struct jul_app
*jul_create_app(pid_t pid
, struct lttcomm_sock
*sock
)
487 app
= zmalloc(sizeof(*app
));
489 PERROR("zmalloc JUL create");
495 /* Flag it invalid until assignation. */
496 app
->ust_app_sock
= -1;
497 lttng_ht_node_init_ulong(&app
->node
, (unsigned long) app
->sock
->fd
);
504 * Lookup JUL app by socket in the global hash table.
506 * RCU read side lock MUST be acquired.
508 * Return object if found else NULL.
510 struct jul_app
*jul_find_app_by_sock(int sock
)
512 struct lttng_ht_node_ulong
*node
;
513 struct lttng_ht_iter iter
;
518 lttng_ht_lookup(jul_apps_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
519 node
= lttng_ht_iter_get_node_ulong(&iter
);
523 app
= caa_container_of(node
, struct jul_app
, node
);
525 DBG3("JUL app pid %d found by sock %d.", app
->pid
, sock
);
529 DBG3("JUL app NOT found by sock %d.", sock
);
534 * Add JUL application object to a given hash table.
536 void jul_add_app(struct jul_app
*app
)
540 DBG3("JUL adding app sock: %d and pid: %d to ht", app
->sock
->fd
, app
->pid
);
543 lttng_ht_add_unique_ulong(jul_apps_ht_by_sock
, &app
->node
);
548 * Attach a given JUL application to an UST app object. This is done by copying
549 * the socket fd value into the ust app obj. atomically.
551 void jul_attach_app(struct jul_app
*japp
)
553 struct ust_app
*uapp
;
558 uapp
= ust_app_find_by_pid(japp
->pid
);
563 uatomic_set(&uapp
->jul_app_sock
, japp
->sock
->fd
);
565 DBG3("JUL app pid: %d, sock: %d attached to UST app.", japp
->pid
,
574 * Remove JUL app. reference from an UST app object and set it to NULL.
576 void jul_detach_app(struct jul_app
*japp
)
578 struct ust_app
*uapp
;
584 if (japp
->ust_app_sock
< 0) {
588 uapp
= ust_app_find_by_sock(japp
->ust_app_sock
);
593 uapp
->jul_app_sock
= -1;
601 * Delete JUL application from the global hash table.
603 void jul_delete_app(struct jul_app
*app
)
606 struct lttng_ht_iter iter
;
610 DBG3("JUL deleting app pid: %d and sock: %d", app
->pid
, app
->sock
->fd
);
612 iter
.iter
.node
= &app
->node
.node
;
614 ret
= lttng_ht_del(jul_apps_ht_by_sock
, &iter
);
620 * Destroy a JUL application object by detaching it from its corresponding UST
621 * app if one, closing the socket and freeing the memory.
623 void jul_destroy_app(struct jul_app
*app
)
628 app
->sock
->ops
->close(app
->sock
);
629 lttcomm_destroy_sock(app
->sock
);
632 call_rcu(&app
->node
.head
, destroy_app_jul_rcu
);
636 * Initialize an already allocated JUL domain object.
638 * Return 0 on success or else a negative errno value.
640 int jul_init_domain(struct jul_domain
*dom
)
646 dom
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
659 * Create a newly allocated JUL event data structure. If name is valid, it's
660 * copied into the created event.
662 * Return a new object else NULL on error.
664 struct jul_event
*jul_create_event(const char *name
)
666 struct jul_event
*event
;
668 DBG3("JUL create new event with name %s", name
);
670 event
= zmalloc(sizeof(*event
));
676 strncpy(event
->name
, name
, sizeof(event
->name
));
677 event
->name
[sizeof(event
->name
) - 1] = '\0';
678 lttng_ht_node_init_str(&event
->node
, event
->name
);
686 * Unique add of a JUL event to a given domain.
688 void jul_add_event(struct jul_event
*event
, struct jul_domain
*dom
)
694 DBG3("JUL adding event %s to domain", event
->name
);
697 lttng_ht_add_unique_str(dom
->events
, &event
->node
);
703 * Find a JUL event in the given domain using name.
705 * RCU read side lock MUST be acquired.
707 * Return object if found else NULL.
709 struct jul_event
*jul_find_by_name(const char *name
, struct jul_domain
*dom
)
711 struct lttng_ht_node_str
*node
;
712 struct lttng_ht_iter iter
;
718 lttng_ht_lookup(dom
->events
, (void *)name
, &iter
);
719 node
= lttng_ht_iter_get_node_str(&iter
);
724 DBG3("JUL found by name %s in domain.", name
);
725 return caa_container_of(node
, struct jul_event
, node
);
728 DBG3("JUL NOT found by name %s in domain.", name
);
733 * Delete JUL event from given domain. Events hash table MUST be initialized.
735 void jul_delete_event(struct jul_event
*event
, struct jul_domain
*dom
)
738 struct lttng_ht_iter iter
;
744 DBG3("JUL deleting event %s from domain", event
->name
);
746 iter
.iter
.node
= &event
->node
.node
;
748 ret
= lttng_ht_del(dom
->events
, &iter
);
754 * Free given JUl event. After this call, the pointer is not usable anymore.
756 void jul_destroy_event(struct jul_event
*event
)
764 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
765 * thus a reference can be passed to this function.
767 void jul_destroy_domain(struct jul_domain
*dom
)
769 struct lttng_ht_node_str
*node
;
770 struct lttng_ht_iter iter
;
774 DBG3("JUL destroy domain");
777 * Just ignore if no events hash table exists. This is possible if for
778 * instance a JUL domain object was allocated but not initialized.
785 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, node
, node
) {
788 ret
= lttng_ht_del(dom
->events
, &iter
);
790 call_rcu(&node
->head
, destroy_event_jul_rcu
);
794 lttng_ht_destroy(dom
->events
);
798 * Initialize JUL subsystem.
802 jul_apps_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
803 if (!jul_apps_ht_by_sock
) {
811 * Update a JUL application (given socket) using the given domain.
813 * Note that this function is most likely to be used with a tracing session
814 * thus the caller should make sure to hold the appropriate lock(s).
816 void jul_update(struct jul_domain
*domain
, int sock
)
820 struct jul_event
*event
;
821 struct lttng_ht_iter iter
;
826 DBG("JUL updating app socket %d", sock
);
829 cds_lfht_for_each_entry(domain
->events
->ht
, &iter
.iter
, event
, node
.node
) {
830 /* Skip event if disabled. */
831 if (!event
->enabled
) {
835 app
= jul_find_app_by_sock(sock
);
837 * We are in the registration path thus if the application is gone,
838 * there is a serious code flow error.
842 ret
= enable_event(app
, event
);
843 if (ret
!= LTTNG_OK
) {
844 DBG2("JUL update unable to enable event %s on app pid: %d sock %d",
845 event
->name
, app
->pid
, app
->sock
->fd
);
846 /* Let's try the others here and don't assume the app is dead. */