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