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