Commit | Line | Data |
---|---|---|
d3e8f6bb | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
d3e8f6bb | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
d3e8f6bb | 5 | * |
d3e8f6bb DG |
6 | */ |
7 | ||
d3e8f6bb | 8 | #include <assert.h> |
d3e8f6bb DG |
9 | #include <stdio.h> |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | #include <unistd.h> | |
13 | #include <time.h> | |
8273250b | 14 | #include <urcu.h> |
d3e8f6bb | 15 | |
10a8a223 DG |
16 | #include <lttng/lttng.h> |
17 | #include <bin/lttng-sessiond/lttng-ust-abi.h> | |
990570ed | 18 | #include <common/defaults.h> |
edf4b93e | 19 | #include <common/compat/errno.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 | |
881fc67f MD |
24 | #include <lttng/ust-sigbus.h> |
25 | ||
657270a4 CB |
26 | #include <tap/tap.h> |
27 | ||
d3e8f6bb DG |
28 | /* This path will NEVER be created in this test */ |
29 | #define PATH1 "/tmp/.test-junk-lttng" | |
30 | ||
98612240 MD |
31 | #define RANDOM_STRING_LEN 11 |
32 | ||
657270a4 | 33 | /* Number of TAP tests in this file */ |
1cda50f2 | 34 | #define NUM_TESTS 16 |
657270a4 | 35 | |
881fc67f MD |
36 | DEFINE_LTTNG_UST_SIGBUS_STATE(); |
37 | ||
d3e8f6bb DG |
38 | static const char alphanum[] = |
39 | "0123456789" | |
40 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
41 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 42 | static char random_string[RANDOM_STRING_LEN]; |
d3e8f6bb | 43 | |
d3e8f6bb DG |
44 | /* |
45 | * Return random string of 10 characters. | |
98612240 | 46 | * Not thread-safe. |
d3e8f6bb DG |
47 | */ |
48 | static char *get_random_string(void) | |
49 | { | |
50 | int i; | |
d3e8f6bb | 51 | |
98612240 MD |
52 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
53 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
d3e8f6bb DG |
54 | } |
55 | ||
98612240 | 56 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
d3e8f6bb | 57 | |
98612240 | 58 | return random_string; |
d3e8f6bb DG |
59 | } |
60 | ||
657270a4 | 61 | static void test_create_one_ust_session(void) |
d3e8f6bb | 62 | { |
30b66335 JG |
63 | struct ltt_ust_session *usess = |
64 | trace_ust_create_session(42); | |
d3e8f6bb | 65 | |
657270a4 CB |
66 | ok(usess != NULL, "Create UST session"); |
67 | ||
67b2f51c JR |
68 | if (!usess) { |
69 | skip(1, "UST session is null"); | |
70 | return; | |
71 | } | |
72 | ||
657270a4 | 73 | ok(usess->id == 42 && |
14fb1ebe | 74 | usess->active == 0 && |
657270a4 | 75 | usess->domain_global.channels != NULL && |
657270a4 CB |
76 | usess->uid == 0 && |
77 | usess->gid == 0, | |
78 | "Validate UST session"); | |
d3e8f6bb DG |
79 | |
80 | trace_ust_destroy_session(usess); | |
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 | { | |
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 */ | |
88329be5 PP |
168 | exclusion = zmalloc(sizeof(*exclusion) + |
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, | |
179 | LTTNG_SYMBOL_NAME_LEN); | |
180 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name, | |
181 | LTTNG_SYMBOL_NAME_LEN); | |
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 PP |
187 | |
188 | exclusion = zmalloc(sizeof(*exclusion) + | |
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 | ||
4b6816b6 FD |
196 | exclusion_copy = zmalloc(sizeof(*exclusion) + |
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 PP |
209 | exclusion->count = exclusion_count; |
210 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), | |
211 | get_random_string(), LTTNG_SYMBOL_NAME_LEN); | |
212 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), | |
213 | get_random_string(), LTTNG_SYMBOL_NAME_LEN); | |
214 | ||
4b6816b6 FD |
215 | exclusion_copy->count = exclusion_count; |
216 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0), | |
217 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN); | |
218 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1), | |
219 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN); | |
220 | ||
39687410 | 221 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
e196f4f4 | 222 | exclusion = NULL; |
39687410 | 223 | ok(ret == LTTNG_OK, "Create UST event with different exclusion names"); |
41d7b959 | 224 | |
67b2f51c JR |
225 | if (!event) { |
226 | skip(1, "UST event with exclusion is null"); | |
227 | goto end; | |
228 | } | |
229 | ||
41d7b959 | 230 | ok(event->enabled == 0 && |
fc4b93fa | 231 | event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
881fa57f JG |
232 | strcmp(event->attr.name, ev.name) == 0 && |
233 | event->exclusion != NULL && | |
234 | event->exclusion->count == exclusion_count && | |
235 | !memcmp(event->exclusion->names, exclusion_copy->names, | |
236 | LTTNG_SYMBOL_NAME_LEN * exclusion_count) && | |
fc4b93fa | 237 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
881fa57f | 238 | "Validate UST event and exclusion"); |
41d7b959 | 239 | |
41d7b959 | 240 | trace_ust_destroy_event(event); |
67b2f51c | 241 | end: |
e196f4f4 | 242 | free(exclusion); |
4b6816b6 | 243 | free(exclusion_copy); |
67b2f51c | 244 | return; |
41d7b959 JI |
245 | } |
246 | ||
247 | ||
657270a4 | 248 | static void test_create_ust_context(void) |
d3e8f6bb | 249 | { |
e38021f8 | 250 | struct lttng_event_context ectx; |
d3e8f6bb DG |
251 | struct ltt_ust_context *uctx; |
252 | ||
e38021f8 DG |
253 | ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
254 | ||
e38021f8 | 255 | uctx = trace_ust_create_context(&ectx); |
657270a4 | 256 | ok(uctx != NULL, "Create UST context"); |
d3e8f6bb | 257 | |
77f5299f | 258 | if (uctx) { |
fc4b93fa | 259 | ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID, |
77f5299f JG |
260 | "Validate UST context"); |
261 | } else { | |
262 | skip(1, "Skipping UST context validation as creation failed"); | |
263 | } | |
f949b23e | 264 | free(uctx); |
d3e8f6bb DG |
265 | } |
266 | ||
267 | int main(int argc, char **argv) | |
268 | { | |
657270a4 | 269 | plan_tests(NUM_TESTS); |
d3e8f6bb | 270 | |
e3bef725 CB |
271 | diag("UST data structures unit test"); |
272 | ||
8273250b JR |
273 | rcu_register_thread(); |
274 | ||
657270a4 | 275 | test_create_one_ust_session(); |
657270a4 CB |
276 | test_create_ust_channel(); |
277 | test_create_ust_event(); | |
278 | test_create_ust_context(); | |
41d7b959 | 279 | test_create_ust_event_exclusion(); |
d3e8f6bb | 280 | |
8273250b JR |
281 | rcu_unregister_thread(); |
282 | ||
657270a4 | 283 | return exit_status(); |
d3e8f6bb | 284 | } |