Makes write operation a parameter for tp_memcpy macro
[lttng-modules.git] / probes / lttng-events.h
index 219f63e6a7fcb01e5d1862c6d8e83f7f33b1ff00..492423acce0ea5786cab65fb7818775050945f21 100644 (file)
@@ -2,18 +2,30 @@
  * lttng-events.h
  *
  * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
- * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * Dual LGPL v2.1/GPL v2 license.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; only
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-
+#include <linux/uaccess.h>
 #include <linux/debugfs.h>
 #include "lttng.h"
 #include "lttng-types.h"
 #include "../wrapper/vmalloc.h"        /* for wrapper_vmalloc_sync_all() */
 #include "../wrapper/ringbuffer/frontend_types.h"
-#include "../ltt-events.h"
-#include "../ltt-tracer-core.h"
+#include "../lttng-events.h"
+#include "../lttng-tracer-core.h"
 
 /*
  * Macro declarations used for all stages.
@@ -188,6 +200,10 @@ void trace_##_name(void *__data);
                },                                              \
        },
 
+#undef __string_from_user
+#define __string_from_user(_item, _src)                                \
+       __string(_item, _src)
+
 #undef TP_STRUCT__entry
 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
 
@@ -315,19 +331,19 @@ static __used struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
 
 #undef __field_full
 #define __field_full(_type, _item, _order, _base)                             \
-       __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
+       __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
        __event_len += sizeof(_type);
 
 #undef __array_enc_ext
 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)       \
-       __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
+       __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
        __event_len += sizeof(_type) * (_length);
 
 #undef __dynamic_array_enc_ext
 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
-       __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32));   \
+       __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(u32));   \
        __event_len += sizeof(u32);                                            \
-       __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
+       __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
        __dynamic_len[__dynamic_len_idx] = (_length);                          \
        __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx];       \
        __dynamic_len_idx++;
@@ -336,6 +352,15 @@ static __used struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
 #define __string(_item, _src)                                                 \
        __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
 
+/*
+ * strlen_user includes \0. If returns 0, it faulted, so we set size to
+ * 1 (\0 only).
+ */
+#undef __string_from_user
+#define __string_from_user(_item, _src)                                               \
+       __event_len += __dynamic_len[__dynamic_len_idx++] =                    \
+               max_t(size_t, strlen_user(_src), 1);
+
 #undef TP_PROTO
 #define TP_PROTO(args...) args
 
@@ -369,20 +394,23 @@ static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
 
 #undef __field_full
 #define __field_full(_type, _item, _order, _base)                        \
-       __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
+       __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
 
 #undef __array_enc_ext
 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)  \
-       __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
+       __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
 
 #undef __dynamic_array_enc_ext
 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
-       __event_align = max_t(size_t, __event_align, ltt_alignof(u32));   \
-       __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
+       __event_align = max_t(size_t, __event_align, lttng_alignof(u32));         \
+       __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
 
 #undef __string
 #define __string(_item, _src)
 
+#undef __string_from_user
+#define __string_from_user(_item, _src)
+
 #undef TP_PROTO
 #define TP_PROTO(args...) args
 
@@ -424,7 +452,11 @@ static inline size_t __event_get_align__##_name(_proto)                          \
        _type   _item;
 
 #undef __string
-#define __string(_item, _src)  char _item;
+#define __string(_item, _src)                  char _item;
+
+#undef __string_from_user
+#define __string_from_user(_item, _src)                \
+       __string(_item, _src)
 
 #undef TP_STRUCT__entry
 #define TP_STRUCT__entry(args...) args
@@ -473,6 +505,10 @@ __end_field_##_item##_2:
        goto __assign_##_item;                                          \
 __end_field_##_item:
 
+#undef __string_from_user
+#define __string_from_user(_item, _src)                                        \
+       __string(_item, _src)
+
 /*
  * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
  * strcpy().
@@ -482,55 +518,73 @@ __end_field_##_item:
 __assign_##dest:                                                       \
        {                                                               \
                __typeof__(__typemap.dest) __tmp = (src);               \
-               lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__tmp));  \
+               lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));        \
                __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
        }                                                               \
        goto __end_field_##dest;
 
-#undef tp_memcpy
-#define tp_memcpy(dest, src, len)                                      \
+/* fixed length array memcpy */
+#undef tp_memcpy_gen
+#define tp_memcpy_gen(write_ops, dest, src, len)                       \
 __assign_##dest:                                                       \
        if (0)                                                          \
                (void) __typemap.dest;                                  \
-       lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
-       __chan->ops->event_write(&__ctx, src, len);                     \
+       lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest));       \
+       __chan->ops->write_ops(&__ctx, src, len);                       \
        goto __end_field_##dest;
 
-#undef tp_memcpy_dyn
-#define tp_memcpy_dyn(dest, src)                                       \
+#undef tp_memcpy
+#define tp_memcpy(dest, src, len)                                      \
+       tp_memcpy_gen(event_write, dest, src, len)
+
+#undef tp_memcpy_from_user
+#define tp_memcpy_from_user(dest, src, len)                            \
+       tp_memcpy_gen(event_write_from_user, dest, src, len)
+
+/* variable length sequence memcpy */
+#undef tp_memcpy_dyn_gen
+#define tp_memcpy_dyn_gen(write_ops, dest, src)                                \
 __assign_##dest##_1:                                                   \
        {                                                               \
                u32 __tmpl = __dynamic_len[__dynamic_len_idx];          \
-               lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(u32));    \
+               lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(u32));  \
                __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
        }                                                               \
        goto __end_field_##dest##_1;                                    \
 __assign_##dest##_2:                                                   \
