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