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