Implement gather bool
[libside.git] / include / side / trace.h
index b47bb96cd307a1df492f91ab6b1b8d45906903aa..4c978d1b4ef9d92da33a14f9029f5c5f43b9255b 100644 (file)
 #include <side/macros.h>
 #include <side/endian.h>
 
-/* SIDE stands for "Static Instrumentation Dynamically Enabled" */
+/*
+ * SIDE stands for "Static Instrumentation Dynamically Enabled"
+ *
+ * This is an instrumentation API for Linux user-space, which exposes an
+ * instrumentation type system and facilities allowing a kernel or
+ * user-space tracer to consume user-space instrumentation.
+ *
+ * This instrumentation API exposes 3 type systems:
+ *
+ * * Stack-copy type system: This is the core type system which can
+ *   represent all supported types and into which all other type systems
+ *   can be nested. This type system requires that every type is
+ *   statically or dynamically declared and then registered, thus giving
+ *   tracers a complete description of the events and their associated
+ *   fields before the associated instrumentation is invoked. The
+ *   application needs to copy each argument (side_arg_...()) onto the
+ *   stack when calling the instrumentation.
+ *
+ *   This is the most expressive of the 3 type systems, althrough not the
+ *   fastest due to the extra copy of the arguments.
+ *
+ * * Data-gathering type system: This type system requires every type to
+ *   be statically or dynamically declared and registered, but does not
+ *   require the application to copy its arguments onto the stack.
+ *   Instead, the type description contains all the required information
+ *   to fetch the data from the application memory. The only argument
+ *   required from the instrumentation is the base pointer from which
+ *   the data should be fetched.
+ *
+ *   This type system can be used as an event field, or nested within
+ *   the stack-copy type system. Nesting of gather-vla within
+ *   gather-array and gather-vla types is not allowed.
+ *
+ *   This type system is has the least overhead of the 3 type systems.
+ *
+ * * Dynamic type system: This type system receives both type
+ *   description and actual data onto the stack at runtime. It has more
+ *   overhead that the 2 other type systems, but does not require a
+ *   prior registration of event field description. This makes it useful
+ *   for seldom used types which are not performance critical, but for
+ *   which registering each individual events would needlessly grow the
+ *   number of events to declare and register.
+ *
+ *   Another use-case for this type system is for use by dynamically
+ *   typed language runtimes, where the field type is only known when
+ *   the instrumentation is called.
+ *
+ *   Those dynamic types can be either used as arguments to a variadic
+ *   field list, or as on-stack instrumentation argument for a static
+ *   type SIDE_TYPE_DYNAMIC place holder in the stack-copy type system.
+ */
 
 //TODO: as those structures will be ABI, we need to either consider them
 //fixed forever, or think of a scheme that would allow their binary
