Adapt to lttng-ust ust-abi.h naming prefix update
[lttng-tools.git] / tests / unit / test_ust_data.c
CommitLineData
d3e8f6bb 1/*
9d16b343 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
d3e8f6bb 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
d3e8f6bb 5 *
d3e8f6bb
DG
6 */
7
d3e8f6bb 8#include <assert.h>
d3e8f6bb
DG
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <time.h>
8273250b 14#include <urcu.h>
d3e8f6bb 15
10a8a223
DG
16#include <lttng/lttng.h>
17#include <bin/lttng-sessiond/lttng-ust-abi.h>
990570ed 18#include <common/defaults.h>
edf4b93e 19#include <common/compat/errno.h>
10a8a223 20#include <bin/lttng-sessiond/trace-ust.h>
7972aab2 21#include <bin/lttng-sessiond/ust-app.h>
83b45089 22#include <bin/lttng-sessiond/notification-thread.h>
10a8a223 23
657270a4
CB
24#include <tap/tap.h>
25
d3e8f6bb
DG
26/* This path will NEVER be created in this test */
27#define PATH1 "/tmp/.test-junk-lttng"
28
98612240
MD
29#define RANDOM_STRING_LEN 11
30
657270a4 31/* Number of TAP tests in this file */
1cda50f2 32#define NUM_TESTS 16
657270a4 33
ad7c9c18 34/* For error.h */
97e19046
DG
35int lttng_opt_quiet = 1;
36int lttng_opt_verbose;
c7e35b03 37int lttng_opt_mi;
d3e8f6bb
DG
38
39static const char alphanum[] =
40 "0123456789"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz";
98612240 43static char random_string[RANDOM_STRING_LEN];
d3e8f6bb 44
d3e8f6bb
DG
45/*
46 * Return random string of 10 characters.
98612240 47 * Not thread-safe.
d3e8f6bb
DG
48 */
49static char *get_random_string(void)
50{
51 int i;
d3e8f6bb 52
98612240
MD
53 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
54 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
55 }
56
98612240 57 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 58
98612240 59 return random_string;
d3e8f6bb
DG
60}
61
657270a4 62static void test_create_one_ust_session(void)
d3e8f6bb 63{
30b66335
JG
64 struct ltt_ust_session *usess =
65 trace_ust_create_session(42);
d3e8f6bb 66
657270a4
CB
67 ok(usess != NULL, "Create UST session");
68
67b2f51c
JR
69 if (!usess) {
70 skip(1, "UST session is null");
71 return;
72 }
73
657270a4 74 ok(usess->id == 42 &&
14fb1ebe 75 usess->active == 0 &&
657270a4 76 usess->domain_global.channels != NULL &&
657270a4
CB
77 usess->uid == 0 &&
78 usess->gid == 0,
79 "Validate UST session");
d3e8f6bb
DG
80
81 trace_ust_destroy_session(usess);
82}
83
657270a4 84static void test_create_ust_channel(void)
d3e8f6bb
DG
85{
86 struct ltt_ust_channel *uchan;
87 struct lttng_channel attr;
82b4ebce 88 struct lttng_channel_extended extended;
d3e8f6bb 89
441c16a7 90 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
91 memset(&extended, 0, sizeof(extended));
92 attr.attr.extended.ptr = &extended;
441c16a7 93
1b0eb865
MD
94 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
95 "Validate channel name length");
51755dc8 96 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
97 ok(uchan != NULL, "Create UST channel");
98
30b66335
JG
99 if (!uchan) {
100 skip(1, "UST channel is null");
67b2f51c
JR
101 return;
102 }
103
657270a4 104 ok(uchan->enabled == 0 &&
657270a4 105 strncmp(uchan->name, "channel0", 8) == 0 &&
fc4b93fa 106 uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' &&
657270a4
CB
107 uchan->ctx != NULL &&
108 uchan->events != NULL &&
109 uchan->attr.overwrite == attr.attr.overwrite,
110 "Validate UST channel");
d3e8f6bb
DG
111
112 trace_ust_destroy_channel(uchan);
113}
114
657270a4 115static void test_create_ust_event(void)
d3e8f6bb
DG
116{
117 struct ltt_ust_event *event;
118 struct lttng_event ev;
39687410 119 enum lttng_error_code ret;
d3e8f6bb 120
441c16a7 121 memset(&ev, 0, sizeof(ev));
a84aca51
MD
122 ok(lttng_strncpy(ev.name, get_random_string(),
123 LTTNG_SYMBOL_NAME_LEN) == 0,
124 "Validate string length");
d3e8f6bb 125 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 126 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 127
39687410 128 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 129
39687410 130 ok(ret == LTTNG_OK, "Create UST event");
657270a4 131
67b2f51c
JR
132 if (!event) {
133 skip(1, "UST event is null");
134 return;
135 }
136
657270a4 137 ok(event->enabled == 0 &&
fc4b93fa 138 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
657270a4 139 strcmp(event->attr.name, ev.name) == 0 &&
fc4b93fa 140 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
657270a4 141 "Validate UST event");
d3e8f6bb
DG
142
143 trace_ust_destroy_event(event);
144}
145
41d7b959
JI
146static void test_create_ust_event_exclusion(void)
147{
39687410 148 enum lttng_error_code ret;
41d7b959
JI
149 struct ltt_ust_event *event;
150 struct lttng_event ev;
151 char *name;
88329be5 152 char *random_name;
e196f4f4 153 struct lttng_event_exclusion *exclusion = NULL;
4b6816b6 154 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 155 const int exclusion_count = 2;
41d7b959
JI
156
157 memset(&ev, 0, sizeof(ev));
158
159 /* make a wildcarded event name */
160 name = get_random_string();
161 name[strlen(name) - 1] = '*';
61a046d9
MD
162 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
163 "Validate string length");
41d7b959
JI
164
165 ev.type = LTTNG_EVENT_TRACEPOINT;
166 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
167
168 /* set up an exclusion set */
88329be5
PP
169 exclusion = zmalloc(sizeof(*exclusion) +
170 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 171 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 172 if (!exclusion) {
67b2f51c
JR
173 skip(4, "zmalloc failed");
174 goto end;
c710ece7
MD
175 }
176
88329be5
PP
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);
41d7b959 183
39687410 184 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 185 exclusion = NULL;
41d7b959 186
39687410 187 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
188
189 exclusion = zmalloc(sizeof(*exclusion) +
190 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 191 ok(exclusion != NULL, "Create UST exclusion");
88329be5 192 if (!exclusion) {
67b2f51c
JR
193 skip(2, "zmalloc failed");
194 goto end;
88329be5
PP
195 }
196
4b6816b6
FD
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
88329be5
PP
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
4b6816b6
FD
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
39687410 222 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
e196f4f4 223 exclusion = NULL;
39687410 224 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 225
67b2f51c
JR
226 if (!event) {
227 skip(1, "UST event with exclusion is null");
228 goto end;
229 }
230
41d7b959 231 ok(event->enabled == 0 &&
fc4b93fa 232 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
881fa57f
JG
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) &&
fc4b93fa 238 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
881fa57f 239 "Validate UST event and exclusion");
41d7b959 240
41d7b959 241 trace_ust_destroy_event(event);
67b2f51c 242end:
e196f4f4 243 free(exclusion);
4b6816b6 244 free(exclusion_copy);
67b2f51c 245 return;
41d7b959
JI
246}
247
248
657270a4 249static void test_create_ust_context(void)
d3e8f6bb 250{
e38021f8 251 struct lttng_event_context ectx;
d3e8f6bb
DG
252 struct ltt_ust_context *uctx;
253
e38021f8
DG
254 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
255
e38021f8 256 uctx = trace_ust_create_context(&ectx);
657270a4 257 ok(uctx != NULL, "Create UST context");
d3e8f6bb 258
77f5299f 259 if (uctx) {
fc4b93fa 260 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
77f5299f
JG
261 "Validate UST context");
262 } else {
263 skip(1, "Skipping UST context validation as creation failed");
264 }
f949b23e 265 free(uctx);
d3e8f6bb
DG
266}
267
268int main(int argc, char **argv)
269{
657270a4 270 plan_tests(NUM_TESTS);
d3e8f6bb 271
e3bef725
CB
272 diag("UST data structures unit test");
273
8273250b
JR
274 rcu_register_thread();
275
657270a4 276 test_create_one_ust_session();
657270a4
CB
277 test_create_ust_channel();
278 test_create_ust_event();
279 test_create_ust_context();
41d7b959 280 test_create_ust_event_exclusion();
d3e8f6bb 281
8273250b
JR
282 rcu_unregister_thread();
283
657270a4 284 return exit_status();
d3e8f6bb 285}
This page took 0.057033 seconds and 4 git commands to generate.