2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/compat/errno.hpp>
9 #include <common/defaults.hpp>
11 #include <lttng/lttng.h>
12 #include <lttng/ust-sigbus.h>
14 #include <bin/lttng-sessiond/lttng-ust-abi.hpp>
15 #include <bin/lttng-sessiond/notification-thread.hpp>
16 #include <bin/lttng-sessiond/trace-ust.hpp>
17 #include <bin/lttng-sessiond/ust-app.hpp>
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 */
34 LTTNG_EXPORT
DEFINE_LTTNG_UST_SIGBUS_STATE();
36 static const char alphanum
[] = "0123456789"
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38 "abcdefghijklmnopqrstuvwxyz";
39 static char random_string
[RANDOM_STRING_LEN
];
42 * Return random string of 10 characters.
45 static char *get_random_string()
49 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
50 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
53 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
58 static void test_create_one_ust_session()
60 struct ltt_ust_session
*usess
= trace_ust_create_session(42);
62 ok(usess
!= nullptr, "Create UST session");
65 skip(1, "UST session is null");
69 ok(usess
->id
== 42 && !usess
->active
&& usess
->domain_global
.channels
!= nullptr &&
70 usess
->uid
== 0 && usess
->gid
== 0,
71 "Validate UST session");
73 trace_ust_destroy_session(usess
);
74 trace_ust_free_session(usess
);
77 static void test_create_ust_channel()
79 struct ltt_ust_channel
*uchan
;
80 struct lttng_channel attr
;
81 struct lttng_channel_extended extended
;
83 memset(&attr
, 0, sizeof(attr
));
84 memset(&extended
, 0, sizeof(extended
));
85 attr
.attr
.extended
.ptr
= &extended
;
87 ok(lttng_strncpy(attr
.name
, "channel0", sizeof(attr
.name
)) == 0,
88 "Validate channel name length");
89 uchan
= trace_ust_create_channel(&attr
, LTTNG_DOMAIN_UST
);
90 ok(uchan
!= nullptr, "Create UST channel");
93 skip(1, "UST channel is null");
97 ok(!uchan
->enabled
&& strncmp(uchan
->name
, "channel0", 8) == 0 &&
98 uchan
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0' && uchan
->ctx
!= nullptr &&
99 uchan
->events
!= nullptr && uchan
->attr
.overwrite
== attr
.attr
.overwrite
,
100 "Validate UST channel");
102 trace_ust_destroy_channel(uchan
);
105 static void test_create_ust_event()
107 struct ltt_ust_event
*event
;
108 struct lttng_event ev
;
109 enum lttng_error_code ret
;
111 memset(&ev
, 0, sizeof(ev
));
112 ok(lttng_strncpy(ev
.name
, get_random_string(), LTTNG_SYMBOL_NAME_LEN
) == 0,
113 "Validate string length");
114 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
115 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
117 ret
= trace_ust_create_event(&ev
, nullptr, nullptr, nullptr, false, &event
);
119 ok(ret
== LTTNG_OK
, "Create UST event");
122 skip(1, "UST event is null");
126 ok(!event
->enabled
&& event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
127 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
128 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
129 "Validate UST event");
131 trace_ust_destroy_event(event
);
134 static void test_create_ust_event_exclusion()
137 enum lttng_error_code ret
;
138 struct ltt_ust_event
*event
;
139 struct lttng_event ev
;
142 struct lttng_event_exclusion
*exclusion
= nullptr;
143 struct lttng_event_exclusion
*exclusion_copy
= nullptr;
144 const int exclusion_count
= 2;
146 memset(&ev
, 0, sizeof(ev
));
148 /* make a wildcarded event name */
149 name
= get_random_string();
150 name
[strlen(name
) - 1] = '*';
151 ok(lttng_strncpy(ev
.name
, name
, LTTNG_SYMBOL_NAME_LEN
) == 0, "Validate string length");
153 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
154 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
156 /* set up an exclusion set */
157 exclusion
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
158 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
159 ok(exclusion
!= nullptr, "Create UST exclusion");
161 skip(4, "zmalloc failed");
165 exclusion
->count
= exclusion_count
;
166 random_name
= get_random_string();
167 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
169 LTTNG_SYMBOL_NAME_LEN
- 1);
170 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
172 LTTNG_SYMBOL_NAME_LEN
- 1);
174 ret
= trace_ust_create_event(&ev
, nullptr, nullptr, exclusion
, false, &event
);
177 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
179 exclusion
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
180 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
181 ok(exclusion
!= nullptr, "Create UST exclusion");
183 skip(2, "zmalloc failed");
187 exclusion_copy
= zmalloc
<lttng_event_exclusion
>(sizeof(*exclusion
) +
188 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
189 if (!exclusion_copy
) {
190 skip(2, "zmalloc failed");
195 * We are giving ownership of the exclusion struct to the
196 * trace_ust_create_event() function. Make a copy of the exclusion struct
197 * so we can compare it later.
200 exclusion
->count
= exclusion_count
;
201 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
203 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0)));
204 LTTNG_ASSERT(copy_ret
== 0);
205 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
207 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1)));
208 LTTNG_ASSERT(copy_ret
== 0);
210 exclusion_copy
->count
= exclusion_count
;
211 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
212 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
213 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0)));
214 LTTNG_ASSERT(copy_ret
== 0);
215 copy_ret
= lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
216 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
217 sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1)));
218 LTTNG_ASSERT(copy_ret
== 0);
220 ret
= trace_ust_create_event(&ev
, nullptr, nullptr, exclusion
, false, &event
);
222 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
225 skip(1, "UST event with exclusion is null");
229 ok(!event
->enabled
&& event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
230 strcmp(event
->attr
.name
, ev
.name
) == 0 && event
->exclusion
!= nullptr &&
231 event
->exclusion
->count
== exclusion_count
&&
232 !memcmp(event
->exclusion
->names
,
233 exclusion_copy
->names
,
234 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
235 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
236 "Validate UST event and exclusion");
238 trace_ust_destroy_event(event
);
241 free(exclusion_copy
);
245 static void test_create_ust_context()
247 struct lttng_event_context ectx
;
248 struct ltt_ust_context
*uctx
;
250 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
252 uctx
= trace_ust_create_context(&ectx
);
253 ok(uctx
!= nullptr, "Create UST context");
256 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_VTID
, "Validate UST context");
258 skip(1, "Skipping UST context validation as creation failed");
265 plan_tests(NUM_TESTS
);
267 diag("UST data structures unit test");
269 rcu_register_thread();
271 test_create_one_ust_session();
272 test_create_ust_channel();
273 test_create_ust_event();
274 test_create_ust_context();
275 test_create_ust_event_exclusion();
277 rcu_unregister_thread();
279 return exit_status();