@@ -34,7 +84,7 @@ struct side_arg_dynamic_struct;
 struct side_events_register_handle;
 
 enum side_type_label {
-       /* Statically declared types */
+       /* Stack-copy basic types */
        SIDE_TYPE_NULL,
        SIDE_TYPE_BOOL,
        SIDE_TYPE_U8,
@@ -54,7 +104,7 @@ enum side_type_label {
        SIDE_TYPE_FLOAT_BINARY128,
        SIDE_TYPE_STRING,
 
-       /* Statically declared compound types */
+       /* Stack-copy compound types */
        SIDE_TYPE_STRUCT,
        SIDE_TYPE_ARRAY,
        SIDE_TYPE_VLA,
@@ -84,21 +134,26 @@ enum side_type_label {
        SIDE_TYPE_VLA_POINTER32,
        SIDE_TYPE_VLA_POINTER64,
 
-       /* Statically declared enumeration types */
+       /* Stack-copy enumeration types */
        SIDE_TYPE_ENUM,
        SIDE_TYPE_ENUM_BITMAP,
 
+       /* Stack-copy place holder for dynamic types */
        SIDE_TYPE_DYNAMIC,
 
-       /* Gather types */
+       /* Gather basic types */
+       SIDE_TYPE_GATHER_BOOL,
        SIDE_TYPE_GATHER_UNSIGNED_INT,
        SIDE_TYPE_GATHER_SIGNED_INT,
+       SIDE_TYPE_GATHER_BYTE,
        SIDE_TYPE_GATHER_FLOAT,
+
+       /* Gather compound types */
        SIDE_TYPE_GATHER_STRUCT,
        SIDE_TYPE_GATHER_ARRAY,
        SIDE_TYPE_GATHER_VLA,
 
-       /* Dynamic types */
+       /* Dynamic basic  types */
        SIDE_TYPE_DYNAMIC_NULL,
        SIDE_TYPE_DYNAMIC_BOOL,
        SIDE_TYPE_DYNAMIC_U8,
@@ -187,6 +242,11 @@ enum side_type_label_byte_order {
 # define SIDE_TYPE_FLOAT_WORD_ORDER_HOST       SIDE_TYPE_BYTE_ORDER_BE
 #endif
 
+enum side_type_gather_access_mode {
+       SIDE_TYPE_GATHER_ACCESS_DIRECT,
+       SIDE_TYPE_GATHER_ACCESS_POINTER,        /* Pointer dereference */
+};
+
 typedef enum side_visitor_status (*side_visitor_func)(
                const struct side_tracer_visitor_ctx *tracer_ctx,
                void *app_ctx);
@@ -245,6 +305,9 @@ struct side_type_null {
 struct side_type_bool {
        const struct side_attr *attr;
        uint32_t nr_attr;
+       uint16_t bool_size_bits;        /* bits */
+       uint16_t len_bits;              /* bits */
+       uint8_t byte_order;             /* enum side_type_label_byte_order */
 } SIDE_PACKED;
 
 struct side_type_byte {
@@ -336,6 +399,19 @@ struct side_type_enum_bitmap {
        const struct side_type *elem_type;
 } SIDE_PACKED;
 
+struct side_type_gather_bool {
+       uint64_t offset;        /* bytes */
+       uint8_t access_mode;    /* enum side_type_gather_access_mode */
+       struct side_type_bool type;
+       uint16_t offset_bits;   /* bits */
+} SIDE_PACKED;
+
+struct side_type_gather_byte {
+       uint64_t offset;        /* bytes */
+       uint8_t access_mode;    /* enum side_type_gather_access_mode */
+       struct side_type_byte type;
+} SIDE_PACKED;
+
 struct side_type_gather_integer {
        uint64_t offset;        /* bytes */
        uint8_t access_mode;    /* enum side_type_gather_access_mode */
@@ -370,13 +446,10 @@ struct side_type_gather_vla {
        struct side_type_vla type;
 } SIDE_PACKED;
 
-enum side_type_gather_access_mode {
-       SIDE_TYPE_GATHER_ACCESS_ADDRESS,
-       SIDE_TYPE_GATHER_ACCESS_POINTER,
-};
-
 struct side_type_gather {
        union {
+               struct side_type_gather_bool side_bool;
+               struct side_type_gather_byte side_byte;
                struct side_type_gather_integer side_integer;
                struct side_type_gather_float side_float;
                struct side_type_gather_array side_array;
@@ -385,11 +458,10 @@ struct side_type_gather {
        } SIDE_PACKED u;
 } SIDE_PACKED;
 
-/* Statically defined types. */
 struct side_type {
        uint32_t type;  /* enum side_type_label */
        union {
-               /* Basic types */
+               /* Stack-copy basic types */
                struct side_type_null side_null;
                struct side_type_bool side_bool;
                struct side_type_byte side_byte;
@@ -397,13 +469,13 @@ struct side_type {
                struct side_type_integer side_integer;
                struct side_type_float side_float;
 
-               /* Compound types */
+               /* Stack-copy compound types */
                struct side_type_array side_array;
                struct side_type_vla side_vla;
                struct side_type_vla_visitor side_vla_visitor;
                const struct side_type_struct *side_struct;
 
-               /* Enumeration types */
+               /* Stack-copy enumeration types */
                struct side_type_enum side_enum;
                struct side_type_enum_bitmap side_enum_bitmap;
 
@@ -435,14 +507,14 @@ struct side_callback {
 } SIDE_PACKED;
 
 struct side_arg_static {
-       /* Basic types */
+       /* Stack-copy basic types */
        uint8_t bool_value;
        uint8_t byte_value;
        uint64_t string_value;  /* const char * */
        union side_integer_value integer_value;
        union side_float_value float_value;
 
-       /* Compound types */
+       /* Stack-copy compound types */
        const struct side_arg_vec *side_struct;
        const struct side_arg_vec *side_array;
        const struct side_arg_vec *side_vla;
@@ -453,9 +525,13 @@ struct side_arg_static {
                uint32_t length;
        } SIDE_PACKED side_vla_fixint;
 
-       /* Gather types */
+       /* Gather basic types */
+       void *side_bool_gather_ptr;
+       void *side_byte_gather_ptr;
        void *side_integer_gather_ptr;
        void *side_float_gather_ptr;
+
+       /* Gather compound types */
        void *side_array_gather_ptr;
        void *side_struct_gather_ptr;
        struct {
@@ -493,35 +569,30 @@ struct side_dynamic_vla_visitor {
 } SIDE_PACKED;
 
 struct side_arg_dynamic {
-       /* Basic types */
+       /* Dynamic basic types */
        struct side_type_null side_null;
        struct {
                struct side_type_bool type;
                uint8_t value;
        } SIDE_PACKED side_bool;
-
        struct {
                struct side_type_byte type;
                uint8_t value;
        } SIDE_PACKED side_byte;
-
        struct {
                struct side_type_string type;
                uint64_t value; /* const char * */
        } SIDE_PACKED side_string;
-
-       /* Integer type */
        struct {
                struct side_type_integer type;
                union side_integer_value value;
        } SIDE_PACKED side_integer;
-
        struct {
                struct side_type_float type;
                union side_float_value value;
        } SIDE_PACKED side_float;
 
-       /* Compound types */
+       /* Dynamic compound types */
        const struct side_arg_dynamic_struct *side_dynamic_struct;
        const struct side_arg_dynamic_vla *side_dynamic_vla;
 
@@ -621,7 +692,59 @@ struct side_event_description {
 #define side_attr_float_binary128(_val)        { .type = SIDE_ATTR_TYPE_FLOAT_BINARY128, .u = { .float_value = { .side_float_binary128 = (_val) } } }
 #define side_attr_string(_val)         { .type = SIDE_ATTR_TYPE_STRING, .u = { .string_value = (uintptr_t) (_val) } }
 
-/* Static field definition */
+/* Stack-copy enumeration type definitions */
+
+#define side_define_enum(_identifier, _mappings, _attr) \
+       const struct side_enum_mappings _identifier = { \
+               .mappings = _mappings, \
+               .attr = _attr, \
+               .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
+               .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+       }
+
+#define side_enum_mapping_list(...) \
+       SIDE_COMPOUND_LITERAL(const struct side_enum_mapping, __VA_ARGS__)
+
+#define side_enum_mapping_range(_label, _begin, _end) \
+       { \
+               .range_begin = _begin, \
+               .range_end = _end, \
+               .label = _label, \
+       }
+
+#define side_enum_mapping_value(_label, _value) \
+       { \
+               .range_begin = _value, \
+               .range_end = _value, \
+               .label = _label, \
+       }
+
+#define side_define_enum_bitmap(_identifier, _mappings, _attr) \
+       const struct side_enum_bitmap_mappings _identifier = { \
+               .mappings = _mappings, \
+               .attr = _attr, \
+               .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
+               .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+       }
+
+#define side_enum_bitmap_mapping_list(...) \
+       SIDE_COMPOUND_LITERAL(const struct side_enum_bitmap_mapping, __VA_ARGS__)
+
+#define side_enum_bitmap_mapping_range(_label, _begin, _end) \
+       { \
+               .range_begin = _begin, \
+               .range_end = _end, \
+               .label = _label, \
+       }
+
+#define side_enum_bitmap_mapping_value(_label, _value) \
+       { \
+               .range_begin = _value, \
+               .range_end = _value, \
+               .label = _label, \
+       }
+
+/* Stack-copy field and type definitions */
 
 #define side_type_null(_attr) \
        { \
@@ -641,6 +764,9 @@ struct side_event_description {
                        .side_bool = { \
                                .attr = _attr, \
                                .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+                               .bool_size_bits = 8, \
+                               .len_bits = 1, \
+                               .byte_order = SIDE_TYPE_BYTE_ORDER_HOST, \
                        }, \
                }, \
        }
@@ -888,7 +1014,64 @@ struct side_event_description {
 #define side_field_vla_visitor(_name, _elem_type, _visitor, _attr) \
        _side_field(_name, side_type_vla_visitor(SIDE_PARAM(_elem_type), _visitor, SIDE_PARAM(_attr)))
 
-/* Gather fields */
+/* Gather field and type definitions */
+
+#define side_type_gather_byte(_offset, _access_mode, _attr) \
+       { \
+               .type = SIDE_TYPE_GATHER_BYTE, \
+               .u = { \
+                       .side_gather = { \
+                               .u = { \
+                                       .side_byte = { \
+                                               .offset = _offset, \
+                                               .access_mode = _access_mode, \
+                                               .type = { \
+                                                       .attr = _attr, \
+                                                       .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+                                               }, \
+                                       }, \
+                               }, \
+                       }, \
+               }, \
+       }
+#define side_field_gather_byte(_name, _offset, _access_mode, _attr) \
+       _side_field(_name, side_type_gather_byte(_offset, _access_mode, SIDE_PARAM(_attr)))
+
+#define _side_type_gather_bool(_byte_order, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       { \
+               .type = SIDE_TYPE_GATHER_BOOL, \
+               .u = { \
+                       .side_gather = { \
+                               .u = { \
+                                       .side_bool = { \
+                                               .offset = _offset, \
+                                               .access_mode = _access_mode, \
+                                               .type = { \
+                                                       .attr = _attr, \
+                                                       .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+                                                       .bool_size_bits = _bool_size_bits, \
+                                                       .len_bits = _len_bits, \
+                                                       .byte_order = _byte_order, \
+                                               }, \
+                                               .offset_bits = _offset_bits, \
+                                       }, \
+                               }, \
+                       }, \
+               }, \
+       }
+#define side_type_gather_bool(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_type_gather_bool(SIDE_TYPE_BYTE_ORDER_HOST, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr)
+#define side_type_gather_bool_le(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_type_gather_bool(SIDE_TYPE_BYTE_ORDER_LE, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr)
+#define side_type_gather_bool_be(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_type_gather_bool(SIDE_TYPE_BYTE_ORDER_BE, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr)
+
+#define side_field_gather_bool(_name, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_field(_name, side_type_gather_bool(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, SIDE_PARAM(_attr)))
+#define side_field_gather_bool_le(_name, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_field(_name, side_type_gather_bool_le(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, SIDE_PARAM(_attr)))
+#define side_field_gather_bool_be(_name, _offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
+       _side_field(_name, side_type_gather_bool_be(_offset, _bool_size_bits, _offset_bits, _len_bits, _access_mode, SIDE_PARAM(_attr)))
 
 #define _side_type_gather_integer(_type, _signedness, _byte_order, _offset, \
                _integer_size_bits, _offset_bits, _len_bits, _access_mode, _attr) \
@@ -1060,7 +1243,7 @@ struct side_event_description {
 #define side_field_list(...) \
        SIDE_COMPOUND_LITERAL(const struct side_event_field, __VA_ARGS__)
 
-/* Static field arguments */
+/* Stack-copy field arguments */
 
 #define side_arg_null(_val)            { .type = SIDE_TYPE_NULL }
 #define side_arg_bool(_val)            { .type = SIDE_TYPE_BOOL, .u = { .side_static = { .bool_value = !!(_val) } } }
@@ -1115,6 +1298,9 @@ struct side_event_description {
 #define side_arg_vla_pointer(_ptr, _length) { .type = SIDE_TYPE_VLA_POINTER_HOST, .u = { .side_static = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } }
 
 /* Gather field arguments */
+
+#define side_arg_gather_bool(_ptr)             { .type = SIDE_TYPE_GATHER_BOOL, .u = { .side_static = { .side_bool_gather_ptr = (_ptr) } } }
+#define side_arg_gather_byte(_ptr)             { .type = SIDE_TYPE_GATHER_BYTE, .u = { .side_static = { .side_byte_gather_ptr = (_ptr) } } }
 #define side_arg_gather_unsigned_integer(_ptr) { .type = SIDE_TYPE_GATHER_UNSIGNED_INT, .u = { .side_static = { .side_integer_gather_ptr = (_ptr) } } }
 #define side_arg_gather_signed_integer(_ptr)   { .type = SIDE_TYPE_GATHER_SIGNED_INT, .u = { .side_static = { .side_integer_gather_ptr = (_ptr) } } }
 #define side_arg_gather_float(_ptr)            { .type = SIDE_TYPE_GATHER_FLOAT, .u = { .side_static = { .side_float_gather_ptr = (_ptr) } } }
@@ -1146,6 +1332,9 @@ struct side_event_description {
                                        .type = { \
                                                .attr = _attr, \
                                                .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
+                                               .bool_size_bits = sizeof(uint8_t) * CHAR_BIT, \
+                                               .len_bits = 1, \
+                                               .byte_order = SIDE_TYPE_BYTE_ORDER_HOST, \
                                        }, \
                                        .value = !!(_val), \
                                }, \
@@ -1378,57 +1567,12 @@ struct side_event_description {
                .elem = _elem, \
        }
 
-#define side_arg_list(...)     __VA_ARGS__
-
-#define side_define_enum(_identifier, _mappings, _attr) \
-       const struct side_enum_mappings _identifier = { \
-               .mappings = _mappings, \
-               .attr = _attr, \
-               .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
-               .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
-       }
-
-#define side_enum_mapping_list(...) \
-       SIDE_COMPOUND_LITERAL(const struct side_enum_mapping, __VA_ARGS__)
-
-#define side_enum_mapping_range(_label, _begin, _end) \
-       { \
-               .range_begin = _begin, \
-               .range_end = _end, \
-               .label = _label, \
-       }
-
-#define side_enum_mapping_value(_label, _value) \
-       { \
-               .range_begin = _value, \
-               .range_end = _value, \
-               .label = _label, \
-       }
-
-#define side_define_enum_bitmap(_identifier, _mappings, _attr) \
-       const struct side_enum_bitmap_mappings _identifier = { \
-               .mappings = _mappings, \
-               .attr = _attr, \
-               .nr_mappings = SIDE_ARRAY_SIZE(SIDE_PARAM(_mappings)), \
-               .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \
-       }
-
-#define side_enum_bitmap_mapping_list(...) \
-       SIDE_COMPOUND_LITERAL(const struct side_enum_bitmap_mapping, __VA_ARGS__)
-
-#define side_enum_bitmap_mapping_range(_label, _begin, _end) \
-       { \
-               .range_begin = _begin, \
-               .range_end = _end, \
-               .label = _label, \
-       }
+/*
+ * Event instrumentation description registration, runtime enabled state
+ * check, and instrumentation invocation.
+ */
 
-#define side_enum_bitmap_mapping_value(_label, _value) \
-       { \
-               .range_begin = _value, \
-               .range_end = _value, \
-               .label = _label, \
-       }
+#define side_arg_list(...)     __VA_ARGS__
 
 #define side_event_cond(_identifier) \
        if (side_unlikely(__atomic_load_n(&side_event_enable__##_identifier, \
@@ -1525,6 +1669,16 @@ void side_call_variadic(const struct side_event_description *desc,
        const struct side_arg_vec *side_arg_vec,
        const struct side_arg_dynamic_struct *var_struct);
 
+struct side_events_register_handle *side_events_register(struct side_event_description **events,
+               uint32_t nr_events);
+void side_events_unregister(struct side_events_register_handle *handle);
+
+/*
+ * Userspace tracer registration API. This allows userspace tracers to
+ * register event notification callbacks to be notified of the currently
+ * registered instrumentation, and to register their callbacks to
+ * specific events.
+ */
 typedef void (*side_tracer_callback_func)(const struct side_event_description *desc,
                        const struct side_arg_vec *side_arg_vec,
                        void *priv);
@@ -1546,10 +1700,6 @@ int side_tracer_callback_variadic_unregister(struct side_event_description *desc
                side_tracer_callback_variadic_func call_variadic,
                void *priv);
 
-struct side_events_register_handle *side_events_register(struct side_event_description **events,
-               uint32_t nr_events);
-void side_events_unregister(struct side_events_register_handle *handle);
-
 enum side_tracer_notification {
        SIDE_TRACER_NOTIFICATION_INSERT_EVENTS,
        SIDE_TRACER_NOTIFICATION_REMOVE_EVENTS,
@@ -1562,9 +1712,19 @@ struct side_tracer_handle *side_tracer_event_notification_register(
                void *priv);
 void side_tracer_event_notification_unregister(struct side_tracer_handle *handle);
 
+/*
+ * Explicit hooks to initialize/finalize the side instrumentation
+ * library. Those are also library constructor/destructor.
+ */
 void side_init(void);
 void side_exit(void);
 
+/*
+ * The following constructors/destructors perform automatic registration
+ * of the declared side events. Those may have to be called explicitly
+ * in a statically linked library.
+ */
+
 /*
  * These weak symbols, the constructor, and destructor take care of
  * registering only _one_ instance of the side instrumentation per
This page took 0.03583 seconds and 4 git commands to generate.