2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <lttng/lttng.h>
17 #include <bin/lttng-sessiond/lttng-ust-abi.h>
18 #include <common/defaults.h>
19 #include <common/compat/errno.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>
26 /* This path will NEVER be created in this test */
27 #define PATH1 "/tmp/.test-junk-lttng"
29 #define RANDOM_STRING_LEN 11
31 /* Number of TAP tests in this file */
35 int lttng_opt_quiet
= 1;
36 int lttng_opt_verbose
;
39 static const char alphanum
[] =
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz";
43 static char random_string
[RANDOM_STRING_LEN
];
46 * Return random string of 10 characters.
49 static char *get_random_string(void)
53 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
54 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
57 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
62 static void test_create_one_ust_session(void)
64 struct ltt_ust_session
*usess
=
65 trace_ust_create_session(42);
67 ok(usess
!= NULL
, "Create UST session");
70 skip(1, "UST session is null");
76 usess
->domain_global
.channels
!= NULL
&&
79 "Validate UST session");
81 trace_ust_destroy_session(usess
);
84 static void test_create_ust_channel(void)
86 struct ltt_ust_channel
*uchan
;
87 struct lttng_channel attr
;
88 struct lttng_channel_extended extended
;
90 memset(&attr
, 0, sizeof(attr
));
91 memset(&extended
, 0, sizeof(extended
));
92 attr
.attr
.extended
.ptr
= &extended
;
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");
100 skip(1, "UST channel is null");
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");
112 trace_ust_destroy_channel(uchan
);
115 static void test_create_ust_event(void)
117 struct ltt_ust_event
*event
;
118 struct lttng_event ev
;
119 enum lttng_error_code ret
;
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
;
128 ret
= trace_ust_create_event(0, ev
.name
, NULL
, ev
.type
, ev
.loglevel_type
,
129 ev
.loglevel
, NULL
, NULL
, NULL
, false, &event
);
131 ok(ret
== LTTNG_OK
, "Create UST event");
134 skip(1, "UST event is null");
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");
144 trace_ust_destroy_event(event
);
147 static void test_create_ust_event_exclusion(void)
149 enum lttng_error_code ret
;
150 struct ltt_ust_event
*event
;
151 struct lttng_event ev
;
154 struct lttng_event_exclusion
*exclusion
= NULL
;
155 struct lttng_event_exclusion
*exclusion_copy
= NULL
;
156 const int exclusion_count
= 2;
158 memset(&ev
, 0, sizeof(ev
));
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");
166 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
167 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
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");
174 skip(4, "zmalloc failed");
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
);
182 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
183 LTTNG_SYMBOL_NAME_LEN
);
185 ret
= trace_ust_create_event(0, ev
.name
, NULL
, ev
.type
, ev
.loglevel_type
,
186 ev
.loglevel
, NULL
, NULL
, exclusion
, false, &event
);
189 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
191 exclusion
= zmalloc(sizeof(*exclusion
) +
192 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
193 ok(exclusion
!= NULL
, "Create UST exclusion");
195 skip(2, "zmalloc failed");
199 exclusion_copy
= zmalloc(sizeof(*exclusion
) +
200 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
201 if (!exclusion_copy
) {
202 skip(2, "zmalloc failed");
207 * We are giving ownership of the exclusion struct to the
208 * trace_ust_create_event() function. Make a copy of the exclusion struct
209 * so we can compare it later.
212 exclusion
->count
= exclusion_count
;
213 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
214 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
216 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
218 exclusion_copy
->count
= exclusion_count
;
219 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
220 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), LTTNG_SYMBOL_NAME_LEN
);
221 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
222 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), LTTNG_SYMBOL_NAME_LEN
);
224 ret
= trace_ust_create_event(0, ev
.name
, NULL
, ev
.type
, ev
.loglevel_type
,
225 ev
.loglevel
, NULL
, NULL
, exclusion
, false, &event
);
227 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
230 skip(1, "UST event with exclusion is null");
234 ok(event
->enabled
== 0 &&
235 event
->attr
.instrumentation
== LTTNG_UST_TRACEPOINT
&&
236 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
237 event
->exclusion
!= NULL
&&
238 event
->exclusion
->count
== exclusion_count
&&
239 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
240 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
241 event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] == '\0',
242 "Validate UST event and exclusion");
244 trace_ust_destroy_event(event
);
247 free(exclusion_copy
);
252 static void test_create_ust_context(void)
254 struct lttng_event_context ectx
;
255 struct ltt_ust_context
*uctx
;
257 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
259 uctx
= trace_ust_create_context(&ectx
);
260 ok(uctx
!= NULL
, "Create UST context");
263 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_VTID
,
264 "Validate UST context");
266 skip(1, "Skipping UST context validation as creation failed");
271 int main(int argc
, char **argv
)
273 plan_tests(NUM_TESTS
);
275 diag("UST data structures unit test");
277 rcu_register_thread();
279 test_create_one_ust_session();
280 test_create_ust_channel();
281 test_create_ust_event();
282 test_create_ust_context();
283 test_create_ust_event_exclusion();
285 rcu_unregister_thread();
287 return exit_status();