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