Rename C++ header files to .hpp
[lttng-tools.git] / tests / unit / test_ust_data.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
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.hpp>
17 #include <common/defaults.hpp>
18 #include <common/compat/errno.hpp>
19 #include <bin/lttng-sessiond/trace-ust.hpp>
20 #include <bin/lttng-sessiond/ust-app.hpp>
21 #include <bin/lttng-sessiond/notification-thread.hpp>
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 int copy_ret;
147 enum lttng_error_code ret;
148 struct ltt_ust_event *event;
149 struct lttng_event ev;
150 char *name;
151 char *random_name;
152 struct lttng_event_exclusion *exclusion = NULL;
153 struct lttng_event_exclusion *exclusion_copy = NULL;
154 const int exclusion_count = 2;
155
156 memset(&ev, 0, sizeof(ev));
157
158 /* make a wildcarded event name */
159 name = get_random_string();
160 name[strlen(name) - 1] = '*';
161 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
162 "Validate string length");
163
164 ev.type = LTTNG_EVENT_TRACEPOINT;
165 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
166
167 /* set up an exclusion set */
168 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
169 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
170 ok(exclusion != NULL, "Create UST exclusion");
171 if (!exclusion) {
172 skip(4, "zmalloc failed");
173 goto end;
174 }
175
176 exclusion->count = exclusion_count;
177 random_name = get_random_string();
178 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
179 LTTNG_SYMBOL_NAME_LEN - 1);
180 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
181 LTTNG_SYMBOL_NAME_LEN - 1);
182
183 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
184 exclusion = NULL;
185
186 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
187
188 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
189 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
190 ok(exclusion != NULL, "Create UST exclusion");
191 if (!exclusion) {
192 skip(2, "zmalloc failed");
193 goto end;
194 }
195
196 exclusion_copy = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
197 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
198 if (!exclusion_copy) {
199 skip(2, "zmalloc failed");
200 goto end;
201 }
202
203 /*
204 * We are giving ownership of the exclusion struct to the
205 * trace_ust_create_event() function. Make a copy of the exclusion struct
206 * so we can compare it later.
207 */
208
209 exclusion->count = exclusion_count;
210 copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
211 get_random_string(),
212 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0)));
213 LTTNG_ASSERT(copy_ret == 0);
214 copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
215 get_random_string(),
216 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1)));
217 LTTNG_ASSERT(copy_ret == 0);
218
219 exclusion_copy->count = exclusion_count;
220 copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
221 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
222 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0)));
223 LTTNG_ASSERT(copy_ret == 0);
224 copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
225 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
226 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1)));
227 LTTNG_ASSERT(copy_ret == 0);
228
229 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
230 exclusion = NULL;
231 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
232
233 if (!event) {
234 skip(1, "UST event with exclusion is null");
235 goto end;
236 }
237
238 ok(event->enabled == 0 &&
239 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
240 strcmp(event->attr.name, ev.name) == 0 &&
241 event->exclusion != NULL &&
242 event->exclusion->count == exclusion_count &&
243 !memcmp(event->exclusion->names, exclusion_copy->names,
244 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
245 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
246 "Validate UST event and exclusion");
247
248 trace_ust_destroy_event(event);
249 end:
250 free(exclusion);
251 free(exclusion_copy);
252 return;
253 }
254
255
256 static void test_create_ust_context(void)
257 {
258 struct lttng_event_context ectx;
259 struct ltt_ust_context *uctx;
260
261 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
262
263 uctx = trace_ust_create_context(&ectx);
264 ok(uctx != NULL, "Create UST context");
265
266 if (uctx) {
267 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
268 "Validate UST context");
269 } else {
270 skip(1, "Skipping UST context validation as creation failed");
271 }
272 free(uctx);
273 }
274
275 int main(void)
276 {
277 plan_tests(NUM_TESTS);
278
279 diag("UST data structures unit test");
280
281 rcu_register_thread();
282
283 test_create_one_ust_session();
284 test_create_ust_channel();
285 test_create_ust_event();
286 test_create_ust_context();
287 test_create_ust_event_exclusion();
288
289 rcu_unregister_thread();
290
291 return exit_status();
292 }
This page took 0.035005 seconds and 4 git commands to generate.