syscalls: Make mmap()'s fields `prot` and `flags` enums
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 5 Jun 2020 15:38:14 +0000 (11:38 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 5 Aug 2020 19:30:41 +0000 (15:30 -0400)
The `prot` flags is a simple CTF enumeration.

The `flags` field is a CTF struct of 2 CTF enumerations (`type` and
`options`). This is needed to express the two parts of this integer
flag. The 4 least significant bits of the integer are reserved to
express the type of the mapping (MAP_SHARED=0x1, MAP_PRIVATE=0x2, and
MAP_SHARED_VALIDATE=0x3).

The remaining 28 bits are used to specify optional configurations on the
mapping. As opposed to the type part, the options part is bit flag
field where all values are power of 2. This part can be expressed as
ORed bit flag values.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I5ae78754b5863b31d9a3ba1b1173502e1ae284d3

include/instrumentation/syscalls/headers/syscalls_integers_override.h
src/lttng-syscalls.c

index e5578640ae6a1b5a3c022196a2cf6321751ba672..3ad03526381f3af51829df20aec38c6df636c689 100644 (file)
@@ -2,6 +2,119 @@
 
 #ifndef CREATE_SYSCALL_TABLE
 
+/*
+ * The `flags` argument of the mmap syscall is split in two parts:
+ * - The type of mapping is described by the four least significant bits of the 4
+ *   bytes integer,
+ * - The options on the mapping are described by the remaining 28 most
+ *   significant bits.
+ */
+#define MAPPING_TYPE_RESERVED_BITS 4
+#define LTTNG_MMAP_FLAGS_TO_CTF(x) ((x) >> MAPPING_TYPE_RESERVED_BITS)
+
+/*
+ * Enumeration of the mmap flags, as described in the 'mmap'
+ * system call man page.
+ */
+SC_LTTNG_TRACEPOINT_ENUM(lttng_mmap_protection,
+       TP_ENUM_VALUES(
+               ctf_enum_value("PROT_EXEC",     PROT_EXEC)
+               ctf_enum_value("PROT_READ",     PROT_READ)
+               ctf_enum_value("PROT_WRITE",    PROT_WRITE)
+               ctf_enum_value("PROT_NONE",     PROT_NONE)
+       )
+)
+
+SC_LTTNG_TRACEPOINT_ENUM(lttng_mmap_flags_mapping_type,
+       TP_ENUM_VALUES(
+               ctf_enum_value("MAP_SHARED",            MAP_SHARED)
+               ctf_enum_value("MAP_PRIVATE",           MAP_PRIVATE)
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0))
+               ctf_enum_value("MAP_SHARED_VALIDATE",   MAP_SHARED_VALIDATE)
+#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)) */
+       )
+)
+
+/*
+ * Shift the values of the options so we can read them from the `flags` integer
+ * directly.
+ */
+SC_LTTNG_TRACEPOINT_ENUM(lttng_mmap_flags_options,
+       TP_ENUM_VALUES(
+               ctf_enum_value("<none>",                0)
+               ctf_enum_value("MAP_32BIT",             LTTNG_MMAP_FLAGS_TO_CTF(MAP_32BIT))
+               ctf_enum_value("MAP_ANONYMOUS",         LTTNG_MMAP_FLAGS_TO_CTF(MAP_ANONYMOUS))
+               ctf_enum_value("MAP_DENYWRITE",         LTTNG_MMAP_FLAGS_TO_CTF(MAP_DENYWRITE))
+               ctf_enum_value("MAP_EXECUTABLE",        LTTNG_MMAP_FLAGS_TO_CTF(MAP_EXECUTABLE))
+               ctf_enum_value("MAP_FIXED",             LTTNG_MMAP_FLAGS_TO_CTF(MAP_FIXED))
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0))
+               ctf_enum_value("MAP_FIXED_NOREPLACE",   LTTNG_MMAP_FLAGS_TO_CTF(MAP_FIXED_NOREPLACE))
+#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)) */
+               ctf_enum_value("MAP_GROWSDOWN",         LTTNG_MMAP_FLAGS_TO_CTF(MAP_GROWSDOWN))
+               ctf_enum_value("MAP_HUGETLB",           LTTNG_MMAP_FLAGS_TO_CTF(MAP_HUGETLB))
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))
+               ctf_enum_value("MAP_HUGETLB_2MB",       LTTNG_MMAP_FLAGS_TO_CTF(MAP_HUGE_2MB))
+               ctf_enum_value("MAP_HUGETLB_1GB",       LTTNG_MMAP_FLAGS_TO_CTF(MAP_HUGE_1GB))
+#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)) */
+               ctf_enum_value("MAP_LOCKED",            LTTNG_MMAP_FLAGS_TO_CTF(MAP_LOCKED))
+               ctf_enum_value("MAP_NONBLOCK",          LTTNG_MMAP_FLAGS_TO_CTF(MAP_NONBLOCK))
+               ctf_enum_value("MAP_NORESERVE",         LTTNG_MMAP_FLAGS_TO_CTF(MAP_NORESERVE))
+               ctf_enum_value("MAP_POPULATE",          LTTNG_MMAP_FLAGS_TO_CTF(MAP_POPULATE))
+               ctf_enum_value("MAP_STACK",             LTTNG_MMAP_FLAGS_TO_CTF(MAP_STACK))
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0))
+               ctf_enum_value("MAP_SYNC",              LTTNG_MMAP_FLAGS_TO_CTF(MAP_SYNC))
+#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)) */
+               ctf_enum_value("MAP_UNINITIALIZED",     LTTNG_MMAP_FLAGS_TO_CTF(MAP_UNINITIALIZED))
+       )
+)
+
+#define LTTNG_MMAP_FLAGS_TYPE                                          \
+{                                                                      \
+       .name = "type",                                                 \
+       .type = {                                                       \
+               .atype = atype_enum_nestable,                           \
+               .u = {                                                  \
+                       .enum_nestable = {                              \
+                               .desc = &__enum_lttng_mmap_flags_mapping_type,          \
+                               .container_type =  __LTTNG_COMPOUND_LITERAL(            \
+                                       struct lttng_type, __type_integer(uint32_t,     \
+                                               4, 1, -1, __BYTE_ORDER, 16, none)),     \
+                       },                                              \
+               },                                                      \
+       },                                                              \
+}
+
+#define LTTNG_MMAP_FLAGS_OPTIONS                                       \
+{                                                                      \
+       .name = "options",                                              \
+       .type = {                                                       \
+               .atype = atype_enum_nestable,                           \
+               .u = {                                                  \
+                       .enum_nestable = {                              \
+                               .desc = &__enum_lttng_mmap_flags_options,           \
+                               .container_type = __LTTNG_COMPOUND_LITERAL(         \
+                                       struct lttng_type, __type_integer(uint32_t, \
+                                               28, 1, -1, __BYTE_ORDER, 16, none)),\
+                       },                                              \
+               },                                                      \
+       },                                                              \
+}
+
+#if (__BYTE_ORDER == __LITTLE_ENDIAN)
+#define LTTNG_MMAP_FLAGS                       \
+       [0] = LTTNG_MMAP_FLAGS_TYPE,            \
+       [1] = LTTNG_MMAP_FLAGS_OPTIONS,
+
+#else
+#define LTTNG_MMAP_FLAGS                       \
+       [0] = LTTNG_MMAP_FLAGS_OPTIONS,         \
+       [1] = LTTNG_MMAP_FLAGS_TYPE,
+#endif
+
+/*
+ * Use a custom field here so that tracer writes a single integer and the
+ * work of splitting it up in two fields is left to the trace reader.
+ */
 #define OVERRIDE_32_mmap
 #define OVERRIDE_64_mmap
 SC_LTTNG_TRACEPOINT_EVENT(mmap,
@@ -13,8 +126,26 @@ SC_LTTNG_TRACEPOINT_EVENT(mmap,
        TP_FIELDS(sc_exit(ctf_integer_hex(unsigned long, ret, ret))
                sc_in(ctf_integer_hex(unsigned long, addr, addr))
                sc_in(ctf_integer(size_t, len, len))
-               sc_in(ctf_integer(int, prot, prot))
-               sc_in(ctf_integer(int, flags, flags))
+               sc_in(ctf_enum(lttng_mmap_protection, int, prot, prot))
+               sc_in(
+                       ctf_custom_field(
+                               ctf_custom_type(
+                                       {
+                                               .atype = atype_struct_nestable,
+                                               .u.struct_nestable.nr_fields = 2,
+                                               .u.struct_nestable.fields =
+                                                       __LTTNG_COMPOUND_LITERAL(struct lttng_event_field,
+                                                               LTTNG_MMAP_FLAGS
+                                                       ),
+                                               .u.struct_nestable.alignment = lttng_alignof(uint32_t) * CHAR_BIT,
+                                       }
+                               ),
+                               flags,
+                               ctf_custom_code(
+                                       ctf_integer_type(uint32_t, flags)
+                               )
+                       )
+               )
                sc_in(ctf_integer(int, fd, fd))
                sc_in(ctf_integer(off_t, offset, off))
        )
index a5b5f403fe359e6531650111fc05cb82a6c60aba..0c28a467872b27d07bec36ba5ef4dbbed21cc16f 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/file.h>
 #include <linux/anon_inodes.h>
 #include <linux/fcntl.h>
+#include <linux/mman.h>
 #include <asm/ptrace.h>
 #include <asm/syscall.h>
 
This page took 0.0277 seconds and 4 git commands to generate.