Refactoring: struct lttng_enum_desc and lttng_enum_entry
[lttng-ust.git] / liblttng-ust / lttng-ust-dynamic-type.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * UST dynamic type implementation.
7 */
8
9 #define _LGPL_SOURCE
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stddef.h>
13 #include <inttypes.h>
14
15 #include <ust-helper.h>
16 #include <ust-dynamic-type.h>
17
18 #define ctf_enum_value(_string, _value) \
19 __LTTNG_COMPOUND_LITERAL(struct lttng_ust_enum_entry, { \
20 .struct_size = sizeof(struct lttng_ust_enum_entry), \
21 .start = { \
22 .signedness = lttng_is_signed_type(__typeof__(_value)), \
23 .value = lttng_is_signed_type(__typeof__(_value)) ? \
24 (long long) (_value) : (_value), \
25 }, \
26 .end = { \
27 .signedness = lttng_is_signed_type(__typeof__(_value)), \
28 .value = lttng_is_signed_type(__typeof__(_value)) ? \
29 (long long) (_value) : (_value), \
30 }, \
31 .string = (_string), \
32 }),
33
34 static const struct lttng_ust_enum_entry *dt_enum[_NR_LTTNG_UST_DYNAMIC_TYPES] = {
35 [LTTNG_UST_DYNAMIC_TYPE_NONE] = ctf_enum_value("_none", 0)
36 [LTTNG_UST_DYNAMIC_TYPE_S8] = ctf_enum_value("_int8", 1)
37 [LTTNG_UST_DYNAMIC_TYPE_S16] = ctf_enum_value("_int16", 2)
38 [LTTNG_UST_DYNAMIC_TYPE_S32] = ctf_enum_value("_int32", 3)
39 [LTTNG_UST_DYNAMIC_TYPE_S64] = ctf_enum_value("_int64", 4)
40 [LTTNG_UST_DYNAMIC_TYPE_U8] = ctf_enum_value("_uint8", 5)
41 [LTTNG_UST_DYNAMIC_TYPE_U16] = ctf_enum_value("_uint16", 6)
42 [LTTNG_UST_DYNAMIC_TYPE_U32] = ctf_enum_value("_uint32", 7)
43 [LTTNG_UST_DYNAMIC_TYPE_U64] = ctf_enum_value("_uint64", 8)
44 [LTTNG_UST_DYNAMIC_TYPE_FLOAT] = ctf_enum_value("_float", 9)
45 [LTTNG_UST_DYNAMIC_TYPE_DOUBLE] = ctf_enum_value("_double", 10)
46 [LTTNG_UST_DYNAMIC_TYPE_STRING] = ctf_enum_value("_string", 11)
47 };
48
49 static const struct lttng_ust_enum_desc dt_enum_desc = {
50 .name = "dynamic_type_enum",
51 .entries = dt_enum,
52 .nr_entries = LTTNG_ARRAY_SIZE(dt_enum),
53 };
54
55 const struct lttng_ust_event_field *dt_var_fields[_NR_LTTNG_UST_DYNAMIC_TYPES] = {
56 [LTTNG_UST_DYNAMIC_TYPE_NONE] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
57 .struct_size = sizeof(struct lttng_ust_event_field),
58 .name = "none",
59 .type = {
60 .atype = atype_struct_nestable,
61 .u.struct_nestable.nr_fields = 0, /* empty struct. */
62 .u.struct_nestable.alignment = 0,
63 },
64 .nowrite = 0,
65 }),
66 [LTTNG_UST_DYNAMIC_TYPE_S8] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
67 .struct_size = sizeof(struct lttng_ust_event_field),
68 .name = "int8",
69 .type = __type_integer(int8_t, BYTE_ORDER, 10, none),
70 .nowrite = 0,
71 }),
72 [LTTNG_UST_DYNAMIC_TYPE_S16] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
73 .struct_size = sizeof(struct lttng_ust_event_field),
74 .name = "int16",
75 .type = __type_integer(int16_t, BYTE_ORDER, 10, none),
76 .nowrite = 0,
77 }),
78 [LTTNG_UST_DYNAMIC_TYPE_S32] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
79 .struct_size = sizeof(struct lttng_ust_event_field),
80 .name = "int32",
81 .type = __type_integer(int32_t, BYTE_ORDER, 10, none),
82 .nowrite = 0,
83 }),
84 [LTTNG_UST_DYNAMIC_TYPE_S64] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
85 .struct_size = sizeof(struct lttng_ust_event_field),
86 .name = "int64",
87 .type = __type_integer(int64_t, BYTE_ORDER, 10, none),
88 .nowrite = 0,
89 }),
90 [LTTNG_UST_DYNAMIC_TYPE_U8] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
91 .struct_size = sizeof(struct lttng_ust_event_field),
92 .name = "uint8",
93 .type = __type_integer(uint8_t, BYTE_ORDER, 10, none),
94 .nowrite = 0,
95 }),
96 [LTTNG_UST_DYNAMIC_TYPE_U16] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
97 .struct_size = sizeof(struct lttng_ust_event_field),
98 .name = "uint16",
99 .type = __type_integer(uint16_t, BYTE_ORDER, 10, none),
100 .nowrite = 0,
101 }),
102 [LTTNG_UST_DYNAMIC_TYPE_U32] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
103 .struct_size = sizeof(struct lttng_ust_event_field),
104 .name = "uint32",
105 .type = __type_integer(uint32_t, BYTE_ORDER, 10, none),
106 .nowrite = 0,
107 }),
108 [LTTNG_UST_DYNAMIC_TYPE_U64] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
109 .struct_size = sizeof(struct lttng_ust_event_field),
110 .name = "uint64",
111 .type = __type_integer(uint64_t, BYTE_ORDER, 10, none),
112 .nowrite = 0,
113 }),
114 [LTTNG_UST_DYNAMIC_TYPE_FLOAT] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
115 .struct_size = sizeof(struct lttng_ust_event_field),
116 .name = "float",
117 .type = __type_float(float),
118 .nowrite = 0,
119 }),
120 [LTTNG_UST_DYNAMIC_TYPE_DOUBLE] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
121 .struct_size = sizeof(struct lttng_ust_event_field),
122 .name = "double",
123 .type = __type_float(double),
124 .nowrite = 0,
125 }),
126 [LTTNG_UST_DYNAMIC_TYPE_STRING] = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_event_field, {
127 .struct_size = sizeof(struct lttng_ust_event_field),
128 .name = "string",
129 .type = {
130 .atype = atype_string,
131 .u.string.encoding = lttng_encode_UTF8,
132 },
133 .nowrite = 0,
134 }),
135 };
136
137 static const struct lttng_ust_event_field dt_enum_field = {
138 .name = NULL,
139 .type.atype = atype_enum_nestable,
140 .type.u.enum_nestable.desc = &dt_enum_desc,
141 .type.u.enum_nestable.container_type =
142 __LTTNG_COMPOUND_LITERAL(struct lttng_type,
143 __type_integer(char, BYTE_ORDER, 10, none)),
144 .nowrite = 0,
145 };
146
147 const struct lttng_ust_event_field *lttng_ust_dynamic_type_field(int64_t value)
148 {
149 if (value >= _NR_LTTNG_UST_DYNAMIC_TYPES || value < 0)
150 return NULL;
151 return dt_var_fields[value];
152 }
153
154 int lttng_ust_dynamic_type_choices(size_t *nr_choices, const struct lttng_ust_event_field ***choices)
155 {
156 *nr_choices = _NR_LTTNG_UST_DYNAMIC_TYPES;
157 *choices = dt_var_fields;
158 return 0;
159 }
160
161 const struct lttng_ust_event_field *lttng_ust_dynamic_type_tag_field(void)
162 {
163 return &dt_enum_field;
164 }
This page took 0.033566 seconds and 5 git commands to generate.