-       lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
-       __chan->ops->event_write(&__ctx, src,                           \
+       lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest));       \
+       __chan->ops->write_ops(&__ctx, src,                             \
                sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
        goto __end_field_##dest##_2;
 
-#undef tp_memcpy_from_user
-#define tp_memcpy_from_user(dest, src, len)                            \
-       __assign_##dest:                                                \
-       if (0)                                                          \
-               (void) __typemap.dest;                                  \
-       lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
-       __chan->ops->event_write_from_user(&__ctx, src, len);           \
-       goto __end_field_##dest;
+#undef tp_memcpy_dyn
+#define tp_memcpy_dyn(dest, src)                                       \
+       tp_memcpy_dyn_gen(event_write, dest, src)
+
+#undef tp_memcpy_dyn_from_user
+#define tp_memcpy_dyn_from_user(dest, src)                             \
+       tp_memcpy_dyn_gen(event_write_from_user, dest, src)
 
+/*
+ * The string length including the final \0.
+ */
 #undef tp_copy_string_from_user
 #define tp_copy_string_from_user(dest, src)                            \
        __assign_##dest:                                                \
-       if (0)                                                          \
-               (void) __typemap.dest;                                  \
-       lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
-       __chan->ops->event_write_from_user(&__ctx, src,                 \
-               __get_dynamic_array_len(dest) - 1);                     \
-       __chan->ops->event_memset(&__ctx, 0, 1);                        \
+       {                                                               \
+               size_t __ustrlen;                                       \
+                                                                       \
+               if (0)                                                  \
+                       (void) __typemap.dest;                          \
+               lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest));\
+               __ustrlen = __get_dynamic_array_len(dest);              \
+               if (likely(__ustrlen > 1)) {                            \
+                       __chan->ops->event_write_from_user(&__ctx, src, \
+                               __ustrlen - 1);                         \
+               }                                                       \
+               __chan->ops->event_memset(&__ctx, 0, 1);                \
+       }                                                               \
        goto __end_field_##dest;
-
 #undef tp_strcpy
 #define tp_strcpy(dest, src)                                           \
        tp_memcpy(dest, src, __get_dynamic_array_len(dest))
@@ -559,12 +613,23 @@ __assign_##dest##_2:                                                      \
 #undef TP_fast_assign
 #define TP_fast_assign(args...) args
 
+/*
+ * For state dump, check that "session" argument (mandatory) matches the
+ * session this event belongs to. Ensures that we write state dump data only
+ * into the started session, not into all sessions.
+ */
+#ifdef TP_SESSION_CHECK
+#define _TP_SESSION_CHECK(session, csession)   (session == csession)
+#else /* TP_SESSION_CHECK */
+#define _TP_SESSION_CHECK(session, csession)   1
+#endif /* TP_SESSION_CHECK */
+
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print)  \
 static void __event_probe__##_name(void *__data, _proto)                     \
 {                                                                            \
-       struct ltt_event *__event = __data;                                   \
-       struct ltt_channel *__chan = __event->chan;                           \
+       struct lttng_event *__event = __data;                                 \
+       struct lttng_channel *__chan = __event->chan;                         \
        struct lib_ring_buffer_ctx __ctx;                                     \
        size_t __event_len, __event_align;                                    \
        size_t __dynamic_len_idx = 0;                                         \
@@ -572,8 +637,12 @@ static void __event_probe__##_name(void *__data, _proto)                 \
        struct __event_typemap__##_name __typemap;                            \
        int __ret;                                                            \
                                                                              \
-       if (0)                                                                \
+       if (0) {                                                              \
                (void) __dynamic_len_idx;       /* don't warn if unused */    \
+               (void) __typemap;               /* don't warn if unused */    \
+       }                                                                     \
+       if (!_TP_SESSION_CHECK(session, __chan->session))                     \
+               return;                                                       \
        if (unlikely(!ACCESS_ONCE(__chan->session->active)))                  \
                return;                                                       \
        if (unlikely(!ACCESS_ONCE(__chan->enabled)))                          \
@@ -599,12 +668,14 @@ static void __event_probe__##_name(void *__data, _proto)                \
 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print)         \
 static void __event_probe__##_name(void *__data)                             \
 {                                                                            \
-       struct ltt_event *__event = __data;                                   \
-       struct ltt_channel *__chan = __event->chan;                           \
+       struct lttng_event *__event = __data;                                 \
+       struct lttng_channel *__chan = __event->chan;                         \
        struct lib_ring_buffer_ctx __ctx;                                     \
        size_t __event_len, __event_align;                                    \
        int __ret;                                                            \
                                                                              \
+       if (!_TP_SESSION_CHECK(session, __chan->session))                     \
+               return;                                                       \
        if (unlikely(!ACCESS_ONCE(__chan->session->active)))                  \
                return;                                                       \
        if (unlikely(!ACCESS_ONCE(__chan->enabled)))                          \
@@ -647,14 +718,14 @@ static void __event_probe__##_name(void *__data)                        \
 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
 {
        wrapper_vmalloc_sync_all();
-       return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
+       return lttng_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
 }
 
 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
 
 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
 {
-       ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
+       lttng_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
 }
 
 module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
This page took 0.027288 seconds and 4 git commands to generate.