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 | 15 | #include <lttng/lttng.h> |
c9e313bc SM |
16 | #include <bin/lttng-sessiond/lttng-ust-abi.hpp> |
17 | #include <common/defaults.hpp> | |
18 | #include <common/compat/errno.hpp> | |
19 | #include <bin/lttng-sessiond/trace-ust.hpp> | |
20 | #include <bin/lttng-sessiond/ust-app.hpp> | |
21 | #include <bin/lttng-sessiond/notification-thread.hpp> | |
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); | |
75a6fdfe | 80 | trace_ust_free_session(usess); |
d3e8f6bb DG |
81 | } |
82 | ||
657270a4 | 83 | static void test_create_ust_channel(void) |
d3e8f6bb DG |
84 | { |
85 | struct ltt_ust_channel *uchan; | |
86 | struct lttng_channel attr; | |
82b4ebce | 87 | struct lttng_channel_extended extended; |
d3e8f6bb | 88 | |
441c16a7 | 89 | memset(&attr, 0, sizeof(attr)); |
82b4ebce JG |
90 | memset(&extended, 0, sizeof(extended)); |
91 | attr.attr.extended.ptr = &extended; | |
441c16a7 | 92 | |
1b0eb865 MD |
93 | ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0, |
94 | "Validate channel name length"); | |
51755dc8 | 95 | uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST); |
657270a4 CB |
96 | ok(uchan != NULL, "Create UST channel"); |
97 | ||
30b66335 JG |
98 | if (!uchan) { |
99 | skip(1, "UST channel is null"); | |
67b2f51c JR |
100 | return; |
101 | } | |
102 | ||
657270a4 | 103 | ok(uchan->enabled == 0 && |
657270a4 | 104 | strncmp(uchan->name, "channel0", 8) == 0 && |
fc4b93fa | 105 | uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' && |
657270a4 CB |
106 | uchan->ctx != NULL && |
107 | uchan->events != NULL && | |
108 | uchan->attr.overwrite == attr.attr.overwrite, | |
109 | "Validate UST channel"); | |
d3e8f6bb DG |
110 | |
111 | trace_ust_destroy_channel(uchan); | |
112 | } | |
113 | ||
657270a4 | 114 | static void test_create_ust_event(void) |
d3e8f6bb DG |
115 | { |
116 | struct ltt_ust_event *event; | |
117 | struct lttng_event ev; | |
39687410 | 118 | enum lttng_error_code ret; |
d3e8f6bb | 119 | |
441c16a7 | 120 | memset(&ev, 0, sizeof(ev)); |
a84aca51 MD |
121 | ok(lttng_strncpy(ev.name, get_random_string(), |
122 | LTTNG_SYMBOL_NAME_LEN) == 0, | |
123 | "Validate string length"); | |
d3e8f6bb | 124 | ev.type = LTTNG_EVENT_TRACEPOINT; |
441c16a7 | 125 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
d3e8f6bb | 126 | |
39687410 | 127 | ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event); |
d3e8f6bb | 128 | |
39687410 | 129 | ok(ret == LTTNG_OK, "Create UST event"); |
657270a4 | 130 | |
67b2f51c JR |
131 | if (!event) { |
132 | skip(1, "UST event is null"); | |
133 | return; | |
134 | } | |
135 | ||
657270a4 | 136 | ok(event->enabled == 0 && |
fc4b93fa | 137 | event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
657270a4 | 138 | strcmp(event->attr.name, ev.name) == 0 && |
fc4b93fa | 139 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
657270a4 | 140 | "Validate UST event"); |
d3e8f6bb DG |
141 | |
142 | trace_ust_destroy_event(event); | |
143 | } | |
144 | ||
41d7b959 JI |
145 | static void test_create_ust_event_exclusion(void) |
146 | { | |
f2c057a6 | 147 | int copy_ret; |
39687410 | 148 | enum lttng_error_code ret; |
41d7b959 JI |
149 | struct ltt_ust_event *event; |
150 | struct lttng_event ev; | |
151 | char *name; | |
88329be5 | 152 | char *random_name; |
e196f4f4 | 153 | struct lttng_event_exclusion *exclusion = NULL; |
4b6816b6 | 154 | struct lttng_event_exclusion *exclusion_copy = NULL; |
88329be5 | 155 | const int exclusion_count = 2; |
41d7b959 JI |
156 | |
157 | memset(&ev, 0, sizeof(ev)); | |
158 | ||
159 | /* make a wildcarded event name */ | |
160 | name = get_random_string(); | |
161 | name[strlen(name) - 1] = '*'; | |
61a046d9 MD |
162 | ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0, |
163 | "Validate string length"); | |
41d7b959 JI |
164 | |
165 | ev.type = LTTNG_EVENT_TRACEPOINT; | |
166 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
167 | ||
168 | /* set up an exclusion set */ | |
64803277 | 169 | exclusion = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
88329be5 | 170 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
3111dcc4 | 171 | ok(exclusion != NULL, "Create UST exclusion"); |
c710ece7 | 172 | if (!exclusion) { |
67b2f51c JR |
173 | skip(4, "zmalloc failed"); |
174 | goto end; | |
c710ece7 MD |
175 | } |
176 | ||
88329be5 PP |
177 | exclusion->count = exclusion_count; |
178 | random_name = get_random_string(); | |
179 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name, | |
b43eb2f6 | 180 | LTTNG_SYMBOL_NAME_LEN - 1); |
88329be5 | 181 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name, |
b43eb2f6 | 182 | LTTNG_SYMBOL_NAME_LEN - 1); |
41d7b959 | 183 | |
39687410 | 184 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
3111dcc4 | 185 | exclusion = NULL; |
41d7b959 | 186 | |
39687410 | 187 | ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails"); |
88329be5 | 188 | |
64803277 | 189 | exclusion = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
88329be5 | 190 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
3111dcc4 | 191 | ok(exclusion != NULL, "Create UST exclusion"); |
88329be5 | 192 | if (!exclusion) { |
67b2f51c JR |
193 | skip(2, "zmalloc failed"); |
194 | goto end; | |
88329be5 PP |
195 | } |
196 | ||
64803277 | 197 | exclusion_copy = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
4b6816b6 FD |
198 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
199 | if (!exclusion_copy) { | |
200 | skip(2, "zmalloc failed"); | |
201 | goto end; | |
202 | } | |
203 | ||
204 | /* | |
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. | |
208 | */ | |
209 | ||
88329be5 | 210 | exclusion->count = exclusion_count; |
f2c057a6 JG |
211 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), |
212 | get_random_string(), | |
213 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0))); | |
214 | LTTNG_ASSERT(copy_ret == 0); | |
215 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), | |
216 | get_random_string(), | |
217 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1))); | |
218 | LTTNG_ASSERT(copy_ret == 0); | |
88329be5 | 219 | |
4b6816b6 | 220 | exclusion_copy->count = exclusion_count; |
f2c057a6 JG |
221 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0), |
222 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), | |
223 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0))); | |
224 | LTTNG_ASSERT(copy_ret == 0); | |
225 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1), | |
226 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), | |
227 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1))); | |
228 | LTTNG_ASSERT(copy_ret == 0); | |
4b6816b6 | 229 | |
39687410 | 230 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
e196f4f4 | 231 | exclusion = NULL; |
39687410 | 232 | ok(ret == LTTNG_OK, "Create UST event with different exclusion names"); |
41d7b959 | 233 | |
67b2f51c JR |
234 | if (!event) { |
235 | skip(1, "UST event with exclusion is null"); | |
236 | goto end; | |
237 | } | |
238 | ||
41d7b959 | 239 | ok(event->enabled == 0 && |
fc4b93fa | 240 | event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
881fa57f JG |
241 | strcmp(event->attr.name, ev.name) == 0 && |
242 | event->exclusion != NULL && | |
243 | event->exclusion->count == exclusion_count && | |
244 | !memcmp(event->exclusion->names, exclusion_copy->names, | |
245 | LTTNG_SYMBOL_NAME_LEN * exclusion_count) && | |
fc4b93fa | 246 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
881fa57f | 247 | "Validate UST event and exclusion"); |
41d7b959 | 248 | |
41d7b959 | 249 | trace_ust_destroy_event(event); |
67b2f51c | 250 | end: |
e196f4f4 | 251 | free(exclusion); |
4b6816b6 | 252 | free(exclusion_copy); |
67b2f51c | 253 | return; |
41d7b959 JI |
254 | } |
255 | ||
256 | ||
657270a4 | 257 | static void test_create_ust_context(void) |
d3e8f6bb | 258 | { |
e38021f8 | 259 | struct lttng_event_context ectx; |
d3e8f6bb DG |
260 | struct ltt_ust_context *uctx; |
261 | ||
e38021f8 DG |
262 | ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
263 | ||
e38021f8 | 264 | uctx = trace_ust_create_context(&ectx); |
657270a4 | 265 | ok(uctx != NULL, "Create UST context"); |
d3e8f6bb | 266 | |
77f5299f | 267 | if (uctx) { |
fc4b93fa | 268 | ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID, |
77f5299f JG |
269 | "Validate UST context"); |
270 | } else { | |
271 | skip(1, "Skipping UST context validation as creation failed"); | |
272 | } | |
f949b23e | 273 | free(uctx); |
d3e8f6bb DG |
274 | } |
275 | ||
f46376a1 | 276 | int main(void) |
d3e8f6bb | 277 | { |
657270a4 | 278 | plan_tests(NUM_TESTS); |
d3e8f6bb | 279 | |
e3bef725 CB |
280 | diag("UST data structures unit test"); |
281 | ||
8273250b JR |
282 | rcu_register_thread(); |
283 | ||
657270a4 | 284 | test_create_one_ust_session(); |
657270a4 CB |
285 | test_create_ust_channel(); |
286 | test_create_ust_event(); | |
287 | test_create_ust_context(); | |
41d7b959 | 288 | test_create_ust_event_exclusion(); |
d3e8f6bb | 289 | |
8273250b JR |
290 | rcu_unregister_thread(); |
291 | ||
657270a4 | 292 | return exit_status(); |
d3e8f6bb | 293 | } |