Implement consumer ring buffer position sampling
[lttng-tools.git] / tests / unit / test_ust_data.c
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
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 #include <urcu.h>
27
28 #include <lttng/lttng.h>
29 #include <bin/lttng-sessiond/lttng-ust-abi.h>
30 #include <common/defaults.h>
31 #include <bin/lttng-sessiond/trace-ust.h>
32 #include <bin/lttng-sessiond/ust-app.h>
33
34 #include <tap/tap.h>
35
36 /* This path will NEVER be created in this test */
37 #define PATH1 "/tmp/.test-junk-lttng"
38
39 #define RANDOM_STRING_LEN 11
40
41 /* Number of TAP tests in this file */
42 #define NUM_TESTS 16
43
44 /* For error.h */
45 int lttng_opt_quiet = 1;
46 int lttng_opt_verbose;
47 int lttng_opt_mi;
48
49 int ust_consumerd32_fd;
50 int ust_consumerd64_fd;
51
52 /* Global variable required by sessiond objects being linked-in */
53 struct lttng_ht *agent_apps_ht_by_sock;
54
55 static const char alphanum[] =
56 "0123456789"
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
58 "abcdefghijklmnopqrstuvwxyz";
59 static char random_string[RANDOM_STRING_LEN];
60
61 static struct ltt_ust_session *usess;
62 static struct lttng_domain dom;
63
64 /*
65 * Return random string of 10 characters.
66 * Not thread-safe.
67 */
68 static char *get_random_string(void)
69 {
70 int i;
71
72 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
73 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
74 }
75
76 random_string[RANDOM_STRING_LEN - 1] = '\0';
77
78 return random_string;
79 }
80
81 static void test_create_one_ust_session(void)
82 {
83 dom.type = LTTNG_DOMAIN_UST;
84
85 usess = trace_ust_create_session(42);
86 ok(usess != NULL, "Create UST session");
87
88 if (!usess) {
89 skip(1, "UST session is null");
90 return;
91 }
92
93 ok(usess->id == 42 &&
94 usess->active == 0 &&
95 usess->domain_global.channels != NULL &&
96 usess->uid == 0 &&
97 usess->gid == 0,
98 "Validate UST session");
99
100 trace_ust_destroy_session(usess);
101 }
102
103 static void test_create_ust_channel(void)
104 {
105 struct ltt_ust_channel *uchan;
106 struct lttng_channel attr;
107
108 memset(&attr, 0, sizeof(attr));
109
110 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
111 "Validate channel name length");
112 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
113 ok(uchan != NULL, "Create UST channel");
114
115 if (!usess) {
116 skip(1, "UST session is null");
117 return;
118 }
119
120 ok(uchan->enabled == 0 &&
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");
127
128 trace_ust_destroy_channel(uchan);
129 }
130
131 static void test_create_ust_event(void)
132 {
133 struct ltt_ust_event *event;
134 struct lttng_event ev;
135
136 memset(&ev, 0, sizeof(ev));
137 ok(lttng_strncpy(ev.name, get_random_string(),
138 LTTNG_SYMBOL_NAME_LEN) == 0,
139 "Validate string length");
140 ev.type = LTTNG_EVENT_TRACEPOINT;
141 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
142
143 event = trace_ust_create_event(&ev, NULL, NULL, NULL, false);
144
145 ok(event != NULL, "Create UST event");
146
147 if (!event) {
148 skip(1, "UST event is null");
149 return;
150 }
151
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");
157
158 trace_ust_destroy_event(event);
159 }
160
161 static void test_create_ust_event_exclusion(void)
162 {
163 struct ltt_ust_event *event;
164 struct lttng_event ev;
165 char *name;
166 char *random_name;
167 struct lttng_event_exclusion *exclusion;
168 const int exclusion_count = 2;
169
170 memset(&ev, 0, sizeof(ev));
171
172 /* make a wildcarded event name */
173 name = get_random_string();
174 name[strlen(name) - 1] = '*';
175 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
176 "Validate string length");
177
178 ev.type = LTTNG_EVENT_TRACEPOINT;
179 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
180
181 /* set up an exclusion set */
182 exclusion = zmalloc(sizeof(*exclusion) +
183 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
184 ok(exclusion != NULL, "Create UST exclusion");
185 if (!exclusion) {
186 skip(4, "zmalloc failed");
187 goto end;
188 }
189
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);
196
197 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
198 exclusion = NULL;
199
200 ok(!event, "Create UST event with identical exclusion names fails");
201
202 exclusion = zmalloc(sizeof(*exclusion) +
203 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
204 ok(exclusion != NULL, "Create UST exclusion");
205 if (!exclusion) {
206 skip(2, "zmalloc failed");
207 goto end;
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);
217 ok(event != NULL, "Create UST event with different exclusion names");
218
219 if (!event) {
220 skip(1, "UST event with exclusion is null");
221 goto end;
222 }
223
224 ok(event->enabled == 0 &&
225 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
226 strcmp(event->attr.name, ev.name) == 0 &&
227 event->exclusion != NULL &&
228 event->exclusion->count == exclusion_count &&
229 !memcmp(event->exclusion->names, exclusion->names,
230 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
231 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
232 "Validate UST event and exclusion");
233
234 trace_ust_destroy_event(event);
235 end:
236 return;
237 }
238
239
240 static void test_create_ust_context(void)
241 {
242 struct lttng_event_context ectx;
243 struct ltt_ust_context *uctx;
244
245 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
246
247 uctx = trace_ust_create_context(&ectx);
248 ok(uctx != NULL, "Create UST context");
249
250 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
251 "Validate UST context");
252 free(uctx);
253 }
254
255 int main(int argc, char **argv)
256 {
257 plan_tests(NUM_TESTS);
258
259 diag("UST data structures unit test");
260
261 rcu_register_thread();
262
263 test_create_one_ust_session();
264 test_create_ust_channel();
265 test_create_ust_event();
266 test_create_ust_context();
267 test_create_ust_event_exclusion();
268
269 rcu_unregister_thread();
270
271 return exit_status();
272 }
This page took 0.035118 seconds and 5 git commands to generate.