2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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
);
84 static void test_create_ust_channel(void)
86 struct ltt_ust_channel
*uchan
;
87 struct lttng_channel attr
;
88 struct lttng_channel_extended extended
;
90 memset(&attr
, 0, sizeof(attr
));
91 memset(&extended
, 0, sizeof(extended
));
92 attr
.attr
.extended
.ptr
= &extended
;
94 ok(lttng_strncpy(attr
.name
, "channel0", sizeof(attr
.name
)) == 0,
95 "Validate channel name length");
96 uchan
= trace_ust_create_channel(&attr
, LTTNG_DOMAIN_UST
);
97 ok(uchan
!= NULL
, "Create UST channel");
100 skip(1, "UST channel is null");
104 ok(uchan
->enabled
== 0 &&
105 strncmp(uchan
->name
, "channel0", 8) == 0 &&
106 uchan
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] == '\0' &&
107 uchan
->ctx
!= NULL
&&
108 uchan
->events
!= NULL
&&
109 uchan
->attr
.overwrite
== attr
.attr
.overwrite
,
110 "Validate UST channel");
112 trace_ust_destroy_channel(uchan
);
115 static void test_create_ust_event(void)
117 struct ltt_ust_event
*event
;
118 struct lttng_event ev
;
119 enum lttng_error_code ret
;
121 memset(&ev
, 0, sizeof(ev
));
122 ok(lttng_strncpy(ev
.name
, get_random_string(),
123 LTTNG_SYMBOL_NAME_LEN
) == 0,
124 "Validate string length");
125 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
126 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
128 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, NULL
, false, &event
);
130 ok(ret
== LTTNG_OK
, "Create UST event");
133 skip(1, "UST event is null");
137 ok(event
->enabled
== 0 &&
138 event
->attr
.instrumentation
== LTTNG_UST_TRACEPOINT
&&
139 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
140 event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] == '\0',
141 "Validate UST event");
143 trace_ust_destroy_event(event
);
146 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(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
);
181 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
182 LTTNG_SYMBOL_NAME_LEN
);
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(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(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 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
212 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
213 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
214 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
216 exclusion_copy
->count
= exclusion_count
;
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
218 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), LTTNG_SYMBOL_NAME_LEN
);
219 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
220 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), LTTNG_SYMBOL_NAME_LEN
);
222 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
224 ok(ret
== LTTNG_OK
, "Create UST event with different exclusion names");
227 skip(1, "UST event with exclusion is null");
231 ok(event
->enabled
== 0 &&
232 event
->attr
.instrumentation
== LTTNG_UST_TRACEPOINT
&&
233 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
234 event
->exclusion
!= NULL
&&
235 event
->exclusion
->count
== exclusion_count
&&
236 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
237 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
238 event
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] == '\0',
239 "Validate UST event and exclusion");
241 trace_ust_destroy_event(event
);
244 free(exclusion_copy
);
249 static void test_create_ust_context(void)
251 struct lttng_event_context ectx
;
252 struct ltt_ust_context
*uctx
;
254 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
256 uctx
= trace_ust_create_context(&ectx
);
257 ok(uctx
!= NULL
, "Create UST context");
260 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_CONTEXT_VTID
,
261 "Validate UST context");
263 skip(1, "Skipping UST context validation as creation failed");
268 int main(int argc
, char **argv
)
270 plan_tests(NUM_TESTS
);
272 diag("UST data structures unit test");
274 rcu_register_thread();
276 test_create_one_ust_session();
277 test_create_ust_channel();
278 test_create_ust_event();
279 test_create_ust_context();
280 test_create_ust_event_exclusion();
282 rcu_unregister_thread();
284 return exit_status();