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