2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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)
146 enum lttng_error_code ret
;
147 struct ltt_ust_event
*event
;
148 struct lttng_event ev
;
151 struct lttng_event_exclusion
*exclusion
= NULL
;
152 struct lttng_event_exclusion
*exclusion_copy
= NULL
;
153 const int exclusion_count
= 2;
155 memset(&ev
, 0, sizeof(ev
));
157 /* make a wildcarded event name */
158 name
= get_random_string();
159 name
[strlen(name
) - 1] = '*';
160 ok(lttng_strncpy(ev
.name
, name
, LTTNG_SYMBOL_NAME_LEN
) == 0,
161 "Validate string length");
163 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
164 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
166 /* set up an exclusion set */
167 exclusion
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
168 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
169 ok(exclusion
!= NULL
, "Create UST exclusion");
171 skip(4, "zmalloc failed");
175 exclusion
->count
= exclusion_count
;
176 random_name
= get_random_string();
177 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), random_name
,
178 LTTNG_SYMBOL_NAME_LEN
);
179 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), random_name
,
180 LTTNG_SYMBOL_NAME_LEN
);
182 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, exclusion
, false, &event
);
185 ok(ret
!= LTTNG_OK
, "Create UST event with identical exclusion names fails");
187 exclusion
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
188 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
189 ok(exclusion
!= NULL
, "Create UST exclusion");
191 skip(2, "zmalloc failed");
195 exclusion_copy
= (lttng_event_exclusion
*) zmalloc(sizeof(*exclusion
) +
196 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
);
197 if (!exclusion_copy
) {
198 skip(2, "zmalloc failed");
203 * We are giving ownership of the exclusion struct to the
204 * trace_ust_create_event() function. Make a copy of the exclusion struct
205 * so we can compare it later.
208 exclusion
->count
= exclusion_count
;
209 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0),
210 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
211 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1),
212 get_random_string(), LTTNG_SYMBOL_NAME_LEN
);
214 exclusion_copy
->count
= exclusion_count
;
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 0),
216 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 0), LTTNG_SYMBOL_NAME_LEN
);
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy
, 1),
218 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, 1), LTTNG_SYMBOL_NAME_LEN
);
220 ret
= trace_ust_create_event(&ev
, NULL
, NULL
, 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
== 0 &&
230 event
->attr
.instrumentation
== LTTNG_UST_ABI_TRACEPOINT
&&
231 strcmp(event
->attr
.name
, ev
.name
) == 0 &&
232 event
->exclusion
!= NULL
&&
233 event
->exclusion
->count
== exclusion_count
&&
234 !memcmp(event
->exclusion
->names
, exclusion_copy
->names
,
235 LTTNG_SYMBOL_NAME_LEN
* exclusion_count
) &&
236 event
->attr
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] == '\0',
237 "Validate UST event and exclusion");
239 trace_ust_destroy_event(event
);
242 free(exclusion_copy
);
247 static void test_create_ust_context(void)
249 struct lttng_event_context ectx
;
250 struct ltt_ust_context
*uctx
;
252 ectx
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
254 uctx
= trace_ust_create_context(&ectx
);
255 ok(uctx
!= NULL
, "Create UST context");
258 ok((int) uctx
->ctx
.ctx
== LTTNG_UST_ABI_CONTEXT_VTID
,
259 "Validate UST context");
261 skip(1, "Skipping UST context validation as creation failed");
266 int main(int argc
, char **argv
)
268 plan_tests(NUM_TESTS
);
270 diag("UST data structures unit test");
272 rcu_register_thread();
274 test_create_one_ust_session();
275 test_create_ust_channel();
276 test_create_ust_event();
277 test_create_ust_context();
278 test_create_ust_event_exclusion();
280 rcu_unregister_thread();
282 return exit_status();