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