X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fcmd.c;h=589cfd95eb13be53bdf26a0994c027070b13ddf5;hb=5266dbf6ca1ab794bddeb50766b4e5f0ff423a3a;hp=62324455fe68aeee93fca8a4ebd8c00f8c19b6a3;hpb=9247b8d08f812a0830f75bf36ad1eb99849a3b98;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 62324455f..589cfd95e 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -17,6 +17,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -173,7 +174,10 @@ static void list_lttng_channels(int domain, struct ltt_session *session, rcu_read_lock(); cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, &iter.iter, uchan, node.node) { - strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); + if (lttng_strncpy(channels[i].name, uchan->name, + LTTNG_SYMBOL_NAME_LEN)) { + break; + } channels[i].attr.overwrite = uchan->attr.overwrite; channels[i].attr.subbuf_size = uchan->attr.subbuf_size; channels[i].attr.num_subbuf = uchan->attr.num_subbuf; @@ -219,7 +223,9 @@ static int list_lttng_agent_events(struct agent *agt, DBG3("Listing agent events"); + rcu_read_lock(); nb_event = lttng_ht_get_count(agt->events); + rcu_read_unlock(); if (nb_event == 0) { ret = nb_event; goto error; @@ -515,12 +521,15 @@ static int add_uri_to_consumer(struct consumer_output *consumer, DBG2("Setting trace directory path from URI to %s", uri->dst.path); memset(consumer->dst.trace_path, 0, sizeof(consumer->dst.trace_path)); - strncpy(consumer->dst.trace_path, uri->dst.path, - sizeof(consumer->dst.trace_path)); + /* Explicit length checks for strcpy and strcat. */ + if (strlen(uri->dst.path) + strlen(default_trace_dir) + >= sizeof(consumer->dst.trace_path)) { + ret = LTTNG_ERR_FATAL; + goto error; + } + strcpy(consumer->dst.trace_path, uri->dst.path); /* Append default trace dir */ - strncat(consumer->dst.trace_path, default_trace_dir, - sizeof(consumer->dst.trace_path) - - strlen(consumer->dst.trace_path) - 1); + strcat(consumer->dst.trace_path, default_trace_dir); /* Flag consumer as local. */ consumer->type = CONSUMER_DST_LOCAL; break; @@ -936,11 +945,21 @@ int cmd_enable_channel(struct ltt_session *session, int ret; struct ltt_ust_session *usess = session->ust_session; struct lttng_ht *chan_ht; + size_t len; assert(session); assert(attr); assert(domain); + len = strnlen(attr->name, sizeof(attr->name)); + + /* Validate channel name */ + if (attr->name[0] == '.' || + memchr(attr->name, '/', len) != NULL) { + ret = LTTNG_ERR_INVALID_CHANNEL_NAME; + goto end; + } + DBG("Enabling channel %s for session %s", attr->name, session->name); rcu_read_lock(); @@ -1021,6 +1040,7 @@ int cmd_enable_channel(struct ltt_session *session, error: rcu_read_unlock(); +end: return ret; } @@ -1039,7 +1059,8 @@ int cmd_disable_event(struct ltt_session *session, int domain, event_name = event->name; - if (event->loglevel_type || event->loglevel || event->enabled + /* Error out on unhandled search criteria */ + if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid || event->filter || event->exclusion) { return LTTNG_ERR_UNK; } @@ -1122,7 +1143,14 @@ int cmd_disable_event(struct ltt_session *session, int domain, switch (event->type) { case LTTNG_EVENT_ALL: - ret = event_ust_disable_tracepoint(usess, uchan, event_name); + if (strlen(event->name) == 1 && + !strncmp(event->name, "*", 1)) { + ret = event_ust_disable_all_tracepoints(usess, + uchan); + } else { + ret = event_ust_disable_tracepoint(usess, uchan, + event_name); + } if (ret != LTTNG_OK) { goto error; } @@ -1384,6 +1412,7 @@ end: /* * Command LTTNG_ENABLE_EVENT processed by the client thread. + * We own filter, exclusion, and filter_expression. */ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, char *channel_name, struct lttng_event *event, @@ -1439,7 +1468,12 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, ret = LTTNG_ERR_FATAL; goto error; } - strncpy(attr->name, channel_name, sizeof(attr->name)); + if (lttng_strncpy(attr->name, channel_name, + sizeof(attr->name))) { + ret = LTTNG_ERR_INVALID; + free(attr); + goto error; + } ret = cmd_enable_channel(session, domain, attr, wpipe); if (ret != LTTNG_OK) { @@ -1517,7 +1551,12 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, ret = LTTNG_ERR_FATAL; goto error; } - strncpy(attr->name, channel_name, sizeof(attr->name)); + if (lttng_strncpy(attr->name, channel_name, + sizeof(attr->name))) { + ret = LTTNG_ERR_INVALID; + free(attr); + goto error; + } ret = cmd_enable_channel(session, domain, attr, wpipe); if (ret != LTTNG_OK) { @@ -1535,6 +1574,10 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, /* At this point, the session and channel exist on the tracer */ ret = event_ust_enable_tracepoint(usess, uchan, event, filter_expression, filter, exclusion); + /* We have passed ownership */ + filter_expression = NULL; + filter = NULL; + exclusion = NULL; if (ret != LTTNG_OK) { goto error; } @@ -1555,7 +1598,7 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, if (!agt) { agt = agent_create(domain->type); if (!agt) { - ret = -LTTNG_ERR_NOMEM; + ret = LTTNG_ERR_NOMEM; goto error; } agent_add(agt, usess->agents); @@ -1567,7 +1610,7 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; default_event_name = event_get_default_agent_ust_name(domain->type); if (!default_event_name) { - ret = -LTTNG_ERR_FATAL; + ret = LTTNG_ERR_FATAL; goto error; } strncpy(uevent.name, default_event_name, sizeof(uevent.name)); @@ -1587,17 +1630,57 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, default_chan_name = DEFAULT_JUL_CHANNEL_NAME; } - ret = cmd_enable_event(session, &tmp_dom, (char *) default_chan_name, - &uevent, filter_expression, filter, NULL, wpipe); + { + struct lttng_filter_bytecode *filter_copy = NULL; + char *filter_expression_copy = NULL; + + if (filter) { + filter_copy = zmalloc( + sizeof(struct lttng_filter_bytecode) + + filter->len); + if (!filter_copy) { + ret = LTTNG_ERR_NOMEM; + goto error; + } + + memcpy(filter_copy, filter, + sizeof(struct lttng_filter_bytecode) + + filter->len); + } + + if (filter_expression) { + filter_expression_copy = + strdup(filter_expression); + if (!filter_expression) { + ret = LTTNG_ERR_NOMEM; + goto error_free_copy; + } + } + + ret = cmd_enable_event(session, &tmp_dom, + (char *) default_chan_name, + &uevent, filter_expression_copy, + filter_copy, NULL, wpipe); + filter_copy = NULL; + filter_expression_copy = NULL; +error_free_copy: + free(filter_copy); + free(filter_expression_copy); + } + if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) { goto error; } /* The wild card * means that everything should be enabled. */ if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { - ret = event_agent_enable_all(usess, agt, event, filter); + ret = event_agent_enable_all(usess, agt, event, filter, + filter_expression); + filter = NULL; } else { - ret = event_agent_enable(usess, agt, event, filter); + ret = event_agent_enable(usess, agt, event, filter, + filter_expression); + filter = NULL; } if (ret != LTTNG_OK) { goto error; @@ -1618,6 +1701,9 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, ret = LTTNG_OK; error: + free(filter_expression); + free(filter); + free(exclusion); rcu_read_unlock(); return ret; } @@ -1836,7 +1922,9 @@ int cmd_start_trace(struct ltt_session *session) * possible to enable channel thus inform the client. */ if (usess && usess->domain_global.channels) { + rcu_read_lock(); nb_chan += lttng_ht_get_count(usess->domain_global.channels); + rcu_read_unlock(); } if (ksession) { nb_chan += ksession->channel_count; @@ -1905,7 +1993,15 @@ int cmd_stop_trace(struct ltt_session *session) if (ksession && ksession->active) { DBG("Stop kernel tracing"); - /* Flush metadata if exist */ + ret = kernel_stop_session(ksession); + if (ret < 0) { + ret = LTTNG_ERR_KERN_STOP_FAIL; + goto error; + } + + kernel_wait_quiescent(kernel_tracer_fd); + + /* Flush metadata after stopping (if exists) */ if (ksession->metadata_stream_fd >= 0) { ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); if (ret < 0) { @@ -1913,7 +2009,7 @@ int cmd_stop_trace(struct ltt_session *session) } } - /* Flush all buffers before stopping */ + /* Flush all buffers after stopping */ cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { ret = kernel_flush_buffer(kchan); if (ret < 0) { @@ -1921,14 +2017,6 @@ int cmd_stop_trace(struct ltt_session *session) } } - ret = kernel_stop_session(ksession); - if (ret < 0) { - ret = LTTNG_ERR_KERN_STOP_FAIL; - goto error; - } - - kernel_wait_quiescent(kernel_tracer_fd); - ksession->active = 0; } @@ -1957,13 +2045,12 @@ error: /* * Command LTTNG_SET_CONSUMER_URI processed by the client thread. */ -int cmd_set_consumer_uri(int domain, struct ltt_session *session, - size_t nb_uri, struct lttng_uri *uris) +int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, + struct lttng_uri *uris) { int ret, i; struct ltt_kernel_session *ksess = session->kernel_session; struct ltt_ust_session *usess = session->ust_session; - struct consumer_output *consumer = NULL; assert(session); assert(uris); @@ -1975,41 +2062,41 @@ int cmd_set_consumer_uri(int domain, struct ltt_session *session, goto error; } - /* - * This case switch makes sure the domain session has a temporary consumer - * so the URL can be set. - */ - switch (domain) { - case 0: - /* Code flow error. A session MUST always have a consumer object */ - assert(session->consumer); - /* - * The URL will be added to the tracing session consumer instead of a - * specific domain consumer. - */ - consumer = session->consumer; - break; - case LTTNG_DOMAIN_KERNEL: - /* Code flow error if we don't have a kernel session here. */ - assert(ksess); - assert(ksess->consumer); - consumer = ksess->consumer; - break; - case LTTNG_DOMAIN_UST: - /* Code flow error if we don't have a kernel session here. */ - assert(usess); - assert(usess->consumer); - consumer = usess->consumer; - break; - } - + /* Set the "global" consumer URIs */ for (i = 0; i < nb_uri; i++) { - ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name); + ret = add_uri_to_consumer(session->consumer, + &uris[i], 0, session->name); if (ret != LTTNG_OK) { goto error; } } + /* Set UST session URIs */ + if (session->ust_session) { + for (i = 0; i < nb_uri; i++) { + ret = add_uri_to_consumer( + session->ust_session->consumer, + &uris[i], LTTNG_DOMAIN_UST, + session->name); + if (ret != LTTNG_OK) { + goto error; + } + } + } + + /* Set kernel session URIs */ + if (session->kernel_session) { + for (i = 0; i < nb_uri; i++) { + ret = add_uri_to_consumer( + session->kernel_session->consumer, + &uris[i], LTTNG_DOMAIN_KERNEL, + session->name); + if (ret != LTTNG_OK) { + goto error; + } + } + } + /* * Make sure to set the session in output mode after we set URI since a * session can be created without URL (thus flagged in no output mode). @@ -2017,7 +2104,9 @@ int cmd_set_consumer_uri(int domain, struct ltt_session *session, session->output_traces = 1; if (ksess) { ksess->output_traces = 1; - } else if (usess) { + } + + if (usess) { usess->output_traces = 1; } @@ -2079,7 +2168,7 @@ int cmd_create_session_uri(char *name, struct lttng_uri *uris, } if (uris) { - ret = cmd_set_consumer_uri(0, session, nb_uri, uris); + ret = cmd_set_consumer_uri(session, nb_uri, uris); if (ret != LTTNG_OK) { goto consumer_error; } @@ -2117,7 +2206,7 @@ int cmd_create_session_snapshot(char *name, struct lttng_uri *uris, * Create session in no output mode with URIs set to NULL. The uris we've * received are for a default snapshot output if one. */ - ret = cmd_create_session_uri(name, NULL, 0, creds, -1); + ret = cmd_create_session_uri(name, NULL, 0, creds, 0); if (ret != LTTNG_OK) { goto error; } @@ -2368,12 +2457,14 @@ ssize_t cmd_list_domains(struct ltt_session *session, DBG3("Listing domains found UST global domain"); nb_dom++; + rcu_read_lock(); cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, agt, node.node) { if (agt->being_used) { nb_dom++; } } + rcu_read_unlock(); } *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); @@ -2384,6 +2475,10 @@ ssize_t cmd_list_domains(struct ltt_session *session, if (session->kernel_session != NULL) { (*domains)[index].type = LTTNG_DOMAIN_KERNEL; + + /* Kernel session buffer type is always GLOBAL */ + (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; + index++; } @@ -2392,6 +2487,7 @@ ssize_t cmd_list_domains(struct ltt_session *session, (*domains)[index].buf_type = session->ust_session->buffer_type; index++; + rcu_read_lock(); cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, agt, node.node) { if (agt->being_used) { @@ -2400,6 +2496,7 @@ ssize_t cmd_list_domains(struct ltt_session *session, index++; } } + rcu_read_unlock(); } return nb_dom; @@ -2431,8 +2528,10 @@ ssize_t cmd_list_channels(int domain, struct ltt_session *session, break; case LTTNG_DOMAIN_UST: if (session->ust_session != NULL) { + rcu_read_lock(); nb_chan = lttng_ht_get_count( - session->ust_session->domain_global.channels); + session->ust_session->domain_global.channels); + rcu_read_unlock(); } DBG3("Number of UST global channels %zd", nb_chan); if (nb_chan < 0) { @@ -2492,10 +2591,12 @@ ssize_t cmd_list_events(int domain, struct ltt_session *session, struct lttng_ht_iter iter; struct agent *agt; + rcu_read_lock(); cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, agt, node.node) { nb_event = list_lttng_agent_events(agt, events); } + rcu_read_unlock(); } break; default: @@ -2744,7 +2845,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, struct lttng_snapshot_output **outputs) { int ret, idx = 0; - struct lttng_snapshot_output *list; + struct lttng_snapshot_output *list = NULL; struct lttng_ht_iter iter; struct snapshot_output *output; @@ -2758,7 +2859,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, * set in no output mode. */ if (session->output_traces) { - ret = LTTNG_ERR_EPERM; + ret = -LTTNG_ERR_EPERM; goto error; } @@ -2769,47 +2870,56 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, list = zmalloc(session->snapshot.nb_output * sizeof(*list)); if (!list) { - ret = LTTNG_ERR_NOMEM; + ret = -LTTNG_ERR_NOMEM; goto error; } /* Copy list from session to the new list object. */ + rcu_read_lock(); cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, output, node.node) { assert(output->consumer); list[idx].id = output->id; list[idx].max_size = output->max_size; - strncpy(list[idx].name, output->name, sizeof(list[idx].name)); + if (lttng_strncpy(list[idx].name, output->name, + sizeof(list[idx].name))) { + ret = -LTTNG_ERR_INVALID; + goto error; + } if (output->consumer->type == CONSUMER_DST_LOCAL) { - strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path, - sizeof(list[idx].ctrl_url)); + if (lttng_strncpy(list[idx].ctrl_url, + output->consumer->dst.trace_path, + sizeof(list[idx].ctrl_url))) { + ret = -LTTNG_ERR_INVALID; + goto error; + } } else { /* Control URI. */ ret = uri_to_str_url(&output->consumer->dst.net.control, list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); if (ret < 0) { - ret = LTTNG_ERR_NOMEM; - goto free_error; + ret = -LTTNG_ERR_NOMEM; + goto error; } /* Data URI. */ ret = uri_to_str_url(&output->consumer->dst.net.data, list[idx].data_url, sizeof(list[idx].data_url)); if (ret < 0) { - ret = LTTNG_ERR_NOMEM; - goto free_error; + ret = -LTTNG_ERR_NOMEM; + goto error; } } idx++; } *outputs = list; - return session->snapshot.nb_output; - -free_error: - free(list); + list = NULL; + ret = session->snapshot.nb_output; error: - return -ret; + free(list); + rcu_read_unlock(); + return ret; } /* @@ -2865,7 +2975,7 @@ error: */ static int record_kernel_snapshot(struct ltt_kernel_session *ksess, struct snapshot_output *output, struct ltt_session *session, - int wait, uint64_t max_stream_size) + int wait, uint64_t nb_packets_per_stream) { int ret; @@ -2873,13 +2983,6 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess, assert(output); assert(session); - /* Get the datetime for the snapshot output directory. */ - ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, - sizeof(output->datetime)); - if (!ret) { - ret = LTTNG_ERR_INVALID; - goto error; - } /* * Copy kernel session sockets so we can communicate with the right @@ -2896,17 +2999,19 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess, goto error_snapshot; } - ret = kernel_snapshot_record(ksess, output, wait, max_stream_size); + ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream); if (ret != LTTNG_OK) { goto error_snapshot; } ret = LTTNG_OK; + goto end; error_snapshot: /* Clean up copied sockets so this output can use some other later on. */ consumer_destroy_output_sockets(output->consumer); error: +end: return ret; } @@ -2917,7 +3022,7 @@ error: */ static int record_ust_snapshot(struct ltt_ust_session *usess, struct snapshot_output *output, struct ltt_session *session, - int wait, uint64_t max_stream_size) + int wait, uint64_t nb_packets_per_stream) { int ret; @@ -2925,14 +3030,6 @@ static int record_ust_snapshot(struct ltt_ust_session *usess, assert(output); assert(session); - /* Get the datetime for the snapshot output directory. */ - ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, - sizeof(output->datetime)); - if (!ret) { - ret = LTTNG_ERR_INVALID; - goto error; - } - /* * Copy UST session sockets so we can communicate with the right * consumer for the snapshot record command. @@ -2948,15 +3045,12 @@ static int record_ust_snapshot(struct ltt_ust_session *usess, goto error_snapshot; } - ret = ust_app_snapshot_record(usess, output, wait, max_stream_size); + ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream); if (ret < 0) { switch (-ret) { case EINVAL: ret = LTTNG_ERR_INVALID; break; - case ENODATA: - ret = LTTNG_ERR_SNAPSHOT_NODATA; - break; default: ret = LTTNG_ERR_SNAPSHOT_FAIL; break; @@ -2973,67 +3067,90 @@ error: return ret; } -/* - * Return the biggest subbuffer size of all channels in the given session. - */ -static uint64_t get_session_max_subbuf_size(struct ltt_session *session) +static +uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session, + uint64_t cur_nr_packets) { - uint64_t max_size = 0; - - assert(session); + uint64_t tot_size = 0; if (session->kernel_session) { struct ltt_kernel_channel *chan; struct ltt_kernel_session *ksess = session->kernel_session; - /* - * For each channel, add to the max size the size of each subbuffer - * multiplied by their sized. - */ cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { - if (chan->channel->attr.subbuf_size > max_size) { - max_size = chan->channel->attr.subbuf_size; + if (cur_nr_packets >= chan->channel->attr.num_subbuf) { + /* + * Don't take channel into account if we + * already grab all its packets. + */ + continue; } + tot_size += chan->channel->attr.subbuf_size + * chan->stream_count; } } if (session->ust_session) { - struct lttng_ht_iter iter; - struct ltt_ust_channel *uchan; struct ltt_ust_session *usess = session->ust_session; - cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, - uchan, node.node) { - if (uchan->attr.subbuf_size > max_size) { - max_size = uchan->attr.subbuf_size; - } - } + tot_size += ust_app_get_size_one_more_packet_per_stream(usess, + cur_nr_packets); } - return max_size; + return tot_size; } /* - * Returns the total number of streams for a session or a negative value - * on error. + * Calculate the number of packets we can grab from each stream that + * fits within the overall snapshot max size. + * + * Returns -1 on error, 0 means infinite number of packets, else > 0 is + * the number of packets per stream. + * + * TODO: this approach is not perfect: we consider the worse case + * (packet filling the sub-buffers) as an upper bound, but we could do + * better if we do this calculation while we actually grab the packet + * content: we would know how much padding we don't actually store into + * the file. + * + * This algorithm is currently bounded by the number of packets per + * stream. + * + * Since we call this algorithm before actually grabbing the data, it's + * an approximation: for instance, applications could appear/disappear + * in between this call and actually grabbing data. */ -static unsigned int get_session_nb_streams(struct ltt_session *session) +static +int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size) { - unsigned int total_streams = 0; - - if (session->kernel_session) { - struct ltt_kernel_session *ksess = session->kernel_session; + int64_t size_left; + uint64_t cur_nb_packets = 0; - total_streams += ksess->stream_count_global; + if (!max_size) { + return 0; /* Infinite */ } - if (session->ust_session) { - struct ltt_ust_session *usess = session->ust_session; + size_left = max_size; + for (;;) { + uint64_t one_more_packet_tot_size; - total_streams += ust_app_get_nb_stream(usess); + one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session, + cur_nb_packets); + if (!one_more_packet_tot_size) { + /* We are already grabbing all packets. */ + break; + } + size_left -= one_more_packet_tot_size; + if (size_left < 0) { + break; + } + cur_nb_packets++; } - - return total_streams; + if (!cur_nb_packets) { + /* Not enough room to grab one packet of each stream, error. */ + return -1; + } + return cur_nb_packets; } /* @@ -3050,14 +3167,22 @@ int cmd_snapshot_record(struct ltt_session *session, int ret = LTTNG_OK; unsigned int use_tmp_output = 0; struct snapshot_output tmp_output; - unsigned int nb_streams, snapshot_success = 0; - uint64_t session_max_size = 0, max_stream_size = 0; + unsigned int snapshot_success = 0; + char datetime[16]; assert(session); assert(output); DBG("Cmd snapshot record for session %s", session->name); + /* Get the datetime for the snapshot output directory. */ + ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, + sizeof(datetime)); + if (!ret) { + ret = LTTNG_ERR_INVALID; + goto error; + } + /* * Permission denied to create an output if the session is not * set in no output mode. @@ -3088,151 +3213,104 @@ int cmd_snapshot_record(struct ltt_session *session, } /* Use the global session count for the temporary snapshot. */ tmp_output.nb_snapshot = session->snapshot.nb_snapshot; - use_tmp_output = 1; - } - - /* - * Get the session maximum size for a snapshot meaning it will compute the - * size of all streams from all domain. - */ - max_stream_size = get_session_max_subbuf_size(session); - nb_streams = get_session_nb_streams(session); - if (nb_streams) { - /* - * The maximum size of the snapshot is the number of streams multiplied - * by the biggest subbuf size of all channels in a session which is the - * maximum stream size available for each stream. The session max size - * is now checked against the snapshot max size value given by the user - * and if lower, an error is returned. - */ - session_max_size = max_stream_size * nb_streams; + /* Use the global datetime */ + memcpy(tmp_output.datetime, datetime, sizeof(datetime)); + use_tmp_output = 1; } - DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64, - session_max_size, max_stream_size); + if (use_tmp_output) { + int64_t nb_packets_per_stream; - /* - * If we use a temporary output, check right away if the max size fits else - * for each output the max size will be checked. - */ - if (use_tmp_output && - (tmp_output.max_size != 0 && - tmp_output.max_size < session_max_size)) { - ret = LTTNG_ERR_MAX_SIZE_INVALID; - goto error; - } + nb_packets_per_stream = get_session_nb_packets_per_stream(session, + tmp_output.max_size); + if (nb_packets_per_stream < 0) { + ret = LTTNG_ERR_MAX_SIZE_INVALID; + goto error; + } - if (session->kernel_session) { - struct ltt_kernel_session *ksess = session->kernel_session; + if (session->kernel_session) { + ret = record_kernel_snapshot(session->kernel_session, + &tmp_output, session, + wait, nb_packets_per_stream); + if (ret != LTTNG_OK) { + goto error; + } + } - if (use_tmp_output) { - ret = record_kernel_snapshot(ksess, &tmp_output, session, - wait, max_stream_size); + if (session->ust_session) { + ret = record_ust_snapshot(session->ust_session, + &tmp_output, session, + wait, nb_packets_per_stream); if (ret != LTTNG_OK) { goto error; } - snapshot_success = 1; - } else { - struct snapshot_output *sout; - struct lttng_ht_iter iter; + } - rcu_read_lock(); - cds_lfht_for_each_entry(session->snapshot.output_ht->ht, - &iter.iter, sout, node.node) { - /* - * Make a local copy of the output and assign the possible - * temporary value given by the caller. - */ - memset(&tmp_output, 0, sizeof(tmp_output)); - memcpy(&tmp_output, sout, sizeof(tmp_output)); + snapshot_success = 1; + } else { + struct snapshot_output *sout; + struct lttng_ht_iter iter; - /* Use temporary max size. */ - if (output->max_size != (uint64_t) -1ULL) { - tmp_output.max_size = output->max_size; - } + rcu_read_lock(); + cds_lfht_for_each_entry(session->snapshot.output_ht->ht, + &iter.iter, sout, node.node) { + int64_t nb_packets_per_stream; - if (tmp_output.max_size != 0 && - tmp_output.max_size < session_max_size) { - rcu_read_unlock(); - ret = LTTNG_ERR_MAX_SIZE_INVALID; - goto error; - } + /* + * Make a local copy of the output and assign the possible + * temporary value given by the caller. + */ + memset(&tmp_output, 0, sizeof(tmp_output)); + memcpy(&tmp_output, sout, sizeof(tmp_output)); - /* Use temporary name. */ - if (*output->name != '\0') { - strncpy(tmp_output.name, output->name, - sizeof(tmp_output.name)); - } + if (output->max_size != (uint64_t) -1ULL) { + tmp_output.max_size = output->max_size; + } - tmp_output.nb_snapshot = session->snapshot.nb_snapshot; + nb_packets_per_stream = get_session_nb_packets_per_stream(session, + tmp_output.max_size); + if (nb_packets_per_stream < 0) { + ret = LTTNG_ERR_MAX_SIZE_INVALID; + rcu_read_unlock(); + goto error; + } - ret = record_kernel_snapshot(ksess, &tmp_output, - session, wait, max_stream_size); - if (ret != LTTNG_OK) { + /* Use temporary name. */ + if (*output->name != '\0') { + if (lttng_strncpy(tmp_output.name, output->name, + sizeof(tmp_output.name))) { + ret = LTTNG_ERR_INVALID; rcu_read_unlock(); goto error; } - snapshot_success = 1; } - rcu_read_unlock(); - } - } - if (session->ust_session) { - struct ltt_ust_session *usess = session->ust_session; - - if (use_tmp_output) { - ret = record_ust_snapshot(usess, &tmp_output, session, - wait, max_stream_size); - if (ret != LTTNG_OK) { - goto error; - } - snapshot_success = 1; - } else { - struct snapshot_output *sout; - struct lttng_ht_iter iter; + tmp_output.nb_snapshot = session->snapshot.nb_snapshot; + memcpy(tmp_output.datetime, datetime, sizeof(datetime)); - rcu_read_lock(); - cds_lfht_for_each_entry(session->snapshot.output_ht->ht, - &iter.iter, sout, node.node) { - /* - * Make a local copy of the output and assign the possible - * temporary value given by the caller. - */ - memset(&tmp_output, 0, sizeof(tmp_output)); - memcpy(&tmp_output, sout, sizeof(tmp_output)); - - /* Use temporary max size. */ - if (output->max_size != (uint64_t) -1ULL) { - tmp_output.max_size = output->max_size; - } - - if (tmp_output.max_size != 0 && - tmp_output.max_size < session_max_size) { + if (session->kernel_session) { + ret = record_kernel_snapshot(session->kernel_session, + &tmp_output, session, + wait, nb_packets_per_stream); + if (ret != LTTNG_OK) { rcu_read_unlock(); - ret = LTTNG_ERR_MAX_SIZE_INVALID; goto error; } + } - /* Use temporary name. */ - if (*output->name != '\0') { - strncpy(tmp_output.name, output->name, - sizeof(tmp_output.name)); - } - - tmp_output.nb_snapshot = session->snapshot.nb_snapshot; - - ret = record_ust_snapshot(usess, &tmp_output, session, - wait, max_stream_size); + if (session->ust_session) { + ret = record_ust_snapshot(session->ust_session, + &tmp_output, session, + wait, nb_packets_per_stream); if (ret != LTTNG_OK) { rcu_read_unlock(); goto error; } - snapshot_success = 1; } - rcu_read_unlock(); + snapshot_success = 1; } + rcu_read_unlock(); } if (snapshot_success) {