2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
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>
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
);
82 trace_ust_free_session(usess
);
85 static void test_create_ust_channel(void)
87 struct ltt_ust_channel
*uchan
;
88 struct lttng_channel attr
;
89 struct lttng_channel_extended extended
;
91 memset(&attr
, 0, sizeof(attr
));
92 memset(&extended
, 0, sizeof(extended
));
93 attr
.attr
.extended
.ptr
= &extended
;
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");
101 skip(1, "UST channel is null");
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");
113 trace_ust_destroy_channel(uchan
);
116 static void test_create_ust_event(void)
118 struct ltt_ust_event
*event
;
119 struct lttng_event ev
;
120 enum lttng_error_code ret
;
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
;
129 ret
= trace_ust_create_event(&ev
, 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
- 1);
182 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
183 LTTNG_SYMBOL_NAME_LEN
- 1);
185 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
188 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
190 exclusion
= zmalloc(sizeof(*exclusion
) +
191 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
192 ok(exclusion
!= NULL
, "Create UST exclusion");
194 skip(2, "zmalloc failed");
198 exclusion_copy
= zmalloc(sizeof(*exclusion
) +
199 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
200 if (!exclusion_copy
) {
201 skip(2, "zmalloc failed");
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.
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);
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
);
223 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
225 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
228 skip(1, "UST event with exclusion is null");
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");
242 trace_ust_destroy_event(event
);
245 free(exclusion_copy
);
250 static void test_create_ust_context(void)
252 struct lttng_event_context ectx
;
253 struct ltt_ust_context
*uctx
;
255 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
257 uctx
= trace_ust_create_context(&ectx
);
258 ok(uctx
!= NULL
, "Create UST context");
261 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_VTID
,
262 "Validate UST context");
264 skip(1, "Skipping UST context validation as creation failed");
269 int main(int argc
, char **argv
)
271 plan_tests(NUM_TESTS
);
273 diag("UST data structures unit test");
275 rcu_register_thread();
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();
283 rcu_unregister_thread();
285 return exit_status();