X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=lttng-events.c;h=1f2863945a74f977fcc38b2635020064b80f84aa;hb=241ae9a8fb62c3ce467d244e280062c24e73eb7a;hp=430307686b10a3a2f4c2ae3403fc349db51e2592;hpb=4ecb5ad551c82311205afbcda492739a08d6b132;p=lttng-modules.git diff --git a/lttng-events.c b/lttng-events.c index 43030768..1f286394 100644 --- a/lttng-events.c +++ b/lttng-events.c @@ -20,6 +20,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/* + * This page_alloc.h wrapper needs to be included before gfpflags.h because it + * overrides a function with a define. + */ +#include "wrapper/page_alloc.h" + #include #include #include @@ -30,18 +36,21 @@ #include #include #include -#include "wrapper/file.h" +#include #include - -#include "wrapper/uuid.h" -#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ -#include "wrapper/random.h" -#include "wrapper/tracepoint.h" -#include "wrapper/list.h" -#include "lttng-kernel-version.h" -#include "lttng-events.h" -#include "lttng-tracer.h" -#include "lttng-abi-old.h" +#include +#include + +#include +#include /* for wrapper_vmalloc_sync_all() */ +#include +#include +#include +#include +#include +#include +#include +#include #define METADATA_CACHE_DEFAULT_SIZE 4096 @@ -125,12 +134,12 @@ struct lttng_session *lttng_session_create(void) GFP_KERNEL); if (!metadata_cache) goto err_free_session; - metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE, - GFP_KERNEL); + metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE); if (!metadata_cache->data) goto err_free_cache; metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; kref_init(&metadata_cache->refcount); + mutex_init(&metadata_cache->lock); session->metadata_cache = metadata_cache; INIT_LIST_HEAD(&metadata_cache->metadata_stream); memcpy(&metadata_cache->uuid, &session->uuid, @@ -155,7 +164,7 @@ void metadata_cache_destroy(struct kref *kref) { struct lttng_metadata_cache *cache = container_of(kref, struct lttng_metadata_cache, refcount); - kfree(cache->data); + vfree(cache->data); kfree(cache); } @@ -261,6 +270,48 @@ end: return ret; } +int lttng_session_metadata_regenerate(struct lttng_session *session) +{ + int ret = 0; + struct lttng_channel *chan; + struct lttng_event *event; + struct lttng_metadata_cache *cache = session->metadata_cache; + struct lttng_metadata_stream *stream; + + mutex_lock(&sessions_mutex); + if (!session->active) { + ret = -EBUSY; + goto end; + } + + mutex_lock(&cache->lock); + memset(cache->data, 0, cache->cache_alloc); + cache->metadata_written = 0; + cache->version++; + list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) { + stream->metadata_out = 0; + stream->metadata_in = 0; + } + mutex_unlock(&cache->lock); + + session->metadata_dumped = 0; + list_for_each_entry(chan, &session->chan, list) { + chan->metadata_dumped = 0; + } + + list_for_each_entry(event, &session->events, list) { + event->metadata_dumped = 0; + } + + ret = _lttng_session_metadata_statedump(session); + +end: + mutex_unlock(&sessions_mutex); + return ret; +} + + + int lttng_channel_enable(struct lttng_channel *channel) { int ret = 0; @@ -320,8 +371,23 @@ int lttng_event_enable(struct lttng_event *event) ret = -EEXIST; goto end; } - ACCESS_ONCE(event->enabled) = 1; - lttng_session_sync_enablers(event->chan->session); + switch (event->instrumentation) { + case LTTNG_KERNEL_TRACEPOINT: + case LTTNG_KERNEL_SYSCALL: + ret = -EINVAL; + break; + case LTTNG_KERNEL_KPROBE: + case LTTNG_KERNEL_FUNCTION: + case LTTNG_KERNEL_NOOP: + ACCESS_ONCE(event->enabled) = 1; + break; + case LTTNG_KERNEL_KRETPROBE: + ret = lttng_kretprobes_event_enable_state(event, 1); + break; + default: + WARN_ON_ONCE(1); + ret = -EINVAL; + } end: mutex_unlock(&sessions_mutex); return ret; @@ -340,8 +406,23 @@ int lttng_event_disable(struct lttng_event *event) ret = -EEXIST; goto end; } - ACCESS_ONCE(event->enabled) = 0; - lttng_session_sync_enablers(event->chan->session); + switch (event->instrumentation) { + case LTTNG_KERNEL_TRACEPOINT: + case LTTNG_KERNEL_SYSCALL: + ret = -EINVAL; + break; + case LTTNG_KERNEL_KPROBE: + case LTTNG_KERNEL_FUNCTION: + case LTTNG_KERNEL_NOOP: + ACCESS_ONCE(event->enabled) = 0; + break; + case LTTNG_KERNEL_KRETPROBE: + ret = lttng_kretprobes_event_enable_state(event, 0); + break; + default: + WARN_ON_ONCE(1); + ret = -EINVAL; + } end: mutex_unlock(&sessions_mutex); return ret; @@ -492,7 +573,7 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, name_len = strlen(event_name); hash = jhash(event_name, name_len, 0); head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; - hlist_for_each_entry(event, head, hlist) { + lttng_hlist_for_each_entry(event, head, hlist) { WARN_ON_ONCE(!event->desc); if (!strncmp(event->desc->name, event_name, LTTNG_KERNEL_SYM_NAME_LEN - 1) @@ -512,6 +593,7 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, event->id = chan->free_event_id++; event->instrumentation = itype; event->evtype = LTTNG_TYPE_EVENT; + INIT_LIST_HEAD(&event->bytecode_runtime_head); INIT_LIST_HEAD(&event->enablers_ref_head); switch (itype) { @@ -528,7 +610,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, smp_wmb(); break; case LTTNG_KERNEL_KPROBE: - event->enabled = 1; + /* + * Needs to be explicitly enabled after creation, since + * we may want to apply filters. + */ + event->enabled = 0; event->registered = 1; /* * Populate lttng_event structure before event @@ -552,7 +638,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, struct lttng_event *event_return; /* kretprobe defines 2 events */ - event->enabled = 1; + /* + * Needs to be explicitly enabled after creation, since + * we may want to apply filters. + */ + event->enabled = 0; event->registered = 1; event_return = kmem_cache_zalloc(event_cache, GFP_KERNEL); @@ -563,7 +653,7 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, event_return->chan = chan; event_return->filter = filter; event_return->id = chan->free_event_id++; - event_return->enabled = 1; + event_return->enabled = 0; event_return->registered = 1; event_return->instrumentation = itype; /* @@ -598,7 +688,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, break; } case LTTNG_KERNEL_FUNCTION: - event->enabled = 1; + /* + * Needs to be explicitly enabled after creation, since + * we may want to apply filters. + */ + event->enabled = 0; event->registered = 1; /* * Populate lttng_event structure before event @@ -616,7 +710,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, break; case LTTNG_KERNEL_NOOP: case LTTNG_KERNEL_SYSCALL: - event->enabled = 1; + /* + * Needs to be explicitly enabled after creation, since + * we may want to apply filters. + */ + event->enabled = 0; event->registered = 0; event->desc = event_desc; if (!event->desc) { @@ -636,7 +734,6 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan, } hlist_add_head(&event->hlist, head); list_add(&event->list, &chan->session->events); - mutex_unlock(&sessions_mutex); return event; statedump_error: @@ -670,7 +767,7 @@ static void register_event(struct lttng_event *event) { const struct lttng_event_desc *desc; - int ret; + int ret = -EINVAL; if (event->registered) return; @@ -988,17 +1085,22 @@ int lttng_session_list_tracker_pids(struct lttng_session *session) ret = PTR_ERR(tracker_pids_list_file); goto file_error; } + if (atomic_long_add_unless(&session->file->f_count, + 1, INT_MAX) == INT_MAX) { + goto refcount_error; + } ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file); if (ret < 0) goto open_error; m = tracker_pids_list_file->private_data; m->private = session; fd_install(file_fd, tracker_pids_list_file); - atomic_long_inc(&session->file->f_count); return file_fd; open_error: + atomic_long_dec(&session->file->f_count); +refcount_error: fput(tracker_pids_list_file); file_error: put_unused_fd(file_fd); @@ -1130,7 +1232,7 @@ void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler) */ hash = jhash(event_name, name_len, 0); head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)]; - hlist_for_each_entry(event, head, hlist) { + lttng_hlist_for_each_entry(event, head, hlist) { if (event->desc == desc && event->chan == enabler->chan) found = 1; @@ -1217,6 +1319,11 @@ int lttng_enabler_ref_events(struct lttng_enabler *enabler) &event->enablers_ref_head); } + /* + * Link filter bytecodes if not linked yet. + */ + lttng_enabler_event_link_bytecode(event, enabler); + /* TODO: merge event context. */ } return 0; @@ -1246,11 +1353,12 @@ struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type, if (!enabler) return NULL; enabler->type = type; + INIT_LIST_HEAD(&enabler->filter_bytecode_head); memcpy(&enabler->event_param, event_param, sizeof(enabler->event_param)); enabler->chan = chan; /* ctx left NULL */ - enabler->enabled = 1; + enabler->enabled = 0; enabler->evtype = LTTNG_TYPE_ENABLER; mutex_lock(&sessions_mutex); list_add(&enabler->node, &enabler->chan->session->enablers_head); @@ -1277,6 +1385,36 @@ int lttng_enabler_disable(struct lttng_enabler *enabler) return 0; } +int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler, + struct lttng_kernel_filter_bytecode __user *bytecode) +{ + struct lttng_filter_bytecode_node *bytecode_node; + uint32_t bytecode_len; + int ret; + + ret = get_user(bytecode_len, &bytecode->len); + if (ret) + return ret; + bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len, + GFP_KERNEL); + if (!bytecode_node) + return -ENOMEM; + ret = copy_from_user(&bytecode_node->bc, bytecode, + sizeof(*bytecode) + bytecode_len); + if (ret) + goto error_free; + bytecode_node->enabler = enabler; + /* Enforce length based on allocated size */ + bytecode_node->bc.len = bytecode_len; + list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head); + lttng_session_lazy_sync_enablers(enabler->chan->session); + return 0; + +error_free: + kfree(bytecode_node); + return ret; +} + int lttng_enabler_attach_context(struct lttng_enabler *enabler, struct lttng_kernel_context *context_param) { @@ -1286,6 +1424,14 @@ int lttng_enabler_attach_context(struct lttng_enabler *enabler, static void lttng_enabler_destroy(struct lttng_enabler *enabler) { + struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node; + + /* Destroy filter bytecode */ + list_for_each_entry_safe(filter_node, tmp_filter_node, + &enabler->filter_bytecode_head, node) { + kfree(filter_node); + } + /* Destroy contexts */ lttng_destroy_context(enabler->ctx); @@ -1313,7 +1459,8 @@ void lttng_session_sync_enablers(struct lttng_session *session) */ list_for_each_entry(event, &session->events, list) { struct lttng_enabler_ref *enabler_ref; - int enabled = 0; + struct lttng_bytecode_runtime *runtime; + int enabled = 0, has_enablers_without_bytecode = 0; switch (event->instrumentation) { case LTTNG_KERNEL_TRACEPOINT: @@ -1348,6 +1495,23 @@ void lttng_session_sync_enablers(struct lttng_session *session) } else { _lttng_event_unregister(event); } + + /* Check if has enablers without bytecode enabled */ + list_for_each_entry(enabler_ref, + &event->enablers_ref_head, node) { + if (enabler_ref->ref->enabled + && list_empty(&enabler_ref->ref->filter_bytecode_head)) { + has_enablers_without_bytecode = 1; + break; + } + } + event->has_enablers_without_bytecode = + has_enablers_without_bytecode; + + /* Enable filters */ + list_for_each_entry(runtime, + &event->bytecode_runtime_head, node) + lttng_filter_sync_state(runtime); } } @@ -1370,10 +1534,12 @@ void lttng_session_lazy_sync_enablers(struct lttng_session *session) /* * Serialize at most one packet worth of metadata into a metadata * channel. - * We have exclusive access to our metadata buffer (protected by the - * sessions_mutex), so we can do racy operations such as looking for - * remaining space left in packet and write, since mutual exclusion - * protects us from concurrent writes. + * We grab the metadata cache mutex to get exclusive access to our metadata + * buffer and to the metadata cache. Exclusive access to the metadata buffer + * allows us to do racy operations such as looking for remaining space left in + * packet and write, since mutual exclusion protects us from concurrent writes. + * Mutual exclusion on the metadata cache allow us to read the cache content + * without racing against reallocation of the cache by updates. * Returns the number of bytes written in the channel, 0 if no data * was written and a negative value on error. */ @@ -1385,17 +1551,23 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, size_t len, reserve_len; /* - * Ensure we support mutiple get_next / put sequences followed - * by put_next. The metadata stream lock internally protects - * reading the metadata cache. It can indeed be read - * concurrently by "get_next_subbuf" and "flush" operations on - * the buffer invoked by different processes. + * Ensure we support mutiple get_next / put sequences followed by + * put_next. The metadata cache lock protects reading the metadata + * cache. It can indeed be read concurrently by "get_next_subbuf" and + * "flush" operations on the buffer invoked by different processes. + * Moreover, since the metadata cache memory can be reallocated, we + * need to have exclusive access against updates even though we only + * read it. */ - mutex_lock(&stream->lock); + mutex_lock(&stream->metadata_cache->lock); WARN_ON(stream->metadata_in < stream->metadata_out); if (stream->metadata_in != stream->metadata_out) goto end; + /* Metadata regenerated, change the version. */ + if (stream->metadata_cache->version != stream->version) + stream->version = stream->metadata_cache->version; + len = stream->metadata_cache->metadata_written - stream->metadata_in; if (!len) @@ -1421,13 +1593,15 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream, ret = reserve_len; end: - mutex_unlock(&stream->lock); + mutex_unlock(&stream->metadata_cache->lock); return ret; } /* * Write the metadata to the metadata cache. * Must be called with sessions_mutex held. + * The metadata cache lock protects us from concurrent read access from + * thread outputting metadata content to ring buffer. */ int lttng_metadata_printf(struct lttng_session *session, const char *fmt, ...) @@ -1446,6 +1620,7 @@ int lttng_metadata_printf(struct lttng_session *session, return -ENOMEM; len = strlen(str); + mutex_lock(&session->metadata_cache->lock); if (session->metadata_cache->metadata_written + len > session->metadata_cache->cache_alloc) { char *tmp_cache_realloc; @@ -1454,10 +1629,16 @@ int lttng_metadata_printf(struct lttng_session *session, tmp_cache_alloc_size = max_t(unsigned int, session->metadata_cache->cache_alloc + len, session->metadata_cache->cache_alloc << 1); - tmp_cache_realloc = krealloc(session->metadata_cache->data, - tmp_cache_alloc_size, GFP_KERNEL); + tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size); if (!tmp_cache_realloc) goto err; + if (session->metadata_cache->data) { + memcpy(tmp_cache_realloc, + session->metadata_cache->data, + session->metadata_cache->cache_alloc); + vfree(session->metadata_cache->data); + } + session->metadata_cache->cache_alloc = tmp_cache_alloc_size; session->metadata_cache->data = tmp_cache_realloc; } @@ -1465,6 +1646,7 @@ int lttng_metadata_printf(struct lttng_session *session, session->metadata_cache->metadata_written, str, len); session->metadata_cache->metadata_written += len; + mutex_unlock(&session->metadata_cache->lock); kfree(str); list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) @@ -1473,6 +1655,7 @@ int lttng_metadata_printf(struct lttng_session *session, return 0; err: + mutex_unlock(&session->metadata_cache->lock); kfree(str); return -ENOMEM; } @@ -1517,6 +1700,14 @@ int _lttng_field_statedump(struct lttng_session *session, const struct lttng_basic_type *elem_type; elem_type = &field->type.u.array.elem_type; + if (field->type.u.array.elem_alignment) { + ret = lttng_metadata_printf(session, + " struct { } align(%u) _%s_padding;\n", + field->type.u.array.elem_alignment * CHAR_BIT, + field->name); + if (ret) + return ret; + } ret = lttng_metadata_printf(session, " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", elem_type->u.basic.integer.size, @@ -1563,6 +1754,14 @@ int _lttng_field_statedump(struct lttng_session *session, if (ret) return ret; + if (field->type.u.sequence.elem_alignment) { + ret = lttng_metadata_printf(session, + " struct { } align(%u) _%s_padding;\n", + field->type.u.sequence.elem_alignment * CHAR_BIT, + field->name); + if (ret) + return ret; + } ret = lttng_metadata_printf(session, " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", elem_type->u.basic.integer.size, @@ -1767,6 +1966,7 @@ int _lttng_stream_packet_context_declare(struct lttng_session *session) " uint64_clock_monotonic_t timestamp_end;\n" " uint64_t content_size;\n" " uint64_t packet_size;\n" + " uint64_t packet_seq_num;\n" " unsigned long events_discarded;\n" " uint32_t cpu_id;\n" "};\n\n" @@ -1823,11 +2023,15 @@ int _lttng_event_header_declare(struct lttng_session *session) * taken at start of trace. * Yes, this is only an approximation. Yes, we can (and will) do better * in future versions. + * This function may return a negative offset. It may happen if the + * system sets the REALTIME clock to 0 after boot. */ static -uint64_t measure_clock_offset(void) +int64_t measure_clock_offset(void) { - uint64_t offset, monotonic[2], realtime; + uint64_t monotonic_avg, monotonic[2], realtime; + uint64_t tcf = trace_clock_freq(); + int64_t offset; struct timespec rts = { 0, 0 }; unsigned long flags; @@ -1838,10 +2042,17 @@ uint64_t measure_clock_offset(void) monotonic[1] = trace_clock_read64(); local_irq_restore(flags); - offset = (monotonic[0] + monotonic[1]) >> 1; - realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC; - realtime += rts.tv_nsec; - offset = realtime - offset; + monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; + realtime = (uint64_t) rts.tv_sec * tcf; + if (tcf == NSEC_PER_SEC) { + realtime += rts.tv_nsec; + } else { + uint64_t n = rts.tv_nsec * tcf; + + do_div(n, NSEC_PER_SEC); + realtime += n; + } + offset = (int64_t) realtime - monotonic_avg; return offset; } @@ -1888,6 +2099,7 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) " uint32_t magic;\n" " uint8_t uuid[16];\n" " uint32_t stream_id;\n" + " uint64_t stream_instance_id;\n" " };\n" "};\n\n", lttng_alignof(uint8_t) * CHAR_BIT, @@ -1933,8 +2145,8 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) ret = lttng_metadata_printf(session, "clock {\n" - " name = %s;\n", - "monotonic" + " name = \"%s\";\n", + trace_clock_name() ); if (ret) goto end; @@ -1949,13 +2161,14 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) } ret = lttng_metadata_printf(session, - " description = \"Monotonic Clock\";\n" + " description = \"%s\";\n" " freq = %llu; /* Frequency, in Hz */\n" " /* clock value offset from Epoch is: offset * (1/freq) */\n" - " offset = %llu;\n" + " offset = %lld;\n" "};\n\n", + trace_clock_description(), (unsigned long long) trace_clock_freq(), - (unsigned long long) measure_clock_offset() + (long long) measure_clock_offset() ); if (ret) goto end; @@ -1963,20 +2176,23 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) ret = lttng_metadata_printf(session, "typealias integer {\n" " size = 27; align = 1; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint27_clock_monotonic_t;\n" "\n" "typealias integer {\n" " size = 32; align = %u; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint32_clock_monotonic_t;\n" "\n" "typealias integer {\n" " size = 64; align = %u; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint64_clock_monotonic_t;\n\n", + trace_clock_name(), lttng_alignof(uint32_t) * CHAR_BIT, - lttng_alignof(uint64_t) * CHAR_BIT + trace_clock_name(), + lttng_alignof(uint64_t) * CHAR_BIT, + trace_clock_name() ); if (ret) goto end; @@ -2052,10 +2268,18 @@ static int __init lttng_events_init(void) ret = wrapper_lttng_fixup_sig(THIS_MODULE); if (ret) return ret; - - ret = lttng_tracepoint_init(); + ret = wrapper_get_pfnblock_flags_mask_init(); + if (ret) + return ret; + ret = wrapper_get_pageblock_flags_mask_init(); if (ret) return ret; + ret = lttng_context_init(); + if (ret) + return ret; + ret = lttng_tracepoint_init(); + if (ret) + goto error_tp; event_cache = KMEM_CACHE(lttng_event, 0); if (!event_cache) { ret = -ENOMEM; @@ -2075,6 +2299,8 @@ error_abi: kmem_cache_destroy(event_cache); error_kmem: lttng_tracepoint_exit(); +error_tp: + lttng_context_exit(); return ret; } @@ -2090,6 +2316,7 @@ static void __exit lttng_events_exit(void) lttng_session_destroy(session); kmem_cache_destroy(event_cache); lttng_tracepoint_exit(); + lttng_context_exit(); } module_exit(lttng_events_exit);