Test fix: leak of exclusions on allocation error
[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 * Stub to prevent an undefined reference in this test without having to link
68 * the entire tree because of a cascade of dependencies. This is not used,
69 * it is just there to prevent GCC from complaining.
70 */
71 int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain,
72 struct ltt_session *session)
73 {
74 ERR("Stub called instead of the real function");
75 abort();
76 return -1;
77 }
78
79 /*
80 * Return random string of 10 characters.
81 * Not thread-safe.
82 */
83 static char *get_random_string(void)
84 {
85 int i;
86
87 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
88 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
89 }
90
91 random_string[RANDOM_STRING_LEN - 1] = '\0';
92
93 return random_string;
94 }
95
96 static void test_create_one_ust_session(void)
97 {
98 dom.type = LTTNG_DOMAIN_UST;
99
100 usess = trace_ust_create_session(42);
101 ok(usess != NULL, "Create UST session");
102
103 if (!usess) {
104 skip(1, "UST session is null");
105 return;
106 }
107
108 ok(usess->id == 42 &&
109 usess->active == 0 &&
110 usess->domain_global.channels != NULL &&
111 usess->uid == 0 &&
112 usess->gid == 0,
113 "Validate UST session");
114
115 trace_ust_destroy_session(usess);
116 }
117
118 static void test_create_ust_channel(void)
119 {
120 struct ltt_ust_channel *uchan;
121 struct lttng_channel attr;
122 struct lttng_channel_extended extended;
123
124 memset(&attr, 0, sizeof(attr));
125 memset(&extended, 0, sizeof(extended));
126 attr.attr.extended.ptr = &extended;
127
128 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
129 "Validate channel name length");
130 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
131 ok(uchan != NULL, "Create UST channel");
132
133 if (!usess) {
134 skip(1, "UST session is null");
135 return;
136 }
137
138 ok(uchan->enabled == 0 &&
139 strncmp(uchan->name, "channel0", 8) == 0 &&
140 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
141 uchan->ctx != NULL &&
142 uchan->events != NULL &&
143 uchan->attr.overwrite == attr.attr.overwrite,
144 "Validate UST channel");
145
146 trace_ust_destroy_channel(uchan);
147 }
148
149 static void test_create_ust_event(void)
150 {
151 struct ltt_ust_event *event;
152 struct lttng_event ev;
153 enum lttng_error_code ret;
154
155 memset(&ev, 0, sizeof(ev));
156 ok(lttng_strncpy(ev.name, get_random_string(),
157 LTTNG_SYMBOL_NAME_LEN) == 0,
158 "Validate string length");
159 ev.type = LTTNG_EVENT_TRACEPOINT;
160 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
161
162 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
163
164 ok(ret == LTTNG_OK, "Create UST event");
165
166 if (!event) {
167 skip(1, "UST event is null");
168 return;
169 }
170
171 ok(event->enabled == 0 &&
172 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
173 strcmp(event->attr.name, ev.name) == 0 &&
174 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
175 "Validate UST event");
176
177 trace_ust_destroy_event(event);
178 }
179
180 static void test_create_ust_event_exclusion(void)
181 {
182 enum lttng_error_code ret;
183 struct ltt_ust_event *event;
184 struct lttng_event ev;
185 char *name;
186 char *random_name;
187 struct lttng_event_exclusion *exclusion = NULL;
188 struct lttng_event_exclusion *exclusion_copy = NULL;
189 const int exclusion_count = 2;
190
191 memset(&ev, 0, sizeof(ev));
192
193 /* make a wildcarded event name */
194 name = get_random_string();
195 name[strlen(name) - 1] = '*';
196 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
197 "Validate string length");
198
199 ev.type = LTTNG_EVENT_TRACEPOINT;
200 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
201
202 /* set up an exclusion set */
203 exclusion = zmalloc(sizeof(*exclusion) +
204 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
205 ok(exclusion != NULL, "Create UST exclusion");
206 if (!exclusion) {
207 skip(4, "zmalloc failed");
208 goto end;
209 }
210
211 exclusion->count = exclusion_count;
212 random_name = get_random_string();
213 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
214 LTTNG_SYMBOL_NAME_LEN);
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
216 LTTNG_SYMBOL_NAME_LEN);
217
218 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
219 exclusion = NULL;
220
221 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
222
223 exclusion = zmalloc(sizeof(*exclusion) +
224 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
225 ok(exclusion != NULL, "Create UST exclusion");
226 if (!exclusion) {
227 skip(2, "zmalloc failed");
228 goto end;
229 }
230
231 exclusion_copy = zmalloc(sizeof(*exclusion) +
232 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
233 if (!exclusion_copy) {
234 skip(2, "zmalloc failed");
235 goto end;
236 }
237
238 /*
239 * We are giving ownership of the exclusion struct to the
240 * trace_ust_create_event() function. Make a copy of the exclusion struct
241 * so we can compare it later.
242 */
243
244 exclusion->count = exclusion_count;
245 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
246 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
247 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
248 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
249
250 exclusion_copy->count = exclusion_count;
251 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
252 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
253 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
254 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
255
256 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
257 exclusion = NULL;
258 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
259
260 if (!event) {
261 skip(1, "UST event with exclusion is null");
262 goto end;
263 }
264
265 ok(event->enabled == 0 &&
266 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
267 strcmp(event->attr.name, ev.name) == 0 &&
268 event->exclusion != NULL &&
269 event->exclusion->count == exclusion_count &&
270 !memcmp(event->exclusion->names, exclusion_copy->names,
271 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
272 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
273 "Validate UST event and exclusion");
274
275 trace_ust_destroy_event(event);
276 end:
277 free(exclusion);
278 free(exclusion_copy);
279 return;
280 }
281
282
283 static void test_create_ust_context(void)
284 {
285 struct lttng_event_context ectx;
286 struct ltt_ust_context *uctx;
287
288 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
289
290 uctx = trace_ust_create_context(&ectx);
291 ok(uctx != NULL, "Create UST context");
292
293 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
294 "Validate UST context");
295 free(uctx);
296 }
297
298 int main(int argc, char **argv)
299 {
300 plan_tests(NUM_TESTS);
301
302 diag("UST data structures unit test");
303
304 rcu_register_thread();
305
306 test_create_one_ust_session();
307 test_create_ust_channel();
308 test_create_ust_event();
309 test_create_ust_context();
310 test_create_ust_event_exclusion();
311
312 rcu_unregister_thread();
313
314 return exit_status();
315 }
This page took 0.035669 seconds and 5 git commands to generate.