Tests: fix: leak of some attributes of ltt_ust_session
[lttng-tools.git] / tests / unit / test_ust_data.c
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
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 trace_ust_free_session(usess);
83 }
84
85 static void test_create_ust_channel(void)
86 {
87 struct ltt_ust_channel *uchan;
88 struct lttng_channel attr;
89 struct lttng_channel_extended extended;
90
91 memset(&attr, 0, sizeof(attr));
92 memset(&extended, 0, sizeof(extended));
93 attr.attr.extended.ptr = &extended;
94
95 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
96 "Validate channel name length");
97 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
98 ok(uchan != NULL, "Create UST channel");
99
100 if (!uchan) {
101 skip(1, "UST channel is null");
102 return;
103 }
104
105 ok(uchan->enabled == 0 &&
106 strncmp(uchan->name, "channel0", 8) == 0 &&
107 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
108 uchan->ctx != NULL &&
109 uchan->events != NULL &&
110 uchan->attr.overwrite == attr.attr.overwrite,
111 "Validate UST channel");
112
113 trace_ust_destroy_channel(uchan);
114 }
115
116 static void test_create_ust_event(void)
117 {
118 struct ltt_ust_event *event;
119 struct lttng_event ev;
120 enum lttng_error_code ret;
121
122 memset(&ev, 0, sizeof(ev));
123 ok(lttng_strncpy(ev.name, get_random_string(),
124 LTTNG_SYMBOL_NAME_LEN) == 0,
125 "Validate string length");
126 ev.type = LTTNG_EVENT_TRACEPOINT;
127 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
128
129 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
130
131 ok(ret == LTTNG_OK, "Create UST event");
132
133 if (!event) {
134 skip(1, "UST event is null");
135 return;
136 }
137
138 ok(event->enabled == 0 &&
139 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
140 strcmp(event->attr.name, ev.name) == 0 &&
141 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
142 "Validate UST event");
143
144 trace_ust_destroy_event(event);
145 }
146
147 static void test_create_ust_event_exclusion(void)
148 {
149 enum lttng_error_code ret;
150 struct ltt_ust_event *event;
151 struct lttng_event ev;
152 char *name;
153 char *random_name;
154 struct lttng_event_exclusion *exclusion = NULL;
155 struct lttng_event_exclusion *exclusion_copy = NULL;
156 const int exclusion_count = 2;
157
158 memset(&ev, 0, sizeof(ev));
159
160 /* make a wildcarded event name */
161 name = get_random_string();
162 name[strlen(name) - 1] = '*';
163 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
164 "Validate string length");
165
166 ev.type = LTTNG_EVENT_TRACEPOINT;
167 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
168
169 /* set up an exclusion set */
170 exclusion = zmalloc(sizeof(*exclusion) +
171 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
172 ok(exclusion != NULL, "Create UST exclusion");
173 if (!exclusion) {
174 skip(4, "zmalloc failed");
175 goto end;
176 }
177
178 exclusion->count = exclusion_count;
179 random_name = get_random_string();
180 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
181 LTTNG_SYMBOL_NAME_LEN - 1);
182 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
183 LTTNG_SYMBOL_NAME_LEN - 1);
184
185 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
186 exclusion = NULL;
187
188 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
189
190 exclusion = zmalloc(sizeof(*exclusion) +
191 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
192 ok(exclusion != NULL, "Create UST exclusion");
193 if (!exclusion) {
194 skip(2, "zmalloc failed");
195 goto end;
196 }
197
198 exclusion_copy = zmalloc(sizeof(*exclusion) +
199 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
200 if (!exclusion_copy) {
201 skip(2, "zmalloc failed");
202 goto end;
203 }
204
205 /*
206 * We are giving ownership of the exclusion struct to the
207 * trace_ust_create_event() function. Make a copy of the exclusion struct
208 * so we can compare it later.
209 */
210
211 exclusion->count = exclusion_count;
212 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
213 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
214 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
215 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
216
217 exclusion_copy->count = exclusion_count;
218 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
219 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
220 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
221 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
222
223 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
224 exclusion = NULL;
225 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
226
227 if (!event) {
228 skip(1, "UST event with exclusion is null");
229 goto end;
230 }
231
232 ok(event->enabled == 0 &&
233 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
234 strcmp(event->attr.name, ev.name) == 0 &&
235 event->exclusion != NULL &&
236 event->exclusion->count == exclusion_count &&
237 !memcmp(event->exclusion->names, exclusion_copy->names,
238 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
239 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
240 "Validate UST event and exclusion");
241
242 trace_ust_destroy_event(event);
243 end:
244 free(exclusion);
245 free(exclusion_copy);
246 return;
247 }
248
249
250 static void test_create_ust_context(void)
251 {
252 struct lttng_event_context ectx;
253 struct ltt_ust_context *uctx;
254
255 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
256
257 uctx = trace_ust_create_context(&ectx);
258 ok(uctx != NULL, "Create UST context");
259
260 if (uctx) {
261 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
262 "Validate UST context");
263 } else {
264 skip(1, "Skipping UST context validation as creation failed");
265 }
266 free(uctx);
267 }
268
269 int main(int argc, char **argv)
270 {
271 plan_tests(NUM_TESTS);
272
273 diag("UST data structures unit test");
274
275 rcu_register_thread();
276
277 test_create_one_ust_session();
278 test_create_ust_channel();
279 test_create_ust_event();
280 test_create_ust_context();
281 test_create_ust_event_exclusion();
282
283 rcu_unregister_thread();
284
285 return exit_status();
286 }
This page took 0.034094 seconds and 4 git commands to generate.