docs: Add supported versions and fix-backport policy
[lttng-tools.git] / tests / unit / test_ust_data.cpp
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include <urcu.h>
14
15 #include <lttng/lttng.h>
16 #include <bin/lttng-sessiond/lttng-ust-abi.h>
17 #include <common/defaults.h>
18 #include <common/compat/errno.h>
19 #include <bin/lttng-sessiond/trace-ust.h>
20 #include <bin/lttng-sessiond/ust-app.h>
21 #include <bin/lttng-sessiond/notification-thread.h>
22
23 #include <lttng/ust-sigbus.h>
24
25 #include <tap/tap.h>
26
27 /* This path will NEVER be created in this test */
28 #define PATH1 "/tmp/.test-junk-lttng"
29
30 #define RANDOM_STRING_LEN 11
31
32 /* Number of TAP tests in this file */
33 #define NUM_TESTS 16
34
35 LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE();
36
37 static const char alphanum[] =
38 "0123456789"
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz";
41 static char random_string[RANDOM_STRING_LEN];
42
43 /*
44 * Return random string of 10 characters.
45 * Not thread-safe.
46 */
47 static char *get_random_string(void)
48 {
49 int i;
50
51 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
52 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
53 }
54
55 random_string[RANDOM_STRING_LEN - 1] = '\0';
56
57 return random_string;
58 }
59
60 static void test_create_one_ust_session(void)
61 {
62 struct ltt_ust_session *usess =
63 trace_ust_create_session(42);
64
65 ok(usess != NULL, "Create UST session");
66
67 if (!usess) {
68 skip(1, "UST session is null");
69 return;
70 }
71
72 ok(usess->id == 42 &&
73 usess->active == 0 &&
74 usess->domain_global.channels != NULL &&
75 usess->uid == 0 &&
76 usess->gid == 0,
77 "Validate UST session");
78
79 trace_ust_destroy_session(usess);
80 }
81
82 static void test_create_ust_channel(void)
83 {
84 struct ltt_ust_channel *uchan;
85 struct lttng_channel attr;
86 struct lttng_channel_extended extended;
87
88 memset(&attr, 0, sizeof(attr));
89 memset(&extended, 0, sizeof(extended));
90 attr.attr.extended.ptr = &extended;
91
92 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
93 "Validate channel name length");
94 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
95 ok(uchan != NULL, "Create UST channel");
96
97 if (!uchan) {
98 skip(1, "UST channel is null");
99 return;
100 }
101
102 ok(uchan->enabled == 0 &&
103 strncmp(uchan->name, "channel0", 8) == 0 &&
104 uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' &&
105 uchan->ctx != NULL &&
106 uchan->events != NULL &&
107 uchan->attr.overwrite == attr.attr.overwrite,
108 "Validate UST channel");
109
110 trace_ust_destroy_channel(uchan);
111 }
112
113 static void test_create_ust_event(void)
114 {
115 struct ltt_ust_event *event;
116 struct lttng_event ev;
117 enum lttng_error_code ret;
118
119 memset(&ev, 0, sizeof(ev));
120 ok(lttng_strncpy(ev.name, get_random_string(),
121 LTTNG_SYMBOL_NAME_LEN) == 0,
122 "Validate string length");
123 ev.type = LTTNG_EVENT_TRACEPOINT;
124 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
125
126 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
127
128 ok(ret == LTTNG_OK, "Create UST event");
129
130 if (!event) {
131 skip(1, "UST event is null");
132 return;
133 }
134
135 ok(event->enabled == 0 &&
136 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
137 strcmp(event->attr.name, ev.name) == 0 &&
138 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
139 "Validate UST event");
140
141 trace_ust_destroy_event(event);
142 }
143
144 static void test_create_ust_event_exclusion(void)
145 {
146 enum lttng_error_code ret;
147 struct ltt_ust_event *event;
148 struct lttng_event ev;
149 char *name;
150 char *random_name;
151 struct lttng_event_exclusion *exclusion = NULL;
152 struct lttng_event_exclusion *exclusion_copy = NULL;
153 const int exclusion_count = 2;
154
155 memset(&ev, 0, sizeof(ev));
156
157 /* make a wildcarded event name */
158 name = get_random_string();
159 name[strlen(name) - 1] = '*';
160 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
161 "Validate string length");
162
163 ev.type = LTTNG_EVENT_TRACEPOINT;
164 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
165
166 /* set up an exclusion set */
167 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
168 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
169 ok(exclusion != NULL, "Create UST exclusion");
170 if (!exclusion) {
171 skip(4, "zmalloc failed");
172 goto end;
173 }
174
175 exclusion->count = exclusion_count;
176 random_name = get_random_string();
177 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
178 LTTNG_SYMBOL_NAME_LEN);
179 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
180 LTTNG_SYMBOL_NAME_LEN);
181
182 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
183 exclusion = NULL;
184
185 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
186
187 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
188 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
189 ok(exclusion != NULL, "Create UST exclusion");
190 if (!exclusion) {
191 skip(2, "zmalloc failed");
192 goto end;
193 }
194
195 exclusion_copy = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
196 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
197 if (!exclusion_copy) {
198 skip(2, "zmalloc failed");
199 goto end;
200 }
201
202 /*
203 * We are giving ownership of the exclusion struct to the
204 * trace_ust_create_event() function. Make a copy of the exclusion struct
205 * so we can compare it later.
206 */
207
208 exclusion->count = exclusion_count;
209 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
210 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
211 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
212 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
213
214 exclusion_copy->count = exclusion_count;
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
216 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
218 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
219
220 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
221 exclusion = NULL;
222 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
223
224 if (!event) {
225 skip(1, "UST event with exclusion is null");
226 goto end;
227 }
228
229 ok(event->enabled == 0 &&
230 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
231 strcmp(event->attr.name, ev.name) == 0 &&
232 event->exclusion != NULL &&
233 event->exclusion->count == exclusion_count &&
234 !memcmp(event->exclusion->names, exclusion_copy->names,
235 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
236 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
237 "Validate UST event and exclusion");
238
239 trace_ust_destroy_event(event);
240 end:
241 free(exclusion);
242 free(exclusion_copy);
243 return;
244 }
245
246
247 static void test_create_ust_context(void)
248 {
249 struct lttng_event_context ectx;
250 struct ltt_ust_context *uctx;
251
252 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
253
254 uctx = trace_ust_create_context(&ectx);
255 ok(uctx != NULL, "Create UST context");
256
257 if (uctx) {
258 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
259 "Validate UST context");
260 } else {
261 skip(1, "Skipping UST context validation as creation failed");
262 }
263 free(uctx);
264 }
265
266 int main(int argc, char **argv)
267 {
268 plan_tests(NUM_TESTS);
269
270 diag("UST data structures unit test");
271
272 rcu_register_thread();
273
274 test_create_one_ust_session();
275 test_create_ust_channel();
276 test_create_ust_event();
277 test_create_ust_context();
278 test_create_ust_event_exclusion();
279
280 rcu_unregister_thread();
281
282 return exit_status();
283 }
This page took 0.033788 seconds and 4 git commands to generate.