2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <lttng/lttng.h>
16 #include <bin/lttng-sessiond/lttng-ust-abi.hpp>
17 #include <common/defaults.hpp>
18 #include <common/compat/errno.hpp>
19 #include <bin/lttng-sessiond/trace-ust.hpp>
20 #include <bin/lttng-sessiond/ust-app.hpp>
21 #include <bin/lttng-sessiond/notification-thread.hpp>
23 #include <lttng/ust-sigbus.h>
27 /* This path will NEVER be created in this test */
28 #define PATH1 "/tmp/.test-junk-lttng"
30 #define RANDOM_STRING_LEN 11
32 /* Number of TAP tests in this file */
35 LTTNG_EXPORT
DEFINE_LTTNG_UST_SIGBUS_STATE();
37 static const char alphanum
[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz";
41 static char random_string
[RANDOM_STRING_LEN
];
44 * Return random string of 10 characters.
47 static char *get_random_string(void)
51 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
52 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
55 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
60 static void test_create_one_ust_session(void)
62 struct ltt_ust_session
*usess
=
63 trace_ust_create_session(42);
65 ok(usess
!= NULL
, "Create UST session");
68 skip(1, "UST session is null");
74 usess
->domain_global
.channels
!= NULL
&&
77 "Validate UST session");
79 trace_ust_destroy_session(usess
);
80 trace_ust_free_session(usess
);
83 static void test_create_ust_channel(void)
85 struct ltt_ust_channel
*uchan
;
86 struct lttng_channel attr
;
87 struct lttng_channel_extended extended
;
89 memset(&attr
, 0, sizeof(attr
));
90 memset(&extended
, 0, sizeof(extended
));
91 attr
.attr
.extended
.ptr
= &extended
;
93 ok(lttng_strncpy(attr
.name
, "channel0", sizeof(attr
.name
)) == 0,
94 "Validate channel name length");
95 uchan
= trace_ust_create_channel(&attr
, LTTNG_DOMAIN_UST
);
96 ok(uchan
!= NULL
, "Create UST channel");
99 skip(1, "UST channel is null");
103 ok(uchan
->enabled
== 0 &&
104 strncmp(uchan
->name
, "channel0", 8) == 0 &&
105 uchan
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0' &&
106 uchan
->ctx
!= NULL
&&
107 uchan
->events
!= NULL
&&
108 uchan
->attr
.overwrite
== attr
.attr
.overwrite
,
109 "Validate UST channel");
111 trace_ust_destroy_channel(uchan
);
114 static void test_create_ust_event(void)
116 struct ltt_ust_event
*event
;
117 struct lttng_event ev
;
118 enum lttng_error_code ret
;
120 memset(&ev
, 0, sizeof(ev
));
121 ok(lttng_strncpy(ev
.name
, get_random_string(),
122 LTTNG_SYMBOL_NAME_LEN
) == 0,
123 "Validate string length");
124 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
125 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
127 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, NULL
, false, &event
);
129 ok(ret
== LTTNG_OK
, "Create UST event");
132 skip(1, "UST event is null");
136 ok(event
->enabled
== 0 &&
137 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
138 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
139 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
140 "Validate UST event");
142 trace_ust_destroy_event(event
);
145 static void test_create_ust_event_exclusion(void)
148 enum lttng_error_code ret
;
149 struct ltt_ust_event
*event
;
150 struct lttng_event ev
;
153 struct lttng_event_exclusion
*exclusion
= NULL
;
154 struct lttng_event_exclusion
*exclusion_copy
= NULL
;
155 const int exclusion_count
= 2;
157 memset(&ev
, 0, sizeof(ev
));
159 /* make a wildcarded event name */
160 name
= get_random_string();
161 name
[strlen(name
) - 1] = '*';
162 ok(lttng_strncpy(ev
.name
, name
, LTTNG_SYMBOL_NAME_LEN
) == 0,
163 "Validate string length");
165 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
166 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
168 /* set up an exclusion set */
169 exclusion
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
170 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
171 ok(exclusion
!= NULL
, "Create UST exclusion");
173 skip(4, "zmalloc failed");
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
- 1);
181 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
182 LTTNG_SYMBOL_NAME_LEN
- 1);
184 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
187 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
189 exclusion
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
190 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
191 ok(exclusion
!= NULL
, "Create UST exclusion");
193 skip(2, "zmalloc failed");
197 exclusion_copy
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
198 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
199 if (!exclusion_copy
) {
200 skip(2, "zmalloc failed");
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.
210 exclusion
->count
= exclusion_count
;
211 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
213 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0)));
214 LTTNG_ASSERT(copy_ret
== 0);
215 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
217 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1)));
218 LTTNG_ASSERT(copy_ret
== 0);
220 exclusion_copy
->count
= exclusion_count
;
221 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
222 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
223 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0)));
224 LTTNG_ASSERT(copy_ret
== 0);
225 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
226 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
227 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1)));
228 LTTNG_ASSERT(copy_ret
== 0);
230 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
232 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
235 skip(1, "UST event with exclusion is null");
239 ok(event
->enabled
== 0 &&
240 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
241 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
242 event
->exclusion
!= NULL
&&
243 event
->exclusion
->count
== exclusion_count
&&
244 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
245 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
246 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
247 "Validate UST event and exclusion");
249 trace_ust_destroy_event(event
);
252 free(exclusion_copy
);
257 static void test_create_ust_context(void)
259 struct lttng_event_context ectx
;
260 struct ltt_ust_context
*uctx
;
262 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
264 uctx
= trace_ust_create_context(&ectx
);
265 ok(uctx
!= NULL
, "Create UST context");
268 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_VTID
,
269 "Validate UST context");
271 skip(1, "Skipping UST context validation as creation failed");
278 plan_tests(NUM_TESTS
);
280 diag("UST data structures unit test");
282 rcu_register_thread();
284 test_create_one_ust_session();
285 test_create_ust_channel();
286 test_create_ust_event();
287 test_create_ust_context();
288 test_create_ust_event_exclusion();
290 rcu_unregister_thread();
292 return exit_status();