3d97c1a30f6d46f771b8b05fe73f77e0885a918a
[lttng-ust.git] / tests / ust-variant / ust-variant.c
1 /*
2 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
7 * the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 /* Internal UST API: ust-variant.h */
24 #include <lttng/ust-variant.h>
25 #include <lttng/ust-events.h>
26 #include <helper.h>
27
28 #define NR_ENTRIES 5
29
30 static const struct lttng_enum_entry myentries[NR_ENTRIES] = {
31 [0] = {
32 .start = 0,
33 .end = 0,
34 .string = "_mystring",
35 },
36 [1] = {
37 .start = 1,
38 .end = 1,
39 .string = "_myint32",
40 },
41 [2] = {
42 .start = 2,
43 .end = 2,
44 .string = "_myuint16",
45 },
46 [3] = {
47 .start = 3,
48 .end = 3,
49 .string = "_mychar",
50 },
51 [4] = {
52 .start = 4,
53 .end = 4,
54 .string = "_mylonglong",
55 },
56 };
57
58 static const struct lttng_enum_desc myenum_desc = {
59 .name = "myenum",
60 .entries = myentries,
61 .nr_entries = LTTNG_ARRAY_SIZE(myentries),
62 };
63
64 const struct lttng_event_field myvarfields[NR_ENTRIES] = {
65 [0] = {
66 .name = "mystring",
67 .type = {
68 .atype = atype_string,
69 .u.basic.string.encoding = lttng_encode_UTF8,
70 },
71 .nowrite = 0,
72 },
73 [1] = {
74 .name = "myint32",
75 .type = __type_integer(int32_t, BYTE_ORDER, 10, none),
76 .nowrite = 0,
77 },
78 [2] = {
79 .name = "myuint16",
80 .type = __type_integer(uint16_t, BYTE_ORDER, 10, none),
81 .nowrite = 0,
82 },
83 [3] = {
84 .name = "mychar",
85 .type = __type_integer(char, BYTE_ORDER, 10, none),
86 .nowrite = 0,
87 },
88 [4] = {
89 .name = "mylonglong",
90 .type = __type_integer(long long, BYTE_ORDER, 10, none),
91 .nowrite = 0,
92 },
93 };
94
95 static const struct lttng_event_field *get_field(const struct lttng_ust_type_variant *variant,
96 int64_t value)
97 {
98 if (value >= NR_ENTRIES || value < 0)
99 return NULL;
100 return &myvarfields[value];
101 }
102
103 static int get_choices(const struct lttng_ust_type_variant *variant,
104 size_t *nr_choices, const struct lttng_event_field **choices)
105 {
106 *nr_choices = NR_ENTRIES;
107 *choices = myvarfields;
108 return 0;
109 }
110
111 static const struct lttng_event_field myfields[];
112
113 static const struct lttng_ust_type_variant myvariant = {
114 .tag = &myfields[0],
115 .get_field = get_field,
116 .get_choices = get_choices,
117 .free_priv = NULL,
118 .priv = NULL,
119 };
120
121 /* dummy event */
122
123 static void __event_probe__myprobe___myevent(void * __tp_data)
124 {
125 }
126
127 static const struct lttng_event_field myfields[] = {
128 [0] = {
129 .name = "mytag",
130 .type.atype = atype_enum,
131 .type.u.basic.enumeration.desc = &myenum_desc,
132 .type.u.basic.enumeration.container_type = {
133 .size = sizeof(char) * CHAR_BIT,
134 .alignment = lttng_alignof(char) * CHAR_BIT,
135 .signedness = lttng_is_signed_type(char),
136 .reverse_byte_order = 0,
137 .base = 10,
138 .encoding = lttng_encode_none,
139 },
140 .nowrite = 0,
141 },
142 [1] = {
143 .name = "myfield",
144 .type = {
145 .atype = atype_variant,
146 .u.variant = &myvariant,
147 },
148 .nowrite = 0,
149 },
150 };
151
152 static const struct lttng_event_desc myevent_desc = {
153 .name = "myprobe:myevent",
154 .probe_callback = (void (*)(void)) &__event_probe__myprobe___myevent,
155 .ctx = NULL,
156 .fields = myfields,
157 .nr_fields = LTTNG_ARRAY_SIZE(myfields),
158 .loglevel = NULL,
159 .signature = "mysig",
160 .u = {
161 .ext = {
162 .model_emf_uri = NULL,
163 },
164 },
165 };
166
167 static const struct lttng_event_desc *event_desc_array[] = {
168 [0] = &myevent_desc,
169 };
170
171 /* Dummy probe. */
172
173 static struct lttng_probe_desc __probe_desc___myprobe = {
174 .provider = "myprobe",
175 .event_desc = event_desc_array,
176 .nr_events = LTTNG_ARRAY_SIZE(event_desc_array),
177 .head = { NULL, NULL },
178 .lazy_init_head = { NULL, NULL },
179 .lazy = 0,
180 .major = LTTNG_UST_PROVIDER_MAJOR,
181 .minor = LTTNG_UST_PROVIDER_MINOR,
182 };
183
184 int main(int argc, char **argv)
185 {
186 int ret;
187
188 ret = lttng_probe_register(&__probe_desc___myprobe);
189 if (ret)
190 abort();
191 sleep(5);
192 lttng_probe_unregister(&__probe_desc___myprobe);
193
194 return 0;
195 }
This page took 0.032057 seconds and 3 git commands to generate.