X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fsave.c;h=e57f0e03a6b451b920592a6cb2a08d5f61d1fd29;hp=c76abbac633c0688565d0008c3bf68b823b25b3b;hb=87597c2c3bbaa1502ad2025cbf16704829f3b464;hpb=091ced216b4da7ba8e1abdd8d18237dffb457642 diff --git a/src/bin/lttng-sessiond/save.c b/src/bin/lttng-sessiond/save.c index c76abbac6..e57f0e03a 100644 --- a/src/bin/lttng-sessiond/save.c +++ b/src/bin/lttng-sessiond/save.c @@ -15,7 +15,6 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE #define _LGPL_SOURCE #include #include @@ -25,7 +24,7 @@ #include #include -#include +#include #include #include #include @@ -33,8 +32,9 @@ #include "kernel.h" #include "save.h" #include "session.h" -#include "syscall.h" +#include "lttng-syscall.h" #include "trace-ust.h" +#include "agent.h" static int save_kernel_channel_attributes(struct config_writer *writer, @@ -104,6 +104,26 @@ int save_kernel_channel_attributes(struct config_writer *writer, if (ret) { goto end; } + + if (attr->extended.ptr) { + struct lttng_channel_extended *ext = NULL; + + ext = (struct lttng_channel_extended *) attr->extended.ptr; + ret = config_writer_write_element_unsigned_int(writer, + config_element_monitor_timer_interval, + ext->monitor_timer_interval); + if (ret) { + goto end; + } + + ret = config_writer_write_element_signed_int(writer, + config_element_blocking_timeout, + ext->blocking_timeout); + if (ret) { + goto end; + } + } + end: return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0; } @@ -113,6 +133,7 @@ int save_ust_channel_attributes(struct config_writer *writer, struct lttng_ust_channel_attr *attr) { int ret; + struct ltt_ust_channel *channel = NULL; ret = config_writer_write_element_string(writer, config_element_overwrite_mode, @@ -156,6 +177,26 @@ int save_ust_channel_attributes(struct config_writer *writer, if (ret) { goto end; } + + ret = config_writer_write_element_signed_int(writer, + config_element_blocking_timeout, + attr->u.s.blocking_timeout); + if (ret) { + goto end; + } + + /* + * Fetch the monitor timer which is located in the parent of + * lttng_ust_channel_attr + */ + channel = caa_container_of(attr, struct ltt_ust_channel, attr); + ret = config_writer_write_element_unsigned_int(writer, + config_element_monitor_timer_interval, + channel->monitor_timer_interval); + if (ret) { + goto end; + } + end: return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0; } @@ -174,13 +215,16 @@ const char *get_kernel_instrumentation_string( instrumentation_string = config_event_type_tracepoint; break; case LTTNG_KERNEL_KPROBE: - instrumentation_string = config_event_type_kprobe; + instrumentation_string = config_event_type_probe; + break; + case LTTNG_KERNEL_UPROBE: + instrumentation_string = config_event_type_userspace_probe; break; case LTTNG_KERNEL_FUNCTION: - instrumentation_string = config_event_type_function; + instrumentation_string = config_event_type_function_entry; break; case LTTNG_KERNEL_KRETPROBE: - instrumentation_string = config_event_type_kretprobe; + instrumentation_string = config_event_type_function; break; case LTTNG_KERNEL_NOOP: instrumentation_string = config_event_type_noop; @@ -232,6 +276,24 @@ const char *get_kernel_context_type_string( case LTTNG_KERNEL_CONTEXT_HOSTNAME: context_type_string = config_event_context_hostname; break; + case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE: + context_type_string = config_event_context_interruptible; + break; + case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE: + context_type_string = config_event_context_preemptible; + break; + case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE: + context_type_string = config_event_context_need_reschedule; + break; + case LTTNG_KERNEL_CONTEXT_MIGRATABLE: + context_type_string = config_event_context_migratable; + break; + case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: + context_type_string = config_event_context_callstack_user; + break; + case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: + context_type_string = config_event_context_callstack_kernel; + break; default: context_type_string = NULL; } @@ -261,6 +323,9 @@ const char *get_ust_context_type_string( case LTTNG_UST_CONTEXT_PTHREAD_ID: context_type_string = config_event_context_pthread_id; break; + case LTTNG_UST_CONTEXT_APP_CONTEXT: + context_type_string = config_event_context_app; + break; case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: /* * Error, should not be stored in the XML, perf contexts @@ -320,9 +385,384 @@ const char *get_loglevel_type_string( return loglevel_type_string; } +static +int save_kernel_function_event(struct config_writer *writer, + struct ltt_kernel_event *event) +{ + int ret; + + ret = config_writer_open_element(writer, config_element_function_attributes); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, config_element_name, + event->event->u.ftrace.symbol_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* /function attributes */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + +static +int save_kernel_kprobe_event(struct config_writer *writer, + struct ltt_kernel_event *event) +{ + int ret; + const char *symbol_name; + uint64_t addr; + uint64_t offset; + + switch (event->event->instrumentation) { + case LTTNG_KERNEL_KPROBE: + /* + * Comments in lttng-kernel.h mention that + * either addr or symbol_name are set, not both. + */ + addr = event->event->u.kprobe.addr; + offset = event->event->u.kprobe.offset; + symbol_name = addr ? NULL : event->event->u.kprobe.symbol_name; + break; + case LTTNG_KERNEL_KRETPROBE: + addr = event->event->u.kretprobe.addr; + offset = event->event->u.kretprobe.offset; + symbol_name = addr ? NULL : event->event->u.kretprobe.symbol_name; + break; + default: + assert(1); + ERR("Unsupported kernel instrumentation type."); + ret = LTTNG_ERR_INVALID; + goto end; + } + + ret = config_writer_open_element(writer, config_element_probe_attributes); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + if (addr) { + ret = config_writer_write_element_unsigned_int( writer, + config_element_address, addr); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + } else if (symbol_name) { + ret = config_writer_write_element_string(writer, + config_element_symbol_name, symbol_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + /* If the offset is non-zero, write it.*/ + if (offset) { + ret = config_writer_write_element_unsigned_int(writer, + config_element_offset, offset); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + } + } else { + /* + * This really should not happen as we are either setting the + * address or the symbol above. + */ + ERR("Invalid probe/function description."); + ret = LTTNG_ERR_INVALID; + goto end; + } + + + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + +/* + * Save the userspace probe tracepoint event associated with the event to the + * config writer. + */ +static +int save_kernel_userspace_probe_tracepoint_event(struct config_writer *writer, + struct ltt_kernel_event *event) +{ + int ret = 0; + const char *probe_name, *provider_name, *binary_path; + const struct lttng_userspace_probe_location *userspace_probe_location; + const struct lttng_userspace_probe_location_lookup_method *lookup_method; + enum lttng_userspace_probe_location_lookup_method_type lookup_type; + + /* Get userspace probe location from the event. */ + userspace_probe_location = event->userspace_probe_location; + if (!userspace_probe_location) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Get lookup method and lookup method type. */ + lookup_method = lttng_userspace_probe_location_get_lookup_method(userspace_probe_location); + if (!lookup_method) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); + + /* Get the binary path, probe name and provider name. */ + binary_path = + lttng_userspace_probe_location_tracepoint_get_binary_path( + userspace_probe_location); + if (!binary_path) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + probe_name = + lttng_userspace_probe_location_tracepoint_get_probe_name( + userspace_probe_location); + if (!probe_name) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + provider_name = + lttng_userspace_probe_location_tracepoint_get_provider_name( + userspace_probe_location); + if (!provider_name) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Open a userspace probe tracepoint attribute. */ + ret = config_writer_open_element(writer, config_element_userspace_probe_tracepoint_attributes); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + switch (lookup_type) { + case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_lookup, + config_element_userspace_probe_lookup_tracepoint_sdt); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + break; + default: + ERR("Unsupported kernel userspace probe tracepoint lookup method."); + ret = LTTNG_ERR_INVALID; + goto end; + } + + /* Write the binary path, provider name and the probe name. */ + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_location_binary_path, + binary_path); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_tracepoint_location_provider_name, + provider_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_tracepoint_location_probe_name, + probe_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Close the userspace probe tracepoint attribute. */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + +end: + return ret; +} + +/* + * Save the userspace probe function event associated with the event to the + * config writer. + */ +static +int save_kernel_userspace_probe_function_event(struct config_writer *writer, + struct ltt_kernel_event *event) +{ + int ret = 0; + const char *function_name, *binary_path; + const struct lttng_userspace_probe_location *userspace_probe_location; + const struct lttng_userspace_probe_location_lookup_method *lookup_method; + enum lttng_userspace_probe_location_lookup_method_type lookup_type; + + /* Get userspace probe location from the event. */ + userspace_probe_location = event->userspace_probe_location; + if (!userspace_probe_location) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Get lookup method and lookup method type. */ + lookup_method = lttng_userspace_probe_location_get_lookup_method( + userspace_probe_location); + if (!lookup_method) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Get the binary path and the function name. */ + binary_path = + lttng_userspace_probe_location_function_get_binary_path( + userspace_probe_location); + if (!binary_path) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + function_name = + lttng_userspace_probe_location_function_get_function_name( + userspace_probe_location); + if (!function_name) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Open a userspace probe function attribute. */ + ret = config_writer_open_element(writer, + config_element_userspace_probe_function_attributes); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); + switch (lookup_type) { + case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_lookup, + config_element_userspace_probe_lookup_function_elf); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + break; + case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT: + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_lookup, + config_element_userspace_probe_lookup_function_default); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + break; + default: + ERR("Unsupported kernel userspace probe function lookup method."); + ret = LTTNG_ERR_INVALID; + goto end; + } + + /* Write the binary path and the function name. */ + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_location_binary_path, + binary_path); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_userspace_probe_function_location_function_name, + function_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* Close the userspace probe function attribute. */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + +end: + return ret; +} + +static +int save_kernel_userspace_probe_event(struct config_writer *writer, + struct ltt_kernel_event *event) +{ + int ret; + struct lttng_userspace_probe_location *userspace_probe_location; + + /* Get userspace probe location from the event. */ + userspace_probe_location = event->userspace_probe_location; + if (!userspace_probe_location) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + switch(lttng_userspace_probe_location_get_type(userspace_probe_location)) { + case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION: + { + ret = save_kernel_userspace_probe_function_event(writer, event); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + break; + } + case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT: + { + ret = save_kernel_userspace_probe_tracepoint_event(writer, event); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + break; + } + case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN: + default: + ERR("Unsupported kernel userspace probe location type."); + ret = LTTNG_ERR_INVALID; + goto end; + } + +end: + return ret; +} + static int save_kernel_event(struct config_writer *writer, - struct ltt_kernel_event *event) + struct ltt_kernel_event *event) { int ret; const char *instrumentation_type; @@ -363,8 +803,19 @@ int save_kernel_event(struct config_writer *writer, goto end; } + if (event->filter_expression) { + ret = config_writer_write_element_string(writer, + config_element_filter, + event->filter_expression); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + } + if (event->event->instrumentation == LTTNG_KERNEL_FUNCTION || event->event->instrumentation == LTTNG_KERNEL_KPROBE || + event->event->instrumentation == LTTNG_KERNEL_UPROBE || event->event->instrumentation == LTTNG_KERNEL_KRETPROBE) { ret = config_writer_open_element(writer, @@ -377,94 +828,24 @@ int save_kernel_event(struct config_writer *writer, switch (event->event->instrumentation) { case LTTNG_KERNEL_SYSCALL: case LTTNG_KERNEL_FUNCTION: - ret = config_writer_open_element(writer, - config_element_function_attributes); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = config_writer_write_element_string(writer, - config_element_name, - event->event->u.ftrace.symbol_name); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - /* /function attributes */ - ret = config_writer_close_element(writer); + ret = save_kernel_function_event(writer, event); if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } break; case LTTNG_KERNEL_KPROBE: case LTTNG_KERNEL_KRETPROBE: - { - const char *symbol_name; - uint64_t addr; - uint64_t offset; - - if (event->event->instrumentation == - LTTNG_KERNEL_KPROBE) { - /* - * Comments in lttng-kernel.h mention that - * either addr or symbol_name are set, not both. - */ - addr = event->event->u.kprobe.addr; - offset = event->event->u.kprobe.offset; - symbol_name = addr ? NULL : - event->event->u.kprobe.symbol_name; - } else { - symbol_name = - event->event->u.kretprobe.symbol_name; - addr = event->event->u.kretprobe.addr; - offset = event->event->u.kretprobe.offset; - } - - ret = config_writer_open_element(writer, - config_element_probe_attributes); + ret = save_kernel_kprobe_event(writer, event); if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - - if (symbol_name) { - ret = config_writer_write_element_string(writer, - config_element_symbol_name, - symbol_name); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - } - - if (addr) { - ret = config_writer_write_element_unsigned_int( - writer, config_element_address, addr); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - } - - if (offset) { - ret = config_writer_write_element_unsigned_int( - writer, config_element_offset, offset); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - } - - ret = config_writer_close_element(writer); + break; + case LTTNG_KERNEL_UPROBE: + ret = save_kernel_userspace_probe_event(writer, event); if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } break; - } default: ERR("Unsupported kernel instrumentation type."); ret = LTTNG_ERR_INVALID; @@ -489,57 +870,6 @@ end: return ret; } -static -int save_kernel_syscall(struct config_writer *writer, - struct ltt_kernel_channel *kchan) -{ - int ret, i; - ssize_t count; - struct lttng_event *events = NULL; - - assert(writer); - assert(kchan); - - count = syscall_list_channel(kchan, &events, 0); - if (!count) { - /* No syscalls, just gracefully return. */ - ret = 0; - goto end; - } - - for (i = 0; i < count; i++) { - struct ltt_kernel_event *kevent; - - /* Create a temporary kevent in order to save it. */ - /* - * TODO: struct lttng_event does not really work for a filter, - * but unfortunately, it is exposed as external API (and used as - * internal representation. Using NULL meanwhile. - */ - kevent = trace_kernel_create_event(&events[i], - NULL, NULL); - if (!kevent) { - ret = -ENOMEM; - goto end; - } - /* Init list in order so the destroy call can del the node. */ - CDS_INIT_LIST_HEAD(&kevent->list); - - ret = save_kernel_event(writer, kevent); - trace_kernel_destroy_event(kevent); - if (ret) { - goto end; - } - } - - /* Everything went well */ - ret = 0; - -end: - free(events); - return ret; -} - static int save_kernel_events(struct config_writer *writer, struct ltt_kernel_channel *kchan) @@ -560,12 +890,6 @@ int save_kernel_events(struct config_writer *writer, } } - /* Save syscalls if any. */ - ret = save_kernel_syscall(writer, kchan); - if (ret) { - goto end; - } - /* /events */ ret = config_writer_close_element(writer); if (ret) { @@ -632,11 +956,14 @@ int save_ust_event(struct config_writer *writer, goto end; } - ret = config_writer_write_element_signed_int(writer, - config_element_loglevel, event->attr.loglevel); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; + /* The log level is irrelevant if no "filtering" is enabled */ + if (event->attr.loglevel_type != LTTNG_UST_LOGLEVEL_ALL) { + ret = config_writer_write_element_signed_int(writer, + config_element_loglevel, event->attr.loglevel); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } } if (event->filter_expression) { @@ -661,7 +988,8 @@ int save_ust_event(struct config_writer *writer, for (i = 0; i < event->exclusion->count; i++) { ret = config_writer_write_element_string(writer, config_element_exclusion, - &event->exclusion->names[0][i]); + LTTNG_EVENT_EXCLUSION_NAME_AT( + event->exclusion, i)); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; @@ -705,6 +1033,10 @@ int save_ust_events(struct config_writer *writer, cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) { event = caa_container_of(node, struct ltt_ust_event, node); + if (event->internal) { + /* Internal events must not be exposed to clients */ + continue; + } ret = save_ust_event(writer, event); if (ret) { rcu_read_unlock(); @@ -723,6 +1055,96 @@ end: return ret; } +static +int init_ust_event_from_agent_event(struct ltt_ust_event *ust_event, + struct agent_event *agent_event) +{ + int ret = 0; + enum lttng_ust_loglevel_type ust_loglevel_type; + + ust_event->enabled = agent_event->enabled; + ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT; + if (lttng_strncpy(ust_event->attr.name, agent_event->name, + LTTNG_SYMBOL_NAME_LEN)) { + ret = -1; + goto end; + } + switch (agent_event->loglevel_type) { + case LTTNG_EVENT_LOGLEVEL_ALL: + ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; + break; + case LTTNG_EVENT_LOGLEVEL_SINGLE: + ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; + break; + case LTTNG_EVENT_LOGLEVEL_RANGE: + ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; + break; + default: + ERR("Invalid agent_event loglevel_type."); + ret = -1; + goto end; + } + + ust_event->attr.loglevel_type = ust_loglevel_type; + ust_event->attr.loglevel = agent_event->loglevel_value; + ust_event->filter_expression = agent_event->filter_expression; + ust_event->exclusion = agent_event->exclusion; +end: + return ret; +} + +static +int save_agent_events(struct config_writer *writer, + struct agent *agent) +{ + int ret; + struct lttng_ht_iter iter; + struct lttng_ht_node_str *node; + + ret = config_writer_open_element(writer, config_element_events); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + rcu_read_lock(); + cds_lfht_for_each_entry(agent->events->ht, &iter.iter, node, node) { + int ret; + struct agent_event *agent_event; + struct ltt_ust_event fake_event; + + memset(&fake_event, 0, sizeof(fake_event)); + agent_event = caa_container_of(node, struct agent_event, node); + + /* + * Initialize a fake ust event to reuse the same serialization + * function since UST and agent events contain the same info + * (and one could wonder why they don't reuse the same + * structures...). + */ + ret = init_ust_event_from_agent_event(&fake_event, agent_event); + if (ret) { + rcu_read_unlock(); + goto end; + } + ret = save_ust_event(writer, &fake_event); + if (ret) { + rcu_read_unlock(); + goto end; + } + } + rcu_read_unlock(); + + /* /events */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + static int save_kernel_context(struct config_writer *writer, struct lttng_kernel_context *ctx) @@ -740,7 +1162,8 @@ int save_kernel_context(struct config_writer *writer, } if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) { - ret = config_writer_open_element(writer, config_element_perf); + ret = config_writer_open_element(writer, + config_element_context_perf); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; @@ -837,6 +1260,124 @@ end: return ret; } +static +int save_ust_context_perf_thread_counter(struct config_writer *writer, + struct ltt_ust_context *ctx) +{ + int ret; + + assert(writer); + assert(ctx); + + /* Perf contexts are saved as event_perf_context_type */ + ret = config_writer_open_element(writer, config_element_context_perf); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_unsigned_int(writer, + config_element_type, ctx->ctx.u.perf_counter.type); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_unsigned_int(writer, + config_element_config, ctx->ctx.u.perf_counter.config); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, config_element_name, + ctx->ctx.u.perf_counter.name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* /perf */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + +static +int save_ust_context_app_ctx(struct config_writer *writer, + struct ltt_ust_context *ctx) +{ + int ret; + + assert(writer); + assert(ctx); + + /* Application contexts are saved as application_context_type */ + ret = config_writer_open_element(writer, config_element_context_app); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_context_app_provider_name, + ctx->ctx.u.app_ctx.provider_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_context_app_ctx_name, + ctx->ctx.u.app_ctx.ctx_name); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + /* /app */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + +static +int save_ust_context_generic(struct config_writer *writer, + struct ltt_ust_context *ctx) +{ + int ret; + const char *context_type_string; + + assert(writer); + assert(ctx); + + /* Save context as event_context_type_type */ + context_type_string = get_ust_context_type_string( + ctx->ctx.ctx); + if (!context_type_string) { + ERR("Unsupported UST context type."); + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_type, context_type_string); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } +end: + return ret; +} + static int save_ust_context(struct config_writer *writer, struct cds_list_head *ctx_list) @@ -854,9 +1395,6 @@ int save_ust_context(struct config_writer *writer, } cds_list_for_each_entry(ctx, ctx_list, list) { - const char *context_type_string; - - ret = config_writer_open_element(writer, config_element_context); if (ret) { @@ -864,61 +1402,19 @@ int save_ust_context(struct config_writer *writer, goto end; } - if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) { - /* Perf contexts are saved as event_perf_context_type */ - ret = config_writer_open_element(writer, - config_element_perf); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = config_writer_write_element_unsigned_int(writer, - config_element_type, - ctx->ctx.u.perf_counter.type); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = config_writer_write_element_unsigned_int(writer, - config_element_config, - ctx->ctx.u.perf_counter.config); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = config_writer_write_element_string(writer, - config_element_name, - ctx->ctx.u.perf_counter.name); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - /* /perf */ - ret = config_writer_close_element(writer); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - } else { - /* Save context as event_context_type_type */ - context_type_string = get_ust_context_type_string( - ctx->ctx.ctx); - if (!context_type_string) { - ERR("Unsupported UST context type.") - ret = LTTNG_ERR_INVALID; - goto end; - } - - ret = config_writer_write_element_string(writer, - config_element_type, context_type_string); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + switch (ctx->ctx.ctx) { + case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: + ret = save_ust_context_perf_thread_counter(writer, ctx); + break; + case LTTNG_UST_CONTEXT_APP_CONTEXT: + ret = save_ust_context_app_ctx(writer, ctx); + break; + default: + /* Save generic context. */ + ret = save_ust_context_generic(writer, ctx); + } + if (ret) { + goto end; } /* /context */ @@ -1051,67 +1547,39 @@ int save_ust_channel(struct config_writer *writer, goto end; } - ret = save_ust_events(writer, ust_chan->events); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = save_ust_context(writer, &ust_chan->ctx_list); - if (ret) { - goto end; - } - - /* /channel */ - ret = config_writer_close_element(writer); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } -end: - return ret; -} - -static -int save_kernel_session(struct config_writer *writer, - struct ltt_session *session) -{ - int ret; - struct ltt_kernel_channel *kchan; - - assert(writer); - assert(session); - - ret = config_writer_write_element_string(writer, config_element_type, - config_domain_type_kernel); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - - ret = config_writer_write_element_string(writer, - config_element_buffer_type, config_buffer_type_global); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + if (ust_chan->domain == LTTNG_DOMAIN_UST) { + ret = save_ust_events(writer, ust_chan->events); + if (ret) { + goto end; + } + } else { + struct agent *agent = NULL; - ret = config_writer_open_element(writer, - config_element_channels); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + agent = trace_ust_find_agent(session, ust_chan->domain); + if (!agent) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + ERR("Could not find agent associated to UST subdomain"); + goto end; + } - cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, - list) { - ret = save_kernel_channel(writer, kchan); + /* + * Channels associated with a UST sub-domain (such as JUL, Log4j + * or Python) don't have any non-internal events. We retrieve + * the "agent" events associated with this channel and serialize + * them. + */ + ret = save_agent_events(writer, agent); if (ret) { goto end; } } - /* /channels */ + ret = save_ust_context(writer, &ust_chan->ctx_list); + if (ret) { + goto end; + } + + /* /channel */ ret = config_writer_close_element(writer); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; @@ -1122,64 +1590,43 @@ end: } static -int save_ust_session(struct config_writer *writer, - struct ltt_session *session, int save_agent) +int save_kernel_session(struct config_writer *writer, + struct ltt_session *session) { int ret; - struct ltt_ust_channel *ust_chan; - const char *buffer_type_string; - struct lttng_ht_node_str *node; - struct lttng_ht_iter iter; + struct ltt_kernel_channel *kchan; assert(writer); assert(session); ret = config_writer_write_element_string(writer, config_element_type, - save_agent ? config_domain_type_jul : config_domain_type_ust); + config_domain_type_kernel); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - buffer_type_string = get_buffer_type_string( - session->ust_session->buffer_type); - if (!buffer_type_string) { - ERR("Unsupported buffer type."); - ret = LTTNG_ERR_INVALID; - goto end; - } - ret = config_writer_write_element_string(writer, - config_element_buffer_type, buffer_type_string); + config_element_buffer_type, config_buffer_type_global); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - ret = config_writer_open_element(writer, config_element_channels); + ret = config_writer_open_element(writer, + config_element_channels); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - rcu_read_lock(); - cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, - &iter.iter, node, node) { - int agent_channel; - - ust_chan = caa_container_of(node, struct ltt_ust_channel, node); - agent_channel = !strcmp(DEFAULT_JUL_CHANNEL_NAME, ust_chan->name) || - !strcmp(DEFAULT_LOG4J_CHANNEL_NAME, ust_chan->name) || - !strcmp(DEFAULT_PYTHON_CHANNEL_NAME, ust_chan->name); - if (!(save_agent ^ agent_channel)) { - ret = save_ust_channel(writer, ust_chan, session->ust_session); - if (ret) { - rcu_read_unlock(); - goto end; - } + cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, + list) { + ret = save_kernel_channel(writer, kchan); + if (ret) { + goto end; } } - rcu_read_unlock(); /* /channels */ ret = config_writer_close_element(writer); @@ -1191,6 +1638,34 @@ end: return ret; } +static +const char *get_config_domain_str(enum lttng_domain_type domain) +{ + const char *str_dom; + + switch (domain) { + case LTTNG_DOMAIN_KERNEL: + str_dom = config_domain_type_kernel; + break; + case LTTNG_DOMAIN_UST: + str_dom = config_domain_type_ust; + break; + case LTTNG_DOMAIN_JUL: + str_dom = config_domain_type_jul; + break; + case LTTNG_DOMAIN_LOG4J: + str_dom = config_domain_type_log4j; + break; + case LTTNG_DOMAIN_PYTHON: + str_dom = config_domain_type_python; + break; + default: + assert(0); + } + + return str_dom; +} + static int save_pid_tracker(struct config_writer *writer, struct ltt_session *sess, int domain) @@ -1285,45 +1760,90 @@ end: } static -int save_domains(struct config_writer *writer, struct ltt_session *session) +int save_ust_domain(struct config_writer *writer, + struct ltt_session *session, enum lttng_domain_type domain) { - int ret = 0; + int ret; + struct ltt_ust_channel *ust_chan; + const char *buffer_type_string; + struct lttng_ht_node_str *node; + struct lttng_ht_iter iter; + const char *config_domain_name; assert(writer); assert(session); - if (!session->kernel_session && !session->ust_session) { + ret = config_writer_open_element(writer, + config_element_domain); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - ret = config_writer_open_element(writer, config_element_domains); + config_domain_name = get_config_domain_str(domain); + if (!config_domain_name) { + ret = LTTNG_ERR_INVALID; + goto end; + } + + ret = config_writer_write_element_string(writer, + config_element_type, config_domain_name); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } + buffer_type_string = get_buffer_type_string( + session->ust_session->buffer_type); + if (!buffer_type_string) { + ERR("Unsupported buffer type."); + ret = LTTNG_ERR_INVALID; + goto end; + } - if (session->kernel_session) { - ret = config_writer_open_element(writer, - config_element_domain); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + ret = config_writer_write_element_string(writer, + config_element_buffer_type, buffer_type_string); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } - ret = save_kernel_session(writer, session); - if (ret) { - goto end; + ret = config_writer_open_element(writer, config_element_channels); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + rcu_read_lock(); + cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, + &iter.iter, node, node) { + ust_chan = caa_container_of(node, struct ltt_ust_channel, node); + if (domain == ust_chan->domain) { + ret = save_ust_channel(writer, ust_chan, session->ust_session); + if (ret) { + rcu_read_unlock(); + goto end; + } } + } + rcu_read_unlock(); + + /* /channels */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + if (domain == LTTNG_DOMAIN_UST) { ret = config_writer_open_element(writer, - config_element_trackers); + config_element_trackers); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - ret = save_pid_tracker(writer, session, LTTNG_DOMAIN_KERNEL); + ret = save_pid_tracker(writer, session, LTTNG_DOMAIN_UST); if (ret) { goto end; } @@ -1331,20 +1851,41 @@ int save_domains(struct config_writer *writer, struct ltt_session *session) /* /trackers */ ret = config_writer_close_element(writer); if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } - /* /domain */ - ret = config_writer_close_element(writer); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } } - if (session->ust_session) { - unsigned long agent_count; + /* /domain */ + ret = config_writer_close_element(writer); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + +end: + return ret; +} + +static +int save_domains(struct config_writer *writer, struct ltt_session *session) +{ + int ret = 0; + + assert(writer); + assert(session); + + if (!session->kernel_session && !session->ust_session) { + goto end; + } + + ret = config_writer_open_element(writer, config_element_domains); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + + if (session->kernel_session) { ret = config_writer_open_element(writer, config_element_domain); if (ret) { @@ -1352,7 +1893,7 @@ int save_domains(struct config_writer *writer, struct ltt_session *session) goto end; } - ret = save_ust_session(writer, session, 0); + ret = save_kernel_session(writer, session); if (ret) { goto end; } @@ -1364,7 +1905,7 @@ int save_domains(struct config_writer *writer, struct ltt_session *session) goto end; } - ret = save_pid_tracker(writer, session, LTTNG_DOMAIN_UST); + ret = save_pid_tracker(writer, session, LTTNG_DOMAIN_KERNEL); if (ret) { goto end; } @@ -1381,31 +1922,27 @@ int save_domains(struct config_writer *writer, struct ltt_session *session) ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } + } - rcu_read_lock(); - agent_count = - lttng_ht_get_count(session->ust_session->agents); - rcu_read_unlock(); + if (session->ust_session) { + ret = save_ust_domain(writer, session, LTTNG_DOMAIN_UST); + if (ret) { + goto end; + } - if (agent_count > 0) { - ret = config_writer_open_element(writer, - config_element_domain); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + ret = save_ust_domain(writer, session, LTTNG_DOMAIN_JUL); + if (ret) { + goto end; + } - ret = save_ust_session(writer, session, 1); - if (ret) { - goto end; - } + ret = save_ust_domain(writer, session, LTTNG_DOMAIN_LOG4J); + if (ret) { + goto end; + } - /* /domain */ - ret = config_writer_close_element(writer); - if (ret) { - ret = LTTNG_ERR_SAVE_IO_FAIL; - goto end; - } + ret = save_ust_domain(writer, session, LTTNG_DOMAIN_PYTHON); + if (ret) { + goto end; } } @@ -1450,7 +1987,7 @@ int save_consumer_output(struct config_writer *writer, switch (output->type) { case CONSUMER_DST_LOCAL: ret = config_writer_write_element_string(writer, - config_element_path, output->dst.trace_path); + config_element_path, output->dst.session_root_path); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; @@ -1658,6 +2195,83 @@ end: return ret; } +static +int save_session_rotation_schedule(struct config_writer *writer, + enum lttng_rotation_schedule_type type, uint64_t value) +{ + int ret = 0; + const char *element_name; + const char *value_name; + + switch (type) { + case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: + element_name = config_element_rotation_schedule_periodic; + value_name = config_element_rotation_schedule_periodic_time_us; + break; + case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: + element_name = config_element_rotation_schedule_size_threshold; + value_name = config_element_rotation_schedule_size_threshold_bytes; + break; + default: + ret = -1; + goto end; + } + + ret = config_writer_open_element(writer, element_name); + if (ret) { + goto end; + } + + ret = config_writer_write_element_unsigned_int(writer, + value_name, value); + if (ret) { + goto end; + } + + /* Close schedule descriptor element. */ + ret = config_writer_close_element(writer); + if (ret) { + goto end; + } +end: + return ret; +} + +static +int save_session_rotation_schedules(struct config_writer *writer, + struct ltt_session *session) +{ + int ret; + + ret = config_writer_open_element(writer, + config_element_rotation_schedules); + if (session->rotate_timer_period) { + ret = save_session_rotation_schedule(writer, + LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC, + session->rotate_timer_period); + if (ret) { + goto close_schedules; + } + } + if (session->rotate_size) { + ret = save_session_rotation_schedule(writer, + LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD, + session->rotate_size); + if (ret) { + goto close_schedules; + } + } + +close_schedules: + /* Close rotation schedules element. */ + ret = config_writer_close_element(writer); + if (ret) { + goto end; + } +end: + return ret; +} + /* * Save the given session. * @@ -1810,7 +2424,8 @@ int save_session(struct ltt_session *session, goto end; } - if (session->snapshot_mode || session->live_timer) { + if (session->snapshot_mode || session->live_timer || + session->rotate_timer_period || session->rotate_size) { ret = config_writer_open_element(writer, config_element_attributes); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; @@ -1824,7 +2439,7 @@ int save_session(struct ltt_session *session, ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } - } else { + } else if (session->live_timer) { ret = config_writer_write_element_unsigned_int(writer, config_element_live_timer_interval, session->live_timer); if (ret) { @@ -1832,6 +2447,14 @@ int save_session(struct ltt_session *session, goto end; } } + if (session->rotate_timer_period || session->rotate_size) { + ret = save_session_rotation_schedules(writer, + session); + if (ret) { + ret = LTTNG_ERR_SAVE_IO_FAIL; + goto end; + } + } /* /attributes */ ret = config_writer_close_element(writer); @@ -1871,6 +2494,13 @@ end: } } + if (file_opened) { + ret = close(fd); + if (ret) { + PERROR("Closing XML session configuration"); + } + } + return ret; }