Tests: fix: leak of some attributes of ltt_ust_session
[lttng-tools.git] / tests / unit / test_ust_data.c
CommitLineData
d3e8f6bb 1/*
90c106c6 2 * Copyright (C) 2011 EfficiOS Inc.
d3e8f6bb 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
d3e8f6bb 5 *
d3e8f6bb
DG
6 */
7
d3e8f6bb 8#include <assert.h>
d3e8f6bb
DG
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <time.h>
8273250b 14#include <urcu.h>
d3e8f6bb 15
10a8a223
DG
16#include <lttng/lttng.h>
17#include <bin/lttng-sessiond/lttng-ust-abi.h>
990570ed 18#include <common/defaults.h>
edf4b93e 19#include <common/compat/errno.h>
10a8a223 20#include <bin/lttng-sessiond/trace-ust.h>
7972aab2 21#include <bin/lttng-sessiond/ust-app.h>
83b45089 22#include <bin/lttng-sessiond/notification-thread.h>
10a8a223 23
d85707b0
MD
24#include <lttng/ust-sigbus.h>
25
657270a4
CB
26#include <tap/tap.h>
27
d3e8f6bb
DG
28/* This path will NEVER be created in this test */
29#define PATH1 "/tmp/.test-junk-lttng"
30
98612240
MD
31#define RANDOM_STRING_LEN 11
32
657270a4 33/* Number of TAP tests in this file */
1cda50f2 34#define NUM_TESTS 16
657270a4 35
d85707b0
MD
36DEFINE_LTTNG_UST_SIGBUS_STATE();
37
d3e8f6bb
DG
38static const char alphanum[] =
39 "0123456789"
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz";
98612240 42static char random_string[RANDOM_STRING_LEN];
d3e8f6bb 43
d3e8f6bb
DG
44/*
45 * Return random string of 10 characters.
98612240 46 * Not thread-safe.
d3e8f6bb
DG
47 */
48static char *get_random_string(void)
49{
50 int i;
d3e8f6bb 51
98612240
MD
52 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
53 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
54 }
55
98612240 56 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 57
98612240 58 return random_string;
d3e8f6bb
DG
59}
60
657270a4 61static void test_create_one_ust_session(void)
d3e8f6bb 62{
30b66335
JG
63 struct ltt_ust_session *usess =
64 trace_ust_create_session(42);
d3e8f6bb 65
657270a4
CB
66 ok(usess != NULL, "Create UST session");
67
67b2f51c
JR
68 if (!usess) {
69 skip(1, "UST session is null");
70 return;
71 }
72
657270a4 73 ok(usess->id == 42 &&
14fb1ebe 74 usess->active == 0 &&
657270a4 75 usess->domain_global.channels != NULL &&
657270a4
CB
76 usess->uid == 0 &&
77 usess->gid == 0,
78 "Validate UST session");
d3e8f6bb
DG
79
80 trace_ust_destroy_session(usess);
4bd1c41e 81 trace_ust_free_session(usess);
d3e8f6bb
DG
82}
83
657270a4 84static void test_create_ust_channel(void)
d3e8f6bb
DG
85{
86 struct ltt_ust_channel *uchan;
87 struct lttng_channel attr;
82b4ebce 88 struct lttng_channel_extended extended;
d3e8f6bb 89
441c16a7 90 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
91 memset(&extended, 0, sizeof(extended));
92 attr.attr.extended.ptr = &extended;
441c16a7 93
1b0eb865
MD
94 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
95 "Validate channel name length");
51755dc8 96 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
97 ok(uchan != NULL, "Create UST channel");
98
30b66335
JG
99 if (!uchan) {
100 skip(1, "UST channel is null");
67b2f51c
JR
101 return;
102 }
103
657270a4 104 ok(uchan->enabled == 0 &&
657270a4 105 strncmp(uchan->name, "channel0", 8) == 0 &&
fc4b93fa 106 uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' &&
657270a4
CB
107 uchan->ctx != NULL &&
108 uchan->events != NULL &&
109 uchan->attr.overwrite == attr.attr.overwrite,
110 "Validate UST channel");
d3e8f6bb
DG
111
112 trace_ust_destroy_channel(uchan);
113}
114
657270a4 115static void test_create_ust_event(void)
d3e8f6bb
DG
116{
117 struct ltt_ust_event *event;
118 struct lttng_event ev;
39687410 119 enum lttng_error_code ret;
d3e8f6bb 120
441c16a7 121 memset(&ev, 0, sizeof(ev));
a84aca51
MD
122 ok(lttng_strncpy(ev.name, get_random_string(),
123 LTTNG_SYMBOL_NAME_LEN) == 0,
124 "Validate string length");
d3e8f6bb 125 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 126 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 127
39687410 128 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 129
39687410 130 ok(ret == LTTNG_OK, "Create UST event");
657270a4 131
67b2f51c
JR
132 if (!event) {
133 skip(1, "UST event is null");
134 return;
135 }
136
657270a4 137 ok(event->enabled == 0 &&
fc4b93fa 138 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
657270a4 139 strcmp(event->attr.name, ev.name) == 0 &&
fc4b93fa 140 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
657270a4 141 "Validate UST event");
d3e8f6bb
DG
142
143 trace_ust_destroy_event(event);
144}
145
41d7b959
JI
146static void test_create_ust_event_exclusion(void)
147{
39687410 148 enum lttng_error_code ret;
41d7b959
JI
149 struct ltt_ust_event *event;
150 struct lttng_event ev;
151 char *name;
88329be5 152 char *random_name;
e196f4f4 153 struct lttng_event_exclusion *exclusion = NULL;
4b6816b6 154 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 155 const int exclusion_count = 2;
41d7b959
JI
156
157 memset(&ev, 0, sizeof(ev));
158
159 /* make a wildcarded event name */
160 name = get_random_string();
161 name[strlen(name) - 1] = '*';
61a046d9
MD
162 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
163 "Validate string length");
41d7b959
JI
164
165 ev.type = LTTNG_EVENT_TRACEPOINT;
166 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
167
168 /* set up an exclusion set */
88329be5
PP
169 exclusion = zmalloc(sizeof(*exclusion) +
170 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 171 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 172 if (!exclusion) {
67b2f51c
JR
173 skip(4, "zmalloc failed");
174 goto end;
c710ece7
MD
175 }
176
88329be5
PP
177 exclusion->count = exclusion_count;
178 random_name = get_random_string();
179 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
9caa2a3d 180 LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 181 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
9caa2a3d 182 LTTNG_SYMBOL_NAME_LEN - 1);
41d7b959 183
39687410 184 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 185 exclusion = NULL;
41d7b959 186
39687410 187 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
188
189 exclusion = zmalloc(sizeof(*exclusion) +
190 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 191 ok(exclusion != NULL, "Create UST exclusion");
88329be5 192 if (!exclusion) {
67b2f51c
JR
193 skip(2, "zmalloc failed");
194 goto end;
88329be5
PP
195 }
196
4b6816b6
FD
197 exclusion_copy = zmalloc(sizeof(*exclusion) +
198 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
199 if (!exclusion_copy) {
200 skip(2, "zmalloc failed");
201 goto end;
202 }
203
204 /*
205 * We are giving ownership of the exclusion struct to the
206 * trace_ust_create_event() function. Make a copy of the exclusion struct
207 * so we can compare it later.
208 */
209
88329be5
PP
210 exclusion->count = exclusion_count;
211 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
9caa2a3d 212 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 213 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
9caa2a3d 214 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 215
4b6816b6
FD
216 exclusion_copy->count = exclusion_count;
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
218 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
219 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
220 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
221
39687410 222 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
e196f4f4 223 exclusion = NULL;
39687410 224 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 225
67b2f51c
JR
226 if (!event) {
227 skip(1, "UST event with exclusion is null");
228 goto end;
229 }
230
41d7b959 231 ok(event->enabled == 0 &&
fc4b93fa 232 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
881fa57f
JG
233 strcmp(event->attr.name, ev.name) == 0 &&
234 event->exclusion != NULL &&
235 event->exclusion->count == exclusion_count &&
236 !memcmp(event->exclusion->names, exclusion_copy->names,
237 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
fc4b93fa 238 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
881fa57f 239 "Validate UST event and exclusion");
41d7b959 240
41d7b959 241 trace_ust_destroy_event(event);
67b2f51c 242end:
e196f4f4 243 free(exclusion);
4b6816b6 244 free(exclusion_copy);
67b2f51c 245 return;
41d7b959
JI
246}
247
248
657270a4 249static void test_create_ust_context(void)
d3e8f6bb 250{
e38021f8 251 struct lttng_event_context ectx;
d3e8f6bb
DG
252 struct ltt_ust_context *uctx;
253
e38021f8
DG
254 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
255
e38021f8 256 uctx = trace_ust_create_context(&ectx);
657270a4 257 ok(uctx != NULL, "Create UST context");
d3e8f6bb 258
77f5299f 259 if (uctx) {
fc4b93fa 260 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
77f5299f
JG
261 "Validate UST context");
262 } else {
263 skip(1, "Skipping UST context validation as creation failed");
264 }
f949b23e 265 free(uctx);
d3e8f6bb
DG
266}
267
268int main(int argc, char **argv)
269{
657270a4 270 plan_tests(NUM_TESTS);
d3e8f6bb 271
e3bef725
CB
272 diag("UST data structures unit test");
273
8273250b
JR
274 rcu_register_thread();
275
657270a4 276 test_create_one_ust_session();
657270a4
CB
277 test_create_ust_channel();
278 test_create_ust_event();
279 test_create_ust_context();
41d7b959 280 test_create_ust_event_exclusion();
d3e8f6bb 281
8273250b
JR
282 rcu_unregister_thread();
283
657270a4 284 return exit_status();
d3e8f6bb 285}
This page took 0.061907 seconds and 4 git commands to generate.