X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fjul.c;h=87ab654a34f3c109ce7c477fe6b51eb1892ade0d;hp=a38738c3b77537bfb711dbc160af69f892d02c1a;hb=f20baf8e44b5d8a1c90b8b029b876762f6f916a6;hpb=4d0762229f4950d4ad21f6f47870d3b767dd61fa diff --git a/src/bin/lttng-sessiond/jul.c b/src/bin/lttng-sessiond/jul.c index a38738c3b..87ab654a3 100644 --- a/src/bin/lttng-sessiond/jul.c +++ b/src/bin/lttng-sessiond/jul.c @@ -17,10 +17,13 @@ #define _GNU_SOURCE #include +#include #include +#include #include "jul.h" +#include "ust-app.h" #include "utils.h" /* @@ -36,6 +39,511 @@ static void destroy_event_jul_rcu(struct rcu_head *head) free(event); } +/* + * URCU intermediate call to complete destroy a JUL event. + */ +static void destroy_app_jul_rcu(struct rcu_head *head) +{ + struct lttng_ht_node_ulong *node = + caa_container_of(head, struct lttng_ht_node_ulong, head); + struct jul_app *app = + caa_container_of(node, struct jul_app, node); + + free(app); +} + +/* + * Communication with Java agent call. Send the message header to the given + * socket all in big endian. + * + * Return 0 on success or else a negative errno message of sendmsg() op. + */ +static int send_header(struct lttcomm_sock *sock, uint64_t data_size, + uint32_t cmd, uint32_t cmd_version) +{ + int ret; + ssize_t size; + struct lttcomm_jul_hdr msg; + + assert(sock); + + msg.data_size = htobe64(data_size); + msg.cmd = htobe32(cmd); + msg.cmd_version = htobe32(cmd_version); + + size = sock->ops->sendmsg(sock, &msg, sizeof(msg), 0); + if (size < sizeof(msg)) { + ret = -errno; + goto error; + } + ret = 0; + +error: + return ret; +} + +/* + * Communication call with the Java agent. Send the payload to the given + * socket. The header MUST be sent prior to this call. + * + * Return 0 on success or else a negative errno value of sendmsg() op. + */ +static int send_payload(struct lttcomm_sock *sock, void *data, + size_t size) +{ + int ret; + ssize_t len; + + assert(sock); + assert(data); + + len = sock->ops->sendmsg(sock, data, size, 0); + if (len < size) { + ret = -errno; + goto error; + } + ret = 0; + +error: + return ret; +} + +/* + * Communication call with the Java agent. Receive reply from the agent using + * the given socket. + * + * Return 0 on success or else a negative errno value from recvmsg() op. + */ +static int recv_reply(struct lttcomm_sock *sock, void *buf, size_t size) +{ + int ret; + ssize_t len; + + assert(sock); + assert(buf); + + len = sock->ops->recvmsg(sock, buf, size, 0); + if (len < size) { + ret = -errno; + goto error; + } + ret = 0; + +error: + return ret; +} + +/* + * Internal enable JUL event call on a JUL application. This function + * communicates with the Java agent to enable a given event (Logger name). + * + * Return LTTNG_OK on success or else a LTTNG_ERR* code. + */ +static int enable_event(struct jul_app *app, struct jul_event *event) +{ + int ret; + uint64_t data_size; + struct lttcomm_jul_enable msg; + struct lttcomm_jul_generic_reply reply; + + assert(app); + assert(app->sock); + assert(event); + + DBG2("JUL enabling event %s for app pid: %d and socket %d", event->name, + app->pid, app->sock->fd); + + data_size = sizeof(msg); + + ret = send_header(app->sock, data_size, JUL_CMD_ENABLE, 0); + if (ret < 0) { + goto error_io; + } + + strncpy(msg.name, event->name, sizeof(msg.name)); + ret = send_payload(app->sock, &msg, sizeof(msg)); + if (ret < 0) { + goto error_io; + } + + ret = recv_reply(app->sock, &reply, sizeof(reply)); + if (ret < 0) { + goto error_io; + } + + switch (be32toh(reply.ret_code)) { + case JUL_RET_CODE_SUCCESS: + break; + case JUL_RET_CODE_UNKNOWN_NAME: + ret = LTTNG_ERR_UST_EVENT_NOT_FOUND; + goto error; + default: + ERR("Java agent returned an unknown code: %" PRIu32, + be32toh(reply.ret_code)); + ret = LTTNG_ERR_FATAL; + goto error; + } + + return LTTNG_OK; + +error_io: + ret = LTTNG_ERR_UST_ENABLE_FAIL; +error: + return ret; +} + +/* + * Internal disable JUL event call on a JUL application. This function + * communicates with the Java agent to disable a given event (Logger name). + * + * Return LTTNG_OK on success or else a LTTNG_ERR* code. + */ +static int disable_event(struct jul_app *app, struct jul_event *event) +{ + int ret; + uint64_t data_size; + struct lttcomm_jul_disable msg; + struct lttcomm_jul_generic_reply reply; + + assert(app); + assert(app->sock); + assert(event); + + DBG2("JUL disabling event %s for app pid: %d and socket %d", event->name, + app->pid, app->sock->fd); + + data_size = sizeof(msg); + + ret = send_header(app->sock, data_size, JUL_CMD_DISABLE, 0); + if (ret < 0) { + goto error_io; + } + + strncpy(msg.name, event->name, sizeof(msg.name)); + ret = send_payload(app->sock, &msg, sizeof(msg)); + if (ret < 0) { + goto error_io; + } + + ret = recv_reply(app->sock, &reply, sizeof(reply)); + if (ret < 0) { + goto error_io; + } + + switch (be32toh(reply.ret_code)) { + case JUL_RET_CODE_SUCCESS: + break; + case JUL_RET_CODE_UNKNOWN_NAME: + ret = LTTNG_ERR_UST_EVENT_NOT_FOUND; + goto error; + default: + ERR("Java agent returned an unknown code: %" PRIu32, + be32toh(reply.ret_code)); + ret = LTTNG_ERR_FATAL; + goto error; + } + + return LTTNG_OK; + +error_io: + ret = LTTNG_ERR_UST_DISABLE_FAIL; +error: + return ret; +} + +/* + * Enable JUL event on every JUL applications registered with the session + * daemon. + * + * Return LTTNG_OK on success or else a LTTNG_ERR* code. + */ +int jul_enable_event(struct jul_event *event) +{ + int ret; + struct jul_app *app; + struct lttng_ht_iter iter; + + assert(event); + + rcu_read_lock(); + + cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app, + node.node) { + /* Enable event on JUL application through TCP socket. */ + ret = enable_event(app, event); + if (ret != LTTNG_OK) { + goto error; + } + event->enabled = 1; + } + + ret = LTTNG_OK; + +error: + rcu_read_unlock(); + return ret; +} + +/* + * Disable JUL event on every JUL applications registered with the session + * daemon. + * + * Return LTTNG_OK on success or else a LTTNG_ERR* code. + */ +int jul_disable_event(struct jul_event *event) +{ + int ret; + struct jul_app *app; + struct lttng_ht_iter iter; + + assert(event); + + rcu_read_lock(); + + cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app, + node.node) { + /* Enable event on JUL application through TCP socket. */ + ret = disable_event(app, event); + if (ret != LTTNG_OK) { + goto error; + } + event->enabled = 0; + } + + ret = LTTNG_OK; + +error: + rcu_read_unlock(); + return ret; +} + +/* + * Ask every java agent for the list of possible event (logger name). Events is + * allocated with the events of every JUL application. + * + * Return the number of events or else a negative value. + */ +int jul_list_events(struct lttng_event **events) +{ + int ret; + size_t nbmem, count = 0; + struct jul_app *app; + struct lttng_event *tmp_events; + struct lttng_ht_iter iter; + + assert(events); + + nbmem = UST_APP_EVENT_LIST_SIZE; + tmp_events = zmalloc(nbmem * sizeof(*tmp_events)); + if (!tmp_events) { + PERROR("zmalloc jul list events"); + ret = -ENOMEM; + goto error; + } + + rcu_read_lock(); + cds_lfht_for_each_entry(jul_apps_ht_by_sock->ht, &iter.iter, app, + node.node) { + ssize_t nb_ev; + struct lttng_event *jul_events; + + nb_ev = list_events(app, &jul_events); + if (nb_ev < 0) { + ret = nb_ev; + rcu_read_unlock(); + goto error; + } + + if (count >= nbmem) { + /* In case the realloc fails, we free the memory */ + void *ptr; + + DBG2("Reallocating JUL event list from %zu to %zu entries", nbmem, + 2 * nbmem); + nbmem *= 2; + ptr = realloc(tmp_events, nbmem * sizeof(*tmp_events)); + if (!ptr) { + PERROR("realloc JUL events"); + free(tmp_events); + ret = -ENOMEM; + rcu_read_unlock(); + goto error; + } + tmp_events = ptr; + } + memcpy(tmp_events + (count * sizeof(*tmp_events)), jul_events, + nb_ev * sizeof(*tmp_events)); + free(jul_events); + count += nb_ev; + } + rcu_read_unlock(); + + ret = count; + *events = tmp_events; + +error: + return ret; +} + +/* + * Create a JUL app object using the given PID. + * + * Return newly allocated object or else NULL on error. + */ +struct jul_app *jul_create_app(pid_t pid, struct lttcomm_sock *sock) +{ + struct jul_app *app; + + assert(sock); + + app = zmalloc(sizeof(*app)); + if (!app) { + PERROR("zmalloc JUL create"); + goto error; + } + + app->pid = pid; + app->sock = sock; + /* Flag it invalid until assignation. */ + app->ust_app_sock = -1; + lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd); + +error: + return app; +} + +/* + * Lookup JUL app by socket in the global hash table. + * + * RCU read side lock MUST be acquired. + * + * Return object if found else NULL. + */ +struct jul_app *jul_find_app_by_sock(int sock) +{ + struct lttng_ht_node_ulong *node; + struct lttng_ht_iter iter; + struct jul_app *app; + + assert(sock >= 0); + + lttng_ht_lookup(jul_apps_ht_by_sock, (void *)((unsigned long) sock), &iter); + node = lttng_ht_iter_get_node_ulong(&iter); + if (node == NULL) { + goto error; + } + app = caa_container_of(node, struct jul_app, node); + + DBG3("JUL app pid %d found by sock %d.", app->pid, sock); + return app; + +error: + DBG3("JUL app NOT found by sock %d.", sock); + return NULL; +} + +/* + * Add JUL application object to a given hash table. + */ +void jul_add_app(struct jul_app *app) +{ + assert(app); + + DBG3("JUL adding app sock: %d and pid: %d to ht", app->sock->fd, app->pid); + + rcu_read_lock(); + lttng_ht_add_unique_ulong(jul_apps_ht_by_sock, &app->node); + rcu_read_unlock(); +} + +/* + * Attach a given JUL application to an UST app object. This is done by copying + * the socket fd value into the ust app obj. atomically. + */ +void jul_attach_app(struct jul_app *japp) +{ + struct ust_app *uapp; + + assert(japp); + + rcu_read_lock(); + uapp = ust_app_find_by_pid(japp->pid); + if (!uapp) { + goto end; + } + + uatomic_set(&uapp->jul_app_sock, japp->sock->fd); + + DBG3("JUL app pid: %d, sock: %d attached to UST app.", japp->pid, + japp->sock->fd); + +end: + rcu_read_unlock(); + return; +} + +/* + * Remove JUL app. reference from an UST app object and set it to NULL. + */ +void jul_detach_app(struct jul_app *japp) +{ + struct ust_app *uapp; + + assert(japp); + + rcu_read_lock(); + + if (japp->ust_app_sock < 0) { + goto end; + } + + uapp = ust_app_find_by_sock(japp->ust_app_sock); + if (!uapp) { + goto end; + } + + uapp->jul_app_sock = -1; + +end: + rcu_read_unlock(); + return; +} + +/* + * Delete JUL application from the global hash table. + */ +void jul_delete_app(struct jul_app *app) +{ + int ret; + struct lttng_ht_iter iter; + + assert(app); + + DBG3("JUL deleting app pid: %d and sock: %d", app->pid, app->sock->fd); + + iter.iter.node = &app->node.node; + rcu_read_lock(); + ret = lttng_ht_del(jul_apps_ht_by_sock, &iter); + rcu_read_unlock(); + assert(!ret); +} + +/* + * Destroy a JUL application object by detaching it from its corresponding UST + * app if one, closing the socket and freeing the memory. + */ +void jul_destroy_app(struct jul_app *app) +{ + assert(app); + + if (app->sock) { + app->sock->ops->close(app->sock); + lttcomm_destroy_sock(app->sock); + } + + call_rcu(&app->node.head, destroy_app_jul_rcu); +} + /* * Initialize an already allocated JUL domain object. * @@ -79,6 +587,7 @@ struct jul_event *jul_create_event(const char *name) if (name) { strncpy(event->name, name, sizeof(event->name)); event->name[sizeof(event->name) - 1] = '\0'; + lttng_ht_node_init_str(&event->node, event->name); } error: @@ -96,7 +605,9 @@ void jul_add_event(struct jul_event *event, struct jul_domain *dom) DBG3("JUL adding event %s to domain", event->name); + rcu_read_lock(); lttng_ht_add_unique_str(dom->events, &event->node); + rcu_read_unlock(); } /* @@ -191,5 +702,61 @@ void jul_destroy_domain(struct jul_domain *dom) } rcu_read_unlock(); - ht_cleanup_push(dom->events); + lttng_ht_destroy(dom->events); +} + +/* + * Initialize JUL subsystem. + */ +int jul_init(void) +{ + jul_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); + if (!jul_apps_ht_by_sock) { + return -1; + } + + return 0; +} + +/* + * Update a JUL application (given socket) using the given domain. + * + * Note that this function is most likely to be used with a tracing session + * thus the caller should make sure to hold the appropriate lock(s). + */ +void jul_update(struct jul_domain *domain, int sock) +{ + int ret; + struct jul_app *app; + struct jul_event *event; + struct lttng_ht_iter iter; + + assert(domain); + assert(sock >= 0); + + DBG("JUL updating app socket %d", sock); + + rcu_read_lock(); + cds_lfht_for_each_entry(domain->events->ht, &iter.iter, event, node.node) { + /* Skip event if disabled. */ + if (!event->enabled) { + continue; + } + + app = jul_find_app_by_sock(sock); + /* + * We are in the registration path thus if the application is gone, + * there is a serious code flow error. + */ + assert(app); + + ret = enable_event(app, event); + if (ret != LTTNG_OK) { + DBG2("JUL update unable to enable event %s on app pid: %d sock %d", + event->name, app->pid, app->sock->fd); + /* Let's try the others here and don't assume the app is dead. */ + continue; + } + } + rcu_read_unlock(); }