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>
24 #include <lttng/ust-sigbus.h>
28 /* This path will NEVER be created in this test */
29 #define PATH1 "/tmp/.test-junk-lttng"
31 #define RANDOM_STRING_LEN 11
33 /* Number of TAP tests in this file */
36 DEFINE_LTTNG_UST_SIGBUS_STATE();
39 int lttng_opt_quiet
= 1;
40 int lttng_opt_verbose
;
43 static const char alphanum
[] =
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz";
47 static char random_string
[RANDOM_STRING_LEN
];
50 * Return random string of 10 characters.
53 static char *get_random_string(void)
57 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
58 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
61 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
66 static void test_create_one_ust_session(void)
68 struct ltt_ust_session
*usess
=
69 trace_ust_create_session(42);
71 ok(usess
!= NULL
, "Create UST session");
74 skip(1, "UST session is null");
80 usess
->domain_global
.channels
!= NULL
&&
83 "Validate UST session");
85 trace_ust_destroy_session(usess
);
88 static void test_create_ust_channel(void)
90 struct ltt_ust_channel
*uchan
;
91 struct lttng_channel attr
;
92 struct lttng_channel_extended extended
;
94 memset(&attr
, 0, sizeof(attr
));
95 memset(&extended
, 0, sizeof(extended
));
96 attr
.attr
.extended
.ptr
= &extended
;
98 ok(lttng_strncpy(attr
.name
, "channel0", sizeof(attr
.name
)) == 0,
99 "Validate channel name length");
100 uchan
= trace_ust_create_channel(&attr
, LTTNG_DOMAIN_UST
);
101 ok(uchan
!= NULL
, "Create UST channel");
104 skip(1, "UST channel is null");
108 ok(uchan
->enabled
== 0 &&
109 strncmp(uchan
->name
, "channel0", 8) == 0 &&
110 uchan
->name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0' &&
111 uchan
->ctx
!= NULL
&&
112 uchan
->events
!= NULL
&&
113 uchan
->attr
.overwrite
== attr
.attr
.overwrite
,
114 "Validate UST channel");
116 trace_ust_destroy_channel(uchan
);
119 static void test_create_ust_event(void)
121 struct ltt_ust_event
*event
;
122 struct lttng_event ev
;
123 enum lttng_error_code ret
;
125 memset(&ev
, 0, sizeof(ev
));
126 ok(lttng_strncpy(ev
.name
, get_random_string(),
127 LTTNG_SYMBOL_NAME_LEN
) == 0,
128 "Validate string length");
129 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
130 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
132 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, NULL
, false, &event
);
134 ok(ret
== LTTNG_OK
, "Create UST event");
137 skip(1, "UST event is null");
141 ok(event
->enabled
== 0 &&
142 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
143 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
144 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
145 "Validate UST event");
147 trace_ust_destroy_event(event
);
150 static void test_create_ust_event_exclusion(void)
152 enum lttng_error_code ret
;
153 struct ltt_ust_event
*event
;
154 struct lttng_event ev
;
157 struct lttng_event_exclusion
*exclusion
= NULL
;
158 struct lttng_event_exclusion
*exclusion_copy
= NULL
;
159 const int exclusion_count
= 2;
161 memset(&ev
, 0, sizeof(ev
));
163 /* make a wildcarded event name */
164 name
= get_random_string();
165 name
[strlen(name
) - 1] = '*';
166 ok(lttng_strncpy(ev
.name
, name
, LTTNG_SYMBOL_NAME_LEN
) == 0,
167 "Validate string length");
169 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
170 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
172 /* set up an exclusion set */
173 exclusion
= zmalloc(sizeof(*exclusion
) +
174 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
175 ok(exclusion
!= NULL
, "Create UST exclusion");
177 skip(4, "zmalloc failed");
181 exclusion
->count
= exclusion_count
;
182 random_name
= get_random_string();
183 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), random_name
,
184 LTTNG_SYMBOL_NAME_LEN
);
185 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
186 LTTNG_SYMBOL_NAME_LEN
);
188 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
191 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
193 exclusion
= zmalloc(sizeof(*exclusion
) +
194 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
195 ok(exclusion
!= NULL
, "Create UST exclusion");
197 skip(2, "zmalloc failed");
201 exclusion_copy
= zmalloc(sizeof(*exclusion
) +
202 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
203 if (!exclusion_copy
) {
204 skip(2, "zmalloc failed");
209 * We are giving ownership of the exclusion struct to the
210 * trace_ust_create_event() function. Make a copy of the exclusion struct
211 * so we can compare it later.
214 exclusion
->count
= exclusion_count
;
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
216 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
218 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
220 exclusion_copy
->count
= exclusion_count
;
221 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
222 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), LTTNG_SYMBOL_NAME_LEN
);
223 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
224 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), LTTNG_SYMBOL_NAME_LEN
);
226 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
228 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
231 skip(1, "UST event with exclusion is null");
235 ok(event
->enabled
== 0 &&
236 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
237 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
238 event
->exclusion
!= NULL
&&
239 event
->exclusion
->count
== exclusion_count
&&
240 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
241 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
242 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
243 "Validate UST event and exclusion");
245 trace_ust_destroy_event(event
);
248 free(exclusion_copy
);
253 static void test_create_ust_context(void)
255 struct lttng_event_context ectx
;
256 struct ltt_ust_context
*uctx
;
258 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
260 uctx
= trace_ust_create_context(&ectx
);
261 ok(uctx
!= NULL
, "Create UST context");
264 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_VTID
,
265 "Validate UST context");
267 skip(1, "Skipping UST context validation as creation failed");
272 int main(int argc
, char **argv
)
274 plan_tests(NUM_TESTS
);
276 diag("UST data structures unit test");
278 rcu_register_thread();
280 test_create_one_ust_session();
281 test_create_ust_channel();
282 test_create_ust_event();
283 test_create_ust_context();
284 test_create_ust_event_exclusion();
286 rcu_unregister_thread();
288 return exit_status();