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