X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fust-app.c;h=d3c28883c74bea2416d9afb8a5f0db7c87c03f91;hp=0e97b944ca094722f4bb36f468455633e1333608;hb=44cdb3a261262ebabea3fb0cae9360ac05e66f2c;hpb=8de8806159d9d2544e2bc3091cc3c98abe4d97ad diff --git a/src/bin/lttng-sessiond/ust-app.c b/src/bin/lttng-sessiond/ust-app.c index 0e97b944c..d3c28883c 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include @@ -38,13 +37,18 @@ #include "health-sessiond.h" #include "ust-app.h" #include "ust-consumer.h" -#include "ust-ctl.h" +#include "lttng-ust-ctl.h" +#include "lttng-ust-error.h" #include "utils.h" #include "session.h" #include "lttng-sessiond.h" #include "notification-thread-commands.h" #include "rotate.h" +struct lttng_ht *ust_app_ht; +struct lttng_ht *ust_app_ht_by_sock; +struct lttng_ht *ust_app_ht_by_notify_sock; + static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); @@ -741,6 +745,10 @@ error: * nullified. The session lock MUST be held unless the application is * in the destroy path. * + * Do not hold the registry lock while communicating with the consumerd, because + * doing so causes inter-process deadlocks between consumerd and sessiond with + * the metadata request notification. + * * Return 0 on success else a negative value. */ static int close_metadata(struct ust_registry_session *registry, @@ -748,6 +756,8 @@ static int close_metadata(struct ust_registry_session *registry, { int ret; struct consumer_socket *socket; + uint64_t metadata_key; + bool registry_was_already_closed; assert(registry); assert(consumer); @@ -755,8 +765,19 @@ static int close_metadata(struct ust_registry_session *registry, rcu_read_lock(); pthread_mutex_lock(®istry->lock); + metadata_key = registry->metadata_key; + registry_was_already_closed = registry->metadata_closed; + if (metadata_key != 0) { + /* + * Metadata closed. Even on error this means that the consumer + * is not responding or not found so either way a second close + * should NOT be emit for this registry. + */ + registry->metadata_closed = 1; + } + pthread_mutex_unlock(®istry->lock); - if (!registry->metadata_key || registry->metadata_closed) { + if (metadata_key == 0 || registry_was_already_closed) { ret = 0; goto end; } @@ -766,23 +787,15 @@ static int close_metadata(struct ust_registry_session *registry, consumer); if (!socket) { ret = -1; - goto error; + goto end; } - ret = consumer_close_metadata(socket, registry->metadata_key); + ret = consumer_close_metadata(socket, metadata_key); if (ret < 0) { - goto error; + goto end; } -error: - /* - * Metadata closed. Even on error this means that the consumer is not - * responding or not found so either way a second close should NOT be emit - * for this registry. - */ - registry->metadata_closed = 1; end: - pthread_mutex_unlock(®istry->lock); rcu_read_unlock(); return ret; } @@ -1100,7 +1113,7 @@ struct ust_app_event *alloc_ust_app_event(char *name, /* Init most of the default value by allocating and zeroing */ ua_event = zmalloc(sizeof(struct ust_app_event)); if (ua_event == NULL) { - PERROR("malloc"); + PERROR("Failed to allocate ust_app_event structure"); goto error; } @@ -1892,12 +1905,12 @@ static void shadow_copy_session(struct ust_app_session *ua_sess, switch (ua_sess->buffer_type) { case LTTNG_BUFFER_PER_PID: ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), - DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", + "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, datetime); break; case LTTNG_BUFFER_PER_UID: ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), - DEFAULT_UST_TRACE_UID_PATH, + "/" DEFAULT_UST_TRACE_UID_PATH, app->uid, app->bits_per_long); break; default: @@ -3133,7 +3146,7 @@ int create_ust_app_event(struct ust_app_session *ua_sess, ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); if (ua_event == NULL) { - /* Only malloc can failed so something is really wrong */ + /* Only failure mode of alloc_ust_app_event(). */ ret = -ENOMEM; goto end; } @@ -3142,8 +3155,19 @@ int create_ust_app_event(struct ust_app_session *ua_sess, /* Create it on the tracer side */ ret = create_ust_event(app, ua_sess, ua_chan, ua_event); if (ret < 0) { - /* Not found previously means that it does not exist on the tracer */ - assert(ret != -LTTNG_UST_ERR_EXIST); + /* + * Not found previously means that it does not exist on the + * tracer. If the application reports that the event existed, + * it means there is a bug in the sessiond or lttng-ust + * (or corruption, etc.) + */ + if (ret == -LTTNG_UST_ERR_EXIST) { + ERR("Tracer for application reported that an event being created already existed: " + "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", + uevent->attr.name, + app->pid, app->ppid, app->uid, + app->gid); + } goto error; } @@ -4141,75 +4165,6 @@ end: return ret; } -/* - * For a specific UST session, create the channel for all registered apps. - */ -int ust_app_create_channel_glb(struct ltt_ust_session *usess, - struct ltt_ust_channel *uchan) -{ - int ret = 0; - struct cds_lfht_iter iter; - struct ust_app *app; - - assert(usess); - assert(usess->active); - assert(uchan); - - DBG2("UST app adding channel %s to UST domain for session id %" PRIu64, - uchan->name, usess->id); - - rcu_read_lock(); - /* For every registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter, app, pid_n.node) { - struct ust_app_session *ua_sess; - int session_was_created = 0; - - if (!app->compatible || - !trace_ust_pid_tracker_lookup(usess, app->pid)) { - goto error_rcu_unlock; - } - - /* - * Create session on the tracer side and add it to app session HT. Note - * that if session exist, it will simply return a pointer to the ust - * app session. - */ - ret = find_or_create_ust_app_session(usess, app, &ua_sess, - &session_was_created); - if (ret < 0) { - switch (ret) { - case -ENOTCONN: - /* - * The application's socket is not valid. Either a bad - * socket or a timeout on it. We can't inform the caller - * that for a specific app, the session failed so lets - * continue here; it is not an error. - */ - ret = 0; - goto error_rcu_unlock; - case -ENOMEM: - default: - goto error_rcu_unlock; - } - } - - if (ua_sess->deleted) { - continue; - } - ret = ust_app_channel_create(usess, ua_sess, uchan, app, NULL); - if (ret) { - if (session_was_created) { - destroy_app_session(app, ua_sess); - } - goto error_rcu_unlock; - } - } - -error_rcu_unlock: - rcu_read_unlock(); - return ret; -} - /* * Enable event for a specific session and channel on the tracer. */ @@ -4396,6 +4351,11 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) goto end; } + if (ua_sess->enabled) { + pthread_mutex_unlock(&ua_sess->lock); + goto end; + } + /* Upon restart, we skip the setup, already done */ if (ua_sess->started) { goto skip_setup; @@ -4413,7 +4373,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) health_code_update(); skip_setup: - /* This start the UST tracing */ + /* This starts the UST tracing */ pthread_mutex_lock(&app->sock_lock); ret = ustctl_start_session(app->sock, ua_sess->handle); pthread_mutex_unlock(&app->sock_lock); @@ -4436,6 +4396,7 @@ skip_setup: /* Indicate that the session has been started once */ ua_sess->started = 1; + ua_sess->enabled = 1; pthread_mutex_unlock(&ua_sess->lock); @@ -4525,6 +4486,7 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) } health_code_update(); + ua_sess->enabled = 0; /* Quiescent wait after stopping trace */ pthread_mutex_lock(&app->sock_lock); @@ -5164,7 +5126,11 @@ void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) if (!app->compatible) { return; } - if (trace_ust_pid_tracker_lookup(usess, app->pid)) { + if (trace_ust_id_tracker_lookup(LTTNG_TRACKER_VPID, usess, app->pid) && + trace_ust_id_tracker_lookup( + LTTNG_TRACKER_VUID, usess, app->uid) && + trace_ust_id_tracker_lookup( + LTTNG_TRACKER_VGID, usess, app->gid)) { /* * Synchronize the application's internal tracing configuration * and start tracing. @@ -5885,6 +5851,7 @@ enum lttng_error_code ust_app_snapshot_record( enum lttng_error_code status = LTTNG_OK; struct lttng_ht_iter iter; struct ust_app *app; + char *trace_path = NULL; assert(usess); assert(output); @@ -5899,8 +5866,8 @@ enum lttng_error_code ust_app_snapshot_record( cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { struct buffer_reg_channel *reg_chan; struct consumer_socket *socket; - char *trace_path = NULL; char pathname[PATH_MAX]; + size_t consumer_path_offset = 0; if (!reg->registry->reg.ust->metadata_key) { /* Skip since no metadata is present */ @@ -5916,19 +5883,18 @@ enum lttng_error_code ust_app_snapshot_record( } memset(pathname, 0, sizeof(pathname)); - /* - * DEFAULT_UST_TRACE_UID_PATH already contains a path - * separator. - */ ret = snprintf(pathname, sizeof(pathname), - DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH, + DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, reg->uid, reg->bits_per_long); if (ret < 0) { PERROR("snprintf snapshot path"); status = LTTNG_ERR_INVALID; goto error; } - trace_path = setup_channel_trace_path(usess->consumer, pathname); + /* Free path allowed on previous iteration. */ + free(trace_path); + trace_path = setup_channel_trace_path(usess->consumer, pathname, + &consumer_path_offset); if (!trace_path) { status = LTTNG_ERR_INVALID; goto error; @@ -5939,17 +5905,16 @@ enum lttng_error_code ust_app_snapshot_record( status = consumer_snapshot_channel(socket, reg_chan->consumer_key, output, 0, usess->uid, - usess->gid, trace_path, wait, + usess->gid, &trace_path[consumer_path_offset], wait, nb_packets_per_stream); if (status != LTTNG_OK) { - free(trace_path); goto error; } } status = consumer_snapshot_channel(socket, reg->registry->reg.ust->metadata_key, output, 1, - usess->uid, usess->gid, trace_path, wait, 0); - free(trace_path); + usess->uid, usess->gid, &trace_path[consumer_path_offset], + wait, 0); if (status != LTTNG_OK) { goto error; } @@ -5964,8 +5929,8 @@ enum lttng_error_code ust_app_snapshot_record( struct ust_app_channel *ua_chan; struct ust_app_session *ua_sess; struct ust_registry_session *registry; - char *trace_path = NULL; char pathname[PATH_MAX]; + size_t consumer_path_offset = 0; ua_sess = lookup_session_by_app(usess, app); if (!ua_sess) { @@ -5983,14 +5948,17 @@ enum lttng_error_code ust_app_snapshot_record( /* Add the UST default trace dir to path. */ memset(pathname, 0, sizeof(pathname)); - ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "%s", + ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", ua_sess->path); if (ret < 0) { status = LTTNG_ERR_INVALID; PERROR("snprintf snapshot path"); goto error; } - trace_path = setup_channel_trace_path(usess->consumer, pathname); + /* Free path allowed on previous iteration. */ + free(trace_path); + trace_path = setup_channel_trace_path(usess->consumer, pathname, + &consumer_path_offset); if (!trace_path) { status = LTTNG_ERR_INVALID; goto error; @@ -6003,16 +5971,14 @@ enum lttng_error_code ust_app_snapshot_record( .uid, ua_sess->effective_credentials .gid, - trace_path, wait, + &trace_path[consumer_path_offset], wait, nb_packets_per_stream); switch (status) { case LTTNG_OK: break; case LTTNG_ERR_CHAN_NOT_FOUND: - free(trace_path); continue; default: - free(trace_path); goto error; } } @@ -6026,8 +5992,7 @@ enum lttng_error_code ust_app_snapshot_record( registry->metadata_key, output, 1, ua_sess->effective_credentials.uid, ua_sess->effective_credentials.gid, - trace_path, wait, 0); - free(trace_path); + &trace_path[consumer_path_offset], wait, 0); switch (status) { case LTTNG_OK: break; @@ -6045,6 +6010,7 @@ enum lttng_error_code ust_app_snapshot_record( } error: + free(trace_path); rcu_read_unlock(); return status; } @@ -6306,6 +6272,11 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) struct buffer_reg_channel *reg_chan; struct consumer_socket *socket; + if (!reg->registry->reg.ust->metadata_key) { + /* Skip since no metadata is present */ + continue; + } + /* Get consumer socket to use to push the metadata.*/ socket = consumer_find_socket_by_bitness(reg->bits_per_long, usess->consumer); @@ -6440,7 +6411,7 @@ enum lttng_error_code ust_app_create_channel_subdirectories( cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { fmt_ret = asprintf(&pathname_index, - DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, + DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, reg->uid, reg->bits_per_long); if (fmt_ret < 0) { ERR("Failed to format channel index directory"); @@ -6467,6 +6438,17 @@ enum lttng_error_code ust_app_create_channel_subdirectories( { struct ust_app *app; + /* + * Create the toplevel ust/ directory in case no apps are running. + */ + chunk_status = lttng_trace_chunk_create_subdirectory( + usess->current_trace_chunk, + DEFAULT_UST_TRACE_DIR); + if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { + ret = LTTNG_ERR_CREATE_DIR_FAIL; + goto error; + } + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { struct ust_app_session *ua_sess; @@ -6485,7 +6467,7 @@ enum lttng_error_code ust_app_create_channel_subdirectories( } fmt_ret = asprintf(&pathname_index, - DEFAULT_UST_TRACE_DIR "%s/" DEFAULT_INDEX_DIR, + DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, ua_sess->path); if (fmt_ret < 0) { ERR("Failed to format channel index directory"); @@ -6516,3 +6498,151 @@ error: rcu_read_unlock(); return ret; } + +/* + * Clear all the channels of a session. + * + * Return LTTNG_OK on success or else an LTTng error code. + */ +enum lttng_error_code ust_app_clear_session(struct ltt_session *session) +{ + int ret; + enum lttng_error_code cmd_ret = LTTNG_OK; + struct lttng_ht_iter iter; + struct ust_app *app; + struct ltt_ust_session *usess = session->ust_session; + + assert(usess); + + rcu_read_lock(); + + if (usess->active) { + ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); + cmd_ret = LTTNG_ERR_FATAL; + goto end; + } + + switch (usess->buffer_type) { + case LTTNG_BUFFER_PER_UID: + { + struct buffer_reg_uid *reg; + + cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { + struct buffer_reg_channel *reg_chan; + struct consumer_socket *socket; + + /* Get consumer socket to use to push the metadata.*/ + socket = consumer_find_socket_by_bitness(reg->bits_per_long, + usess->consumer); + if (!socket) { + cmd_ret = LTTNG_ERR_INVALID; + goto error_socket; + } + + /* Clear the data channels. */ + cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, + reg_chan, node.node) { + ret = consumer_clear_channel(socket, + reg_chan->consumer_key); + if (ret < 0) { + goto error; + } + } + + (void) push_metadata(reg->registry->reg.ust, usess->consumer); + + /* + * Clear the metadata channel. + * Metadata channel is not cleared per se but we still need to + * perform a rotation operation on it behind the scene. + */ + ret = consumer_clear_channel(socket, + reg->registry->reg.ust->metadata_key); + if (ret < 0) { + goto error; + } + } + break; + } + case LTTNG_BUFFER_PER_PID: + { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { + struct consumer_socket *socket; + struct lttng_ht_iter chan_iter; + struct ust_app_channel *ua_chan; + struct ust_app_session *ua_sess; + struct ust_registry_session *registry; + + ua_sess = lookup_session_by_app(usess, app); + if (!ua_sess) { + /* Session not associated with this app. */ + continue; + } + + /* Get the right consumer socket for the application. */ + socket = consumer_find_socket_by_bitness(app->bits_per_long, + usess->consumer); + if (!socket) { + cmd_ret = LTTNG_ERR_INVALID; + goto error_socket; + } + + registry = get_session_registry(ua_sess); + if (!registry) { + DBG("Application session is being torn down. Skip application."); + continue; + } + + /* Clear the data channels. */ + cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, + ua_chan, node.node) { + ret = consumer_clear_channel(socket, ua_chan->key); + if (ret < 0) { + /* Per-PID buffer and application going away. */ + if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { + continue; + } + goto error; + } + } + + (void) push_metadata(registry, usess->consumer); + + /* + * Clear the metadata channel. + * Metadata channel is not cleared per se but we still need to + * perform rotation operation on it behind the scene. + */ + ret = consumer_clear_channel(socket, registry->metadata_key); + if (ret < 0) { + /* Per-PID buffer and application going away. */ + if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { + continue; + } + goto error; + } + } + break; + } + default: + assert(0); + break; + } + + cmd_ret = LTTNG_OK; + goto end; + +error: + switch (-ret) { + case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: + cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; + break; + default: + cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; + } + +error_socket: +end: + rcu_read_unlock(); + return cmd_ret; +}