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