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.h>
17 #include <common/defaults.h>
18 #include <common/compat/errno.h>
19 #include <bin/lttng-sessiond/trace-ust.h>
20 #include <bin/lttng-sessiond/ust-app.h>
21 #include <bin/lttng-sessiond/notification-thread.h>
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
);
82 static void test_create_ust_channel(void)
84 struct ltt_ust_channel
*uchan
;
85 struct lttng_channel attr
;
86 struct lttng_channel_extended extended
;
88 memset(&attr
, 0, sizeof(attr
));
89 memset(&extended
, 0, sizeof(extended
));
90 attr
.attr
.extended
.ptr
= &extended
;
92 ok(lttng_strncpy(attr
.name
, "channel0", sizeof(attr
.name
)) == 0,
93 "Validate channel name length");
94 uchan
= trace_ust_create_channel(&attr
, LTTNG_DOMAIN_UST
);
95 ok(uchan
!= NULL
, "Create UST channel");
98 skip(1, "UST channel is null");
102 ok(uchan
->enabled
== 0 &&
103 strncmp(uchan
->name
, "channel0", 8) == 0 &&
104 uchan
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0' &&
105 uchan
->ctx
!= NULL
&&
106 uchan
->events
!= NULL
&&
107 uchan
->attr
.overwrite
== attr
.attr
.overwrite
,
108 "Validate UST channel");
110 trace_ust_destroy_channel(uchan
);
113 static void test_create_ust_event(void)
115 struct ltt_ust_event
*event
;
116 struct lttng_event ev
;
117 enum lttng_error_code ret
;
119 memset(&ev
, 0, sizeof(ev
));
120 ok(lttng_strncpy(ev
.name
, get_random_string(),
121 LTTNG_SYMBOL_NAME_LEN
) == 0,
122 "Validate string length");
123 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
124 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
126 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, NULL
, false, &event
);
128 ok(ret
== LTTNG_OK
, "Create UST event");
131 skip(1, "UST event is null");
135 ok(event
->enabled
== 0 &&
136 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
137 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
138 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
139 "Validate UST event");
141 trace_ust_destroy_event(event
);
144 static void test_create_ust_event_exclusion(void)
147 enum lttng_error_code ret
;
148 struct ltt_ust_event
*event
;
149 struct lttng_event ev
;
152 struct lttng_event_exclusion
*exclusion
= NULL
;
153 struct lttng_event_exclusion
*exclusion_copy
= NULL
;
154 const int exclusion_count
= 2;
156 memset(&ev
, 0, sizeof(ev
));
158 /* make a wildcarded event name */
159 name
= get_random_string();
160 name
[strlen(name
) - 1] = '*';
161 ok(lttng_strncpy(ev
.name
, name
, LTTNG_SYMBOL_NAME_LEN
) == 0,
162 "Validate string length");
164 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
165 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
167 /* set up an exclusion set */
168 exclusion
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
169 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
170 ok(exclusion
!= NULL
, "Create UST exclusion");
172 skip(4, "zmalloc failed");
176 exclusion
->count
= exclusion_count
;
177 random_name
= get_random_string();
178 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), random_name
,
179 LTTNG_SYMBOL_NAME_LEN
- 1);
180 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
181 LTTNG_SYMBOL_NAME_LEN
- 1);
183 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
186 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
188 exclusion
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
189 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
190 ok(exclusion
!= NULL
, "Create UST exclusion");
192 skip(2, "zmalloc failed");
196 exclusion_copy
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
197 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
198 if (!exclusion_copy
) {
199 skip(2, "zmalloc failed");
204 * We are giving ownership of the exclusion struct to the
205 * trace_ust_create_event() function. Make a copy of the exclusion struct
206 * so we can compare it later.
209 exclusion
->count
= exclusion_count
;
210 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
212 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0)));
213 LTTNG_ASSERT(copy_ret
== 0);
214 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
216 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1)));
217 LTTNG_ASSERT(copy_ret
== 0);
219 exclusion_copy
->count
= exclusion_count
;
220 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
221 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
222 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0)));
223 LTTNG_ASSERT(copy_ret
== 0);
224 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
225 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
226 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1)));
227 LTTNG_ASSERT(copy_ret
== 0);
229 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
231 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
234 skip(1, "UST event with exclusion is null");
238 ok(event
->enabled
== 0 &&
239 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
240 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
241 event
->exclusion
!= NULL
&&
242 event
->exclusion
->count
== exclusion_count
&&
243 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
244 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
245 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
246 "Validate UST event and exclusion");
248 trace_ust_destroy_event(event
);
251 free(exclusion_copy
);
256 static void test_create_ust_context(void)
258 struct lttng_event_context ectx
;
259 struct ltt_ust_context
*uctx
;
261 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
263 uctx
= trace_ust_create_context(&ectx
);
264 ok(uctx
!= NULL
, "Create UST context");
267 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_VTID
,
268 "Validate UST context");
270 skip(1, "Skipping UST context validation as creation failed");
277 plan_tests(NUM_TESTS
);
279 diag("UST data structures unit test");
281 rcu_register_thread();
283 test_create_one_ust_session();
284 test_create_ust_channel();
285 test_create_ust_event();
286 test_create_ust_context();
287 test_create_ust_event_exclusion();
289 rcu_unregister_thread();
291 return exit_status();