Commit | Line | Data |
---|---|---|
d3e8f6bb | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
d3e8f6bb | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
d3e8f6bb | 5 | * |
d3e8f6bb DG |
6 | */ |
7 | ||
d3e8f6bb DG |
8 | #include <stdio.h> |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | #include <unistd.h> | |
12 | #include <time.h> | |
8273250b | 13 | #include <urcu.h> |
d3e8f6bb | 14 | |
10a8a223 DG |
15 | #include <lttng/lttng.h> |
16 | #include <bin/lttng-sessiond/lttng-ust-abi.h> | |
990570ed | 17 | #include <common/defaults.h> |
edf4b93e | 18 | #include <common/compat/errno.h> |
10a8a223 | 19 | #include <bin/lttng-sessiond/trace-ust.h> |
7972aab2 | 20 | #include <bin/lttng-sessiond/ust-app.h> |
83b45089 | 21 | #include <bin/lttng-sessiond/notification-thread.h> |
10a8a223 | 22 | |
881fc67f MD |
23 | #include <lttng/ust-sigbus.h> |
24 | ||
657270a4 CB |
25 | #include <tap/tap.h> |
26 | ||
d3e8f6bb DG |
27 | /* This path will NEVER be created in this test */ |
28 | #define PATH1 "/tmp/.test-junk-lttng" | |
29 | ||
98612240 MD |
30 | #define RANDOM_STRING_LEN 11 |
31 | ||
657270a4 | 32 | /* Number of TAP tests in this file */ |
1cda50f2 | 33 | #define NUM_TESTS 16 |
657270a4 | 34 | |
4bd69c5f | 35 | LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE(); |
881fc67f | 36 | |
d3e8f6bb DG |
37 | static const char alphanum[] = |
38 | "0123456789" | |
39 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
40 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 41 | static char random_string[RANDOM_STRING_LEN]; |
d3e8f6bb | 42 | |
d3e8f6bb DG |
43 | /* |
44 | * Return random string of 10 characters. | |
98612240 | 45 | * Not thread-safe. |
d3e8f6bb DG |
46 | */ |
47 | static char *get_random_string(void) | |
48 | { | |
49 | int i; | |
d3e8f6bb | 50 | |
98612240 MD |
51 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
52 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
d3e8f6bb DG |
53 | } |
54 | ||
98612240 | 55 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
d3e8f6bb | 56 | |
98612240 | 57 | return random_string; |
d3e8f6bb DG |
58 | } |
59 | ||
657270a4 | 60 | static void test_create_one_ust_session(void) |
d3e8f6bb | 61 | { |
30b66335 JG |
62 | struct ltt_ust_session *usess = |
63 | trace_ust_create_session(42); | |
d3e8f6bb | 64 | |
657270a4 CB |
65 | ok(usess != NULL, "Create UST session"); |
66 | ||
67b2f51c JR |
67 | if (!usess) { |
68 | skip(1, "UST session is null"); | |
69 | return; | |
70 | } | |
71 | ||
657270a4 | 72 | ok(usess->id == 42 && |
14fb1ebe | 73 | usess->active == 0 && |
657270a4 | 74 | usess->domain_global.channels != NULL && |
657270a4 CB |
75 | usess->uid == 0 && |
76 | usess->gid == 0, | |
77 | "Validate UST session"); | |
d3e8f6bb DG |
78 | |
79 | trace_ust_destroy_session(usess); | |
80 | } | |
81 | ||
657270a4 | 82 | static void test_create_ust_channel(void) |
d3e8f6bb DG |
83 | { |
84 | struct ltt_ust_channel *uchan; | |
85 | struct lttng_channel attr; | |
82b4ebce | 86 | struct lttng_channel_extended extended; |
d3e8f6bb | 87 | |
441c16a7 | 88 | memset(&attr, 0, sizeof(attr)); |
82b4ebce JG |
89 | memset(&extended, 0, sizeof(extended)); |
90 | attr.attr.extended.ptr = &extended; | |
441c16a7 | 91 | |
1b0eb865 MD |
92 | ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0, |
93 | "Validate channel name length"); | |
51755dc8 | 94 | uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST); |
657270a4 CB |
95 | ok(uchan != NULL, "Create UST channel"); |
96 | ||
30b66335 JG |
97 | if (!uchan) { |
98 | skip(1, "UST channel is null"); | |
67b2f51c JR |
99 | return; |
100 | } | |
101 | ||
657270a4 | 102 | ok(uchan->enabled == 0 && |
657270a4 | 103 | strncmp(uchan->name, "channel0", 8) == 0 && |
fc4b93fa | 104 | uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' && |
657270a4 CB |
105 | uchan->ctx != NULL && |
106 | uchan->events != NULL && | |
107 | uchan->attr.overwrite == attr.attr.overwrite, | |
108 | "Validate UST channel"); | |
d3e8f6bb DG |
109 | |
110 | trace_ust_destroy_channel(uchan); | |
111 | } | |
112 | ||
657270a4 | 113 | static void test_create_ust_event(void) |
d3e8f6bb DG |
114 | { |
115 | struct ltt_ust_event *event; | |
116 | struct lttng_event ev; | |
39687410 | 117 | enum lttng_error_code ret; |
d3e8f6bb | 118 | |
441c16a7 | 119 | memset(&ev, 0, sizeof(ev)); |
a84aca51 MD |
120 | ok(lttng_strncpy(ev.name, get_random_string(), |
121 | LTTNG_SYMBOL_NAME_LEN) == 0, | |
122 | "Validate string length"); | |
d3e8f6bb | 123 | ev.type = LTTNG_EVENT_TRACEPOINT; |
441c16a7 | 124 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
d3e8f6bb | 125 | |
39687410 | 126 | ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event); |
d3e8f6bb | 127 | |
39687410 | 128 | ok(ret == LTTNG_OK, "Create UST event"); |
657270a4 | 129 | |
67b2f51c JR |
130 | if (!event) { |
131 | skip(1, "UST event is null"); | |
132 | return; | |
133 | } | |
134 | ||
657270a4 | 135 | ok(event->enabled == 0 && |
fc4b93fa | 136 | event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
657270a4 | 137 | strcmp(event->attr.name, ev.name) == 0 && |
fc4b93fa | 138 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
657270a4 | 139 | "Validate UST event"); |
d3e8f6bb DG |
140 | |
141 | trace_ust_destroy_event(event); | |
142 | } | |
143 | ||
41d7b959 JI |
144 | static void test_create_ust_event_exclusion(void) |
145 | { | |
f2c057a6 | 146 | int copy_ret; |
39687410 | 147 | enum lttng_error_code ret; |
41d7b959 JI |
148 | struct ltt_ust_event *event; |
149 | struct lttng_event ev; | |
150 | char *name; | |
88329be5 | 151 | char *random_name; |
e196f4f4 | 152 | struct lttng_event_exclusion *exclusion = NULL; |
4b6816b6 | 153 | struct lttng_event_exclusion *exclusion_copy = NULL; |
88329be5 | 154 | const int exclusion_count = 2; |
41d7b959 JI |
155 | |
156 | memset(&ev, 0, sizeof(ev)); | |
157 | ||
158 | /* make a wildcarded event name */ | |
159 | name = get_random_string(); | |
160 | name[strlen(name) - 1] = '*'; | |
61a046d9 MD |
161 | ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0, |
162 | "Validate string length"); | |
41d7b959 JI |
163 | |
164 | ev.type = LTTNG_EVENT_TRACEPOINT; | |
165 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
166 | ||
167 | /* set up an exclusion set */ | |
7966af57 | 168 | exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + |
88329be5 | 169 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
3111dcc4 | 170 | ok(exclusion != NULL, "Create UST exclusion"); |
c710ece7 | 171 | if (!exclusion) { |
67b2f51c JR |
172 | skip(4, "zmalloc failed"); |
173 | goto end; | |
c710ece7 MD |
174 | } |
175 | ||
88329be5 PP |
176 | exclusion->count = exclusion_count; |
177 | random_name = get_random_string(); | |
178 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name, | |
b43eb2f6 | 179 | LTTNG_SYMBOL_NAME_LEN - 1); |
88329be5 | 180 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name, |
b43eb2f6 | 181 | LTTNG_SYMBOL_NAME_LEN - 1); |
41d7b959 | 182 | |
39687410 | 183 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
3111dcc4 | 184 | exclusion = NULL; |
41d7b959 | 185 | |
39687410 | 186 | ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails"); |
88329be5 | 187 | |
7966af57 | 188 | exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + |
88329be5 | 189 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
3111dcc4 | 190 | ok(exclusion != NULL, "Create UST exclusion"); |
88329be5 | 191 | if (!exclusion) { |
67b2f51c JR |
192 | skip(2, "zmalloc failed"); |
193 | goto end; | |
88329be5 PP |
194 | } |
195 | ||
7966af57 | 196 | exclusion_copy = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + |
4b6816b6 FD |
197 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
198 | if (!exclusion_copy) { | |
199 | skip(2, "zmalloc failed"); | |
200 | goto end; | |
201 | } | |
202 | ||
203 | /* | |
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. | |
207 | */ | |
208 | ||
88329be5 | 209 | exclusion->count = exclusion_count; |
f2c057a6 JG |
210 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), |
211 | get_random_string(), | |
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), | |
215 | get_random_string(), | |
216 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1))); | |
217 | LTTNG_ASSERT(copy_ret == 0); | |
88329be5 | 218 | |
4b6816b6 | 219 | exclusion_copy->count = exclusion_count; |
f2c057a6 JG |
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); | |
4b6816b6 | 228 | |
39687410 | 229 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
e196f4f4 | 230 | exclusion = NULL; |
39687410 | 231 | ok(ret == LTTNG_OK, "Create UST event with different exclusion names"); |
41d7b959 | 232 | |
67b2f51c JR |
233 | if (!event) { |
234 | skip(1, "UST event with exclusion is null"); | |
235 | goto end; | |
236 | } | |
237 | ||
41d7b959 | 238 | ok(event->enabled == 0 && |
fc4b93fa | 239 | event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
881fa57f JG |
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) && | |
fc4b93fa | 245 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
881fa57f | 246 | "Validate UST event and exclusion"); |
41d7b959 | 247 | |
41d7b959 | 248 | trace_ust_destroy_event(event); |
67b2f51c | 249 | end: |
e196f4f4 | 250 | free(exclusion); |
4b6816b6 | 251 | free(exclusion_copy); |
67b2f51c | 252 | return; |
41d7b959 JI |
253 | } |
254 | ||
255 | ||
657270a4 | 256 | static void test_create_ust_context(void) |
d3e8f6bb | 257 | { |
e38021f8 | 258 | struct lttng_event_context ectx; |
d3e8f6bb DG |
259 | struct ltt_ust_context *uctx; |
260 | ||
e38021f8 DG |
261 | ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
262 | ||
e38021f8 | 263 | uctx = trace_ust_create_context(&ectx); |
657270a4 | 264 | ok(uctx != NULL, "Create UST context"); |
d3e8f6bb | 265 | |
77f5299f | 266 | if (uctx) { |
fc4b93fa | 267 | ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID, |
77f5299f JG |
268 | "Validate UST context"); |
269 | } else { | |
270 | skip(1, "Skipping UST context validation as creation failed"); | |
271 | } | |
f949b23e | 272 | free(uctx); |
d3e8f6bb DG |
273 | } |
274 | ||
f46376a1 | 275 | int main(void) |
d3e8f6bb | 276 | { |
657270a4 | 277 | plan_tests(NUM_TESTS); |
d3e8f6bb | 278 | |
e3bef725 CB |
279 | diag("UST data structures unit test"); |
280 | ||
8273250b JR |
281 | rcu_register_thread(); |
282 | ||
657270a4 | 283 | test_create_one_ust_session(); |
657270a4 CB |
284 | test_create_ust_channel(); |
285 | test_create_ust_event(); | |
286 | test_create_ust_context(); | |
41d7b959 | 287 | test_create_ust_event_exclusion(); |
d3e8f6bb | 288 | |
8273250b JR |
289 | rcu_unregister_thread(); |
290 | ||
657270a4 | 291 | return exit_status(); |
d3e8f6bb | 292 | } |